c++经典练习程序代码_第1页
c++经典练习程序代码_第2页
c++经典练习程序代码_第3页
c++经典练习程序代码_第4页
c++经典练习程序代码_第5页
已阅读5页,还剩54页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include main() /用 SIZEOF 计算常量长度 cout main()/测试表达式类型转换 int n=100,m; double x=3.791,y; cout main() int score; coutscore; if(score100) cout=90) cout=80) cout=70) cout=60) cout main() int x,y,z; char c1; cinxc1y; switch(c1) case +:cout main() / for(int i=1;i=1;j-) cout main() int s=0,n=0; do n+; s+=n; while (nx; sum+=x; while (x!=0); cout main() /打印乘法九九表 for(int i=1;i using namespace std; int main() int a23,i,j; /从键盘输入数组元素 for(i=0;iaij; /show array data for(i=0;i using namespace std; #define n 8 int main() /指针与引用的测试 int a,b,c,*ip; / a=100; ip= cout /#include /using namespace std; int main() / int i; struct student int num; char name20; float maths; float physics; float chemistry; double total; ; student st3; coutsti.num; ; cinsti.maths; cinsti.physics; cinsti.chemistry; for(i=0;i /#include /using namespace std; int main() / struct human char name10; int sex; int age; ; struct human x=“WangPing“,1,30,*p= coutname=“; cinp-name; coutsex=“; cinp-sex; coutage=“; cinp-age; coutname=“namesex=“sexage=“age main() struct date int year; int month; int day; ; struct baby int num; float weight; date birthday; ; baby b1=10001,10,2002,12,25; cout main() int i; struct student char name10; int math; int computer; float sum; student *next; ; struct student *head,*tail,*temp; temp=new struct student;/申请内存 head=temp; tail=head; couttemp-name; if(temp-name0!=*) cintemp-mathtemp-computer; temp-sum=temp-math+temp-computer; temp-next=NULL; tail=temp; else delete temp; tail-next=NULL; break; temp-next=new struct student; temp=temp-next; / coutnamemathcomputersumnext; -=- /双链表前后篇历算法。 #include main() int i; struct student char name10; int math; int computer; float sum; student *forw; student *next; ; struct student *head,*tail,*temp; temp=new struct student; head=temp; tail=temp; head-forw=NULL; couttemp-name; if(temp-name0!=*) cintemp-mathtemp-computer; temp-sum=temp-math+temp-computer; temp-next=NULL; tail=temp; else delete temp; tail-next=NULL; break; temp-next=new struct student; temp-next-forw=temp; temp=temp-next; coutnamemathcomputersumnext; couthead:“namemathcomputersumforw; -=- /UNION 联合应用, 联合内各元素,点用同一内存空间, #include main() int i; union utag char c; int k; float x; ; utag u; u.c=*; cout struct student int num; char name20; float grade; ; void main() int i,size; char str=“This a string.“; int int_values=51,23,2,44,45,0,11; float float_values=5.1,13.3,22.2,10.4,1.5; student st_arr=101,“WangLin“,92,102,“LiPing“,85,103,“ZhaoMin“,88; size=sizeof(str)/sizeof(char); cout double add(double x,double y) double z; z=x+y; cout int sgn(double x) if(x0)return (1); if(xx; cout main() float max(float,float); float a,b,Max; couta; coutb; Max=max(a,b); couty)?x:y; return (z); -=- #include struct student char name10; float grade; ; void swap(student temp=x; x=y; y=temp; student void show(student /error /*string=x; /error int i=0; while(*(string+i)!=NULL) i+; return i; void main() char a=“ABCDE“; cout int abs(int x); long abs(long x); float abs(float x); / void main(void) int i1=32767,i2=-32767; long l1=45678,l2=-45678; float x1=1.1234,x2=-1.1234; cout main() int fact(int x); int n,sn; for(int i=1;in; sn=fact(n); cout int main(int argc,char *argv) int i; for(i=0;i struct student char name10; float grade; ; void change(student void change1(student *p,float grade) /指针传递,引用作用相同,都是传地址, 影响实参 p-grade=grade; void change2(student x,float grade)/值传递,不影响实参 x.grade=grade; void show(student for(int i=0;i int get_result(int a,int b,int(*sub)(int,int) int r; r=sub(a,b); return r; int max(int a,int b) coutb)?a:b); /计算最小值 int min(int a,int b) coutab; cout extern int a=1,2,3; extern float p=3.14; show() extern int y; int i; cout count1() static int i=0; return (+i); count2() int i=0; return (+i); main() int i; /调用 COUNT1()12 次 cout #include #include #include main() /从文件读写字符串 FILE *fp1; char str80; cout #include #include #include void main(void) /从文件读写字符 char ch; FILE *fp1; /以写方式打开文件 D.DAT fp1=fopen(“d.dat“,“w“); if(fp1=NULL) coutch; while(ch!=*) fputc(ch,fp1);/把字符写到 fp1 指向的流文件中 cinch; fclose(fp1); if(fp1=fopen(“d.dat“,“r“)=NULL) cout #include #include #include #include void main(void) int i; char ch; FILE *fp1; /以写方式打开文件 if(fp1=fopen(“d.dat“,“w“)=NULL) coutch; while(ch!=*) fputc(ch,fp1); cinch; cout #include #include #include #define MAX 5 /show array void show_array(double x,int size) for(int i=0;i #include #include #include #define MAX 5 struct student int num; char name20; float grade; ; void show_str(student a,char *name) printf(“n“); printf(“ %s“,name); printf(“ %d“,a.num); printf(“ %s“,); printf(“ %f“,a.grade); printf(“n“); int main() FILE *fp; student st=1001,“ZhangBin“,85.5; show_str(st,“st“); /打开 D.DAT 文件 if(fp=fopen(“d.dat“,“wt+“)=NULL) cout #include #include #include int main() char ch; char str20; int n; float x; fprintf(stdout,“char strn“);/stdout,表示从标准输出文件:显示器输出 fscanf(stdin,“%c%s“,/stdin,表示从标准输入设备:键盘输入。 fprintf(stdout,“n xn“); fscanf(stdin,“%d %f“, cout const int MAX=5; /定义 STACK 类 class stack float numMAX; int top; public: void init(void) top=0; void push(float x) if(top=MAX) coutx; b.push(x); for(i=1;i const int MAX=5; class stack float numMAX; int top; public: stack(void) top=0; coutx; b.push(x); for(i=1;i #include 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(void) timer start(8.30),finish(17.30); cout class ex_class private: int iv; double dv; public: ex_class(void); ex_class(int n,double x); void set_ex_class(int n,double x); void show_ex_class(char*); ; /此处类定义如少了分号,运行时提示构造不能有返回类型 ex_class:ex_class():iv(1),dv(1.0) ex_class:ex_class(int n,double x):iv(n),dv(x) void ex_class:set_ex_class(int n,double x) iv=n; dv=x; void ex_class:show_ex_class(char*name) cout class ex static int num; public: ex()num+; ex()num-; disp_count() coutdisp_count(); ex x10; x0.disp_count(); delete p; a.disp_count(); / #include class ex_class int value; public: ex_class(int n) value=n; cout struct ex_class ex_class(int n=1):value(n) void set_value(int n) value=n; show_obj(char*name) cout #include class parent char f_name20; char m_name20; char tel10; public: parent(char *p1=“,char *p2=“,char *p3=“) strcpy(f_name,p1); strcpy(m_name,p2); strcpy(tel,p3); show_parent(void) cout #include class complex float real; float image; public: complex operator+(complex right); complex operator=(complex right); void set_complex(float re,float im); void put_complex(char *name); ; complex complex:operator +(complex right) complex temp; temp.real =real+right.real ; temp.image =image+right.image ; return temp; complex complex:operator =(complex right) this-real =right.real; this-image =right.image ; return *this; void complex:set_complex(float re,float im) real=re; image=im; void complex:put_complex(char *name) cout=0.0)cout #include /example of th friend class class YourClass friend class YourOtherClass; private: int num; public: YourClass(int n)num=n; display(char*YCname) cout class Y; class X public: void disp(Y py,char *name); ; class Y friend void X:disp (Y py,char *name); friend void putY(Y private: int num; dispY(char *name) cout class Date friend istream friend ostream coutdt.mo ; coutdt.da ; return input; ostream cout class ex_class int a; double b; public: ex_class(int n=1,double x=1.0):a(n),b(x) void show_value(char *name) coutshow_value(“p-obj1“); p= (*p).show_value(“(*p)obj2“); p=new ex_class; p-show_value(“p-new“); delete p; / #include class Box int width,height; public: void SetWidth(int w) width=w; void SetHeight(int h) height=h; int GetWidth()return width; int GetHeight()return height; ; class ColoredBox:public Box int color; public: void SetColor(int c) color=c; int GetColor()return color; ; main(void) ColoredBox cbox; cbox.SetColor(3);/用自己的成员函数 cbox.SetWidth(150);/用基类成员函数 cbox.SetHeight(100); cout class First int val1; public: First(void) cout class First int num; float grade; public: First(int n,float v):num(n),grade(v) cout class First int val1; public: First() cout class First int val1; protected: void SetVal1(int v) val1=v; public: show_First(void) cout enum Color Red,Yellow,Green,White; class Circle float radius; public: Circle(float r)radius=r; float Area() return 3.1416*radius*radius; ; /桌子 Table 类定义 class Table float height; public: Table(float h)height=h; float Height() return height; ; /圆桌类 RoundTable 定义 class RoundTable:public Table,public Circle Color color; public: RoundTable(float h,float r,Color c); int GetColor() return color; ; RoundTable:RoundTable(float h,float r,Color c):Table(h),Circle(r) color=c; main() RoundTable cir_table(15.0,2.0,Red); cout enum Color Red,Yellow,Green,White; class Circle float radius; public: Circle(float r) radius=r; cout class Base public: virtual void a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论