烟台大学计算机学院教师,建设系列学习资源,改革教学方法,为IT菜鸟建跑道,让大一的孩子会编程,为迷茫的大学生出主意,一起追求快乐的大学。 著书《逆袭大学:传给IT学子的正能量》,帮助处于迷茫中的大学
贺老师教学链接 C语言及程序设计进阶 本课讲解 示例:以字符为单位复制文件 复制文件a.txt,保存到b.txt中 #include<stdio.h> #include<stdlib.h> int main() { FILE *fp1,*fp2; char ch; if((fp1=fopen("a.txt","r"))
贺老师教学链接 C语言及程序设计进阶 本课讲解 位运算 #include <stdio.h> int main() { unsigned short int n = 3; int i; for(i=0; i<10; i++) { printf("%d\n",n); n<<=1;
贺老师教学链接 C语言及程序设计进阶 本课讲解 联合体的概念 #include <stdio.h> union un { int i; short int si[2]; char c[4]; }; int main() { union un x; x.c[0]='A'; x.c[1]='B'; x.c[
贺老师教学链接 C语言及程序设计进阶 本课讲解 He先生方案一:用整型表示品牌、颜色 #include <stdio.h> int main( ) { int brand,color; //brand=0,1,2分别表示Lavida、Tiggo和Skoda //color=0,1,2分别表示红黑白 for(color=0; c
贺老师教学链接 C语言及程序设计进阶 本课讲解 最短路径问题 #include<stdio.h> #define n 7 #define x 9999 /*用一个尽可能大的开销,代表结点之间没有通路*/ int map[n][n]= /*对图7.33中交通网的描述,map[i][j]代表i结点到j结点的开销*/ { {x,4,5,8,x,x,x
贺老师教学链接 C语言及程序设计进阶 本课讲解 找零钱问题及其求解 #include <stdio.h> int main ( ) { int money[10]={100,50,10,0}; /*最大面额的硬值面值排在最前面,将被优先处理*/ int x; /*找零金额*/ int i=0, n=0, m; scanf
贺老师教学链接 C语言及程序设计进阶 本课讲解 8皇后问题实现代码 #include <stdio.h> #include <math.h> #include <malloc.h> void nQueens(int *x, int n); /*求解n皇后问题*/ int place(int *x, int k); /*判断是否可以
贺老师教学链接 C语言及程序设计进阶 本课讲解 快速排序实现 #include<stdio.h> void quicksort(int data[],int first,int last) { int i, j, t, base; if (first>last) return; base=data[first];
贺老师教学链接 C语言及程序设计进阶 本课讲解 顺序查找 #include <stdio.h> #define SIZE 10 int main( ) { int d[SIZE]={34, 43, 98, 72, 12, 47, 31, 43, 1, 78}; /*也可以通过键盘输入等方式给出数据*/ int i; int key;
贺老师的教学链接 C++中的新成份——string类型 (1) #include <iostream> #include <cstring> using namespace std; int main( ) { char str1[50],str2[50],temp[50]; cout<<"please input s
贺老师的教学链接 使用用函数模板 #include <iostream> using namespace std; template<typename T> //模板声明,其中T为类型参数 T max(T a,T b,T c) //定义一个通用函数,用T作虚拟的类型名 { if(b>a) a=b;
贺老师的教学链接 重载函数:同名同体,但接口不同 #include <iostream> using namespace std; int max(int a,int b,int c); //函数声明 double max(double a,double b,double c); long max(long a,long b,long c
贺老师的教学链接 形参/实参、声明/调用/定义 #include <iostream> using namespace std; int max(int a, int b, int c=0);//仅声明时设默认 int main( ) { int a,b,c; cin>>a>>b>>c; cout&l
贺老师的教学链接 例:函数指定为内置函数 #include <iostream> using namespace std; inline int max(int,int,int); int main( ) { int i=10,j=20,k=30,m; m=max(i,j,k); cout<<"max="<<
贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n';
贺老师的教学链接 第一个C++程序 #include <iostream> //包含头文件iostream using namespace std; //使用命名空间std int main( ) { cout<<"Hello World."<<endl; //输出结果 return
返回:贺老师课程教学链接 项目要求 【项目3-max带来的冲突】 分析下面程序出现的编译错误,给出解决的方案。 #include<iostream> using namespace std; //定义函数模板 template<class T> T max(T a, T b) { return (a>b)?a:b; } in
返回:贺老师课程教学链接 项目要求 【项目2-有些数的阶乘不算了】 求n!的函数,当用户的输入为负数,以及输入数太大时(例如大于12),使用异常处理机制予以拒绝,并给出恰当的提示。 [参考解答] #include <iostream> using namespace std; int fac(int n) { int result=1;
返回:贺老师课程教学链接 项目要求 【项目1-平方根中的异常】 编写一个程序,求输入数的平方根。设置异常处理,当输入负数时采用异常处理机制给出提示。 [参考解答] #include<iostream> #include<cmath> using namespace std; double squareroot(double &a
返回:贺老师课程教学链接 【项目1-平方根中的异常】 编写一个程序,求输入数的平方根。设置异常处理,当输入负数时采用异常处理机制给出提示。 [参考解答] 【项目2-有些数的阶乘不算了】 求n!的函数,当用户的输入为负数,以及输入数太大时(例如大于12),使用异常处理机制予以拒绝,并给出恰当的提示。 [参考解答] 【项目3-max带来的冲突】 分析下面程序出现的
返回:贺老师课程教学链接 阅读下面的程序,写出输出结果 (1) #include <iostream > using namespace std; int a[10]= {1,2, 3, 4, 5, 6, 7, 8, 9, 10}; int fun( int i); int main() { int i ,s=0; for( i=0; i
【项目-二进制文件浏览器】 (1)做一个类似BinaryViewer的查看二进制文件的程序,输入文件名后,可以以16进制和ASCII对照的方式列出该文件的内容,可以参考下图: 提示:循环中,一次读入16个字节,先用16进制形式输出,再用字符形式输出。 [参考解答] #include<iostream> #include<iomanip&g
【项目-用二进制文件处理学生成绩】 (1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。 (2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。 (3)将所有数据保存到一个二进制文件binary_score.dat中,最后通
返回:贺老师课程教学链接 【项目1-用二进制文件处理学生成绩】 (1)定义学生类,其中包含学号、姓名、C++课、高数和英语成绩及总分数据成员,成员函数根据需要确定。 (2)读入学生的成绩,并求出总分,用对象数组进行存储。ASCII文件score.dat中保存的是100名学生的学号、姓名和C++课、高数和英语成绩。 (3)将所有数据保存到一个二进制文件binary_sc
返回:贺老师课程教学链接 1、阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。查看其内容,并理解文件存储的原理。 (1) #include <iostream> #include <fstream> #include <cstdlib&
这篇博文介绍使用命令行参数的编程方法,载体是用于文件复制的程序。这也是我用Markdown编辑器写的第一篇博文。Markdown在写《逆袭大学》时用过,写博文是第一次。 本文正文: 引子 下面的程序,可以完成由a.txt到b.txt的复制。 #include <iostream> #include <fstream> using names
【项目 - 处理C++源代码的程序】 在CodeBlocks等IDE中都提供了代码格式整理的功能。完成这种功能的程序,操作的数据是用C++写的源代码文件。C++源文件是一种文本文件,可以通过程序进行操作。集成开发环境(IDE)对对程序进行编译,操作的“数据”是源程序。编译中,要对源程序进行词法检查和语法检查,后续还要进行目标代码生成、代码优化等工作。相关的技术将在《编译原理》课中学习。这些
【项目-OOP版电子词典】 做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。(1)编程序,由用户输入英文词,显示词性和中文释义。提示1:如果要用OOP完成这个词典(当然也可以用OO方法实现),可以定义一个Word类表示一个词条,其中的数据成员string english; 表示英文单词,strin
【项目-用文件保存的学生名单】 文件score.dat中保存的是若干名学生的姓名和C++课、高数和英语成绩。(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分数据成员。 //定义学生类 class Student{ public: //声明必要的成员函数 private: string name; double cpp; double math;
返回:贺老师课程教学链接 项目要求 【项目1 - 小玩文件】(1)下面程序的功能是统计文本文件abc.txt中的字符个数,请填空将程序补充完整。 #include <iostream> #include <cstdlib> #include _____________ // (1) using namespace std; int main() { fst
返回:贺老师课程教学链接 本周项目中用到的数据文件,请在这里下载:点击打开链接【项目1 - 小玩文件】(1)下面程序的功能是统计文本文件abc.txt中的字符个数,请填空将程序补充完整。 #include <iostream> #include <cstdlib> #include _____________ // (1) using namespace std; i
返回:贺老师课程教学链接 1、阅读并运行下面的示例程序,掌握标准输入输出流的控制 例1#include <iostream> #include <math.h> using namespace std; int main() { float a,b,c,disc; cout<<"please input a,b,c:"; cin&g
贺老师教学链接 C语言及程序设计进阶 本课讲解 可以直接下载完整的项目文件bank.zip运行(点击打开链接,解压后打开项目,或者自建项目后加入源文件和头文件)下面的代码,分别展示各个头文件、源文件:Bank.h #ifndef BANK_H_INCLUDED #define BANK_H_INCLUDED #define upNum 2000 //系统最多容纳的用户数 struct
贺老师教学链接 C语言及程序设计进阶 本课讲解 猴子选大王 #include <stdio.h> #include <malloc.h> struct Monkey { int num; //猴子的编号 struct Monkey *next; //下一只猴子 }; int main() { int m,n,i,j,king; s
贺老师教学链接 C语言及程序设计进阶 本课讲解 回顾:动态分配和撤销内存 #include <stdio.h> #include <malloc.h> struct Student { int num; float score; struct Student *next; }; int main( ) { struct Studen
贺老师教学链接 C语言及程序设计进阶 本课讲解 例 建立并输出一个简单链表 #include <stdio.h> struct Student { int num; float score; struct Student *next; }; int main( ) { struct Student a,b,c,*head,*p; a. nu
贺老师教学链接 C语言及程序设计进阶 本课讲解 有问题吗? #include <stdio.h> #include <string.h> struct Test { int x; char *str; }; int main() { struct Test a; a.x=100; char s[]="Hello"; s
贺老师教学链接 C语言及程序设计进阶 本课讲解 指向结构体变量的指针的应用 #include <stdio.h> #include <string.h> struct Student { int num; char name[12]; char sex; float score; }; int main( ) { struct
贺老师教学链接 C语言及程序设计进阶 本课讲解 开发一个电子词典(下载词库点击打开链接) #include <stdio.h> #include<string.h> #include<stdlib.h> //定义词条类 typedef struct { char english[20]; char chinese[30]; cha
贺老师教学链接 C语言及程序设计进阶 本课讲解 结构体数组应用举例 #include <stdio.h> #include <string.h> typedef struct { char name[20]; int count; } Person; int main( ) { Person person[3]= {{"Li",0},{"Zha
贺老师教学链接 C语言及程序设计进阶 本课讲解 结构体作函数参数 #include <stdio.h> struct Student { int num; char name[20]; char sex; int age; double score; char addr[30]; }; void print(struct Stud
贺老师教学链接 C语言及程序设计进阶 本课讲解 结构体类型变量的定义方法 #include <stdio.h> struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; int main( ) { st
贺老师教学链接 C语言及程序设计进阶 本课讲解 宏定义 #include <stdio.h> #define PI 3.1415926 int main ( ) { float r,l,s,sq,vq; printf("please enter r:"); scanf("%f", &r); l=2 * PI *r; s=r * r * PI;
贺老师教学链接 C语言及程序设计进阶 本课讲解 汉诺塔问题解决方案 #include <stdio.h> #define discCount 4 void move(int, char, char,char); int main() { move(discCount,'A','B','C'); return 0; } void move(int n, char
贺老师教学链接 C语言及程序设计进阶 本课讲解 求n! #include <stdio.h> long fact(int n) { long f; if (n==1) f=1; else f=n*fact(n-1); return f; } int
贺老师教学链接 C语言及程序设计进阶 本课讲解 认识递归:求阶乘 #include <stdio.h> long fact(int n) { long f; if (n==1) f=1; else f=n*fact(n-1); return f; }
贺老师教学链接 C语言及程序设计进阶 本课讲解 常见的全局变量使用形式 #include <stdio.h> int a=15,b=-7; //定义全局变量a,b int max(int,int); int main( ) { printf("%d\n", max(a,b)); return 0; } int max(int x,int y) { int z
贺老师教学链接 C语言及程序设计进阶 本课讲解 演示:建立多文件的项目main.c #include <stdio.h> int max(int x,int y); int main( ) { int a,b,c; printf("输入两数:"); scanf("%d %d", &a, &b); c=max(a,b); printf("max=%
贺老师的教学链接 本课讲解 vector容器类示例 #include <iostream> #include <vector> using namespace std; int main() { int a[] = {2,3,4}; vector<int> v1; vector<int>::iterator iter;
贺老师的教学链接 本课讲解 曾经的查找 //曾经的查找:顺序查找 #include <iostream> using namespace std; int main( ) { int d[10]={2,7,4,8,12,1,3,5,9,11},i,key,index=-1; cout<<"Input a key you want to search:\