. //打开d.dat文件 if ((fp=fopen(\ { cout<<\ cout<<\ exit(1); //结束程序执行 } //以单个元素对数组进行文件读操作 for(i=0;i #include #include #include #define MAX 5 //定义结构类型 struct student { int num; char name[20]; float grade; Word 资料
}; //显示student结构数据 void show_str(student a,char *name) { cout< #include #include int main(void) { //声明变量 char ch; char str[20]; int n; float x; //用stdin从键盘上输入数据 fprintf(stdout,\ fscanf(stdin,\ fprintf(stdout,\ x \\n\ fscanf(stdin,\ %f\ cout<<\ //输出显示 fprintf(stdout,\ fprintf(stdout,\ cout< void main( void ) { int c; /* Create an error by writing to standard input. */ putc( 'A', stdin ); if( ferror( stdin ) ) { perror( \ clearerr( stdin ); } /* See if read causes an error. */ printf( \ c = getc( stdin ); if( ferror( stdin ) ) { perror( \ clearerr( stdin ); } } #include Word 资料
#include //此预处理指令不可少 const double HD=3.1415926/180; main() { cout<<\ for (int i=0;i<=180;i=i+30) cout< //以下是几个简单宏替换预处理指令 #define YES 1 #define PI 3.1415926 #define RAD PI/180 #define MESG \ //以下是主程序 main() { //以下各语句使用了宏替换 cout<<\ if (YES) cout<<\ cout<<\ cout< //以下为带参数宏替换的预处理指令 #define PRINT(k) cout<<(k)<(b) ? (a):(b)) main() { int i=3,j=2; //MAX(a,b)宏替换的使用 cout<<\ cout<<\ cout<<\ //PRINT(k)宏替换的使用 PRINT(5); PRINT(MAX(7,i*j)); } #include #define PI 3.1416 main() { int i=100; #if 1 cout<<\#endif #ifdef PI cout<<\ PI=\#endif #ifndef PI cout<<\ PI=\ //此语句不被编译执行#endif } #include const int MAX=5; //假定栈中最多保存5个数据 . //定义名为stack的类,其具有栈功能 class stack { //数据成员 float num[MAX]; //存放栈数据的数组 int top; //指示栈顶位置的变量 public: //成员函数 void init(void) { top=0; } //初始化函数 void push(float x) //入栈函数 { if (top==MAX){ cout<<\ return; }; num[top]=x; top++; } float pop(void) //出栈函数 { top--; if (top<0){ cout<<\ return 0; }; return num[top]; } } //以下是main()函数,其用stack类创建栈对象,并使用了这些对象 main(void) { //声明变量和对象 int i; float x; stack a,b; //声明(创建)栈对象 //以下对栈对象初始化 a.init(); b.init(); //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈对象 for (i=1; i<=MAX; i++) a.push(2*i); //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示 for (i=1; i<=MAX; i++) cout<>x; b.push(x); } Word 资料
//以下利用循环和pop()成员函数依次弹出b栈中的数据并显示 for (i=1; i<=MAX; i++) cout< const int MAX=5; //假定栈中最多保存5个数据 //定义名为stack的具有栈功能的类 class stack { //数据成员 float num[MAX]; //存放栈数据的数组 int top; //指示栈顶位置的变量 public: //成员函数 stack(void) //初始化函数 { top=0; cout<<\ } void push(float x) //入栈函数 { if (top==MAX){ cout<<\ return; }; num[top]=x; top++; } float pop(void) //出栈函数 { top--; if (top<0){ cout<<\ return 0; }; return num[top]; } } //以下是main()函数,其用stack类创建栈对象,并使用了这些对象 main(void) { //声明变量和对象 int i; float x; stack a,b; //声明(创建)栈对象并初始化 //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈 for (i=1; i<=MAX; i++) a.push(2.0*i); //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示 for (i=1; i<=MAX; i++) cout<>x; b.push(x); } //以下利用循环和pop()成员函数依次弹出b栈中的数据并显示 for (i=1; i<=MAX; i++) cout< const int MAX=5; //假定栈中最多保存5个数据 //定义名为stack的具有栈功能的类 class stack { //数据成员 float num[MAX]; //存放栈数据的数组 int top; //指示栈顶位置的变量 public: //成员函数 stack(char c) //初始化函数 { top=0; cout<<\ } void push(float x) //入栈函数 { if (top==MAX){ cout<<\ return; }; num[top]=x; top++; } float pop(void) //出栈函数 { top--; if (top<0){ cout<<\ return 0; }; return num[top]; } } //以下是main()函数,其用stack类创建栈对象,并使用了这些对象 main(void) { //声明变量和对象 int i; float x; stack a('a'),b('b'); //声明(创建)栈对象并初始化 //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈 Word 资料
for (i=1; i<=MAX; i++) a.push(2.0*i); //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示 for (i=1; i<=MAX; i++) cout< main() { //定义一个名为student的类 class student { int num; char *name; float grade; public: //定义构造函数 student(int n,char *p,float g): num(n),name(p),grade(g){} display(void) { cout< #include //定义timer类 class timer{ long minutes; public: //无参数构造函数 timer(void) { minutes =0; }; //字符指针参数的构造函数 timer(char *m) { minutes = atoi(m); }; //整数类型的构造函数 timer(int h, int m) { minutes = 60*h+m ; }; //双精度浮点型构造函数 timer(double h) { minutes = (int) 60*h ; }; long getminutes(void) { return minutes ; }; }; //main()函数的定义 main(void) . { //使用double类型的构造函数创建对象 timer start(8.30),finish(17.30); cout<<\ cout< //定义rect类 class rect { int length; int width; int area; public: rect(int l=1,int w=1) { length=l; width=w; area=length*width; } void show_rect(char *name) { cout< const int MAX=5; //假定栈中最多保存5个数据 //定义名为stack的具有栈功能的类 class stack { Word 资料
stack(char *name) //构造函数 { top=0; cout<<\ } ~stack(void) //析构函数 { cout << \ //显示信息 } void push(double x) //入栈函数 { if (top==MAX){ cout<<\ return; }; num[top]=x; top++; } double pop(void) //出栈函数 { top--; if (top<0){ cout<<\ return 0; }; return num[top]; } } //以下是main()函数,其用stack类创建栈对象,并使用了这些对象 main(void) { double x; //声明(创建)栈对象并初始化 stack a(\ //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈 for (x=1; x<=MAX; x++) a.push(2.0*x); //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示 cout<<\ for (int i=1; i<=MAX; i++) cout<>x;