C面向对象程序设计源代码.docx
C+面向对象程序设计源代码.txt心是自己的,干嘛总被别人伤没有伞的孩子必须努奔跑鬻敷衍旳青春 总是想太多 怨,只怨现实太现实为什么在起要两个人的同 意、而分手只需要一个人第1章 程序设计与C+概述 /el_7. cpp #include <iostream. h> void main() ( int a, b, c; cin»a»b;输入两个整数c = max (a, b); cout“max is”cendl;/输出信息) int max(int x,int y) ( int z; z = (x>y?x:y); return z; ) /el 8. cpp #include <iostream. h> void main() ( float r;/定义浮点型变量r,用于存放圆的半径cout ”输入圆的半径:"; cin»rJ从键盘上输入圆的半径送给变量rcout"半径为"r”的圆的面积=”3. 14159f * r * r'n;输出运算结果 ) /el_9. cpp #include <iostream. h> float area (float r)定义求半径为r的圆的面积的函数area return 3. 14159f * r * r; void main() ( float r;定义浮点型变量r,用于存放圆的半径cout输入圆的半径:;显示提示信息,提示用户输入数据cin»r;从键盘上输入圆的半径送给变量rcout半径为"r“的圆的面积="area(r)、n'输出运算结果) /el_10. cpp #include <iostream. h> class Circle/定义一个计算圆的面积的类Circle private: float r;定义成员数据变量r,用于存放圆的半径public:Circle(float a) r = a; Circle ( ) void SetRadius(float a) r = a; float GetRadius() return r; 定义构造函数,用于创建和初始化对象 定义析构函数,用于清理和撤销对象 定义成员函数,用于设置圆的半径r 定义成员函数,用于获取圆的半径rfloat AreaO return 3. 14159f * r * r; 定义成员函数Area,用于计算圆的面积);void main()float r;cout 输入圆的半径:;cin»r;Circle c(r);定义浮点型变量r,用于存放圆的半径 显示提示信息,方便用户输入数据 从键盘上输入圆的半径送给变量r 定义Circle类的对象ccout 半径为 c. GetRadius () ”的圆的面积 c. Area。、n' /输出运算结果第2章C+程序设计基础/e2_l. cpp#include <iostream. h>const float PI = 3. 1416void main ()(float r,c,s;cin»r;c=2*PI*r;s=PI*r*r;cout"c="c”t"s="s"、n"/e2_2. cpp#include <iostream. h>void main()(char cl,c2;int dl, d2;cout "请输入两个字符:、n"cin»cl>>c2;dl=int(cl);将cl强制转换为整型值赋给dld2=int (c2):将c2强制转换为整型值赋给d2cout输出整数对应的字符:、n"cout<dl对应的字符是:"cl«"n"cout«d2(对应的字符是:"«c2«"n*;/e2_3. cppinclude <iostream. h>void main ()cout”输出个大数:'80000endl;cout”输出个分数:tt*«(float)5/8«endl;cout"输出个特大数:(double)8000*7000«endl;)第3章 程序流程控制/e3_l. cpp#include <iostream. h>void main()(int numl, num2, max;couty input two numbers:"cin»numl»num2;max=num1;if(max<num2) max=num2;cout“max="maxendl;)/e3 2. cpp#include <iostream. h>void main ()(int numl, num2;cout<<"input two numbers:"cin>>numl>>num2;if(numl>num2)cout"max= <<numl<<endl;elsecout"max二 num2endl;/e3_3. cpp#include <iostrea. h>void main()float score;cout<<please input a score: *;cin>>score;if(score>=85)cout<<"the score "<<score«"is An"else if(score>=75)cout the score "<<score<<"is Bn"else if(score>=60)cout<<"the score ”score is Cn"elsecout the score ”score“is Dn"/e3 4. cpp#include <iostream. h>void main()(int num;cin»num;switch(num)case 1: cout«"Mondayn"break;case 2: cout«"Tuesdayn" break;case 3: cout“Wednesday'n ;break; case 4: cout<<"Tursdayn"break;case 5: cout«"Fridayn*; break;case 6: cout«"Saturdayn" break; case 7: cout«"Sundayn"break; default: cout error'n ;)/e3_5. cpp#include <iostream. h>void main ()int sum=0, i;i=l;while (i<=100)(sum=sum+i;i+;cout"sum= <<sum«endl;/e3_6. cpp#include <iostream. h>void main()(int m, n;for(m二1;m=9;m+)for(n=l;n<=9;n+)cout«m*n«*t*;cout<<endl;/e3_7. cpp#include <iostream. h> void main() int m, n;cout«*please input the number m:n”;cin»m;for(n=2;n<=m/2;n+)if(m%n=0) break;if(n>m/2)cout<<m«* is a prime'n”;elsecout<<m«* is not a prime number'n”;/e3_8. cpp#include <iostream. h>void main() int m, n;cout the prime number is:n”;for(m=3;m<100;m+=2)(for (n=2;n«m/2;n+)if(m%n=0) break;if(n<m/2) continue;coutm"、t"/e3_9cpp#include <iostream. h> void main() int x, y, z, t;coutplease input 3 numbers: cin»x»y»z;if (x>y)t=x;x=y;y=t; if(x>z)t=z;z=x;x=t; if(y>z)t=y;y=z;z=t;交换x,y的值交换x,z的值交换z,y的值cout x 、t' y 、 z);/e3_10. cpp#include <iostream. h> void main() int day, month, year, sum, leap;coutplease input year, month, dayn/z;c in>>year»month»day;switch (month)先计算某月以前月份的总天数(case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default: cout、 data error'"break;sum=sum+day;再加上某天的天数if (year%400=0| | (year%4=0&&year%100! =0)判断是不是闰年leap=l;elseleap=0;if (leap=l&&month>2)如果是闰年且月份大于2,总天数应该加一天sum+;cout"It is the "<<sum«"th day."/e3_l1. cpp#include <iostream. h>void main()int a, b, m, n, t, r;cout<<"please input 2 numbers:n"cin>>m>>n;if(m<n)t=m;m=n;n=t;将 m, n 中大值赋给 ma=m; b=n;while(b!=0)利用辗除法,直到b为为止r=a%b;a=b; b=r;cout«vgreatest common divisor:«a«endl;/e3_12. cpp#include <iostream. h>void main()int i, j, k;for(i=l;i<=4;i+)for(j=l;j<=2*i-l;j+) cout"*"for(k=l;k<=6-2*i;k+) cout«" cout«endl;for (i=l;i=3;i+)for(j=l;j<=7-2*i;j+) cout”;for(k=l;k<=2*i;k+) cout” cout<<endl;第4章数组/e4_l. cpp#include <iostream. h>include < ioman ip. h> 使用控制符 setw,要包含头文件 iomanip. hvoid main()(int i, a7;coutplease input 7 numbers:n/z;for(i二;i二6;i+)cin»ai;for(i=0;i<=6;i+)cout<<setw(2)<<ai;/e4_2. cpp#include <iostream. h>void main() int x10, i, j;j=0;计数器初值赋cout please input 10 numbers: zz<<endl;for(i=0;i<=9; i+)顺序的输入数组x的10个元素cin»xi;for (i=0; i=9; i+)逐个的比较数组的各个元素和的大小if(xi>0) j+;cout"j="j;)/e4_3. cpp#include <iostream. h>void main()(int al1;int i,n,t;/i表示数组下标,n表示遍历次数,t用来交换数据cout<<"input 10 numbers:"«endl;for(i=l;i<ll;i+)cin»ai;cout<<endl;for(n=l;n<=9;n+)for(i=l;i<=10-n;i+) if(ai>ai+l)t=ai ;ai=ai+l ;ai+l=t; cout“the sorted numbers:,z<<endl;for(i=l;i<ll;i+)cout<<ai«""coutendl;/e4_4. cpp#include <iostream. h>void main ()(int a2 3=1, 5,9, 2, 6,8);int b32,i,j;cout<<"Array a: "<<endl; for(i=0;i2;i+)(for(j=0;j<3;j+)(cout«ai j«""bji=aij;cout<<endl;cout<<"Array b:"<<endl;for (i=0;i<3;i+)输出原来数组的数据将两个数组元素相互交换输出交换后的数据元素for(j=0;j2;j+)cout<<bi j« cout<<endl;/e4_5. cpp#include <iostream. h> void main() float score63, sv6, x;int i,j;cout“input the 6 student* s score:"«endl;for (i=0;i<6;i+)cin»x;scorei j=x;for(i=0;i6;i+)(svi=0;for(j=0;j<3;j+)svi=svi+scoreij;svi=svi/3;for(i=0;i<6;i+)for(j=0;j<3;j+)输入每个学生的每门课成绩求每个学生的总成绩求每个学生的平均成绩输出每个学生的平均成绩cout<<"Student "<<i+l«"="«svi<<endl;/e4_7. cpp#include <iostream. h> #include <string. h> void main()(char str20;int i, j;cout please input a string:"cin. get (str, 20);j=strlen(str);for(i=j-l;i>=0;i) cout. put (str i);/e4_8. cpp#include <iostream. h>#include <string. h> void main()char str120, str220, str350;cin. getline(strl, 20);cin. getline(str2, 20);if (strcmp(strl, str2) <0)(strcpy(str3, strl);strcat(str3, str2);)else(strcpy(str3, str2);strcat(str3, strl);)cout<<str3;/e4 9. cpp#include <iostream. h>void main()(float a33, sum=0, x;int i,j;cout“input 3*3 Matrix: *«endl;for (i=0;i3;i+)for(j=0;j<3;j+)(cin>>x;aij=x;for(i=0;i3;i+)sum=sum+aii+ai2-i;cout«"the sum is:"«sum<<endl;/e4_10. cpp#include <iostream. h>#include <cstring>void main ()char str60;int i, j;cin. get(str, 60);i=0;j=strlen(str)-l;while(stri= ') i+;寻找前面第一个不是空格的字符while(strj='')j"!寻找后面第一个不是空格的字符while(i<j && stri=strj)前后对应逐个的比较 i+;j;if(i<j)cout"No"endl;elsecout«"Yes"«endl;/e4_ll. cpp#include <iostream. h>void main ()int i, j, a10, t, k;cout<<"input 10 numbers: "«endl;for (i=0;i<10;i+)cin»ai;for (i=0; i<9; i+)(k=i;for(j=i+l;j<10;j+) if(aj<ak)k二 j;if(k!=i)输入10个要排序的数据排序总共进行了 9趟初始化最小数的下标从当前数的后面寻找最小数的下标记录新的最小数的下标 t=ai ;ai=ak ;ak=t; 第 i 个数和最小数交换 for(i=0;i10;i+)输入排序后的10个数cout<<ai«"" 第5章模块设计 程序1 /e5_l. cpp #include <iostream. h> void main() int x, y, z; cin»x»y; z=x+y; cout<<"n The sum is"<<z; /程序2 #include <iostream. h> int sum(int a, int b)被调用函数sum的定义int s;s=a+b;return s;void main() int x, y, z;int sum(int, int):函数声明cin»x>>y;z=sum(x, y)J函数调用cout<<endl<<* The sum is ”z; /e5_2. cppinclude <iostream. h>void swap(int x, int y);函数声明void main() int a=50, b=60;cout"a="a",b="«b«endl;swap (a, b):调用函数cout"a="a",b="«b«endl;void swap(int x, int y)定义函数(int z;z 二x; x二y; y 二z; /e5_3. cpp#include <iostream. h>int add(int x, int n)定义函数(return (x+n);void main() int add(int x, int n):函数声明int i;int a10 = 9, 8, 7, 6, 5, 4, 3, 2, 1, 0;for(i=0;i<10;i+)ai=add(ai, i);数组元素作函数参数for(i=0;i<10;i+)cout«ai«* "/e5_4. cpp#include <iostream. h>void swap(int *x,int *y); void main()函数声明,形参x和y为指针变量调用函数,实参为变量a和b的地址定义函数int a=50, b=60;cout"a=”a",b="«b<<endl;swap (&a, &b);cout«"a="<<a<<", b="«b«endl;void swap(int *x,int *y) int z;Z=*X;*X=*y;*y=z;/e5_5. cpp#include <iostream. h>float average(float array10);void main() (float score10, aver;int i;cout input 10 scores:z,«endl;for(i=0;i10;i+)cin>>scorei;cout<<endl;aver=average(score);cout«"average score is aver;float average(float array10)(int i;float aver, sum=0. 0;for(i=0;i10;i+)sum=sum+arrayi;aver=sum/10;return(aver);/e5_6. cpp函数声明函数调用,维数组名score作为参数函数定义#include <iostream. h>函数声明int max_element(int array44); void main() (int p4 4 = 1,22, 3, 4, 3,4,6, 18, 6, 5, 2,9, 0,6, 1,34;coutmax is :*<<max_element (p)<<endl; 函数调用,二维数组名 p 作为参数int max element (int array 4 4)函数定义(int i, j, max;max=array00;for(i=0;i<4;i+)for(j=0;j<4;j+) if(arrayij>max) max=arrayij;return(max);/e5_7. cpp#include <iostream. h>void print (int p);函数声明void main() int score 3 4 = 87, 81, 65, 74, 93, 84, 72, 88, 76, 85, 92, 99; int num;cout"input N0(0 - 2) cin>>num;print (scorenum);函数调用,二维数组列地址作为参数void print(int p)函数定义(int i;for (i=0;i<4;i+) cout pi ",";/e5_8. cpp#include <iostream. h>void main() (int i=2, j=3, k;k=i+j;(int k=14;if(i=2) cout«k<<endl;cout<<i<<", "<<k<<endl;/e5_9. cpp#include <iostream. h>int si,s2,s3;int vs( int a, int b, int c)int v;v=a*b*c;sl=a*b;s2=b*c;s3=a*c;return v;)void main()int v, 1, w, h;cout input length, width and height:*;cin»l>>w»h;v=vs (1, w, h);cout"v="v" sl="«sl<<" s2="«s2«" s3="«s3<<endl;/e5_10. cpp#include <iostream. h>int fun(int x, int y, int z) 函数定义(int v;v=x*y*z;return v;)void main ()extern int w,h:外部变量说明,以扩展变量w、h的作用范围int 1=5;cout«"v=*«fun(l, w, h); 函数调用int 1=3, w=4, h=5;外部变量定义/e5_l1. cpp#include <iostream. h>void main()(auto int a, s=10, p=10;cout input a number:;cin»a;if(a>0)(auto int s, p;s=a+a;p=a*a;cout"s=”s" p="<<p«endl;)cout"s=”s" p二"p;/e5_12. cpp#include <iostream. h> void main() int i;void f():函数声明for(i=l;i<=4;i+) f()i函数调用void f()函数定义auto int j=0;+j; cout«j« 工/e5 14.cpp#include <iostream. h>float sum(float x,float y, float z);float average(float a, float b, float c);void main () (float numl, num2, num3;cout“Input the scores of the three courses:" cin>>numl>>num2>>num3;cout«"The total score is:*<<sum(numl, num2, num3)«endl; cout<<"The average score is:"<<average(numl, num2, num3)<<endl; float sum(float x,float y,float z) (float m=x+y+z; return m;float average(float a, float b, float c) (float n= (sum(a, b, c)/3;return n;/e5_15. cpp#include <iostream. h> long fac(int n) long t;if(n=l)|(n=0)return 1;elset=n*fac(n-l);return t;)void main() (long fac(int n);int m;long y;cout Enter m:"cin»m;if(m<0)cout Input data Error! *«endl;elsey=fac(m);coutm"!="yendl;)/e5 16. cpp#include <iostream. h>iomanip. h#include < ioman ip. h>使用控制符setw,要包含头文件#define MAX 100int fun( int m, int primeMAX) int i, j=0, k;for ( k=2;k<m;k+) for(i=2;i<k;i+) if(k%i=O) break; if(i>=k) primej+=k;return j;void main() (int m, i, sum;int primeMAX;cout<<"input a integer number:"cin>>m;sum=fun(m, prime);cout<<"The prime number is:"<<endl;for (i=0;i<sum;i+)if(i%5=0&&i!=0) cout«endl;cout<<setw(5)<<primei;)/e5_17. cpp#include <iostream. h>#include <string. h> void f (char s, char t ) (int i, si;sl=strlen(s);for (i=0;i<=sl;i+) ti=si;for (i=0;i<sl;i+) tsl+i=ssl-i-l;tsl+sl= 0J ;void main()(char s100, t100;cout Please enter string s:" cin. get (s, 100);f (s, t);cout“The result is:"«t<<endl; /e5_18. cpp#include <iostream. h> int max(int x,int y) ( int z;z = (x>y?x:y); return z;float max(float x, float y)( float z; z = (x>y?x:y); return z;void main()(int numl, num2;float num3, num4;cin»numl»num2»num3>>num4;cout max (numl, num2)=/<<max (numl, num2) «endl;coutmax(num3, num4)="max(num3, num4)<<endl;)/e5_19. cpp#include <iostream. h> void main()(int max(int a, int b, int c=0);int a, b, c;cin>>a>>b»c;cout<<"max (a, b, c) = "<<max (a, b, c) «endl;cout<<"max (a, b) = "max (a, b) «endl;int max(int a, int b, int c)(if (b>a) a=b;if (c>a) a=c;return a;/e5_20. cpp#include <iostream. h>template <class T>T max (T x, T y)return x>y?x:y;void main()(int numl, num2;float num3, num4;cin>>numl>>num2>>num3>>num4;cout«"max(numl, num2)="«max (numl, num2) endl;调用模板函数,此时 T 被 int 取代 cout“max(num3, num4) ="<<max (num3, num4) endl;调用模板函数,此时 T 被 float 取代 )/e5_21. cpp#include <iostream. h>template <class Tl, class T2>T2 max (T1 x, T2 y)(return x>y?x:y;)void main()int numl, num2;float num3, num4;cin»numl»num2»num3>>num4;cout«>zmax(numl, num2)=*<<max(numl, num2)«endl:调用模板函数,此时 Tl, T2被int取代cout«*max(num3, num4) =*<<max(num3, num4)endl;调用模板函数,此时 Tl, T2 被 float 取代 /e5_22. cpp#include <iostream. h>define R 1void main() double c, r, s;cout input a number: *;cin>>c;#if Rr=3. 14159*c*c;cout area of round is: rendl;#elses=c*c;cout area of square is: <<s«endl;ttendif/e5_23. cpp#include <iostream. h>define S(a,b) (a>b)?(a): (b) 定义带参数的宏名 Svoid main() ( int x, y; cin»x»y;cout«S (x, y);将 S (x, y)替换成(x>y) ? (x): (y)第