第一章1-2#includeiostreamusingnamespacestd;intmain(){doublea,b,c;cout请输入长:;cina;cout请输入宽:;cinb;cout请输入高:;cinc;doubles,v;s=a*b*2+b*c*2+a*c*2;v=a*b*c;cout长方体的表面积s=sendl;cout长方体的体积v=vendl;return0;}1-7#includeiostream#includecmathusingnamespacestd;intmain(){doublen,y;cout请输入n:;cinn;y=pow((1.0+1.0/n),n);couty=yendl;return0;}1-8#includeiostream#includecmathusingnamespacestd;intmain(){doublex,y;cout请输入一个大于0的数x:;cinx;y=sin(x)-log(x)+sqrt(x)-5;couty=yendl;return0;}1-9#includeiostream#includecmathusingnamespacestd;intmain(){doublex,y;cout请输入x:;cinx;if((x*x-3*x+2)=0)return1;else{y=x/(sqrt(x*x-3*x+2));couty=yendl;}return0;}第二章2-2#includeiostreamusingnamespacestd;intmain(){doublec,f;cout请输入一个摄氏温度:endl;cinc;f=9.0*c/5.0+32;cout对应于的华氏温度为:endlfendl;return0;}2-3#includeiostream#includecmathusingnamespacestd;intmain(){doublex,y;cout输入x:;cinx;y=sin(x*x)/(1-cos(x));couty=yendl;return0;}2-5#includeiostream#includecmathusingnamespacestd;intmain(){intm,n,k;doublei,j;cout请输入三个整数:;cinmnk;i=m+n+k;j=i/3;k=j+0.5;cout和为:iendl;cout平均值为:jendl;cout平均值的四舍五入整数值:kendl;return0;}2-7#includeiostreamusingnamespacestd;intmain(){charch;for(inti=0;i3;i++){cout请输入一个字母:endl;cinch;if(ch='A'&&ch='Z'){ch=ch-'A'+'a';cout大小写转换后为:endlchendl;}elseif(ch='a'&&ch='z'){ch=ch-'a'+'A';cout大小写转换后为:endlchendl;}else{ch=ch;cout大小写转换后为:endlchendl;}}return0;}第三章3-4#includeiostream#includecmathusingnamespacestd;intmain(){doubley,u;inti=1,n;cout请输入n:;cinn;if(n0)cout输入不合法endl;elseif(n=0){couty=1endl;}else{while(i=n,i++)u=u*i;couty=uendl;}return0;}3-5#includeiostreamusingnamespacestd;intmain(){intu=0;inti=1;intn;intk=1;cout请输入n:;cinn;while(k=n){u=u+i;i=i+2;k++;}cout和为:uendl;return0;}3-10#includeiostream#includecmathusingnamespacestd;intmain(){doubleu=0,k=1,i=1;intn;cout请输入n:;cinn;while(i=n){u=u+k;i=i+1;k=k*i;}cout结果为:uendl;return0;}3-13#includeiostream#includecmathusingnamespacestd;intmain(){inta,n,k,u=0,i=1,j=1;cout请输入a,n:;cinan;k=a;while(i=n){u=u+k;k=k+10*j*a;j=j*10;i=i+1;}cout结果为:uendl;return0;}第四章4-1#includeiostreamusingnamespacestd;intmain(){intM[4][4]={5,6,7,9,2,8,5,4,3,7,16,15,1,4,8,11,};intMt[4][4];inti,j;for(i=0;i4;i++)for(j=0;j4;j++){Mt[i][j]=M[j][i];}cout矩阵a的转置阵为:endl;for(i=0;i4;i++){for(j=0;j4;j++)coutMt[i][j]\t;coutendl;}return0;}4-3#includeiostream#includecmathusingnamespacestd;intfac(intn){intresult=1;while(n1){result*=n;n=n-1;}returnresult;}intmain(){intn;cout请输入行数:endl;cinn;for(intl=0;ln;l++){for(intm=0;m=l;m++)coutfac(l)/(fac(m)*fac(l-m))\t;coutendl;}return0;}4-7#includeiostream#includecstringusingnamespacestd;char*my(char*string){char*ptr=string;while(*ptr!=0)ptr++;do{ptr--;}while(*ptr=='');*(++ptr)=0;returnstring;}intmain(){charstr[100];cout请输入一个末尾带空格的字符串endl;cin.get(str,100);cout截取前的原始字符串是:endl[str]endl;cout截取空格后的字符串是:endl;cout[my(str)];coutendl;return0;}4-8#includeiostream#includecstringusingnamespacestd;char*myltrim(char*string){char*ptr=string;while(*ptr=='')ptr++;strcpy(string,ptr);returnstring;}intmain(){charstr[100];cout请输入开头带空格的字符串endl;cin.get(str,100);cout截取前的原始字符串是:endl[str]endl;cout截取空格后的字符串是:endl;cout[myltrim(str)];coutendl;return0;}第五章5-2#includeiostreamusingnamespacestd;intmain(){charch;for(inti=0;i3;i++){cout请输入一个字母:endl;cinch;if(ch='A'&&ch='Z'){ch=ch-'A'+'a';cout大小写转换后为:endlchendl;}elseif(ch='a'&&ch='z'){ch=ch-'a'+'A';cout大小写转换后为:endlchendl;}else{ch=ch;cout大小写转换后为:endlchendl;}}return0;}5-4#includeiostreamintGCD(intx,inty);intLCM(intx,inty,intz);intmain(void){usingnamespacestd;cout请输入两个正整数:\n;intx;inty;cout第一个正整数:;cinx;cout第二个正整数:;ciny;intG_CD=GCD(x,y);//最大公约数intL_CM=LCM(x,y,G_CD);//最小公倍数=两数乘积除以最大公约数cout最大公约数是:G_CDendl;cout最小公倍数是:L_CMendl;return0;}intGCD(intx,inty){intz=x%y;while(z!=0){x=y;y=z;z=x%y;}returny;}intLCM(intx,inty,intz){returnx*y/z;}5-5#includeiostream#includecmathusingnamespacestd;intmtk(intn){intr,s,t,q;r=n/100;s=n%10;t=n%100;q=t/10;t=r+s+q;returnt;}intmain(){for(inti=201;i1000;i++){if(mtk(i)==5)coutiendl;}return0;}5-9#includeiostream#includecmathusingnamespacestd;boolis_prime(intn){if(n2)returnfalse;for(inti=2;i*i=n;i++){if(n%i==0)returnfalse;}returntrue;}intmain(){intn;cout请输入一个数n:;cinn;if(is_prime(n)){cout该数是一个素数endl;}else{cout该数不是一个素数endl;}return0;}第六章6-5#includestdio.hintmain(){intm;char*months[]={January,February,March,April,May,June,July,August,September,October,November,December};printf(InputMonth:);scanf(%d,&m);printf(Themonthis:%s,*(months+m-1));}6-6#includeiostreamusingnamespacestd;intmain(){chara[20];inti=0;cout请输入一串字符:\n;cina;for(;a[i];i++){if(a[i]'a'&&a[i]'z')a[i]-=32;elseif(a[i]'A'&&a[i]'Z')a[i]+=32;}for(i=0;a[i];i++)couta[i];coutendl;return0;}6-7#includeiostreamusingnamespacestd;in