《C++程序设计经典300例2o.docx》由会员分享,可在线阅读,更多相关《C++程序设计经典300例2o.docx(61页珍藏版)》请在三一办公上搜索。
1、C+程序设计经典300例2o1.* /类的多态实现 #include using namespace std; class salary public: salary salary virtual void pay private: ; class member1:public salary public: member1(double a)_total=a; member1 void pay cout每月给员工1的薪水为:_total元endl; private: double _total; ; class member2:public salary public: member2(dou
2、ble a)_total=a; member2 void pay cout每月给员工2的薪水为:_total元pay; sa2-pay; 2* /实例113 动物对象进化 #include using namespace std; class animal_ancestor public: animal_ancestor animal_ancestor void respire/呼吸 cout可以呼吸endl; ; class ape:public animal_ancestor public: ape ape void move/行走 cout可以行走endl; ; class human:
3、public ape public: human human void miss cout可以想念某人endl; ; void main human _human; cout人类; _human.respire; _human.move; _human.miss; 3.* /实例112 求圆的面积和周长 #include using namespace std; #define Pi 3.14159265 class circle public: double area(double x) return Pi*x*x; double circumfer(double y) return 2*P
4、i*y; public: double radius; ; void main circle _circle; cout_circle.radius; cout圆的面积为:_circle.area(_circle.radius)endl; cout圆的周长为:_circle.circumfer(_circle.radius)endl; 4-* /实例111 乡村生产总值) #include using namespace std; class Cun_product public: Cun_productveget_result=0;fruit_result=0;grain_result=0;
5、 Cun_product(double x,double y,double z) veget_result=x; fruit_result=y; grain_result=z; Cun_product friend Cun_product operator+(Cun_product cun1,Cun_product cun2);/重载友元运算符函数 void output coutveget_result,fruit_result,grain_resultendl; private: double veget_result; double fruit_result; double grain_
6、result; ; Cun_product operator+(Cun_product cun1,Cun_product cun2) Cun_product add; add.veget_result=cun1.veget_result+cun2.veget_result; add.fruit_result=cun1.fruit_result+cun2.fruit_result; add.grain_result=cun1.grain_result+cun2.grain_result; return add; void main Cun_product cun_A(1.2,3,4),cun_B
7、(2.3,5,1),cun_C(1.4,3,2),cun; cun=cun_A+cun_B+cun_C; coutABC村总共的蔬菜、水果和稻谷产量分别为:; cun.output; 5* /实例110 产量统计 #include using namespace std; class Cun_A public: Cun_A(double x,double y,double z) acer_veget=x; acer_fruit=y; acer_grain=z; Cun_A void SetPerProduct(double a,double b,double c) output_per_ace
8、rV=a; output_per_acerF=b; output_per_acerG=c; void veget double result=acer_veget*output_per_acerV; coutA村蔬菜的产量为:resultendl; void fruit double result=acer_fruit*output_per_acerF; coutA村水果的产量为:resultendl; void grain double result=acer_grain*output_per_acerG; coutA村稻谷的产量为:resultendl; private: double a
9、cer_veget;/蔬菜作物的占地 double output_per_acerV;/每亩地产蔬菜多少 double acer_fruit;/水果作物的占地 double output_per_acerF;/每亩地产水果多少 double acer_grain;/稻谷作物的占地 double output_per_acerG;/每亩地产稻谷多少 ; void main Cun_A A(1.2,2,3); A.SetPerProduct(500,400,700); A.veget; A.fruit; A.grain; 6.* #include using namespace std; void
10、 cal_num(double *a, int *b, int number) for(int i=0;inumber;i+) if(ai=60 & ai=70 & ai=80 & ai90) b3+; else/优秀 b4+; void main double math_score35;/班级有35个学生 int num5=0;/5个等级的人数 cout请输入35个学生的成绩:endl; for(int i=0;imath_scorei; cal_num(math_score,num,35); cout60分的人数:num0endl; cout介于6070分的人数:num1endl; cou
11、t介于7080分的人数:num2endl; cout介于8090分的人数:num3endl; cout90分的人数:num4endl; 7* #include using namespace std; struct ticket char train_number5;/车次 char train_name50;/从哪儿到哪儿 int yingwo_num;/硬卧票数 float yingwo_cash;/硬卧钱 int yingzuo_num;/硬座票数 float yingzuo_cash;/硬座钱 ; ticket Ticket5;/数据库目前只存5条项目 void init strcpy
12、(Ticket0.train_number, T56); strcpy(Ticket0.train_name,西安-北京西); Ticket0.yingwo_num=100; Ticket0.yingwo_cash=263.5; Ticket0.yingzuo_num=200; Ticket0.yingzuo_cash=148.5; strcpy(Ticket1.train_number, K126); strcpy(Ticket1.train_name,西安-天津); Ticket1.yingwo_num=60; Ticket1.yingwo_cash=289.5; Ticket1.ying
13、zuo_num=100; Ticket1.yingzuo_cash=163.5; strcpy(Ticket2.train_number, T166); strcpy(Ticket2.train_name,西安-上海); Ticket2.yingwo_num=150; Ticket2.yingwo_cash=321.5; Ticket2.yingzuo_num=240; Ticket2.yingzuo_cash=180.5; strcpy(Ticket3.train_number, T266); strcpy(Ticket3.train_name,西安-广州); Ticket3.yingwo_
14、num=50; Ticket3.yingwo_cash=414; Ticket3.yingzuo_num=40; Ticket3.yingzuo_cash=236; strcpy(Ticket4.train_number, T114); strcpy(Ticket4.train_name,西安-南京); Ticket4.yingwo_num=10; Ticket4.yingwo_cash=270.5; Ticket4.yingzuo_num=20; Ticket4.yingzuo_cash=152.5; void query char checi5; bool flag=true; bool
15、whether=false;/是否有 while(flag=true) coutcheci; for(int i=0;i5;i+) if(strcmp(Ticketi.train_number,checi)=0) whether=true; coutTicketi.train_number Ticketi.train_name; cout Ticketi.yingwo_num Ticketi.yingwo_cash; cout Ticketi.yingzuo_num Ticketi.yingzuo_cashendl; i=5; else whether=false; if(whether=tr
16、ue)/有 else cout该车次在系统中没有相应信息,请检查后输入endl; coutx; if(x=0) flag=false; cout退出查询系统endl; else if(x=1) int main cout-火车票当日售卖查询系统-endl; cout现存火车票信息只有从西安发的列车,; cout目的地只有北京西、上海、天津、广州和南京。endl; cout请前往上述5个目的地的旅客进行查询endl; cout车次-始地-目的地-硬卧剩票; cout-硬卧价钱-硬座剩票-硬座价钱endl; init;/初始化信息 query;/查询 return 0; 8* #include u
17、sing namespace std; void fun1 cout功能1实现endl; void fun2 cout功能2实现endl; void fun3 cout功能3实现endl; void function(int a) switch(a) case 1: fun1; break; case 2: fun2; break; case 3: fun3; break; default: break; void main int choice; cout本实例实现最基本的互动式程序,n; cout可实现的功能有3种:1-功能1,2-功能2,3-功能3,n; bool flag=true;
18、while(flag=true) cout请输入功能编号:choice; function(choice); coutx; if(x=0) else if(x=1) flag=false; cout退出成功endl; 9* #include using namespace std; int main(int argc,char *argv) for(int i=0;iargc;i+) coutArgument i is argviendl; if(argc=5) cout一共有4个命令需要106.exe执行endl; else cout不是4个命令endl; return 0; 10-* #i
19、nclude using namespace std; int main(int argc,char *argv) for(int i=0;iargc;i+) coutArgument i is argviendl; return 0; 11* #include #include #include using namespace std; void generate_lower char lower; int lower_index; lower_index=1+(int)(26.0*rand/(RAND_MAX+1.0); lower=char(lower_index+96); coutlo
20、wer; void generate_upper char upper; int upper_index; upper_index=1+(int)(26.0*rand/(RAND_MAX+1.0); upper=char(64+upper_index); coutupper; void generate_number int number; number=0+(int)(9.0*rand/(RAND_MAX+0.0); coutnumber; int main srand(int)time(0); int num; int upper; int lower; int number; while
21、(1) coutnum; cout在这numupperlowernumber; while(upper) generate_upper; upper-; while(lower) generate_lower; lower-; while(number) generate_number; number-; coutendl是否继续产生随机验证码?endl; cout1.是 2.否y; coutendl; if(y=1) if(y=2) cout退出系统endl; break; return 0; 12* #include using namespace std; double jiecheng
22、(int n) if(n=0)/0的阶乘 return 1; else return n*jiecheng(n-1); int main int m=100; coutjiecheng(m)endl; return 0; 13* #include using namespace std; void add cout2+3=2+3endl; void substract cout2-3=2-3endl; int add1 cout2+3=2+3endl; return 0; int sub1 cout2-3=2-3endl; return 0; void main atexit(add); at
23、exit(substract); _onexit(add1); _onexit(sub1); coutmain中endl; 14* #include using namespace std; int add(int a,int b) return a+b; int add(int a,int b,int c=10) return a+b+c; double add(double a,double b) return a+b; void main cout2+3=add(2,3)endl; cout2+3+4=add(2,3,4)endl; cout2.2+3.3=add(2.2,3.3)end
24、l; 15* #include using namespace std; class xingge/抽象类 public: xingge/构造函数 virtual void talk=0;/纯虚函数 virtual void kind_hearted=0;/纯虚函数 ; class child1:public xingge/子类child1 public: child1 void talk cout沉默寡言endl; void kind_hearted cout热心肠endl; ; class child2:public xingge/子类2 public: child2 void talk
25、cout碎碎念endl; void kind_hearted cout热心肠endl; ; void main xingge *_xg;/抽象类指针 child1 _c1;/child1实例化 child2 _c2;/child2实例化 _xg=&_c1;/取址child1 coutchild1的性格:talk; _xg-kind_hearted; _xg=&_c2;/取址child2 coutchild2的性格:talk; _xg-kind_hearted; 16* #include using namespace std; template void output(T a)/模板输出函数
26、coutaendl; void main output(1);/输出整数变量 output(3.4);/输出浮点变量 17* #include using namespace std; #define M 10 #define N 10 class Maze public: Maze;/构造函数 Maze/析构函数 void PrintMaze;/打印迷宫 void GetPos;/获取出入口位置 void SearchMaze;/获取迷宫路径 bool EvaluateMaze(int i,int j);/判断当前点是否可行 private: int mazeMN;/迷宫 int start
27、_row;/入口行 int start_col;/入口列 int end_row;/出口行 int end_col;/出口列 bool succeed;/是否找到出口 ; Maze:Maze int copyMN=1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,1, 1,0,1,0,1,0,1,0,0,1, 1,0,0,1,0,1,1,1,0,1, 1,1,0,1,0,1,1,0,0,0, 1,0,0,0,0,0,1,0,0,1, 1,1,1,1,1,1,1,0,1,1, 1,1,0,1,0,1,1,0,0,1, 1,0,0,0,0,0,1,1,0,1, 1,1,
28、1,1,1,1,1,1,1,1; memcpy(maze,copy,sizeof(maze);/迷宫初始化,0为可行,1为不可行 succeed=false;/初始化为没有 void Maze:PrintMaze cout显示迷宫10*10:endl; for(int i=0;iM;i+)/行 for(int j=0;jN;j+)/列 if(mazeij=1)/不可行 cout1; else/可行 cout0; coutendl;/下一行 void Maze:GetPos coutstart_row; coutstart_col; coutend_row; coutend_col; void
29、Maze:SearchMaze if(EvaluateMaze(start_row,start_col)=false)/没有找到出口 coutendl没有找到出口n; else/找到 coutendl显示路径:endl; for(int i=0;iM;i+)/行 for(int j=0;jN;j+)/列 if(mazeij=1)/打印不可通行位置 cout1; else if(mazeij=2)/打印路径 cout*; else/可通行的但不是路径为空格 cout ; coutendl; 18* #include matrix.h void main int n,m;/行列 int *data
30、;/矩阵内存 coutn;/行 coutm;/列 data = new intn*m; /申请内存 cout请初始化n行m列矩阵元素endl; for (int i=0; in; i+) for (int j=0; jdatai*m+j; /初始化 CMatrix m1(n,m,data);/声明类对象1 CMatrix m2(m1);/声明类对象2 cout使用拷贝构造函数后,m2对象的矩阵为endl; m2.output;/输出 19* #include using namespace std; class A private: int num;/成员变量 public: A(int nu
31、m)/构造函数 num=num; /this-num=num;/初始化 void output cout私有成员变量num=numendl; ; void main A a(10);/定义类对象 a.output;/输出 20* #include using namespace std; class Line private: int x1,y1,x2,y2; public: Line;/默认构造函数 Line(int a=0,int b=0,int c=0,int d=0);/带参数的构造函数 void printPoint;/打印两点坐标 double getDis;/计算两点距离 ; i
32、nline Line:Line(int a,int b,int c,int d)/初始化点 x1=a; y1=b; x2=c; y2=d; inline void Line:printPoint coutA:(x1,y1)endl; coutB:(x2,y2)endl; inline double Line:getDis double dis; dis=sqrt(double(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); return dis; void main Line line(10,68,-5,6); line.printPoint; cout两点距离:line.ge
33、tDisendl; 21.* #include #include #include using namespace std; class TIME private: double timeM;/总时间,换算为毫秒 int time_H;/时 int time_M;/分 int time_S;/秒 double millisecond;/毫秒 public: void cal;/计算当前时间 double getSystime;/获取总时间 int getH;/获取时 int getM;/获取分 int getS;/获取秒 ; void TIME:cal struct _timeb timebu
34、ffer;/时间缓冲变量 char *timeline; unsigned short millitml; char temp_H2;/字符变换量 char temp_M2; char temp_S2; _ftime(&timebuffer);/获取缓冲量 timeline = ctime(&(timebuffer.time);/转换为char*型 millitml = timebuffer.millitm;/获取毫秒值 millisecond = (double)millitml/1000;/变换为真正的毫秒 for(int i = 0; i 2; i+) temp_Hi = timelin
35、ei+11;/从第11个字节开始为时的字符值,下同 for(int j = 0; j 2; j+) temp_Mj = timelinej+14;/分的字符值 for(int k = 0; k 2; k+) temp_Sk = timelinek+17;/秒的字符值 time_H = atoi(temp_H);/时变换为整型 time_M = atoi(temp_M);/分变换为整型 time_S = atoi(temp_S);/秒变换为整型 timeM = double(time_H) * 3600 + double(time_M) * 60 + double(time_S) + millisecond;/计算总时间 double TIME:getSystime return t