谭浩强c++程序设计课后答案
娄警卫
..
第一章
1.5题
#include
cout<<\ cout<<\ cout<<\ return 0; 1.6题
#include
int a,b,c; a=10; b=23; c=a+b;
cout<<\ cout< 1.7七题 #include int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< int f(int x,int y,int z) { int m; if (x return(m); } 1.8题 #include int a,b,c; cin>>a>>b; c=a+b; cout<<\ return 0; } 1.9题 #include int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b); cout<<\ return 0; } int add(int x,int y) {int z; z=x+y; return(z); } 2 / 75 第二章 2.3题 #include {char c1='a',c2='b',c3='c',c4='\\101',c5='\\116'; cout< cout<<\ return 0; } 2.4题 #include {char c1='C',c2='+',c3='+'; cout<<\ cout<<\says: \\\is very interesting!\\\ return 0; } 2.7题 #include m=++i+j++; n=(++i)+(++j)+m; cout< 2.8题 #include {char c1='C', c2='h', c3='i', c4='n', c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; cout<<\ is:\ return 0; } 第三章 3.2题 #include {float h,r,l,s,sq,vq,vz; const float pi=3.1415926; cout<<\ 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< cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ return 0; } 3.3题 #include cout<<\请输入一个华氏温度:\ cin>>f; c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0 cout<<\摄氏温度为:\ return 0; }; 3 / 75 3.4题 #include cout<<\请输入两个字符c1,c2:\ c1=getchar(); //将输入的第一个字符赋给c1 c2=getchar(); //将输入的第二个字符赋给c2 cout<<\用putchar函数输出结果为:\ putchar(c1); putchar(c2); cout< cout<<\用cout语句输出结果为:\ cout< 3.4题另一解 #include cout<<\请输入两个字符c1,c2:\ c1=getchar(); //将输入的第一个字符赋给c1 c2=getchar(); //将输入的第二个字符赋给c2 cout<<\用putchar函数输出结果为:\ putchar(c1); putchar(44); putchar(c2); cout< cout<<\用cout语句输出结果为:\ cout< 3.5题 #include int i1,i2; //定义为整型 cout<<\请输入两个整数i1,i2:\ cin>>i1>>i2; c1=i1; c2=i2; cout<<\按字符输出结果为:\, \ return 0; } 3.8题 #include { int a=3,b=4,c=5,x,y; cout<<(a+b>c && b==c)< cout<<(!(x=a) && (y=b) && 0)< include cout<<\enter three integer numbers:\ cin>>a>>b>>c; if(a cout<<\ else cout<<\ else if (a cout<<\ else cout<<\ cout< 3.9题另一解 #include 4 / 75 using namespace std; int main ( ) {int a,b,c,temp,max ; cout<<\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<<\ return 0; } 3.10题 #include cout<<\ cin>>x; if (x<1) {y=x; cout<<\ } else if (x<10) // 1≤x<10 {y=2*x-1; cout<<\ } else // x≥10 {y=3*x-11; cout<<\y=3*x-11=\ } cout< 3.11题 #include int main () {float score; char grade; cout<<\ cin>>score; while (score>100||score<0) {cout<<\ 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<<\is \grade is \ return 0; } 3.12题 #include indiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数 */ cout<<\ 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<<\ 5 / 75