C++程序设计第10章--谭浩强
- 格式:pdf
- 大小:3.35 MB
- 文档页数:69
谭浩强c++程序设计课后答案娄警卫第一章1.5题#include <iostream> using namespace std; int main(){cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0;1.6题#include <iostream> using namespace std; int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b="; cout<<c;cout<<endl;return 0;}1.7七题#include <iostream> using namespace std; int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl; return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}1.8题#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}1.9题#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y); cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int z;z=x+y;return(z);}第二章2.3题#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2.4题#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is very interesting!\""<< '\n';return 0;}2.7题#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0;}2.8题#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl;return 0;}第三章3.2题#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios:: right)<<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3.3题#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3.4题#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3.4题另一解#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3.5题#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;return 0;}3.8题#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0;}3.9题include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:";cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3.9题另一解#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a和b中的大者存入temp中*/max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,最大者存入max*/cout<<"max="<<max<<endl;return 0;}3.10题#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else// x≥10{y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;}cout<<endl;return 0;}3.11题#include <iostream>using namespace std; int main (){float score;char grade;cout<<"please enter score of student:"; cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is "<<grade<<endl;return 0;}3.12题#include <iostream>using namespace std;int main (){long int num;intindiv,ten,hundred,thousand,ten_thousand,pla ce;/*分别代表个位,十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):"; cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1 000;hundred=(int)(num-ten_thousand*10000-tho usand*1000)/100;ten=(int)(num-ten_thousand*10000-thousan d*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousa nd*1000-hundred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","< <hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten _thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten <<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<en dl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<en dl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3.13题#include <iostream>using namespace std;int main (){ long i; //i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10万元时的奖金bon2=bon1+100000*0.075; //利润为20万元时的奖金bon4=bon2+100000*0.05; //利润为40万元时的奖金bon6=bon4+100000*0.03; //利润为60万元时的奖金bon10=bon6+400000*0.015; //利润为100万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1;//利润在10万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60万元至100万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3.13题另一解#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10; int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break;case 2:case3:bonus=bon2+(i-200000)*0.05;break;case 4:case5:bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break;case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3.14题#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0;}3.15题#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbers n,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n中, 小数放在m中}p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用while (m!=0) //求n和m的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p是原来两个整数的乘积return 0;}3.16题#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<", other:"<<other<<endl;return 0;}3.17题#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn为i个a 组成数的值sn=sn+tn; //赋值后的sn为多项式前i项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3.18题#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3.19题#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3.20题#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数{n=0; // n用来累计a的因子的个数s=a; // s用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i是否为a 的因子if (a%i==0) // 如果i是a的因子{n++; // n加1,表示新找到一个因子s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10{case 1:k1=i; break; // 找出的笫1个因子赋给k1case 2:k2=i; break; // 找出的笫2个因子赋给k2case 3:k3=i; break; // 找出的笫3个因子赋给k3case 4:k4=i; break; // 找出的笫4个因子赋给k4case 5:k5=i; break; // 找出的笫5个因子赋给k5case 6:k6=i; break; // 找出的笫6个因子赋给k6case 7:k7=i; break; // 找出的笫7个因子赋给k7case 8:k8=i; break; // 找出的笫8个因子赋给k8case 9:k9=i; break; // 找出的笫9个因子赋给k9case 10:k10=i; break; // 找出的笫10个因子赋给k10}}if (s==0) // s=0表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1表示a至少有2个因子if (n>2) cout<<","<<k3; // n>2表示至少有3个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3表示至少有4个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}}return 0;}3.20题另一解#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3.21题#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;} 3.22题#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3.23题#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:"; cin>>a; // 输入a的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3.24题#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行}for (i=0;i<=2;i++) // 输出下面3行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3.25题#include <iostream>using namespace std;int main(){char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<"B--"<<j<<" C--"<<k<<endl;return 0;}第四章4.1题#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4.2题#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float); void equal_to_zero(float,float);void smaller_than_zero(float,float); float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl; }else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0时方程的根*/{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程的根*/{x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0时方程的根*/{p=-b/(2*a);q=sqrt(-disc)/(2*a);}4.3题#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl;return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4.4题#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<e ndl;return 0;}int fac(int n){int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4.5题#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl;return 0;}double e(double x){return exp(x);}4.6题#include <iostream>#include <cmath>using namespace std;int main(){doublesolut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d){double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4.7题#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";cin>>n;godbaha(n);return 0;}void godbaha(int n){int prime(int);int a,b;for(a=3;a<=n/2;a=a+2){if(prime(a)){b=n-a;if (prime(b))cout<<n<<"="<<a<<"+"<<b<<endl;}}}int prime(int m){int i,k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0) break;if (i>k) return 1;else return 0;}4.8题#include <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);else if (n==1)return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n);}4.9题#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:"; cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl;hanoi(m,'A','B','C');return 0;}void hanoi(int n,char one,char two,char three) //将n个盘从one座借助two座,移到three座{void move(char x,char y);if(n==1) move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}void move(char x,char y){cout<<x<<"-->"<<y<<endl;}4.10题#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;return 0;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4.11题#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4.12题#include <iostream>#include <cmath>using namespace std;#define S(a,b,c) (a+b+c)/2#define AREA(a,b,c)sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c) -c))int main(){float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl; elsecout<<"It is not a triangle!"<<endl; return 0;}4.14题#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4.15题#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;gets(ch);#if (CHANGE){for (int i=0;i<40;i++){if (ch[i]!='\0')if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z')ch[i]+=1;else if (ch[i]=='z'||ch[i]=='Z')ch[i]-=25;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4.16题file#include <iostream>using namespace std;int a;int main(){extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl;cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl; return 0;}4.16题fileextern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}第五章5.1题#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;return 0;}5.2题#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10个数cout<<endl;;for (i=1;i<=9;i++) //以下8行是对10个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10个数cout<<a[i]<<" ";cout<<endl;return 0;}5.3题#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];cout<<"sum="<<sum<<endl;return 0;}5.4题#include <iostream>using namespace std;int main(){int a[11]={1,4,6,9,13,16,19,28,40,100};int num,i,j;cout<<"array a:"<<endl;for (i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;;cout<<"insert data:";cin>>num;if (num>a[9])a[10]=num;else。
1.5 题#include <iostream> using namespace std; int main(){cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0;1.6 题#include <iostream> using namespace std; int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}1.7 七题#include <iostream> using namespace std; int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl; return 0;}int f(int x,int y,int z) {int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);}#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}1.9 题#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y);cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int z;z=x+y;return(z);}2.3 题#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116';cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2.4 题#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is very interesting!\""<<return 0;}2.7 题#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl;return 0;}2.8 题#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl; return 0;}3.2 题#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios::right) <<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3.3 题#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5 和9 要用实型表示,否则5/9 值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3.4 题#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar 函数输出结果为:";putchar(c1);putchar(c2);cout<<endl;cout<<"用cout 语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3.4 题另一解#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar 函数输出结果为:";putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout 语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3.5 题#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl; return 0;}3.8 题#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl;return 0;}3.9 题include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:"; cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3.9 题另一解#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a 和b 中的大者存入temp 中*/max=(temp>c)?temp:c; /* 将a 和b 中的大者与c 比较,最大者存入max*/cout<<"max="<<max<<endl;return 0;}3.10 题#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else // x≥10{y=3*x-11;cout<<"x="<<x<<", y=3*x-11="<<y;}cout<<endl;return 0;}3.11 题#include <iostream>using namespace std;int main (){float score;char grade;cout<<"please enter score of student:";cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is"<<grade<<endl;return 0;}3.12 题#include <iostream>using namespace std;int main (){long int num;int indiv,ten,hundred,thousand,ten_thousand,place; /* 分别代表个位, 十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):";cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1000; hundred=(int)(num-ten_thousand*10000-thousand*1000)/ 100;ten=(int)(num-ten_thousand*10000-thousand*1000-hund red*100)/10;indiv=(int)(num-ten_thousand*10000-thousand*1000-hun dred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","<<hundred< <","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten_thousand< <endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten<<","<<indiv <<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<endl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3.13 题#include <iostream>using namespace std;int main (){ long i; //i 为利润float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10 万元时的奖金bon2=bon1+100000*0.075; //利润为20 万元时的奖金bon4=bon2+100000*0.05; //利润为40 万元时的奖金bon6=bon4+100000*0.03; //利润为60 万元时的奖金bon10=bon6+400000*0.015; //利润为100 万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1; //利润在10 万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10 万元至20 万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20 万元至40 万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40 万元至60 万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60 万元至100 万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100 万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3.13 题另一解#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10;int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break; case 2:case 3: bonus=bon2+(i-200000)*0.05;break; case 4:case 5: bonus=bon4+(i-400000)*0.03;break; case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break; case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3.14 题#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<",c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl;return 0;}3.15 题#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbersn,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n 中, 小数放在m 中}p=n*m; //先将n 和m 的乘积保存在p 中, 以便求最小公倍数时用while (m!=0) //求n 和m 的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p 是原来两个整数的乘积return 0;}3.16 题#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<",other:"<<other<<endl;return 0;}3.17 题#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn 为i 个a 组成数的值sn=sn+tn; //赋值后的sn 为多项式前i 项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3.18 题#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3.19 题#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3.20 题#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a 是2~1000 之间的整数,检查它是否为完数{n=0; // n 用来累计a 的因子的个数s=a; // s 用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i 是否为a 的因子if (a%i==0) // 如果i 是a 的因子{n++; // n 加1,表示新找到一个因子s=s-i; // s 减去已找到的因子,s 的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10 {case 1:k1=i; break; // 找出的笫1 个因子赋给k1case 2:k2=i; break; // 找出的笫2 个因子赋给k2case 3:k3=i; break; // 找出的笫3 个因子赋给k3case 4:k4=i; break; // 找出的笫4 个因子赋给k4case 5:k5=i; break; // 找出的笫5 个因子赋给k5case 6:k6=i; break; // 找出的笫6 个因子赋给k6case 7:k7=i; break; // 找出的笫7 个因子赋给k7 case 8:k8=i; break; // 找出的笫8 个因子赋给k8 case 9:k9=i; break; // 找出的笫9 个因子赋给k9 case 10:k10=i; break; // 找出的笫10 个因子赋给k10 }}if (s==0) // s=0 表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1 表示a 至少有2 个因子if (n>2) cout<<","<<k3; // n>2 表示至少有3 个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3 表示至少有4 个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3.20 题另一解#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}return 0;}3.20 题另一解#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3.21 题#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;3.22 题#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1 天的桃子数是第2 天桃子数加1 后的2 倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3.23 题#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:";cin>>a; // 输入a 的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3.24 题#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4 行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行for (i=0;i<=2;i++) // 输出下面3 行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3.25 题#include <iostream>using namespace std;int main(){char i,j,k; /* i 是a 的对手;j 是b 的对手;k 是c 的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<" B--"<<j<<"C--"<<k<<endl;return 0;}4.1 题#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4.2 题#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0 时方程的根*/{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0 时方程{x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0 时方程的根*/{p=-b/(2*a);q=sqrt(-disc)/(2*a);}4.3 题#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl;return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4.4 题#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<endl;return 0;}{int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4.5 题#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl;return 0;}double e(double x){return exp(x);}4.6 题#include <iostream>#include <cmath>using namespace std;int main(){double solut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d) {double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4.7 题#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";cin>>n;godbaha(n);return 0;}void godbaha(int n){int prime(int);int a,b;for(a=3;a<=n/2;a=a+2){if(prime(a)){b=n-a;if (prime(b))cout<<n<<"="<<a<<"+"<<b<<endl;} }}int prime(int m){int i,k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0) break;if (i>k) return 1;else return 0;}4.8 题#include <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;; cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n);}4.9 题#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:";cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl; hanoi(m,'A','B','C');return 0;}void hanoi(int n,char one,char two,char three)//将n 个盘从one 座借助two 座,移到three 座{void move(char x,char y);if(n==1) move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}void move(char x,char y){cout<<x<<"-->"<<y<<endl;}4.10 题#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4.11 题#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4.12 题#include <iostream>#include <cmath>using namespace std;#define S(a,b,c) (a+b+c)/2#define AREA(a,b,c)sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)) int main(){float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl;elsecout<<"It is not a triangle!"<<endl;}4.14 题#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4.15 题#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;gets(ch);#if (CHANGE){for (int i=0;i<40;i++){if (ch[i]!='\0')if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z') ch[i]+=1;else if (ch[i]=='z'||ch[i]=='Z')ch[i]-=25;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4.16 题file#include <iostream>using namespace std;int a;{extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl; cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl;return 0;}4.16 题fileextern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}5.1 题#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;}5.2 题#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10 个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10 个数cout<<endl;;for (i=1;i<=9;i++) //以下8 行是对10 个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3 行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10 个数cout<<a[i]<<" ";cout<<endl;return 0;}5.3 题#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];。
第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用N S流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句59 3.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念67 3.4.3用printf函数输出数据68 3.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <stdio.h>#include <math.h>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5); // 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5); // 存活期存款。
c语言程序设计第二版谭浩强C语言程序设计第二版,由谭浩强编著,是一本广受好评的计算机编程教材。
本书不仅涵盖了C语言的基础知识,还深入探讨了C语言的高级特性和应用技巧,适合初学者和有一定编程基础的读者。
第一章:C语言概述C语言是一种通用的、过程式的编程语言,由Dennis Ritchie在20世纪70年代初期开发。
C语言以其高效性、灵活性和可移植性而广受欢迎。
谭浩强在第一章中对C语言的历史、特点以及与其他编程语言的比较进行了介绍。
第二章:C语言基础本章详细介绍了C语言的基本语法,包括数据类型、运算符、表达式和控制语句。
谭浩强通过大量实例,帮助读者理解C语言的基本结构和编程逻辑。
第三章:函数C语言的函数是程序设计中的核心概念之一。
本章讲解了函数的定义、声明、调用以及参数传递机制。
谭浩强还介绍了递归函数和内联函数等高级特性。
第四章:数组数组是存储多个具有相同类型的数据项的集合。
本章详细讨论了一维数组和多维数组的使用方法,包括数组的初始化、遍历和排序等操作。
第五章:指针指针是C语言中非常强大的特性之一。
谭浩强在本章中深入讲解了指针的概念、指针与数组的关系、指针的运算以及动态内存分配。
第六章:结构体和联合体结构体和联合体是C语言中用于创建复杂数据类型的工具。
本章介绍了如何定义和使用结构体和联合体,以及它们在实际编程中的应用。
第七章:预处理指令预处理指令是C语言编译过程中的指令,用于控制编译器的行为。
本章讲解了宏定义、文件包含、条件编译等预处理指令的使用方法。
第八章:文件操作文件操作是程序设计中不可或缺的部分。
谭浩强在本章中介绍了C语言中文件的打开、关闭、读写和定位等操作。
第九章:高级数据结构高级数据结构如链表、栈和队列等,是解决复杂问题的重要工具。
本章详细讲解了这些数据结构的实现和应用。
第十章:C语言的高级特性本章介绍了C语言的一些高级特性,如位操作、信号处理、多线程编程等,为读者提供了更深入的C语言编程知识。
s ,另一个用来 第 9 章 预处理命令9.1 定义一个代参数的宏,使两个参数的值互换,并写出程序,输入两个数作为使用宏时的 实参。
输出已交换后的两个值。
解:# define SWAP(a,b) t=b;b=a;a=tmain ( ){int a,b,t;printf( “ Input two integers a,b: ” );scanf( “ %d,%d, ” *a,*b);SWAP(a,b);printf(“ Now,a=%d,b=%n ”d ,a,b);} 运行结果如下:In put two in tegers a,b : 3,4/Now,a=4,b=39.2 输入两个整数,求它们相除的余数。
用带参的宏来实现,编程序。
解:# define SURPLUS(a,b) a%bmain ( ){int a,b;printf( “Input two integers a,b:”); scanf( “ %d,%d ” ,&a,&b);printf(“Remember i s n%”d , SURPLUS(a , b));} 运行结果如下:Input two integers a,b : 60,13/Remember is 89.3 三角形的面积为: area=其中 s= (a+b+c),a,b,c 为三角形的三边。
定义两个带参的宏,一个用来求 求area 。
写程序,在程序中用带实参的宏名来求面积area 。
解:# include <math.h># define S(a,b,c)(a+b+c)/2# define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)) main ( ) {float a,b,c; printf(“Input a,b,c: ”);scanf( “ %f,%f,%f ” ,&a,&b,&c);if (a+b>c && a+c>b && b+c>a)printf(“ area:%8n.2”f. ,AREA(a,b,c));elseprintf( “ It is not a triangle! ” );}运行结果:①In put a,b,c: 3,4,5/area:6.00②In put a,b,c: 12,3,5/It is not a triangle!9.4给年份year 定义一个宏,以判断该年份是否为闰年。
《C语言程序设计》教案(清华谭浩强)第一章:C语言概述1.1 课程介绍介绍C语言的历史和发展解释C语言的特点和应用范围强调学习C语言的重要性和目的1.2 C语言的基本概念解释编程语言和编译器的概念介绍C语言的基本数据类型和变量讲解C语言的语法结构和程序结构1.3 C语言的编译过程解释编译器的角色和功能介绍编译过程中的预处理、编译、汇编和步骤强调编译过程中产生的文件和它们的作用第二章:基本数据类型和运算符2.1 基本数据类型介绍整型、浮点型、字符型和布尔型的概念和用法解释不同数据类型的存储方式和大小强调数据类型的选择和使用场景2.2 变量和常量解释变量的概念和作用介绍变量的声明和初始化方法讲解常量的概念和用法2.3 运算符介绍算术运算符、关系运算符和逻辑运算符的概念和用法解释赋值运算符和条件运算符的作用强调不同运算符的优先级和使用规则第三章:控制语句3.1 条件语句介绍if语句的语法和用法讲解switch语句的概念和用法强调条件语句的选择和嵌套使用3.2 循环语句介绍for循环、while循环和do-while循环的概念和用法解释循环控制语句如break和continue的作用强调循环条件的设置和循环次数的控制3.3 跳转语句介绍goto语句的概念和用法讲解label标签的作用和跳转规则强调跳转语句的使用场景和可能导致的问题第四章:函数和指针4.1 函数的基本概念介绍函数的定义和声明讲解函数的参数传递和返回值强调函数的命名规则和命名规范4.2 指针的概念和用法解释指针的概念和作用介绍指针的声明和初始化方法讲解指针的赋值和指针运算4.3 指针和数组介绍数组的概念和用法解释指针和数组的关系强调指针在数组操作中的应用第五章:结构体和文件操作5.1 结构体的概念和用法介绍结构体的定义和声明讲解结构体的成员访问和内存布局强调结构体在数据组织中的应用5.2 文件操作的基本概念解释文件的概念和文件操作的重要性介绍文件打开、读写、关闭等操作的方法强调文件操作中的错误处理和文件指针的管理第六章:动态内存分配6.1 动态内存分配的概念介绍动态内存分配的原因和必要性解释malloc、calloc和realloc函数的作用和用法强调动态内存分配的注意事项和错误处理6.2 链表的概念和用法介绍链表的定义和结构讲解链表的创建、插入、删除和遍历操作强调链表的优势和应用场景6.3 动态内存分配的应用实例通过实例演示动态内存分配在实际编程中的应用讲解内存泄漏和内存溢出的概念强调编写高效和安全的程序的重要性第七章:字符串处理7.1 字符串的基本概念介绍字符串的定义和表示方法解释字符串的长度和字符串的结束标志强调字符串与数组的区别和联系7.2 字符串的常用函数介绍字符串的输入输出函数如printf和scanf 讲解字符串的拷贝、连接、比较等操作函数强调字符串处理函数的使用和注意事项7.3 字符串处理的应用实例通过实例演示字符串处理在实际编程中的应用讲解字符串排序、查找和替换等操作强调字符串处理在文本分析和数据处理中的应用第八章:标准库函数8.1 标准输入输出库函数介绍标准输入输出库stdio.h中的常用函数讲解文件读写、数据转换等函数的用法和功能强调标准库函数的使用场景和注意事项8.2 字符串处理库函数介绍字符串处理库string.h中的常用函数讲解字符串比较、查找和替换等函数的用法和功能强调字符串处理库函数的使用和与其他库函数的配合8.3 数学计算库函数介绍数学计算库math.h中的常用函数讲解数学运算、三角函数和指数函数等函数的用法和功能强调数学计算库函数在数学计算和科学计算中的应用第九章:并发编程和同步机制9.1 并发编程的基本概念介绍并发编程的定义和目的解释进程和线程的概念和关系强调并发编程的优势和挑战9.2 并发编程的同步机制介绍互斥锁、条件变量和信号量等同步机制的原理和用法讲解同步机制在多线程编程中的应用和注意事项强调同步机制在避免竞态条件和数据一致性中的重要性9.3 并发编程的应用实例通过实例演示并发编程在实际应用中的优势和挑战讲解多线程的创建、同步和通信等操作强调并发编程在多任务处理和性能优化中的应用第十章:C语言编程实践10.1 编程实践的重要性强调编程实践在学习和掌握C语言中的重要性解释编程实践对于提高编程能力和解决问题的作用强调编程实践中的代码质量和编程规范10.2 编程实践的项目和案例介绍常见的编程实践项目和案例讲解实际编程中的问题解决方法和技巧强调编程实践中的调试和测试的重要性10.3 编程实践的资源和工具介绍编程实践中的常用工具和环境讲解集成开发环境(IDE)的使用和代码管理强调编程实践中的团队合作和代码分享的重要性重点和难点解析重点环节1:C语言的基本概念和特点需要重点关注C语言的历史和发展,以及其特点和应用范围。
《C语言程序设计》谭浩强版-教学教案1章节一:C语言简介教学目标:1. 了解C语言的历史和发展2. 掌握C语言的特点和优势3. 理解C语言在计算机科学中的应用教学内容:1. C语言的历史和发展2. C语言的特点和优势3. C语言的应用领域教学方法:1. 讲解法:讲解C语言的历史和发展,特点和优势2. 案例分析法:分析C语言在实际应用中的例子教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 引入话题:介绍C语言的历史和发展2. 讲解C语言的特点和优势3. 分析C语言在实际应用中的例子教学评估:1. 课堂问答:检查学生对C语言的了解程度2. 课后作业:让学生编写简单的C语言程序,巩固所学知识章节二:C语言基础语法教学目标:1. 掌握C语言的基本语法规则2. 学会使用C语言编写简单的程序教学内容:1. 变量和常量的声明和使用2. 数据类型的定义和使用3. 运算符的用法和优先级4. 控制语句的用法教学方法:1. 讲解法:讲解变量、常量、数据类型、运算符和控制语句的用法2. 案例分析法:分析使用这些语法规则编写的程序教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 讲解变量、常量、数据类型的声明和使用2. 讲解运算符的用法和优先级3. 讲解控制语句的用法4. 分析使用这些语法规则编写的程序教学评估:1. 课堂问答:检查学生对C语言基础语法的掌握程度2. 课后作业:让学生编写使用基础语法规则的C程序,巩固所学知识《C语言程序设计》谭浩强版-教学教案2章节六:函数与递归教学目标:1. 理解函数的概念和作用2. 学会如何定义和调用函数3. 掌握递归函数的定义和应用教学内容:1. 函数的定义和声明2. 函数的参数传递和返回值3. 递归函数的概念和应用教学方法:1. 讲解法:讲解函数的定义、声明、参数传递和返回值2. 案例分析法:分析使用函数和递归函数编写的程序教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 讲解函数的定义和声明2. 讲解函数的参数传递和返回值3. 介绍递归函数的概念和应用4. 分析使用函数和递归函数编写的程序教学评估:1. 课堂问答:检查学生对函数和递归函数的理解程度2. 课后作业:让学生编写使用函数和递归函数的C程序,巩固所学知识章节七:数组和字符串教学目标:1. 理解数组的概念和作用2. 学会如何使用一维和多维数组3. 理解字符串的概念和操作教学内容:1. 数组的定义和声明2. 数组的初始化和使用3. 字符串的概念和操作教学方法:1. 讲解法:讲解数组的定义、声明、初始化和使用2. 案例分析法:分析使用数组和字符串编写的程序教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 讲解数组的定义和声明2. 讲解数组的初始化和使用3. 介绍字符串的概念和操作4. 分析使用数组和字符串编写的程序教学评估:1. 课堂问答:检查学生对数组和字符串的理解程度2. 课后作业:让学生编写使用数组和字符串的C程序,巩固所学知识章节八:指针教学目标:1. 理解指针的概念和作用2. 学会如何声明和使用指针3. 掌握指针与数组、函数的关系教学内容:1. 指针的定义和声明2. 指针的使用和运算3. 指针与数组的关系4. 指针与函数的关系教学方法:1. 讲解法:讲解指针的定义、声明、使用和运算2. 案例分析法:分析使用指针编写的程序教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 讲解指针的定义和声明2. 讲解指针的使用和运算3. 介绍指针与数组的关系4. 介绍指针与函数的关系教学评估:1. 课堂问答:检查学生对指针的理解程度2. 课后作业:让学生编写使用指针的C程序,巩固所学知识章节九:结构体和联合体教学目标:1. 理解结构体的概念和作用2. 学会如何声明和使用结构体3. 理解联合体的概念和作用教学内容:1. 结构体的定义和声明2. 结构体的使用和初始化3. 联合体的定义和声明教学方法:1. 讲解法:讲解结构体的定义、声明、使用和初始化2. 案例分析法:分析使用结构体和联合体编写的程序教学资源:1. PowerPoint课件2. C语言实例代码教学过程:1. 讲解结构体的定义和声明2. 讲解结构体的使用和初始化3. 介绍联合体的概念和作用4. 分析使用结构体和联合体编写的程序教学评估:1. 课堂问答:检查学生对结构体和联合体的理解程度2. 课后作业:让学生编写使用结构体和联合体的C程序,巩固所学知识章节十:文件操作教学目标:1. 理解文件操作的概念和作用2. 学会如何打开、读写和关闭文件3. 掌握文件操作的错误处理教学重点和难点解析:一、章节一:C语言简介补充和说明:通过讲解C语言的历史和发展,让学生了解C语言的起源和演变过程;通过分析C语言的特点和优势,让学生理解C语言在计算机科学中的重要地位;通过介绍C语言的应用领域,让学生了解C语言的实际应用场景。
第10章运算符重载10.1 什么是运算符重载10.2 运算符重载的方法10.3 重载运算符的规则10.4 运算符重载函数作为类成员函数和友元函数10.5 重载双目运算符10.6 重载单目运算符10.7 重载流插入运算符和流提取运算符10.8 不同类型数据间的转换10.1 什么是运算符重载所谓重载,就是重新赋予新的含义。
函数重载是对一个已有的函数赋予新的含义,使之实现新功能。
运算符也可以重载。
实际上,我们已经在不知不觉之中使用了运算符重载。
现在要讨论的问题是:用户能否根据自己的需要对C++已提供的运算符进行重载,赋予它们新的含义,使之一名多用。
譬如,能否用“+”号进行两个复数的相加。
在C++中不能在程序中直接用运算符“+”对复数进行相加运算。
用户必须自己设法实现复数相加。
例如用户可以通过定义一个专门的函数来实现复数相加。
见例10.1。
例10.1 通过函数来实现复数相加。
#include <iostream>using namespace std;class Complex //定义Complex类{public:Complex( ){real=0;imag=0;} //定义构造函数Complex(double r,double i){real=r;imag=i;} //构造函数重载Complex complex_add(Complex &c2); //声明复数相加函数void display( ); //声明输出函数private:double real; //实部double imag; //虚部};Complex Complex∷complex_add(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}void Complex∷display( ) //定义输出函数{cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){Complex c1(3,4),c2(5,-10),c3; //定义3个复数对象c3=plex_add(c2); //调用复数相加函数cout<<″c1=″; c1.display( ); //输出c1的值cout<<″c2=″; c2.display( ); //输出c2的值cout<<″c1+c2=″; c3.display( ); //输出c3的值return 0;}运行结果如下:c1=(3+4i)c2=(5-10i)c1+c2=(8,-6i)结果无疑是正确的,但调用方式不直观、太烦琐,使人感到很不方便。
能否也和整数的加法运算一样,直接用加号“+”来实现复数运算呢?如c3=c1+c2;编译系统就会自动完成c1和c2两个复数相加的运算。
如果能做到,就为对象的运算提供了很大的方便。
这就需要对运算符“+”进行重载。
10.2 运算符重载的方法运算符重载的方法是定义一个重载运算符的函数,在需要执行被重载的运算符时,系统就自动调用该函数,以实现相应的运算。
也就是说,运算符重载是通过定义函数实现的。
运算符重载实质上是函数的重载。
重载运算符的函数一般格式如下:函数类型operator 运算符名称(形参表列){ 对运算符的重载处理}例如,想将“+”用于Complex类(复数)的加法运算,函数的原型可以是这样的:Complex operator+ (Complex& c1,Complex& c2);在定义了重载运算符的函数后,可以说:函数operator+重载了运算符+。
为了说明在运算符重载后,执行表达式就是调用函数的过程,可以把两个整数相加也想像为调用下面的函数:int operator + (int a,int b){return (a+b);}如果有表达式5+8,就调用此函数,将5和8作为调用函数时的实参,函数的返回值为13。
这就是用函数的方法理解运算符。
可以在例10.1程序的基础上重载运算符“+”,使之用于复数相加。
例10.2 改写例10.1,重载运算符“+”,使之能用于两个复数相加。
#include <iostream>using namespace std;class Complex{public:Complex( ){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2); //声明重载运算符的函数void display( );private:double real;double imag;};Complex Complex∷operator+(Complex &c2) //定义重载运算符的函数{ Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}void Complex∷display( ){ cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){ Complex c1(3,4),c2(5,-10),c3;c3=c1+c2; //运算符+用于复数运算cout<<″c1=″;c1.display( );cout<<″c2=″;c2.display( );cout<<″c1+c2=″;c3.display( );return 0;}运行结果与例10.1相同:c1=(3+4i)c2=(5-10i)c1+c2=(8,-6i)请比较例10.1和例10.2,只有两处不同:(1) 在例10.2中以operator+函数取代了例10.1中的complex_add函数,而且只是函数名不同,函数体和函数返回值的类型都是相同的。
(2) 在main函数中,以“c3=c1+c2;”取代了例10.1中的“c3=plex_add(c2);”。
在将运算符+重载为类的成员函数后,C++编译系统将程序中的表达式c1+c2解释为c1.operator+(c2) //其中c1和c2是Complex类的对象即以c2为实参调用c1的运算符重载函数operator+(Complex &c2),进行求值,得到两个复数之和。
虽然重载运算符所实现的功能完全可以用函数实现,但是使用运算符重载能使用户程序易于编写、阅读和维护。
在实际工作中,类的声明和类的使用往往是分离的。
假如在声明Complex类时,对运算符+,-,*,/都进行了重载,那么使用这个类的用户在编程时可以完全不考虑函数是怎么实现的,放心大胆地直接使用+,-,*,/进行复数的运算即可,十分方便。
对上面的运算符重载函数operator+还可以改写得更简练一些:Complex Complex∷operator + (Complex &c2){return Complex(real+c2.real, imag+c2.imag);}需要说明的是:运算符被重载后,其原有的功能仍然保留,没有丧失或改变。
10.3 重载运算符的规则(1) C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载。
(2)C++允许重载的运算符C++中绝大部分的运算符允许重载。
具体规定见书中表10.1。
不能重载的运算符只有5个:. (成员访问运算符).* (成员指针访问运算符)∷(域运算符)sizeof(长度运算符)?: (条件运算符)前两个运算符不能重载是为了保证访问成员的功能不被改变,域运算符和sizeof运算符的运算对象是类型而不是变量或一般表达式,不具重载的特征。
(3) 重载不能改变运算符运算对象(即操作数)个数。
(4) 重载不能改变运算符的优先级别。
(5) 重载不能改变运算符的结合性。
(6) 重载运算符的函数不能有默认的参数,否则就改变了运算符参数的个数,与前面第(3)点矛盾。
(7) 重载的运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应有一个是类对象(或类对象的引用)。
也就是说,参数不能全部是C++的标准类型,以防止用户修改用于标准类型数据的运算符的性质。
(8) 用于类对象的运算符一般必须重载,但有两个例外,运算符“=”和“&”不必用户重载。
①赋值运算符(=)可以用于每一个类对象,可以利用它在同类对象之间相互赋值。
②地址运算符&也不必重载,它能返回类对象在内存中的起始地址。
(9) 应当使重载运算符的功能类似于该运算符作用于标准类型数据时所实现的功能。
(10) 运算符重载函数可以是类的成员函数(如例10.2),也可以是类的友元函数,还可以是既非类的成员函数也不是友元函数的普通函数。
10.4 运算符重载函数作为类成员函数和友元函数在本章例10.2程序中对运算符“+”进行了重载,使之能用于两个复数的相加。
在该例中运算符重载函数operator+作为Complex类中的成员函数。
“+”是双目运算符,为什么在例10.2程序中的重载函数中只有一个参数呢?实际上,运算符重载函数有两个参数,由于重载函数是Complex类中的成员函数,有一个参数是隐含的,运算符函数是用this 指针隐式地访问类对象的成员。
可以看到,重载函数operator+访问了两个对象中的成员,一个是this指针指向的对象中的成员,一个是形参对象中的成员。
如this->real+c2.real,this->real就是c1.real。
在10.2节中已说明,在将运算符函数重载为成员函数后,如果出现含该运算符的表达式,如c1+c2,编译系统把它解释为c1.operator+(c2)即通过对象c1调用运算符重载函数,并以表达式中第二个参数(运算符右侧的类对象c2)作为函数实参。
运算符重载函数的返回值是Complex类型,返回值是复数c1和c2之和(Complex(c1.real +c2.real,c1.imag+c2.imag))。
运算符重载函数除了可以作为类的成员函数外,还可以是非成员函数。
可以将例10.2改写为例10.3。
例10.3 将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。
#include <iostream>using namespace std;class Complex{public:Complex( ){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}friend Complex operator + (Complex &c1,Complex &c2);//重载函数作为友元函数void display( );private:double real;double imag;};Complex operator + (Complex &c1,Complex &c2) //定义作为友元函数的重载函数{return Complex(c1.real+c2.real, c1.imag+c2.imag);}void Complex∷display( ){cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<″c1=″; c1.display( );cout<<″c2=″; c2.display( );cout<<″c1+c2 =″; c3.display( );}与例10.2相比较,只作了一处改动,将运算符函数不作为成员函数,而把它放在类外,在Complex类中声明它为友元函数。