C程序设计(谭浩强)完整版-课后习题标准答案
(完整版)谭浩强c程序设计课后习题答案
谭浩强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。
C语言程序设计第三版谭浩强课后习题答案完整版
C语言程序设计第三版谭浩强课后习题答案完整版在C语言程序设计学习过程中,谭浩强的《C语言程序设计第三版》是一本经典教材,对于提高学生的编程能力和理解C语言的应用具有重要的作用。
然而,学习过程中遇到的习题往往让学生感到困惑,因此,本文将为大家提供C语言程序设计第三版谭浩强课后习题的完整答案,帮助大家更好地理解和掌握C语言的编程技巧。
第一章习题1-1:```c#include <stdio.h>int main(){printf("Hello, World!\n");return 0;}```习题1-2:```c#include <stdio.h>int main(){printf("天\n地\n人\n"); return 0;}```习题1-3:```c#include <stdio.h>int main(){printf(" *\n");printf(" * *\n");printf(" * *\n");printf(" * *\n");printf(" * *\n");printf("***\n");return 0;}```习题1-4:```c#include <stdio.h>int main(){int height, width, area;height = 8;width = 10;area = height * width;printf("长方形的面积为:%d\n", area); return 0;}```第二章习题2-1:```c#include <stdio.h>int main(){int radius;float pi = 3.14159;printf("请输入半径:");scanf("%d", &radius);float area = pi * radius * radius;printf("圆的面积为:%f\n", area); return 0;}```习题2-2:```c#include <stdio.h>int main(){int length, width, area;printf("请输入长方形的长度:"); scanf("%d", &length);printf("请输入长方形的宽度:");scanf("%d", &width);area = length * width;printf("长方形的面积为:%d\n", area);return 0;}```习题2-3:```c#include <stdio.h>int main(){int num1, num2, num3, sum;printf("请输入三个整数,用空格隔开:"); scanf("%d %d %d", &num1, &num2, &num3); sum = num1 + num2 + num3;printf("三个整数的和为:%d\n", sum);return 0;}习题2-4:```c#include <stdio.h>int main(){int high, price;printf("请输入身高(厘米):"); scanf("%d", &high);price = (high - 100) * 0.9;printf("标准体重为:%d\n", price); return 0;}```第三章习题3-1:```c#include <stdio.h>int main()int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= i; j++){printf("%d*%d=%2d ", j, i, i * j); }printf("\n");}return 0;}```习题3-2:```c#include <stdio.h>int main(){int n, sum = 0, i;printf("请输入一个整数:");scanf("%d", &n);for (i = 1; i <= n; i++){sum += i;}printf("1到%d的和为:%d\n", n, sum); return 0;}```习题3-3:```c#include <stdio.h>int main(){int n, i;printf("请输入一个整数:");scanf("%d", &n);for (i = 2; i < n; i++){if (n % i == 0){break;}}if (i == n){printf("%d是素数。
c程序设计(第四版)谭浩强-课后答案(完整版).pdf (1)
C 语言课后习题答案-第四版- 谭浩强(1-7)第一章#include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}#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;}第3章#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;}#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++程序设计课后答案娄警卫第一章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。
(完整版)《C语言程序设计》课后习题答案(第四版)谭浩强
第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程序设计——顺序程序设计373。
1顺序程序设计举例373.2数据的表现形式及其运算393。
2.1常量和变量393.2。
2数据类型423.2.3整型数据443。
2。
4字符型数据473。
2.5浮点型数据493。
2。
6怎样确定常量的类型513。
C语言程序设计第三版谭浩强课后习题答案完整版
C语言程序设计第三版谭浩强课后习题答案完整版第一章1.5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。
main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
c程序设计谭浩强习题答案
c程序设计谭浩强习题答案C程序设计是计算机科学与技术专业学生必修的一门课程,谭浩强教授编写的《C程序设计》教材广泛用于教学中,其习题集也是学生学习过程中不可或缺的练习材料。
以下是针对该教材习题的一些参考答案,供参考使用。
第一章:C语言概述1. 问题1:解释C语言的特点。
- 答案:C语言是一种通用的、过程式的编程语言,具有以下特点:简洁、高效、可移植性高、结构化、支持多种编程范式等。
2. 问题2:C语言的发展历史。
- 答案:C语言由丹尼斯·里奇在1972年开发,最初用于UNIX操作系统的编写。
随着UNIX系统的流行,C语言也逐渐被广泛使用并发展。
第二章:数据类型、运算符和表达式1. 问题1:C语言中基本数据类型有哪些?- 答案:C语言中的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)等。
2. 问题3:解释运算符的优先级。
- 答案:运算符优先级决定了表达式中各运算符的计算顺序。
例如,乘除运算符优先于加减运算符。
第三章:控制语句1. 问题1:解释if语句的用法。
- 答案:if语句用于条件判断,根据条件是否为真来决定执行不同的代码块。
2. 问题2:while循环和for循环的区别是什么?- 答案:while循环在每次迭代前检查条件,而for循环在循环开始前初始化,然后在每次迭代后检查条件。
第四章:数组1. 问题1:一维数组和多维数组的区别。
- 答案:一维数组是线性的,只有一个索引;多维数组可以看作是数组的数组,具有多个索引。
2. 问题2:数组的初始化方法。
- 答案:数组可以通过赋值语句逐个初始化,或者使用初始化列表在声明时初始化。
第五章:指针1. 问题1:指针是什么?- 答案:指针是一种变量,它存储了另一个变量的内存地址。
2. 问题2:指针和数组的关系。
- 答案:数组名可以作为指向数组首元素的指针使用,指针可以用于遍历数组。
第六章:函数1. 问题1:函数的定义和声明。
C程序设计谭浩强完整版课后习题答案
b=4;
sum=a+b;
printf("The sum is %d .\n",sum);
return0;
}
P008调用函数比较两个数的大小.
#include<>
intmain()
{
intmax(intx,inty);inta,b,c;
scanf("%d,%d",&a,&b);c=max(a,b);printf("The max is %d .\n",c);
{
doublea,b,c,disc,x1,x2,p,q;
scanf("%lf %lf %lf",&a,&b,&c);
disc=b*b-4*a*c;
p=-b/*a);
q=sqrt(disc)/*a);
x1=p+q;
x2=p-q;
printf("x1=%\nx2=%",x1,x2);
{
floatoutput_fah;
output_fah=5)*input_cen+32;returnoutput_fah;
}
intmain()
{
intchoice;
floatinput_fah,input_cen,output_fah,output_cen;printf("F_to_C press <1> and C_to_F press <2> !\n");
scanf("%d",&n);
if(n%3==0&&n%5==0)printf ("Can be devide by 3 and 5 !\n");
C语言程序设计第三版谭浩强课后习题答案完整版
C语⾔程序设计第三版谭浩强课后习题答案完整版C语⾔程序设计第三版谭浩强课后习题答案完整版第⼀章1.5请参照本章例题,编写⼀个C程序,输出以下信息:**************************值,输出其中最⼤值。
解:mian(){int a,b,c,max;printf(“请输⼊三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(maxmax=b;if(maxmax=c;\1aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc AㄩN3.7 要将"China"译成密码,译码规律是:⽤原来字母后⾯的第4个字母代替原来的字母.例如,字母"A"后⾯第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编⼀程序,⽤赋初值的⽅法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输{是%c%c%c%c%c\n",c1,c2,c3,c4,c5); }运⾏结果:密码是Glmre3.9求下⾯算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4设x=2.5,a=7,y=4.7(2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5(1)2.5(2)3.5设量。
(1)a+=a (2) a-=2 (3) a*=2+3 (4)a/=a+a(5) a%=(n%=2),n的值等于5(6)a+=a-=a*=a解:(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0第四章4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a ’,c2=c1=c2=printf("a=%2d b=%2dc=%2d\n",a,b,c);printf("x=%f,y=%f,z=%f\n",x,y,z); printf("x+y=%5.2fy+z=%5.2fz+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("%s %s %d%s\n","c1=’a’","or",c1,"(ASCII)");printf("%s %s %d%s\n","c2=’a’","or",c2,"(ASCII)"); }ˊA ˊ,;); 4.8设圆半径r=1.5,圆柱⾼h=3,求圆周长、圆⾯积、圆球表⾯积、圆球体积、圆柱体积。
c程序设计(第四版)谭浩强-课后答案(完整版).pdf (1)
C 语言课后习题答案-第四版- 谭浩强(1-7)第一章#include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}#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;}第3章#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;}#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语言程序设计(谭浩强)习题参考答案
(1)24 (2)10
(3)60 (4)0
(5)0 (6)0
第4章 最简单的C程序设计——顺序程序设计
4.4 若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51247,n=128765,c1=’a’,c2=’b’,想得到以下的输出格式和结果,请写出程序(包括定义变量类型和设计输出)。
printf(“%8.2f, %8.2f,%4f, %4f, %3f, %3f\n”,x,y,x,y,x,y);
printf(“%e,%10.2e\n”,x,y);
printf(“%c,%d,%o,%x\n”,c,c,c,c);
printf(“%ld,%lo,%x\n”,n,n,n);
C程序设计(第三版)课后习题参考解答
第1章 C语言概述
1.5 参照本章例题,编写一个C程序,输出以下信息:
******************************
Very Good!
******************************
解:
main ( )
{printf(“****************************** \n”);
第3章 数据类型、运算符与表达式
3.3请将下面各数用八进制和十六进制数表示:
(1)10 (2)32 (3)75 (4)-617
(5)-111 (6)2483 (7)-28654 (8)21003
变量的类型 25 -2 32769
int型
long型
sort型
signed char(8位)
unsigned int型
unsigned long型
c++程序设计谭浩强课后习题答案(完整版)
第八章#include <iostream> usingnamespace std; classTime{public: //成员改为公用的int hour;int minute;int sec; };Time t;void set_time(void) //在main 函数之前定义{ cin>>t.hour;cin>>t.minute;cin>>t.sec;}void show_time(void) //在main 函数之前定义 {cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; }int main(){set_time();show_time(); return0;}8.2 题#include <iostream> usingnamespace std; classTime{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec; } voidshow_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;} private: int hour;int minute;int sec;};Time t; intmain(){t.set_time();t.show_time(); return 0;}8.3 题#include <iostream> using namespace std; class Time{public:void set_time(void); void show_time(void); private: int hour; int minute;int sec;}; voidTime::set_time(void){cin>>hour;cin>>minute; cin>>sec;} voidTime::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Time t; intmain(){ t.set_time();t.show_time(); return0;}8.4 题//xt8-4.h(student.h) class Student { public:void display( );v oid set_value();private: int num;char name[20];char sex ;};8.4 题//xt8-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt8-4.h" intmain() {Student stud;stud.set_value();stud.display(); return0;}8.4 题另一解//xt8-4-2.cpp(即student.cpp) #include "xt8-4.h" //在此文件中进行函数的定义#include <iostream> using namespace std;//不要漏写此行 void Student::display( ){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name; cin>>sex;}8.5 题//xt8-5.h(arraymax.h)class Array_max {public:void set_value();void max_value(); voidshow_value(); private:int array[10]; int max;};8.5 题//xt8-5-1.cpp(file1.cpp)#include <iostream>#include "xt8-5.h" intmain(){Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0;}8.5 题//xt8-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std; #include "xt8-5.h"void Array_max::set_value() { int i;for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value(){int i;max=array[0]; for(i=1;i<10;i++) if(array[i]>max) max=array[i];}void Array_max::show_value() {cout<<"max="<<max<<endl; }8.6 题#include <iostream>using namespace std;class Box {public:void get_value();float volume(); voiddisplay(); public:float lengh;float width; floatheight;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}int main(){Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is ";box1.display();box2.get_value();cout<<"volmue of bax2 is ";box2.display();box3.get_value();cout<<"volmue of bax3 is ";box3.display(); return 0;}8.6 题另一解#include <iostream> usingnamespace std; class Box{public:void get_value();void volume(); voiddisplay(); public:float lengh;float width; floatheight; float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is "; box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is "; box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is "; box3.display(); return 0;}9.2 题#include <iostream> usingnamespace std; classDate{public:Date(int,int,int);Date(int,int);Date(int);Date(); voiddisplay();private: intmonth; int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y) { }Date::Date(int m,int d):month(m),day(d){year=2005;}Date::Date(int m):month(m){day=1;year=2005;}Date::Date(){month=1;day=1; year=2005;}void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;} intmain(){Date d1(10,13,2005);Date d2(12,30);Date d3(10); Dated4; d1.display();d2.display();d3.display();d4.display();return 0;}9.3 题#include <iostream>using namespace std; classDate{public:Date(int=1,int=1,int=2005);void display(); private: intmonth; int day; int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y) { }void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display(); return0;}9.4 题#include <iostream> using namespace std; class Student{public:Student(int n,float s):num(n),score(s){}void display(); private: int num; floatscore;};void Student::display(){cout<<num<<" "<<score<<endl;}int main(){Student stud[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)};Student *p=stud; for(inti=0;i<=2;p=p+2,i++) p->display(); return0;}9.5 题#include <iostream> usingnamespace std; classStudent{public:Student(int n,float s):num(n),score(s){}int num; float score;};void main(){Student stud[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; voidmax(Student* ); Student *p=&stud[0]; max(p);}void max(Student *arr) {floatmax_score=arr[0].score; int k=0; for(inti=1;i<5;i++) if(arr[i].score>max_score){max_score=arr[i].score;k=i;}cout<<arr[k].num<<" "<<max_score<<endl;}9.6 题#include <iostream> usingnamespace std; classStudent{public:Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display(){cout<<num<<" "<<score<<endl;} private: int num; float score;};int main(){Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display(); return 0;}9.7 题#include <iostream>using namespace std; classStudent{public:Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;}// 可改为:void display() const {cout<<num<<" "<<score<<endl;}private: intnum; floatscore;};int main(){const Student stud(101,78.5);stud.display();//stud.change(101,80.5);stud.display(); return 0;}9.7 题另一解#include <iostream> usingnamespace std; classStudent{public:Student(int n,float s):num(n),score(s){} void change(int n,float s) const {num=n;score=s;} void display() const {cout<<num<<" "<<score<<endl;} private:mutable int num;mutable float score;};int main(){const Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display(); return 0;#include <iostream> usingnamespace std;}9.7 题另一解Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score;};int main(){Student stud(101,78.5);Student *p=&stud;p->display();p->change(101,80.5);p->display(); return 0;}9.7 题另一解#include <iostream> usingnamespace std;AStudent(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private:mutable int num;mutable float score;};int main(){Student stud(101,78.5);const Student *p=&stud;p->display();p->change(101,80.5);p->display(); return 0;9.7 题另一解#include <iostream> usingnamespace std;}BStudent(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() const{cout<<num<<" "<<score<<endl;} private: int num; float score;};int main(){Student stud(101,78.5);const Student *p=&stud;p->display();stud.change(101,80.5);p->display(); return 0;9.7 题另一解#include <iostream> usingnamespace std;}9.7 题另一解#include <iostream> using namespace std;Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score;};int main(){Student stud(101,78.5);Student * const p=&stud;p->display();p->change(101,80.5);p->display(); return 0;}9.8 题Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score;#include <iostream> using};int main(){Student stud(101,78.5);void fun(Student&);fun(stud); return 0;}void fun(Student &stu){stu.display(); stu.change(101,80.5);stu.display();}9.9 题#include <iostream> usingnamespace std; classProduct{public:Product(int n,int q,floatp):num(n),quantity(q),price(p){}; void total(); static floataverage(); static voiddisplay();private: int num;int quantity; floatprice; static floatdiscount; static floatsum; static int n;};void Product::total(){float rate=1.0; if(quantity>10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount);n=n+quantity;}void Product::display(){cout<<sum<<endl;cout<<average()<<endl;}float Product::average(){return(sum/n);}float Product::discount=0.05;float Product::sum=0; intProduct::n=0;int main(){ProductProd[3]={ Product(101,5,23.5),Product(102,12,24.56),Produ ct(103,100,21.5) }; for(inti=0;i<3;i++)Prod[i].total();Product::display();return 0;}9.10 题#include <iostream>using namespace std;class Date; classTime{public:Time(int,int,int); friend void display(const Date&,const Time &); private: int hour; int minute;int sec;};Time::Time(int h,int m,int s){hour=h;minute=m; sec=s;}class Date{public:Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year;};Date::Date(int m,int d,int y){month=m;day=d; year=y;}void display(const Date &d,const Time &t){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; }intmain(){Time t1(10,13,56);Date d1(12,25,2004); display(d1,t1); return0;}9.11 题#include <iostream> using namespace std; class Time;class Date{public:Date(int,int,int);friend Time;private: intmonth; int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y){ }class Time{public:Time(int,int,int); voiddisplay(const Date &);private: int hour; intminute; int sec;};Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ }void Time::display(const Date &d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1); return0;}9.12 题#include <iostream>using namespace std;template<class numtype>class Compare{public:Compare(numtype a,numtype b);numtype max(); numtype min();private:numtype x,y;};template <class numtype>Compare<numtype>::Compare(numtype a,numtype b){x=a;y=b;} template <class numtype>numtype Compare<numtype>::max(){return (x>y)?x:y;} template <classnumtype> numtypeCompare<numtype>::min(){return (x<y)?x:y;}int main(){Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl;Compare<float> cmp2(45.78,93.6); cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl; Compare<char> cmp3('a','A');is the Maximum of two cout<<cmp3.max()<<"characters."<<endl;cout<<cmp3.min()<<" is the Minimum of two characters."<<endl; return0;}10.1 题#include <iostream> usingnamespace std; classComplex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;} double get_real();double get_imag(); voiddisplay(); private:double real;double imag;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){returnComplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.ge t_imag());}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2; cout<<"c3=";c3.display(); return 0;}10.2 题#include <iostream> usingnamespace std; classComplex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2); 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; returnc;}Complex Complex::operator-(Complex &c2) {Complex c;c.real=real-c2.real;c.imag=imag-c2.imag; returnc;}Complex Complex::operator*(Complex &c2) {Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag; returnc;}Complex Complex::operator/(Complex &c2) {Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.i mag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.i mag*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+c2=";c3.display(); c3=c1-c2;cout<<"c1-c2="; c3.display();c3=c1*c2;cout<<"c1*c2=";c3.display();c3=c1/c2;cout<<"c1/c2=";c3.display(); return0;}10.3 题#include <iostream> //用VC++时改为∶ #include <iostream.h> using namespace std; //用VC++时为取消此行 class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); void display(); private:double real;double imag;};Complex Complex::operator+(Complex &c) {return Complex(real+c.real,imag+c.imag);}Complex Complex::operator+(int &i){return Complex(real+i,imag);}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator+(int &i,Complex &c) {return Complex(i+c.real,c.imag);}int main(){Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2; cout<<"c1+c2=";c3.display(); c3=i+c1;cout<<"i+c1="; c3.display();c3=c1+i; cout<<"c1+i=";c3.display(); return 0;}10.4 题#include <iostream> using namespace std; class Matrix //定义Matrix 类{public:Matrix(); //默认构造函数friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+”void input(); //输入数据函数void display(); // 输出数据函数private:int mat[2][3];};Matrix::Matrix() //定义构造函数{for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b) //定义重载运算符“+” 函数{Matrix c; for(inti=0;i<2;i++) for(intj=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} return c;}void Matrix::input() // 定义输入数据函数{cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++) for(int j=0;j<3;j++)cin>>mat[i][j];}void Matrix::display() //定义输出数据函数{for (int i=0;i<2;i++){for(int j=0;j<3;j++){cout<<mat[i][j]<<" ";} cout<<endl;}}int main() {Matrixa,b,c;a.input();b.input(); cout<<endl<<"Matrixa:"<<endl;a.display(); cout<<endl<<"Matrixb:"<<endl;b.display(); c=a+b; //用重载运算符“+”实现两个矩阵相加cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;c.display(); return0;}10.5 题#include <iostream.h> //using namespace std; class Matrix {public: Matrix(); friend Matrixoperator+(Matrix &,Matrix &); friend ostream& operator<<(ostream&,Matrix&); friend istream& operator>>(istream&,Matrix&); private:int mat[2][3];};Matrix::Matrix() {for(inti=0;i<2;i++) for(intj=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b){Matrix c; for(inti=0;i<2;i++) for(intj=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} returnc;}istream& operator>>(istream &in,Matrix &m) {cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++) for(int j=0;j<3;j++)in>>m.mat[i][j]; return in;}ostream& operator<<(ostream &out,Matrix &m) {for (int i=0;i<2;i++){for(int j=0;j<3;j++){out<<m.mat[i][j]<<" ";}out<<endl;} return out;}int main() { Matrix a,b,c; cin>>a; cin>>b;cout<<endl<<"Matrix a:"<<endl<<a<<endl;cout<<endl<<"Matrix b:"<<endl<<b<<endl;c=a+b;cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl<<c<<endl;return 0;}10.6 题#include <iostream> usingnamespace std; classComplex{public:Complex(){real=0;imag=0;}Complex(double r){real=r;imag=0;}Complex(double r,double i){real=r;imag=i;}operator double(){return real;} voiddisplay(); private: double real; doubleimag;}; voidComplex::display(){cout<<"("<<real<<", "<<imag<<")"<<endl;}int main() {Complexc1(3,4),c2; double d1;d1=2.5+c1;cout<<"d1="<<d1<<endl;c2=Complex(d1);cout<<"c2=";c2.display(); return 0;}10.7 题#include <iostream> usingnamespace std; classStudent{public:Student(int,char[],char,float); intget_num(){return num;} char *get_name(){return name;} charget_sex(){return sex;} voiddisplay(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<se x<<"\nscore:"<<score<<"\n\n";}private: intnum; charname[20];char sex;float score;};Student::Student(int n,char nam[],char s,float so){num=n;strcpy(name,nam);sex=s; score=so;}class Teacher{public:Teacher(){}Teacher(Student&);Teacher(int n,char nam[],char sex,float pay); void display(); private: int num; charname[20]; char sex;float pay;};Teacher::Teacher(int n,char nam[],char s,float p) {num=n;strcpy(name,nam);sex=s; pay=p;}Teacher::Teacher(Student& stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex(); pay=1500;}void Teacher::display()。
C程序设计(第四版)谭浩强-课后习题标准答案-共章
C程序设计(第四版)谭浩强-课后习题标准答案-共章C程序设计(第四版)谭浩强-课后习题答案-共章————————————————————————————————作者:————————————————————————————————日期:第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 #includeint main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#includeint 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)< bdsfid="88" p=""></b)<>max=b;if (max<c)< bdsfid="91" p=""></c)<>max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂16 2.1什么是算法16 2.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最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include#includeint 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#includeint 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程序设计(第四版)谭浩强-课后答案(完整版).pdf
C 语言课后习题答案-第四版- 谭浩强(1-7)第一章#include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}#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;}第3章#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;}#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程序设计(第四版)(谭浩强)第一章课后习题答案P006 1.1 向屏幕输出文字.#include<stdio.h>//预编译. 代码均调试成功,若有失误大多不是代码问题.自已找找.int main(){printf("Welcome to \n");return 0; //与int main对应,为了程序可移植性,建议全用int main + return 0;.}P008 1.2 求两个数的和.#include<stdio.h>int main(){int a,b,sum;a=5;b=4;sum=a+b;printf("The sum is %d .\n",sum);return 0;}P008 1.3 调用函数比较两个数的大小.#include<stdio.h>int main(){int max(int x,int y); //被调用函数在主函数后面,用前先声明.int a,b,c;scanf("%d,%d",&a,&b); //输入时要按格式来,此处的逗号,用空格会发生错误.c=max(a,b); //a,b作为实参传入被调用函数中.printf("The max is %d .\n",c);return 0;}int max(int x,int y) //定义了两个形参.{int z; //z属于局部变量,可与主函数中相同名字.if (x>y)z=x;elsez=y;return(z); //z作为整个程序的出口值,赋给主函数中的c.}P015 0.6 三个数的大小.(数字0表示课后练习题)#include<stdio.h>int main(){int a,b,c,d; //d是用于存储最大值的.int max(int x , int y , int z); //测试可知,在VS2008中,可以不预先声明.printf("Please input 3 numbers :\n");scanf("%d %d %d",&a,&b,&c);d=max(a,b,c); //调用函数中有三个形参,这里需要传入三个实参,才可运算.printf("The max is :%d .\n",d); // d可以换成max(a,b,c).}int max(int x , int y , int z){int m;if (x>y && x>z) //求三者之大的一种方法.m=x;if (y>x && y>z)m=y;if (z>y && z>x)m=z;return (m); //返回值m给主函数中的d.}C程序设计(第四版)(谭浩强)第2章课后习题答案算法——程序的灵魂#include<stdio.h>int main(){int i,s=1; //在执行数值操作前一定要先有个初值.for(i=1;i<6;i++) //这里是到6.{s=s*i; //相乘}printf("The sum is %d .\n",s);return 0;}#include<stdio.h> //作出要求:换成1到11间奇数相乘.int main(){int i,s=1; //在执行数值操作前一定要先有个初值.for(i=1;i<12;i++) //这里是到,但题目要求的是取单数.也可以是i=i+2{if(i%2!=0) //i对取模,值为非为奇数;为则为偶数.s=s*i;elsecontinue; //跳过这个for循环的这一次,执行下一次.}printf("The sum is %d .\n",s);return 0;}P019 2.2 按要求输出80分以上的学生信息.暂时没法做.P019 2.3 判断2000-2500年中的闰年,并输出.年的概念是地球围绕太阳一周的时间(所谓公转周期)称为一年,这个周期是相当稳定的,很长时间也不会变动1秒,但是真正的一年是365.2423天(目前)。
c程序设计(第四版)谭浩强-课后答案(完整版).pdf (1)
C 语言课后习题答案-第四版- 谭浩强(1-7)第一章#include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}#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;}第3章#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;}#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程序设计第三版课后答案(整本书).doc
scanf(“%f %e”,&x , &y);
scanf(“%c %c”,&c1 , &c2);
}
解:
a=3□b=7
□8.5□71.82
□A□a
4.7
scanf(“%5d%5d%c%c%f%f%*f,%f”,&a,&b,&c1,&c2,&x,&y,&z);
(7)(-28654)10=(110022)8=(9012)16
(8)(21003)10=(51013)8=(520B)16
3
变量的类型
25
-2
32769
int型
long型
sort型
signed char(8位)
unsigned int型
unsigned long型
unsigned short型
unsigned char型
unsigned u=65535;
printf(“%d%d\n”,a,b);
printf(“%3d%3d\n”,a,b);
printf(“%f,%f\n”,x,y);
printf(“%-10f,%-10f\n”,x,y);
printf(“%8.2f, %8.2f,%4f, %4f, %3f, %3f\n”,x,y,x,y,x,y);
voidmain ( )
{ char c1=’a’, c2=’b’, c3=’c’, c4=’\101’, c5=’\116’;
printf (“a%c b%c\t c%c\t abc\n”, c1, c2, c3);
printf (“\t\b%c %c”, c4, c5);
C程序设计谭浩强_课后习题答案_共11章
1-5 #include <>int main ( ){ printf ("**************************\n\n"); printf(" Very Good!\n\n");printf ("**************************\n"); return 0;}1-6#include <>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;}3-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <>#include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c1=197;c2=198;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}3-7#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y);}else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y);}return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:");scanf("%f",&score);while (score>100||score<0){printf("\n 输入有误,请重输");scanf("%f",&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;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是 %,相应的等级是%c\n ",score,grade);return 0;}4-10-1#include <>int main(){int i;double bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*;bon2=bon1+100000*;bon4=bon2+100000*;bon6=bon4+100000*;bon10=bon6+400000*;printf("请输入利润i:");scanf("%d",&i);if (i<=100000)bonus=i*;else if (i<=200000)bonus=bon1+(i-100000)*;else if (i<=400000)bonus=bon2+(i-200000)*;else if (i<=600000)bonus=bon4+(i-400000)*;else if (i<=1000000)bonus=bon6+(i-600000)*;elsebonus=bon10+(i-1000000)*;printf("奖金是: %\n",bonus);return 0;}4-10-2#include <>int main(){int i;double bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*;bon2=bon1+100000*;bon4=bon2+200000*;bon6=bon4+200000*;bon10=bon6+400000*;printf("请输入利润i:");scanf("%d",&i);branch=i/100000;if (branch>10) branch=10;switch(branch){ case 0:bonus=i*;break;case 1:bonus=bon1+(i-100000)*;break; case 2:case 3: bonus=bon2+(i-200000)*;break; case 4:case 5: bonus=bon4+(i-400000)*;break; case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*;break; case 10: bonus=bon10+(i-1000000)*;}printf("奖金是 %\n",bonus);return 0;}4-11#include <>int main(){int t,a,b,c,d;printf("请输入四个数:");scanf("%d,%d,%d,%d",&a,&b,&c,&d);printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d); 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;}printf("排序结果如下: \n");printf("%d %d %d %d \n" ,a,b,c,d); return 0;}4-12#include <>int main(){int h=10;float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4; printf("请输入一个点(x,y):");scanf("%f,%f",&x,&y);d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf("该点高度为 %d\n",h);return 0;}5-3#include <>int main(){int p,r,n,m,temp;printf("请输入两个正整数n,m:");scanf("%d,%d,",&n,&m);if (n<m){temp=n;n=m;m=temp;}p=n*m;while(m!=0){r=n%m;n=m;m=r;}printf("它们的最大公约数为:%d\n",n);printf("它们的最小公约数为:%d\n",p/n);return 0;}5-4#include <>int main(){char c;int letters=0,space=0,digit=0,other=0;printf("请输入一行字符:\n");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++;}printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);return 0;}5-5#include <>int main(){int a,n,i=1,sn=0,tn=0;printf("a,n=:");scanf("%d,%d",&a,&n);while (i<=n){tn=tn+a; /*赋值后的tn为i个 a组成数的值*/sn=sn+tn; /*赋值后的sn为多项式前i项之和*/a=a*10;++i;}printf("a+aa+aaa+...=%d\n",sn);return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");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)printf("%d ",n);}printf("\n");return 0;}5-9#include <>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){printf("%d,its factors are ",m);for (i=1;i<m;i++)if (m%i==0) printf("%d ",i);printf("\n");}}return 0;}5-10#include <>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s);return 0;}5-11#include <>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn);printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1);return 0;}5-14#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1);return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2; do{printf("enter x1 & x2:"); scanf("%f,%f",&x1,&x2); fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5); printf("x=%\n",x0);return 0;}5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");}for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是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')printf("A--%c\nB--%c\nC--%c\n",i,j,k);return 0;}6-1#include <>#include <>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;}printf("\n");for (i=2,n=0;i<=100;i++){ if(a[i]!=0){printf("%5d",a[i]);n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100};int temp1,temp2,number,end,i,j;printf("array a:\n");for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:");scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n");for (i=0;i<11;i++)printf("%5d",a[i]);printf("\n");return 0;}6-5#include <>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) n",number);;printf("continu or not(Y/N)");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1);gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++){if (text[i][j]>='A'&& text[i][j]<='Z') upp++;else if (text[i][j]>='a' && text[i][j]<='z') low++;else if (text[i][j]>='0' && text[i][j]<='9') dig++;else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}6-11#include <>int main(){ char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z')) tran[j]=219-ch[j];elsetran[j]=ch[j];j++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))ch[j]=219-ch[j];elsech[j]=ch[j];j++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1);return 0;}6-14#include <>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu); return 0;}6-15#include <>#include <>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}7-1-1#include <>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("",h);l=lcd(u,v,h);printf("",l);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);}7-1-2#include <>int Hcf,Lcd;int main(){void hcf(int,int);void lcd(int,int);int u,v;scanf("%d,%d",&u,&v);hcf(u,v);lcd(u,v);printf("",Hcf);printf("",Lcd);return 0;}void 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;}Hcf=v;}void lcd(int u,int v){Lcd=u*v/Hcf;}7-2#include <>#include <>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;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %*x*x+%*x+%=0\n",a,b,c);disc=b*b-4*a*c;printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q); }return 0;}void greater_than_zero(float a,float b){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b){p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n);elseprintf("%d is not a prime.\n",n);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);}7-4#include <>#define N 3int array[N][N];int main(){ void convert(int array[][3]); int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]);printf("\noriginal array :\n"); for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]); printf("\n");}return 0;}void convert(int array[][3]){int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}#include <>#include <>int main(){void inverse(char str[]);char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str);return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j;for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7-8#include <>#include <>int main(){char str[80];void insert(char []);printf("input four digits:");scanf("%s",str);insert(str);return 0;}void insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}printf("output:\n%s\n",str);}7-9#include <>int letter,digit,space,others;int main(){void count(char []);char text[80];printf("input string:\n");gets(text);printf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n",letter,digit,space,others );return 0;}void count(char str[]){int i;for (i=0;str[i]!='\0';i++)if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z'))letter++;else if (str[i]>='0' && str [i]<='9')digit++;else if (str[i]==32)space++;elseothers++;}7-10#include <>#include <>int main(){int alphabetic(char);int longest(char []);int i;char line[100];printf("input one line:\n");gets(line);printf("The longest word is :");for (i=longest(line);alphabetic(line[i]);i++) printf("%c",line[i]);printf("\n");return 0;}int alphabetic(char c){if ((c>='a' && c<='z')||(c>='A'&&c<='z'))return(1);elsereturn(0);}int longest(char string[]){int len=0,i,length=0,flag=1,place=0,point;for (i=0;i<=strlen(string);i++)if (alphabetic(string[i]))if (flag){point=i;flag=0;}elselen++;else{flag=1;if (len>=length){length=len;place=point;len=0;}}return(place);}7-11#include <>#include <>#define N 10char str[N];int main(){void sort(char []);int i,flag;for (flag=1;flag==1;){printf("input string:\n");scanf("%s",&str);if (strlen(str)>N)printf("string too long,input again!"); elseflag=0;}sort(str);printf("string sorted:\n");for (i=0;i<N;i++)printf("%c",str[i]);printf("\n");return 0;}void sort(char str[]){int i,j;char t;for(j=1;j<N;j++)for (i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7-12#include <>#include <>int main(){float solut(float a,float b,float c,float d); float a,b,c,d;printf("input a,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%\n",solut(a,b,c,d));return 0;}float solut(float a,float b,float c,float d) {float 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-3);return(x);}7-16#include <>#define MAX 1000int main(){ int htoi(char s[]);int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;printf("input a HEX number:");while((c=getchar())!='\0' && i<MAX&& flag1){if (c>='0' && c<='9'||c>='a' && c<='f'||c>='A' && c<='F') {flag=1;t[i++]=c;}else if (flag){t[i]='\0';printf("decimal number %d\n",htoi(t));printf("continue or not");c=getchar();if (c=='N'||c=='n')flag1=0;else{flag=0;i=0;printf("\ninput a HEX number:");}}}return 0;}int htoi(char s[]){ int i,n;n=0;for (i=0;s[i]!='\0';i++){if (s[i]>='0'&& s[i]<='9')n=n*16+s[i]-'0';if (s[i]>='a' && s[i]<='f')n=n*16+s[i]-'a'+10;if (s[i]>='A' && s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}7-17#include <>int main(){ void convert(int n);int number;printf("input an integer: ");scanf("%d",&number);printf("output: ");if (number<0){putchar('-');putchar(' '); /* 先输出一个‘-’号和空格 */ number=-number;}convert(number);printf("\n");return 0;}void convert(int n){ int i;if ((i=n/10)!=0)convert(i);putchar(n%10+'0');putchar(32);}7-18#include <>int main(){int sum_day(int month,int day);int leap(int year);int year,month,day,days;printf("input date(year,month,day):");scanf("%d,%d,%d",&year,&month,&day);printf("%d/%d/%d ",year,month,day);days=sum_day(month,day); /* 调用函数sum_day */ if(leap(year)&&month>=3) /* 调用函数leap */days=days+1;printf("is the %dth day in this year.\n",days);return 0;}int sum_day(int month,int day) /* 函数sum_day:计算日期 */ {int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i;for (i=1;i<month;i++)day+=day_tab[i]; /* 累加所在月之前天数 */return(day);} /* 函数leap:判断是否为闰年 */int leap(int year){int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}8-1#include <>int main(){ void swap(int *p1,int *p2);int n1,n2,n3;int *p1,*p2,*p3;printf("input three integer n1,n2,n3:");scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2) swap(p1,p2);if(n1>n3) swap(p1,p3);if(n2>n3) swap(p2,p3);printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);return 0;}void swap(int *p1,int *p2){int p;p=*p1; *p1=*p2; *p2=p;}8-2#include <>#include <>int main(){void swap(char *,char *);char str1[20],str2[20],str3[20];printf("input three line:\n");gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); printf("Now,the order is:\n");printf("%s\n%s\n%s\n",str1,str2,str3);return 0;}void swap(char *p1,char *p2){char p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); }8-3#include <>int main(){ void input(int *);void max_min_value(int *);void output(int *);int number[10];input(number); max_min_value(number); output(number);return 0;}void input(int *number){int i;printf("input 10 numbers:");for (i=0;i<10;i++)scanf("%d",&number[i]);}void max_min_value(int *number) { int *max,*min,*p,temp;max=min=number;for (p=number+1;p<number+10;p++)if (*p>*max) max=p;else if (*p<*min) min=p;。
c语言程序设计谭浩强课后习题答案
c语言程序设计谭浩强课后习题答案1. 一个C语言程序是由()。
[单选题] *一个主程序和若干子程序组成函数组成(正确答案)若干过程组成若干子程序组成2. 一个C程序的执行是从()。
[单选题] *本程序的main函数开始,到main函数结束(正确答案)本程序文件的第一个函数开始,到本程序文件的最后一个函数结束本程序的main函数开始,到本程序文件的最后一个函数结束本程序文件的第一个函数开始,到本程序main函数结束3. 同时定义相同类型的多个变量要使用()。
[单选题] *句号分号逗号(正确答案)冒号4. 标准C语言程序编译生成的文件后缀为()。
[单选题] *.c.cpp.obj(正确答案).exe5. 下面的注释那个写法是正确的()。
[单选题] */*this is a comment*\/*this is a comment/* it is error*//*this is a comment*/ (正确答案)/*this one seems like a comment doesn’t it6. 以下叙述中正确的是()。
[单选题] *用C程序实现的算法必须要有输入和输出操作用C程序实现的算法可以没有输出但必须要有输入用C程序实现的算法可以没有输入但必须要有输出(正确答案)用C程序实现的算法可以既没有输入也没有输出7. C语言中3种基本结构是()。
[单选题] *顺序结构、选择结构、循环结构(正确答案)if、switch、breakfor、while、do-whileif、for、continue8. 以下叙述中错误的是()。
[单选题] *用户所定义的标识符允许使用关键字(正确答案)用户所定义的标识符应尽量做到“见名知意”用户所定义的标识符必须以字母或下划线开头用户定义的标识符中,大、小写字母代表不同标识9. 以下不能定义为用户标识符的是()。
[单选题] *Main_0_intsizeof(正确答案)10. 以下选项中合法的用户标识符是()。
c 程序设计谭浩强课后习题答案
c 程序设计谭浩强课后习题答案C程序设计谭浩强课后习题答案C程序设计是计算机科学与技术领域中非常重要的一门课程,它涵盖了计算机编程的基础知识和技巧。
谭浩强是中国著名的计算机科学家和教育家,他的《C程序设计》一书是学习C语言的经典教材之一。
在学习过程中,课后习题是检验自己理解和掌握程度的重要途径。
本文将为大家提供一些C程序设计谭浩强课后习题的答案,希望能够对大家的学习有所帮助。
1. 编写一个程序,输出从1到100的所有偶数。
```c#include <stdio.h>int main() {int i;for (i = 1; i <= 100; i++) {if (i % 2 == 0) {printf("%d ", i);}}return 0;}```2. 编写一个程序,计算并输出1到100的所有奇数之和。
```c#include <stdio.h>int main() {int i, sum = 0;for (i = 1; i <= 100; i++) {if (i % 2 != 0) {sum += i;}}printf("奇数之和:%d\n", sum);return 0;}```3. 编写一个程序,输入一个整数n,计算并输出1到n的所有整数之和。
```c#include <stdio.h>int main() {int n, i, sum = 0;printf("请输入一个整数n:");scanf("%d", &n);for (i = 1; i <= n; i++) {sum += i;}printf("整数之和:%d\n", sum);return 0;}```4. 编写一个程序,输入一个整数n,计算并输出n的阶乘。
```c#include <stdio.h>int main() {int n, i, factorial = 1;printf("请输入一个整数n:");scanf("%d", &n);for (i = 1; i <= n; i++) {factorial *= i;}printf("%d的阶乘:%d\n", n, factorial);return 0;}```5. 编写一个程序,输入一个正整数n,判断并输出n是否为素数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C程序设计(谭浩强)完整版-课后习题答案————————————————————————————————作者:————————————————————————————————日期:C程序设计(第四版)(谭浩强)第一章课后习题答案P006 1.1 向屏幕输出文字.#include<stdio.h>//预编译. 代码均调试成功,若有失误大多不是代码问题.自已找找.int main(){printf("Welcome to \n");return 0; //与int main对应,为了程序可移植性,建议全用int main + return 0;.}P008 1.2 求两个数的和.#include<stdio.h>int main(){int a,b,sum;a=5;b=4;sum=a+b;printf("The sum is %d .\n",sum);return 0;}P008 1.3 调用函数比较两个数的大小.#include<stdio.h>int main(){int max(int x,int y); //被调用函数在主函数后面,用前先声明.int a,b,c;scanf("%d,%d",&a,&b); //输入时要按格式来,此处的逗号,用空格会发生错误.c=max(a,b); //a,b作为实参传入被调用函数中.printf("The max is %d .\n",c);return 0;}int max(int x,int y) //定义了两个形参.{int z; //z属于局部变量,可与主函数中相同名字.if (x>y)z=x;elsez=y;return(z); //z作为整个程序的出口值,赋给主函数中的c.}P015 0.6 三个数的大小.(数字0表示课后练习题)#include<stdio.h>int main(){int a,b,c,d; //d是用于存储最大值的.int max(int x , int y , int z); //测试可知,在VS2008中,可以不预先声明.printf("Please input 3 numbers :\n");scanf("%d %d %d",&a,&b,&c);d=max(a,b,c); //调用函数中有三个形参,这里需要传入三个实参,才可运算.printf("The max is :%d .\n",d); // d可以换成max(a,b,c).}int max(int x , int y , int z){int m;if (x>y && x>z) //求三者之大的一种方法.m=x;if (y>x && y>z)m=y;if (z>y && z>x)m=z;return (m); //返回值m给主函数中的d.}C程序设计(第四版)(谭浩强)第2章课后习题答案算法——程序的灵魂P017 2.1 计算机1-5相乘的积.#include<stdio.h>int main(){int i,s=1; //在执行数值操作前一定要先有个初值.for(i=1;i<6;i++) //这里是到6.{s=s*i; //相乘}printf("The sum is %d .\n",s);return 0;}#include<stdio.h> //作出要求:换成1到11间奇数相乘.int main(){int i,s=1; //在执行数值操作前一定要先有个初值.for(i=1;i<12;i++) //这里是到,但题目要求的是取单数.也可以是i=i+2{if(i%2!=0) //i对取模,值为非为奇数;为则为偶数.s=s*i;elsecontinue; //跳过这个for循环的这一次,执行下一次.}printf("The sum is %d .\n",s);return 0;}P019 2.2 按要求输出80分以上的学生信息.暂时没法做.P019 2.3 判断2000-2500年中的闰年,并输出.年的概念是地球围绕太阳一周的时间(所谓公转周期)称为一年,这个周期是相当稳定的,很长时间也不会变动1秒,但是真正的一年是365.2423天(目前)。
所以,如果每年定义为365天的话,1年就会多出0.2423天,4年就会多出0.9692天,非常接近1天,这样闰年就出现了,也就是每4年要有1年设置为366天,来抵消这多出的1天。
规则为:·1)如果年份能被4整除,则该年计为闰年;可是,假如不做调整的话,当400年的时候,累计才多出96.92天,接近于多出97天,闰年的设置却多出来100天,所以要在400年内,再撤销3个闰年。
怎么撤销呢?就有了下面这个规则:·2)如果年份能被100整除,则不计为闰年;问题又来了,400年里有4个100年吧,去掉后四个100年后,又少了一个,所以再加一个规则就是:·3)如果年份能被400整除,则计为闰年。
简单说来每400年里设置了97个闰年,也就是400里的前3个100年不设置闰年,误差被调整到400年仅有0.08天。
#include<stdio.h>int main(){int i;for(i=2000;i<=2200;i++){if((i%4==0 && i%100!=0)||i%400==0) //闰年的经典判断条件.printf("%d is a leap year !\n",i);elseprintf("%d is not a leap year !\n",i);}}P020 2.4 求(1)-(1/2)+(1/3)……+(1/99)-(1/100)的值.#include<stdio.h>int main(){float sign=1 , sum=0 , deno , term ; //关于有小数的计算应该用float.for (deno=1;deno<=100;deno++){term=sign*(1/deno); //term临时值,sing符号,deno为分母.sum=sum+term; //第一次总全存储.sign=(-1)*sign; //换号.}printf("The sum of deno(指定值) is %f .\n",sum);return 0; //若是到100的话,结果为0.68172}P021 2.5 得到一个大于3的数,并判断是否为素数.#include<stdio.h>int main(){int i , num ,n ; //像13,37,这样只能让和它本身除以外,没有其它除数的叫素数.printf("Please input a number :\n"); //获取一个数值scanf("%d",&num);for(i=2;i<num;i++) //在到此数值间进行循环除,如果除得尽,就不满足素数要求.{if(num%i==0) //除得尽,则不是.{n=1;break; //设置跳出,才不会执行多次.}n=0; //按程序走的话,这里会执行多次,不能用printf语句,会输出多次.}if(n==0) //单出口问题,虽然n=0执行多次,但仍然是n=0,这样才会只输出一次.{printf("It is do a prime number !"); //如果是100-200可以加个”%d”,nmu进去.}else{printf("It is not a prime number !");}return 0;}2.6-2.10要求用流程图表示上列算法.2.11-2.16要求用N-S图表示上列算法.…………还要伪代码P036 0.1 调换二个参数的值.#include<stdio.h>int main(){char a='a',b='b',c;c=a;b=c;printf("a now is %c , b now is %c !\n",a,b);return 0;}P036 0.2 输入10个数,并输出最大的那一个数.#include<stdio.h>int main(){int i,a[10],s=0 ;printf("Please input 10 numbers :\n");for(i=0;i<=9;i++){scanf("%d",&a[i]);}for(i=0;i<=9;i++){if (s<a[i]) s=a[i];}printf("%d is the biggest number !\n",s);return 0;}P036 0.3 按大小顺序输出一些数.#include<stdio.h>int main(){int i , j , a[4] , s=0 ;printf("Please input 5 numbers :\n"); //此处要回车.for (i=0 ; i<=4 ; i++){scanf("%d",&a[i]); //这是取多个数的代码,可以用while控制取到\n符为止.}for (i=0 ; i<=3 ; i++) //双重循环,先一个外循,再轮所有小循,轮完后再下一个大循.{for (j=i+1 ; j<=4 ; j++) //第一个循环是至n-1,第二个循环是到n.{if (a[i]>a[j]) //如果成立,则对换,小于号表示降序排列,大于号表示升序排列.s=a[i];a[i]=a[j];a[j]=s;}}}for (i=0 ; i<=4 ; i++) //重新定义循环输出数组中新排列的数.printf("%d-",a[i]);return 0;}P036 0.4 求1至100的总合.#include<stdio.h>int main(){int i , sum=0 ; //这是一个简单的程序.for (i=0 ; i<101 ; i++)sum=sum+i;printf("The sum of one to one hundred is %d !\n",sum);return 0;}P036 0.5 判断一个数能否同时被3和5整除.#include<stdio.h>int main(){int n ;printf("Please input a number :\n");scanf("%d",&n);if(n%3==0&&n%5==0) //关键句,用"并"的符号.printf ("Can be devide by 3 and 5 !\n");elseprintf ("Can not be devide by 3 and 5 !\n");return 0;}P036 0.6 输出100-200间的素数.//********************第一种方法.#include<stdio.h> //网络版本,采用调用函数和求根取素.#include<math.h>//下面用到sqrt,所以需要包含数据函数.sqrt是求根,属数学函数.int main() //学会一个程序采用”主函数+被调用函数”的方式,会比较规范.{int i;for (i=100; i<=200; i++) //为了书写规范,最好加上{},以示清范围.if(prime_number(i) == 1) //i是实参.printf("%d ",i);return 0;}int prime_number(double m) //m是形参.{int j,k;k=(int)sqrt(m); //求根,(int)是强制转换,求根里要是浮点型.for(j=2;j<=k;j++){if(m%j==0)return 0; //此值返回给主函数中的实参.}return 1;}//********************第二种方法.#include<stdio.h>#include<math.h>int main(){int i;for(i=100;i<=200;i++) //指定至间.{if(prime(i)==1) //这里i为实参,传入形参j中进行运算,然后再由形参j带出返回.printf ("%d is the prime number !\n",i);}return 0;}int prime(int j) //j是形参,用于接收实参i给的值.{int m, n;m=(int)sqrt(j);for (n=2;n<=m;n++){if(j%n==0)return 0; //返回值,0为不是素数,1是素数.}return 1;}//********************第二种方法.请仿照2.5来写.P036 0.7 最大公约数和最小公倍数.#include<stdio.h>//最大公约数用累除法,除到无余数时的被除数是最大公约数.main (){int m, n, c, d;int gcd(); //这是最大公约数的缩写,此处调用函数,可以不写里面的实参.int lcm(); //这是最小公倍数的缩写,此处调用函数,可以不写里面的实参.printf("Please input two number :\n");scanf("%d %d",&m,&n);c=gcd(m,n); //c获取最大公约数d=lcm(m,n); //d获取最小公倍数printf("The GCD of %d and %d is : %d !\n", m, n, c);printf("The LCM of %d and %d is : %d !\n", m, n, d);return 0;}int gcd(int x, int y) //最大公约数Greatest Common Divisor{int temp;while(x%y!=0){temp=y; //y在下一轮中作为除数,即是下一轮中的X,所以先闪一边去.y=x%y; //x,y的余数作为下一轮中的Y,由x%y来取得.x=temp; //刚才temp中存储了y的值,现在拿出来作为下一轮中的X使用.}return y; //这是每一轮中的被除数,按原理来,这就是最大公约数,即累除法的原理. }int lcm(int x, int y) //最小公倍数Lowest Common Multiple{int i, temp;if(x<y) //保证二者大的数在X上,小的数在Y上.小于号降序,大于号升序.{ //以下为经典三行码,实现两个数的互换.temp=x;x=y;y=temp;for(i=1; i<=y; i++) //设定一个区间,从1至小的数之间的循环.{if(!((x*i)%y)) //除得尽为零,非零为真,则成立并返回.{ //如此往复,直到取模无余数,那么小的数X乘以区间当前的I值,就是最小公倍数.return x*i;}}}最简单的C程序设计——顺序程序设计P037 3.1 把华氏温度转化为摄氏表示法.#include<stdio.h>float F_to_C(float input_fah) //代表华转摄,input_fah是局部变量.{float output_cen; //这里也是局部变量.output_cen=(5.0/9)*(input_fah-32); //函数的功能体.return output_cen; //返回值,注意,返回值的数据类型应该和函数一致.}float C_to_F(float input_cen){float output_fah;output_fah=(9.0/5)*input_cen+32; //转换过程.return output_fah;}int main(){int choice;float input_fah,input_cen,output_fah,output_cen; //局部变量的调用及参数传递.printf("F_to_C press <1> and C_to_F press <2> !\n");scanf("%d",&choice);if(choice==1){printf("Please input fahrenheit :");scanf("%f",&input_fah); //这个是主函数定义的变量,虽然和调用函数同名.output_cen=F_to_C(input_fah);printf("The 华氏 is %d , 摄氏 is %d .",(int)input_fah,(int)output_cen);}if(choice==2)printf("Please input centigrade :");scanf("%f",&input_cen);output_fah=C_to_F(input_cen);printf("The Centigrade is %d , and the Fahrenheit is%d .",(int)input_cen,(int)output_fah);}return 0;}P038 3.2 计算存款利息(关于精度问题).#include<stdio.h>int main(){float p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;p1=p0*(1+r1);p2=p0*(1+r2);p3=p0*(1+r3/2)*(1+r3/2);printf("p1=%f\np2=%f\np3=%f\n",p1,p2,p3);return 0;}P055 3.3 大写转换成小写#include<stdio.h>int main() //小写范围是97-122,大写范围是65-90,大写加上即得小写.26个字母. {char c1, c2;c1='A';c2=c1+32;printf("%c %d",c2,c2);return 0;}P059 3.4 给出三角形边长,算出面积.#include<stdio.h>#include<math.h>int main(){double a=3.67, b=5.43, c=6.21, s, area;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf("area is %f\n",area); //默认可以组成三角形.return 0;}P065 3.5 求一无二次等式的根,默认两个不同根.#include<stdio.h>#include<math.h>int main(){double a,b,c,disc,x1,x2,p,q;scanf("%lf %lf %lf",&a,&b,&c);disc=b*b-4*a*c;p=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf("x1=%7.2f\nx2=%7.2f",x1,x2);return 0;}P071 3.6 用%f输出实数,只能得到六位小数.#include<stdio.h>#include<math.h>int main(){double a=1.0; //1是整型,1.0是浮点型,默认是double.可以是float.printf("%.9f\n",a/3);return 0;}P072 3.7 float型数据的有效位数.#include<stdio.h>#include<math.h>int main(){float a; //输出的结果是.333252,float精度6位,所以第七位后不可信.a=10000/3.0;printf("%f\n",a);return 0;}P078 3.8 使用putchar函数输出.#include<stdio.h>#include<math.h>int main(){char a='B',b='O',c='Y';putchar(a);putchar(b);putchar(c);putchar('\n');putchar(101); //101是A的ASCII代码的缩写,因为此函数只能输出字符.putchar(66);return 0;}P079 3.9 使用getchar得到字符.#include<stdio.h>#include<math.h>int main(){char a,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c); //这也是基本回显的C程序代码.putchar('\n');return 0;}P081 3.10 getchar得到大写,putchar输出小写.#include<stdio.h>#include<math.h>int main(){char a,b;a=getchar();b=a+32;putchar(b);putchar('\n');return 0;}P082 0.1 国民生产总值10年后的增长倍数.#include<stdio.h>#include<math.h>int main(){double p,r=0.09,n=10;p=pow((1+r),n); //这是数学函数, pow(x,y)计算x的y次方.printf("P is %lf when 10 years later .\n",p);return 0; //结果是0.36倍.}P082 0.2 求各种存款的利息数.#include<stdio.h>#include<math.h>int main(){double p,r,n; //年份和当年利率均有给出.p=1000*(1+5*0.0585);printf("5 years is %lf !\n",p); //1292.5,这是全五年期.lf输出的是double型.p=(1000*(1+2*0.0468));p=(p*(1+3*0.0540));printf("5 years is %lf !\n",p); //1270.76,这是先二年,再三年的.p=(1000*(1+3*0.0540));p=(p*(1+2*0.0468));printf("5 years is %lf !\n",p); //1270.76,这是先三年,再二年的.证明,是一样的.p=1000*pow((1+0.0414),5);printf("5 years is %lf !\n",p); //1224.86,这难道说是,相对的存死期越久越值钱.p=1000*pow((1+0.072/4),4*5);printf("5 years is %lf !\n",p); //1428.74.return 0;}P083 0.3 求几个月要以还贷.#include<stdio.h>#include<math.h>int main(){double m,r=0.01,d=300000,p=6000;m=(log10(p/(p-d*r)))/(log10(1+r));printf("%.1lf",m); //按要求只留一个小数,所以要写%.1lf.return 0;}P084 0.6 字母密码转换,调用函数及临界处理.#include<stdio.h>char printcode(char f){if(((int)f>86&&(int)f<91)||((int)f>118&&(int)f<123)){return(f-26+4); //因为putchar会自动返回,所以改成return,因为这是在函数中,调用需要返回值.}else{return(f+4);}}int main(){char a,b,c,d,e;printf("Please input :\n");a=getchar();b=getchar();c=getchar();d=getchar();e=getchar(); //临界问题.printf("%c%c%c%c%c",printcode(a),printcode(b),printcode(c),printcode(d),printc ode(e));putchar(putcharcode(a));putchar(putcharcode(b));putchar(putcharcode(c));putchar(putcharcode(d));putchar(putcharcode(e));return 0; //注意理解自定义函数的定义,使用,及形参实参的流向.//p84的是计算问题,自己看着办,最后要求小数点后两位,用的是%.2lf 来实现,因为没有要求实部,所以m.n格式中m不写.//p84的是定义问题,第一问,两者都行,但是定义字母时,scanf要写%c来获取,而定义数值时则要用%d来获取.// 第二问,putchar貌似只能输出字符,所以用printf.putchar本身就是字符输出函数啦,字符,有木有字符啊!!尽管它的参数可以是putchar('\101'),效果是输出字符A啊.// 第三问,出现"任何"及"无条件",那么答案明显是"否".可以转换,但要在某此条件下,例如输出和读入时,%c是字母,而%d是数值,看着办.}选择结构程序设计P086 4.1 一无二次方程求根的二分支.#include<stdio.h>#include<math.h>int main(){double a,b,c,disc,x1,x2,p,q;scanf("%lf %lf %lf",&a,&b,&c);disc=b*b-4*a*c;if(disc<0) //这是选择结构和其判断条件的示例.printf("This equation hasn't real roots\n");else{p=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf("x1=%7.2f\nx2=%7.2f",x1,x2);}return 0;}P087 4.2 二个数按大小输出.#include<stdio.h>int main() //此程序代表按大小顺序输出.{float a,b,t;scanf("%f %f",&a,&b); //出错时,注意检查这里是否按格式输入了.比如有个逗号.if(a>b){t=a;a=b;b=t;}printf("%5.2f,%5.2f\n",a,b);return 0;}P088 4.3 三个数按大小输出.#include<stdio.h>int main() //此程序代表按大小顺序输出.{float a,b,c,t;scanf("%f %f %f",&a,&b,&c);if(a>b) //此处执行后,a为小者.{t=a;a=b;b=t;}if(a>c) //此处执行后,a为小者.{t=a;a=c;c=t;}if(b>c) //上面已经搞定a是最小者,现在对比得出次小者,并且已经归到变量中.{t=b;b=c;c=t;}printf("%5.2f,%5.2f%5.2f\n",a,b,c);return 0;}P099 4.4 判断输入字符,并最终按小写输出.#include<stdio.h>int main(){char ch;scanf("%c",&ch);ch=(ch>='A'&&ch<='Z')?(ch+32):ch; //条件运算符,真取前,假取后.printf("%c\n",ch);return 0;}P100 4.5 按要求输出相应的Y值.#include<stdio.h>int main(){int x,y;scanf("%d",&x);if(x>=0){if(x>0) //这个范围要基于x>=0来判断.{y=1;}else{y=0;}}else//这个是x<0的范围.{y=-1;}printf("x=%d,y=%d",x,y);return 0;}P102 4.6 switch的简单应用.#include<stdio.h>int main(){char grade;scanf("%c",&grade);printf("Your score:");switch(grade){case'a':printf("85-100\n");break; //当多分支结构过于多时,可以用switch来解决.case'b':printf("70-84\n");break;case'c':printf("60-69\n");break;case'd':printf("<60\n");break;default:printf("Enter data error!\n");}return 0;}P104 4.7 按输入执行操作,并且不分大小写. #include<stdio.h>void action1(int x,int y){printf("x+y=%d\n",x+y);}void action2(int x,int y){printf("x*y=%d\n",x*y);}int main(){char ch;int a=15,b=23;ch=getchar();switch(ch){case'a':case'A':action1(a,b);break; //a,A共用一个执行语句.case'b':case'B':action2(a,b);break;default:putchar('\a'); //'\a'可以控制电脑发出蜂鸣声.}return 0;}P106 4.8 用if的分支来做闰年问题#include<stdio.h>int main(){int year,leap;printf("Please input the year:\n");scanf("%d",&year);if(year%4==0) //400年里,在100个可除尽4的年里计算,以下是在全范围内计算.{if(year%100==0) //既是100个内的,又是可以除尽100的,算是且.{if(year%400==0) //相当于且了两次,只余下唯一一个.{leap=1;}else//400年里只有1个除得尽,其它都不是.{leap=0;}}else//这里的范围是(year%4==0)&&(year%100!=0){leap=1; //在合法的100内,又不是2100,2200一类的,当然属闰年.}}else{leap=0;}if(leap){printf("%d is ",year);}else{printf("%d is not ",year);}printf("a leap year !"); //不管上面如何判断,最后还是会输出这一句.return 0;}P108 4.9 一元二次等式的全计算过程.#include<stdio.h>#include<math.h>int main(){double a,b,c,disc,x1,x2,realpart,imagpart;scanf("%lf %lf %lf",&a,&b,&c);printf("The equation ");if(fabs(a)<=1e-6) //fabs是绝对值, le-6,即是的负六次方,接近或是等于零.{printf("is not a quadratic !\n");printf("x1=x2=%lf",-c/b);}else{disc=b*b-4*a*c;if(fabs(disc)<=1e-6) //指数形式,数字1.{printf("has two equal roots : %lf\n",-b/(2*a));}else{if(disc>1e-6){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("has distinct real roots : %lf and %lf \n",x1,x2);}else{realpart=-b/(2*a);imagpart=sqrt(-disc)/(2*a);printf("has complex roots: \n");printf("%lf + %lfi\n",realpart,imagpart);printf("%lf + %lfi\n",realpart,imagpart);}}}return 0;}P109 4.10 关于多个区间的计算,运费问题为例. #include<stdio.h>int main(){double p,w,s,d,f;printf("p,w,s\n");scanf("%lf %lf %lf",&p,&w,&s);if(s<250){d=0.0;f=p*w*s*(1-d);printf("%lf",f);}else if(s<500){d=0.02;f=p*w*s*(1-d);printf("%lf",f);}else if(s<1000){d=0.05;f=p*w*s*(1-d);printf("%lf",f);}else if(s<2000){d=0.08;f=p*w*s*(1-d);printf("%lf",f);}else if(s<3000){d=0.1;f=p*w*s*(1-d);printf("%lf",f);}else{d=0.15;f=p*w*s*(1-d);printf("%lf",f);}return 0;}P0112 0.4 键盘输入三个数,输出最大者. #include<stdio.h>int mina,mida,maxa;int max(int a,int b,int c){int m;if(a>b) //凡是比较中,大于号代表升序排列.{m=a;a=b;b=m;}if(a>c){m=a;a=c;c=m;}if(b>c){m=a;a=c;c=m;}return (c); //此排序后,a,b,c由小到大.所以c是返回中最大的.}int main(){int a,b,c;printf("Please input 3 numbers :");scanf("%d %d %d",&a,&b,&c);printf("The max is %d !\n",max(a,b,c));return 0;}P0112 0.5 一个小于1000的数,有判定条件.#include<stdio.h>#include<math.h>int main(){double a,b;printf("Please input a number :");scanf("%lf",&a);if(a<1000){b=sqrt(a);}else{printf("\a"); //控制蜂鸣声printf("Please input a number again :");scanf("%lf",&a);11b=sqrt(a);}printf("%.0lf",b); //按要求,只取整数.return 0;}P0112 0.9 一个不多于5位的数,按条件输出.#include<stdio.h>#include<math.h>int main(){int i,a,b[4],count=0;printf("Please input a number (five-digit number) :"); //意思是五位数以内.scanf("%d",&a);for(i=0;i<=4;i++){b[i]=a%10; //是%10,不是/10000,结果就是b[0]至b[4]中存着个,十,百,千,万位.a=a/10; // /10后得到的结果去掉了个数.if(b[i]!=0){count=count+1;}}printf("%d位数.\n",count);printf("分别输出每一位数字,如下:");for(i=4;i>=0;i--){printf("%d ",b[i]);}printf("\n");printf("倒序输出这一个数字,如下:");for(i=0;i<=4;i++){printf("%d",b[i]);}printf("\n");return 0;}P0112 0.10 按要求提成奖金(if写法). #include<stdio.h>int main(){double a,b;printf("Please input profit amount :");scanf("%lf",&a);if(a<=10) //这个是整个if 的基础.{b=a*0.1;}else if(a<=20) //以上面为基础,这已经代表了10-20间了{b=1+(a-10)*0.075;}else if(a<=40) //同理,这是20-40间.{b=1+0.75+(a-20)*0.05;}else if(a<=60){b=1+0.75+0.1+(a-40)*0.03;}else if(a<=100){b=1+0.75+0.1+0.06+(a-60)*0.15;}else//这里是大于100的.{b=1+0.75+0.1+0.06+(a-100)*0.01;}printf("The reward is %lf !\n",b);return 0;}P0112 0.10 按要求提成奖金(switch写法).#include<stdio.h>double GetProfit(double c){printf("Please input profit amount :");scanf("%lf",&c);return (c); //返回本身了,如果有进一步的计算结果,将值赋给其它变量,也可以返回.}int main(){double a;char c;printf("Your choice :\nA : 0-10 ;\nB : 10-20 ;\nC : 20-40 ;\nD : 40-60 ;\nE :60-100 ;\nF : 100-1000 ;\n");scanf("%c",&c); //不同档时,应照档的规定数值进行输入,否则结果不会是正确的.switch(c) //前三后三用于比较调用函数.{ //GetProfit(c)引入的实参没有意义,返回值和引入的实参没关系,反而由键盘获得.case'A':{ printf("The reward is %lf !\n",GetProfit(c)*0.1);};break; //0-10 0.100case'B':{ printf("The reward is %lf !\n",1+(GetProfit(c)-10)*0.075);}break; //10-20 0.075case'C':{ printf("The reward is %lf !\n",1+0.75+(GetProfit(c)-20)*0.05);}break; //20-40 0.050case'D':{printf("Please input profit amount :");scanf("%lf",&a);printf("Thereward is %lf !\n",1+0.75+0.1+(a-40)*0.03);}break;//40-60 0.030case'E':{printf("Please input profit amount :");scanf("%lf",&a);printf("Thereward is %lf !\n",1+0.75+0.1+0.6+(a-60)*0.15);}break;//60-100 0.015case'F':{printf("Please input profit amount :");scanf("%lf",&a);printf("Thereward is %lf !\n",1+0.75+0.1+0.6+0.6+(a-100)*0.01);}break;//100-1000 0.010default:printf("Error data !");break; //可以有一个执行体,用{}包括起来.}return 0;}P0112 0.11 键盘输入数值,然后排序输出.#include<stdio.h>int main(){int a[10],i,j,m,n; //变量可以在写代码过程中再回来临时添加.printf("请输入数值的个数: (十个以内)");scanf("%d",&m); //到这里需要变量来承载数值的个数,设m,回头去定义变量那写上m.printf("请分别输入相对应个数的数值:");for(i=0;i<m;i++){scanf("%d",&a[i]);}for(i=0;i<m;i++){for(j=i+1;j<m;j++){if(a[i]>a[j]) //这是选择排序法.升序.{n=a[i]; //这里需要一个用于临时变量的n,所以回头去写上.a[i]=a[j];a[j]=n;}}}for(i=0;i<m;i++){printf("%d",a[i]);}return 0;}C程序设计(第四版)(谭浩强)第五章课后习题答案循环结构程序设计P115 5.1 用while计算1至100的合.#include<stdio.h>int main(){int i=1,sum=0;while(i<=100) //对于需要运算的值,要么在运算前可以赋值,要么一开始要指定.{sum=sum+i;i++;}printf("The sum is %d .\n",sum);return 0;}P117 5.2 用do-while来做1至100的合.#include<stdio.h>int main(){int i=1,sum=0;do// do-while可以做的事,用while都可以做到.{ //do-while先做一次执行,再判断条件,而while却是先做一个条件,再执行.sum=sum+i;i++;}while(i<=100);printf("The sum is %d .\n",sum);return 0;}P118 5.3 比较do-while与while的差别.#include<stdio.h>int main(){int i,sum=0;printf("Please input a number :");scanf("%d",&i); //输入10以内,正常,11的话,则sum仍然是0.while(i<=10){sum=sum+i;i++;}printf("The sum of 1-10 is %d .\n",sum);return 0;}#include<stdio.h>int main(){int i,sum=0;printf("Please input a number :");scanf("%d",&i); //输入10以内,结果一样.输入11的话,先做操作,所以sum=11.do{sum=sum+i;i++;}while(i<=10); //此重点在于理解二者的差别.printf("The sum of 1-10 is %d .\n",sum);return 0;}P126 5.4 break的例子.#include<stdio.h>int main(){int i,b=0,a,c;for(i=0;i<=1000;i++){printf("Please input amount :"); //循环体内套有输出语句以及跳出语句.scanf("%d",&a);b=b+a;if(b>=100){break; //break是用于跳出循环,对if无效,对while for switch 这一类.}}c=b/i;printf("conut is %d , aver is %d ",i+1,c); //注意%号后的形式,否则可能输出错误.return 0;P127 5.5 continue的例子.#include<stdio.h>int main(){int i;for(i=1;i<20;i++){if(i%3!=0){continue; //跳过本次I,执行下一个i.}printf("%d ",i);}printf("\n");return 0;}P128 5.6 形成一个4*5的矩阵.#include<stdio.h>int main(){int i,j,a=0; //没有给初值,会出现警告: 使用了未初始化的局部变量“a”.for(i=1;i<=4;i++){for(j=1;j<=5;j++,a++) // a用来控制换行.{if(a%5==0){printf("\n");}printf("%d\t",i*j);}}printf("\n");return 0;P131 5.7 用一个交错的式子求哌的近似值.#include<stdio.h>#include<math.h>int main() //四分之哌等于(1)-(1/3)+(1/5)-(1/7)+(1/9)-(1/11).{float s=1,n=1,m,sum=0,t;for(m=1;;m=m+2) //不确定哪项才会小于等于十的负六次方,所以不指定,无限下去.{ //不指定第二项,执行语句中应该有控制跳出的句子,否则死循环.t=(s)*(n/m); //这是第一项,s是符号,if(fabs(t)<=1e-6) //应该写在这里,题目要求这一项不累加进去.{break;}sum=sum+t; //因为累加项在这里,所以,一旦跳出就不会累加进来了.s=s*(-1); //变号一次.}printf("四分之一哌的值是%f.\n",sum);printf("一个完整哌的值是%f.\n",sum*4);return 0;}//下面这段小代码用来验证关于数值型数据类型的关系.去掉注释,可运行.//如果出现类似值为全1或是全零的话,一般可以考虑数据类型赋值或是定义错了的问题.//#include <stdio.h> //这是关于int float double三者关复杂关系的,乱啊,如果看不懂,可以通过实验自己明白来.////int main() //在C语言中,1.0认为是实数,即是double型,所以,如果你把它用float输出的话,会有警告:警告 1 warning C4305: “=”: 从“double”到“float”截断.//{// float m,a,b,c; //一旦定义了是这种类型的话,输出或是赋值的时候只能扩展不能截断,意思就是能变成double型,不能变成int型啦.而且后面的赋值会跟着它变成相应的类型.比如下面的m=1,其实得到的是m=1.0.// int d,e,f;// m=1;// a=1.0/3;// b=1/3;// c=m/3;// d=1.0;// e=1/4;// f=1.0/4;// printf("%lf(float用double的%lf来输出是可以的.)\n%f\n%f\n%f\n",m,a,b,c); //不管上面定义什么,这边写的输出类型是什么,就按相应的类型输出,有可能会出错,所以建议按定义的类型来输出.当然扩展的是不会错的,截断的是会错的,比如float可以用%lf来输出,而不能用%d来输出.// printf("%d\n%d\n%d\n",d,e,f); //但是,不相应的int型不可以用%f来输出的.因为int float就不同种类,一个是整数,一个是小数,float double同样是有小数点的// return 0;//}P133 5.8 著名的Fibonacci(费波那契数列问题)#include<stdio.h>int main() //这个就是著名的Fibonacci(费波那契数列问题){int f1=1,f2=1,f3,i;printf("%12d\n%12d\n",f1,f2);for(i=1;i<=38;i++) //注意,这是个基础问题,(i=1;i<=5;i++)这里其实进行了次运算,因为有f1,f2,要求有个,所以要有个.要么写<=38,要么写<39,边界问题一定要注意,不可以太随意{f3=f1+f2;printf("%12d\n",f3); //这个问题同样适用于"一对兔子一个月生一对"的问题.,f1=f2; //f1=f1+f2;此时它们各是,所以,现在的f1是.f2=f3; //f2=f2+f1;此时的f1已经是最先二者之和了.可以不用到f3.}return 0;}P135 5.9 还是求素数,方法不一样.#include<stdio.h>#include<math.h>int main(){double num;int i;printf("Please input a number :");scanf("%lf",&num); //因为sqrt要求是浮点型,那就给它浮点型,需要时再强制转换.for(i=2;i<=sqrt(num);i++) //这边是<=号没错.{if((int)num%i==0) //如果在这期间有任何一个可以为零的话,则不是素数.{break; //当然跳出.}} //执行到这里的时候,i=5,已经变成了!!if(i<=sqrt(num)){printf("Not %d",(int)num);}else//如上所述,i=5,超出了求根的值,所以是素数.{printf("Yes %d",(int)num);}return 0;}P137 5.10 求100至200间的素数.#include<stdio.h>//不解释,HOHO>>>>...#include<math.h>int main(){double j;int i,k=0;for(j=100;j<=200;j++){for(i=2;i<=sqrt(j);i++){if((int)j%i==0){break;}}k=k+1; //这里是布局的开头.学习一下,有助逻辑.if(i<=sqrt(j)){printf("Not %d ",(int)j);if(k%5==0) //5个换一次行.{printf("\n");。