谭浩强C语言完整详细答案(第8章)
(完整版)谭浩强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程序设计(第四版)谭浩强_课后习题答案第8章
C程序设计(第四版)谭浩强_课后习题答案第8章第8章善于利用指针2208.1指针是什么2208.2指针变量2228.2.1使用指针变量的例子2228.2.2怎样定义指针变量2238.2.3怎样引用指针变量2248.2.4指针变量作为函数参数2268.3通过指针引用数组2308.3.1数组元素的指针2308.3.2在引用数组元素时指针的运算2318.3.3通过指针引用数组元素2338.3.4用数组名作函数参数2378.3.5通过指针引用多维数组2458.4通过指针引用字符串2558.4.1字符串的引用方式2558.4.2字符指针作函数参数2598.4.3使用字符指针变量和字符数组的比较2638.5指向函数的指针2668.5.1什么是函数指针2668.5.2用函数指针变量调用函数2668.5.3怎样定义和使用指向函数的指针变量2688.5.4用指向函数的指针作函数参数2708.6返回指针值的函数2748.7指针数组和多重指针2778.7.1什么是指针数组2778.7.2指向指针数据的指针2808.7.3指针数组作main函数的形参2828.8动态内存分配与指向它的指针变量2858.8.1什么是内存的动态分配2858.8.2怎样建立内存的动态分配2858.8.3void指针类型2878.9有关指针的小结288习题2918-1#include <stdio.h>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;}#include <stdio.h>#include <string.h>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 <stdio.h>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;temp=number[0];number[0]=*min;*min=temp;if(max==number) max=min;temp=number[9];number[9]=*max;*max=temp; }void output(int *number){int *p;printf("Now,they are: ");for (p=number;p<number+10;p++)printf("%d ",*p);printf("\n");}8-4#include <stdio.h>int main(){void move(int [20],int,int);int number[20],n,m,i;printf("how many numbers?");scanf("%d",&n);printf("input %d numbers:\n",n);for (i=0;i<n;i++)scanf("%d",&number[i]);printf("how many place you want move?"); scanf("%d",&m);move(number,n,m);printf("Now,they are:\n");for (i=0;i<n;i++)printf("%d ",number[i]);printf("\n");return 0;}void move(int array[20],int n,int m){int *p,array_end;array_end=*(array+n-1);for (p=array+n-1;p>array;p--)*p=*(p-1);*array=array_end;m--;if (m>0) move(array,n,m);}8-5#include <stdio.h>int main(){int i,k,m,n,num[50],*p;printf("\ninput number of person: n="); scanf("%d",&n);p=num;for (i=0;i<n;i++)*(p+i)=i+1;i=0;k=0;m=0;while (m<n-1){if (*(p+i)!=0) k++;if (k==3){*(p+i)=0;k=0;m++;}i++;if (i==n) i=0;}while(*p==0) p++;printf("The last one is NO.%d\n",*p); return 0;}8-6#include <stdio.h>int main(){int length(char *p);int len;char str[20];printf("input string: ");scanf("%s",str);len=length(str);printf("The length of string is %d.\n",len); return 0;}int length(char *p){int n;n=0;while (*p!='\0'){n++;p++;}return(n);}8-7#include <stdio.h>#include <string.h>int main(){void copystr(char *,char *,int);int m;char str1[20],str2[20];printf("input string:");gets(str1);printf("which character that begin to copy?"); scanf("%d",&m);if (strlen(str1)<m)printf("input error!");else{copystr(str1,str2,m);printf("result:%s\n",str2);}return 0;}void copystr(char *p1,char *p2,int m){int n;n=0;while (n<m-1){n++;p1++;}while (*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}8-8#include <stdio.h>int main(){int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20];printf("input string: ");while ((s[i]=getchar())!='\n') i++;p=&s[0];while (*p!='\n'){if (('A'<=*p) && (*p<='Z'))++upper;else if (('a'<=*p) && (*p<='z'))++lower;else if (*p==' ')++space;else if ((*p<='9') && (*p>='0'))++digit;else++other;p++;}printf("upper case:%d lower case:%d",upper,lower);printf(" space:%d digit:%d other:%d\n",space,digit,other); return 0;}8-9#include <stdio.h>int main(){void move(int *pointer);int a[3][3],*p,i;printf("input matrix:\n");for (i=0;i<3;i++)scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);p=&a[0][0];move(p);printf("Now,matrix:\n");for (i=0;i<3;i++)printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);return 0;}void move(int *pointer){int i,j,t;for (i=0;i<3;i++)for (j=i;j<3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}8-10-1#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p){int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++)for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12);*(p+12)=*pmax;*pmax=temp;temp=*p;*p=*pmin;*pmin=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && (*pmin>*(p+5*i+j))) pmin=p+5*i+j;temp=*pmin;*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && ((p+5*i+j)!=(p+4)) && ((p+5*i+j)!=(p+20)) && (*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+24);*(p+24)=temp;}8-10-2#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p) //交换函数{int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++) //找最大值和最小值的地址,并赋给pmax,pmin for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12); //将最大值与中心元素互换*(p+12)=*pmax;*pmax=temp;temp=*p; //将最小值与左上角元素互换*p=*pmin;*pmin=temp;pmin=p+1;//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素for (i=0;i<5;i++) //找第二最小值的地址赋给pminfor (j=0;j<5;j++){if(i==0 && j==0) continue;if (*pmin > *(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第二最小值与右上角元素互换*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++) //找第三最小值的地址赋给pminfor (j=0;j<5;j++){if((i==0 && j==0) ||(i==0 && j==4)) continue;if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; // 将第三最小值与左下角元素互换*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++) // 找第四最小值的地址赋给pminfor (j=0;j<5;j++){if ((i==0 && j==0) ||(i==0 && j==4)||(i==4 && j==0)) continue;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第四最小值与右下角元素互换*pmin=*(p+24);*(p+24)=temp;}8-11-1#include <stdio.h>#include <string.h>int main(){void sort(char s[][6]);int i;char str[10][6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);sort(str);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char s[10][6]){int i,j;char *p,temp[10];p=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(p,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],p);}}8-11-2#include <stdio.h>#include <string.h>int main(){void sort(char (*p)[6]);int i;char str[10][6];char (*p)[6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);p=str;sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char (*s)[6]){int i,j;char temp[6],*t=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(t,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],t);}}8-12#include <stdio.h>#include <string.h>int main(){void sort(char *[]);int i;char *p[10],str[10][20];for (i=0;i<10;i++)p[i]=str[i];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",p[i]);sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",p[i]);return 0;}void sort(char *s[]){int i,j;char *temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(*(s+j),*(s+j+1))>0){temp=*(s+j);*(s+j)=*(s+j+1);*(s+j+1)=temp;}}8-13#include<stdio.h>#include<math.h>int main(){float integral(float(*)(float),float,float,int);//对integarl函数的声明float fsin(float); //对fsin函数的声明float fcos(float); //对fcos函数的声明float fexp(float); //对fexp函数的声明float a1,b1,a2,b2,a3,b3,c,(*p)(float);int n=20;printf("input a1,b1:");scanf("%f,%f",&a1,&b1);printf("input a2,b2:");scanf("%f,%f",&a2,&b2);printf("input a3,b3:");scanf("%f,%f",&a3,&b3);p=fsin;c=integral(p,a1,b1,n);printf("The integral of sin(x) is:%f\n",c);p=fcos;c=integral(p,a2,b2,n);printf("The integral of cos(x) is:%f\n",c);p=fexp;c=integral(p,a3,b3,n);printf("The integral of exp(x) is:%f\n",c);return 0;}float integral(float(*p)(float),float a,float b,int n){int i;float x,h,s;h=(b-a)/n;x=a;s=0;for(i=1;i<=n;i++){x=x+h;s=s+(*p)(x)*h;}return(s);}float fsin(float x){return sin(x);}float fcos(float x){return cos(x);}float fexp(float x){return exp(x);}8-14#include <stdio.h>int main(){void sort (char *p,int m);int i,n;char *p,num[20];printf("input n:");scanf("%d",&n);printf("please input these numbers:\n");for (i=0;i<n;i++)scanf("%d",&num[i]);p=&num[0];sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",num[i]);printf("\n");return 0;}void sort (char *p,int m) // 将n个数逆序排列函数{int i;char temp, *p1,*p2;for (i=0;i<m/2;i++){p1=p+i;p2=p+(m-1-i);temp=*p1;*p1=*p2;*p2=temp;}}8-15#include <stdio.h>int main(){void avsco(float *,float *);void avcour1(char (*)[10],float *);void fali2(char course[5][10],int num[],float *pscore,float aver[4]); void good(char course[5][10],int num[4],float *pscore,float aver[4]); int i,j,*pnum,num[4];float score[4][5],aver[4],*pscore,*paver;char course[5][10],(*pcourse)[10];printf("input course:\n");pcourse=course;for (i=0;i<5;i++)scanf("%s",course[i]);printf("input NO. and scores:\n");printf("NO.");for (i=0;i<5;i++)printf(",%s",course[i]);printf("\n");pscore=&score[0][0];pnum=&num[0];for (i=0;i<4;i++){scanf("%d",pnum+i);for (j=0;j<5;j++)scanf("%f",pscore+5*i+j);}paver=&aver[0];printf("\n\n");avsco(pscore,paver); // 求出每个学生的平均成绩avcour1(pcourse,pscore); // 求出第一门课的平均成绩printf("\n\n");fali2(pcourse,pnum,pscore,paver); // 找出2门课不及格的学生printf("\n\n");good(pcourse,pnum,pscore,paver); // 找出成绩好的学生return 0;}void avsco(float *pscore,float *paver) // 求每个学生的平均成绩的函数{int i,j;float sum,average;for (i=0;i<4;i++){sum=0.0;for (j=0;j<5;j++)sum=sum+(*(pscore+5*i+j)); //累计每个学生的各科成绩average=sum/5; //计算平均成绩*(paver+i)=average;}}void avcour1(char (*pcourse)[10],float *pscore) // 求第一课程的平均成绩的函数{int i;float sum,average1;sum=0.0;for (i=0;i<4;i++)sum=sum+(*(pscore+5*i)); //累计每个学生的得分average1=sum/4; //计算平均成绩printf("course 1:%s average score:%7.2f\n",*pcourse,average1);}void fali2(char course[5][10],int num[],float *pscore,float aver[4])// 找两门以上课程不及格的学生的函数{int i,j,k,labe1;printf(" ==========Student who is fail in two courses======= \n"); printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){labe1=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)<60.0) labe1++;if (labe1>=2){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}void good(char course[5][10],int num[4],float *pscore,float aver[4]) // 找成绩优秀学生(各门85以上或平均90分以上)的函数{int i,j,k,n;printf(" ======Students whose score is good======\n");printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){n=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)>85.0) n++;if ((n==5)||(aver[i]>=90)){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}8-16#include <stdio.h>int main(){char str[50],*pstr;int i,j,k,m,e10,digit,ndigit,a[10],*pa;printf("input a string:\n");gets(str);pstr=&str[0]; /*字符指针pstr置于数组str 首地址*/pa=&a[0]; /*指针pa置于a数组首地址*/ndigit=0; /*ndigit代表有多少个整数*/i=0; /*代表字符串中的第几个字符*/j=0;while(*(pstr+i)!='\0'){if((*(pstr+i)>='0') && (*(pstr+i)<='9'))j++;else{if (j>0){digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /*将含有两位以上数的其它位的数值累计于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表该位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值\累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;pa++; /*指针pa指向a数组下一元素*/j=0;}}i++;}if (j>0) /*以数字结尾字符串的最后一个数据*/ {digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /* 将含有两位以上数的其它位的数值累加于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;j=0;}printf("There are %d numbers in this line, they are:\n",ndigit);j=0;pa=&a[0];for (j=0;j<ndigit;j++) /*打印数据*/printf("%d ",*(pa+j));printf("\n");return 0;}8-17#include<stdio.h>int main(){int strcmp(char *p1,char *p2);int m;char str1[20],str2[20],*p1,*p2;printf("input two strings:\n");scanf("%s",str1);scanf("%s",str2);p1=&str1[0];p2=&str2[0];m=strcmp(p1,p2);printf("result:%d,\n",m);return 0;}int strcmp(char *p1,char *p2) //两个字符串比较函数{int i;i=0;while(*(p1+i)==*(p2+i))if (*(p1+i++)=='\0') return(0); //相等时返回结果0return(*(p1+i)-*(p2+i)); //不等时返回结果为第一个不等字符ASCII码的差值}8-18#include <stdio.h>int main(){char *month_name[13]={"illegal month","January","February","March","April", "May","June","july","August","September","October", "November","December"};int n;printf("input month:\n");scanf("%d",&n);if ((n<=12) && (n>=1))printf("It is %s.\n",*(month_name+n));elseprintf("It is wrong.\n");return 0;}8-19-1#include <stdio.h>#define NEWSIZE 1000 //指定开辟存区的最大容量char newbuf[NEWSIZE]; //定义字符数组newbufchar *newp=newbuf; //定义指针变量newp,指向可存区的始端char *new(int n) //定义开辟存区的函数new,开辟存储区后返回指针{if (newp+n<=newbuf+NEWSIZE) // 开辟区未超过newbuf数组的大小{newp+=n; // newp指向存储区的末尾return(newp-n); // 返回一个指针,它指向存区的开始位置}elsereturn(NULL); // 当存区不够分配时,返回一个空指针}8-19-2#include <stdio.h>#define NEWSIZE 1000char newbuf[NEWSIZE];char *newp=newbuf;void free(char *p) //释放存区函数{if (p>=newbuf && p< newbuf + NEWSIZE)newp=p;}8-20#define LINEMAX 20 /*定义字符串的最大长度*/int main(){int i;char **p,*pstr[5],str[5][LINEMAX];for (i=0;i<5;i++)pstr[i]=str[i]; /*将第i个字符串的首地址赋予指针数组pstr 的第i个元素*/ printf("input 5 strings:\n");for (i=0;i<5;i++)scanf("%s",pstr[i]);p=pstr;sort(p);printf("strings sorted:\n");for (i=0;i<5;i++)printf("%s\n",pstr[i]);}sort(char **p) /*冒泡法对5个字符串排序函数*/{int i,j;char *temp;for (i=0;i<5;i++){for (j=i+1;j<5;j++){if (strcmp(*(p+i),*(p+j))>0) /*比较后交换字符串地址*/{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}return 0;}8-21#include<stdio.h>int main(){void sort(int **p,int n);int i,n,data[20],**p,*pstr[20];printf("input n:\n");scanf("%d",&n);for (i=0;i<n;i++)pstr[i]=&data[i]; //将第i个整数的地址赋予指针数组pstr 的第i个元素printf("input %d integer numbers:",n);for (i=0;i<n;i++)scanf("%d",pstr[i]);p=pstr;sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",*pstr[i]);printf("\n");return 0;}void sort(int **p,int n){int i,j,*temp;for (i=0;i<n-1;i++){for (j=i+1;j<n;j++){if (**(p+i)>**(p+j)) //比较后交换整数地址{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}}。
C语言程序设计(第三版)谭浩强著 各章习题答疑
以八、 /* 以八、十六进制输出 */
} ???? 是对应的带符号短整数。 输出结果是八进制整数 十六进制整数
2
3.9 计算某个表达式的值。 通用程序清单如下: stdio.h” #include ”stdio.h stdio.h void main() { ?????? x; 计算表达式值存入x x=( 表达式 );/* 计算表达式值存入x */ printf(”% printf( %?\n”,x); ,x); } ?????? 表达式对应的数据类型符(需事先判断) ? 表达式为整型,则为d 表达式为实型,则为f 3
第七章习题答疑(二)
折半查找法在排序数组中查找某个数。 上课时已有提示。 7.13 字符串连接(b[]中字符串连接到a[]中字符串后) 设:a[10]=”12 ,b[]=”abc 12”, abc” 12 abc (1)找到a串的’\0’的下标(i=2) ’ (2)j=0; 当型循环(b[j]!=’\0’) ’\ ’ a[i]=b[j],i++,j++ (3)a[i]=’\0’ \ 7.15 字符串复制(b[]中字符串复制到a[]中) 设:a[10]=”12”,b[]=”abc” (1)i=0; 当型循环(b[i]!=’\0’) a[i]=b[i],i++ (2)a[i]=’\0’ 7.9
18
第十章习题答疑(一)
输入3个整数,按照由小到大顺序输出。 int x,y,z,*px=&x,*py=&y,*pz=&x; 用以前的程序,其中的x、y、z换成*px、*py、*pz 10.3 输入10个整数存入一维数组a[10],将其中最小数与第1 个数交换,最大数与最后一个数交换。 要求编3个函数: 10 10 输入10个整数、交换、输出交换后的10个整数。 输入函数: void in(int *p,int n) 输出函数: void out(int *p,int n) 交换函数: void change(int *p,int n) 其中寻找最大最小数的方法: int *q,*max,*min; for (max=min=p,q=p+1;q<p+n;q++) { if(*max<*q) max=q; if(*min>*q) min=q; } 19 交换的方法:*p和*min,*(p+n-1)和*max 10.1
C语言程序设计(谭浩强)第八章详细课后答案
#if 0 //8.1#include<stdio.h>#include<math.h>void main(){int Common_divisor(int a,int b);int Common_mutiple(int m,int n);int result_1,result_2;int number_1,number_2;printf("Enter number_1 and number_2 value:");scanf("%d,%d",&number_1,&number_2);result_1 = Common_divisor(number_1,number_2);result_2 = Common_mutiple(number_1,number_2);printf("最大公约数:%d\n最小公倍数:%d\n",result_1,result_2); }int Common_divisor(int a,int b){int r = 1;while(r != 0){if(a>b){r = a%b;a = b;b = r;if(r == 0){return a;break;}}else{r = b%a;b = a;if(r == 0){return b;break;}a = r;}}}int Common_mutiple(int m,int n){int Common_divisor(int a,int b);int temp = 0,result = 0;temp = Common_divisor(m,n);result = (m*n)/temp;return result;}#endif#if 0 //8.2#include<stdio.h>#include<math.h>float result_1 =0.0,result_2 = 0.0;void main(){void Funvtion_1(float a,float b,float temp);void Function_2(float a,float b);float a,b,c;float temp;printf("Enter a,b,c value:");scanf("%f,%f,%f",&a,&b,&c);temp = b*b-4*a*c;if(temp > 0){Funvtion_1(a,b,temp);printf("x1 = %.3f,x2 = %.3f\n",result_1,result_2);}else if(temp < 0){printf("此函数没有根!\n");}else{Function_2(a,b);printf("x1 = x2 = %.3f\n",result_1);}}void Funvtion_1(float a,float b,float temp){result_1 = (-b+temp)/(2*a);result_2 = (-b-temp)/(2*a); }void Function_2(float a,float b) {result_1 = (-b)/(2*a);}#endif#if 0 //8.3#include<stdio.h>#include<math.h>void main(){void Prime(int n);int m;printf("Enter m value:");scanf("%d",&m);Prime(m);}void Prime(int n){int i = 0 ,k;k = (int)sqrt(n);for(i = 2; i <= k;i++){if(n%i == 0){break;}}if(i > k){printf("The number %d is a prime!\n",n);}else{printf("The number %d isn't a prime!\n",n);}}#endif#if 0#include<stdio.h>#include<math.h>int c[3][3];void main(){void Function(int b[3][3]);int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};int i,j;// int d[3][3];int k = sizeof(a[2])/sizeof(int); //列数int n = sizeof(a)/sizeof(int);for(i = 0; i < n/k;i++){for(j = 0;j < k;j++){printf("%d ",a[i][j]);}printf("\n");}Function(a);for(i = 0; i < k;i++){for(j = 0;j < n/k;j++){printf("%d ",c[i][j]);}printf("\n");}}void Function(int b[3][3]){int i = 0,j = 0;int k = sizeof(b[3])/sizeof(int);int n = sizeof(b)/sizeof(int);for(i = 0;i < k ;++i){for(j = 0;j < k; ++j){c[j][i] = b[i][j];}}}#endif#if 0#include<stdio.h>#include<math.h>#include<string.h>void main(){void Function(char a[100],char b[100]);char a[100];extern int b[100];int n = sizeof(a);printf("Enter a Array:");gets(a);Function(a,b);}void Function(char a[],char b[]){int i = 0;int n = sizeof(a)/sizeof(char);int m = strlen(a);for(i = 0; i < m;i++){b[i] = a[m-1-i];}//printf("put b Array:");//puts(b);for(i = 0;i < m;i++){putchar(b[i]);}}#endif#if 0 //8.6#include<stdio.h>#include<math.h>void main(){char a[20];char b[10];printf("Enter string a value:");gets(a);printf("Enter string b value:");gets(b);strcat(a,b);puts(a);}#endif#if 0 //8.6#include<stdio.h>#include<math.h>#include<string.h>int a[20];void main(){void Connect(char a[],char b[]);char b[10];printf("Enter string a value:");gets(a);printf("Enter string b value:");gets(b);Connect(a,b);// puts(a);printf("%s",a);}void Connect(char a[],char b[]) {int i = 0;int j = 0;while(a[i] != '\0'){i++;}while(b[j] != '\0'){a[i++] = b[j++];}// a[i] = '\0';}#endif#if 0//8.7#include<stdio.h>#include<math.h>#include<string.h>char b[100];void main(){void Research_vowel(char a[]);char a[50];printf("Enter string a value:");gets(a);Research_vowel(a);puts(b);}void Research_vowel(char a[]){int i = 0,j = 0;for(; i < strlen(a);i++){if(a[i] == 'a'|| a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u'){b[j] = a[i];j++;}}#endif#if 0//8.8#include<stdio.h>#include<math.h>char a[4];void main(){void Divide(int n);int n = 0,i = 0;printf("Enter n value(<10000):");scanf("%d",&n);Divide(n);for(;i<4;i++){printf("%d ",a[i]);}printf("\n");}void Divide(int n)int i = 0,c = 0,b = 0,m = 1000;while(m >= 1){c = n/m;b = n%m;m = m/10;a[i] = c;n = b;i++;}}#endif#if 0//8.9#include<stdio.h>#include<math.h>#include<string.h>int m = 0,n = 0,r = 0,l = 0;void main(){void Research_number(char a[]);char a[50];int i = 0;printf("enter string value:");gets(a);/*for(;i<strlen(a);i++){printf("%c",a[i]);}*/puts(a);puts("");Research_number(a);printf("字母个数是:%d,数字个数是:%d,空格个数是:%d,其它:%d\n",m,n,r,l);}void Research_number(char a[]){int i = 0;for(i = 0;i<strlen(a);i++){if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')){m++;}else if(a[i] > 47 && a[i] < 58){n++;}else if(a[i] == 32){r++;}else{l++;}}}#endif#if 0 //8.10#include<stdio.h>#include<math.h>#include<string.h>void main(){void Array(char a[]);char b[20];printf("Enter b string(<20):");gets(b);printf("Longest word is : ");Array(b);}void Array(char a[]){int i = 0,place = 0,point = 0,length = 1; //point表示最长单词的开始位置place表示最长单词最后的一个位置int max = 0,j = 0;int flag = 0; //为0 时是空格为1时是单词max = length;for(i = 0;i<=strlen(a);i++){if(a[i] != ' ' && a[i] != '\0'){if(0 == flag){j = i;flag = 1;}elselength++; //这个题让我做的非常的难过以后要好好的复习}else{flag = 0;if(max < length){max = length;point = j;place = i;}length = 1;}}for(i = point;i <= place;i++){printf("%c",a[i]);}printf("\n");}/*int judge_words(char b[20]){int i = 0,flag = 1;for(i = 0;i < strlen(b); i++){if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')){flag = 1;}else{flag = 0;}}}*/#if 0 //8.11#include<stdio.h>#include<math.h>void main(){void bubbling(char a[]);char a[10];printf("enter string a value:");gets(a);bubbling(a);puts(a);}void bubbling(char a[]){int i = 0,j = 0;char temp;for(i = 0;i<strlen(a) - 1;i++){for( j = 0;j<strlen(a)-1-i;j++)if(a[j] > a[j+1]){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}}#endif#if 0 //8.12#include<stdio.h>#include<math.h>void main(){float Function(float x);float x = 1.5,x1 = 0.0;x1 = Function(x);printf("%.3f\n",x1);}float Function(float x){float x1;float y,y1;while(fabs(y1)>=1e-6){x = x1;y1 = x*x*x+2*x*x+3*x+4;y = 3*x*x+4*x+3;x1 = x - (y1/y);}return x1;}#endif#if 0//8.13#include<stdio.h>#include<math.h>void main(){float p(float x,int n);float x = 0.0,result = 0.0;int n = 0;printf("Enter x and n value:");scanf("%f,%d",&x,&n);result = p(x,n);printf("p(x) value :%.3f\n",result);}float p(float x,int n){float y = 0.0;if(n == 0){y = 0;}else if(n == 1){y = x;}else{y = ((2*n-1)*x-p(x,n-1)-(n-1)*p(x,n-2))/n;}return y;}#endif#if 0//8.14#include<stdio.h>#include<math.h>int r = 0,l = 0;void main(){float* EStudent_aver(float scores[10][5]);float* EClass_aver(float scores[10][5]);float Maxscore(float scores[10][5]);float variance(float scores[10][5]);float scores[10][5] = {{76,43,46,73,46},{34,67,89,32,47},{31,68,73,16,87},{31,67,31,37,63},{16 ,37,61,67,86},{31,34,60,49,29},{89,89,87,87,98},{97,34,89,42,89},{45,67,34,18,64},{ 32,16,87,42,15}};float *Student_aver,*Class_aver,Student_max = 0.0,aver_variance = 0.0;int i = 0,j = 0,k = 0;/* char Student_name[] = {"Zhanghua Xiaoming Ligang Xiaotao Zhangsan Xiaoqiang CBC Baixu Gongcheng BOBO "};char score_name[] = {"Chinese Math English Chemitry WuLi"};printf("\t");for(i = 0;i < strlen(score_name);i++){printf("%c",score_name[i]);}puts("");for(i = 0;i < strlen(Student_name);i++){printf("%c",Student_name[i]);if(Student_name[i] == 32){for(j = 0;j < 5;j++){printf(" %.0f\t",scores[k][j]);}k++;puts("");}}*//* printf("\n");for(i = 0; i < 10;i++){for(j = 0;j < 5;j++){printf(" %.0f\t",scores[i][j]);}puts("");}*/// float scores[10][5];printf("NO. course1 course2 course3 course4 course5\n");for(i = 0;i < 10;i++){printf("No.%2d\t",i+1);for(j = 0;j < 5;j++){printf("%.2f\t ",scores[i][j]);}puts("");}Student_aver = EStudent_aver(scores);Class_aver = EClass_aver(scores);Student_max = Maxscore(scores);aver_variance = variance(scores);printf("Student_aver:\n");for(i = 0;i < 10;i++){printf("%.2f ",Student_aver[i]);}printf("\nClass_aver:\n");for(i = 0 ;i < 5;i++){printf("%.2f ",Class_aver[i]);}printf("\n");printf("The Max Score:\n %.2f Name: No.%d , Course: course%d\n",Student_max,r,l);printf("Student average Variance: \n %.2f\n",aver_variance);}float * EStudent_aver(float scores[10][5]){static float aver[10],sum = 0.0;int i = 0,j = 0;for(i = 0;i < 10;i++){for(j = 0; j < 5; j++){sum += scores[i][j];}aver[i] = sum/5;sum = 0;}return aver;}float* EClass_aver(float scores[10][5]) {static float aver_1[5],sum = 0.0;int i = 0,j = 0;for(j = 0;j<5;j++){for(i = 0;i<10;i++){sum +=scores[i][j];}aver_1[j] = sum/10;sum = 0;}return aver_1;}float Maxscore(float scores[10][5]) {int i = 0,j = 0;float max = scores[0][0];for(i = 0;i < 10;i++){for(j = 0;j < 5;j++){if(max < scores[i][j]){max = scores[i][j];r = i + 1;l = j + 1;}}}return max;}float variance(float scores[10][5]){float* EStudent_aver(float scores[10][5]);float s = 0.0;float sum_1 = 0.0,sum_2 = 0.0;float number_1 = 0.0,number_2 = 0.0;float* student_aver;int i = 0,j = 0;student_aver = EStudent_aver(scores);for(i = 0;i<10;i++){sum_1 += student_aver[i]*student_aver[i];sum_2 += student_aver[i];}number_1 = sum_1/10;number_2 = (sum_2/10)*(sum_2/10);s = number_1 - number_2;return s;}#endif#if 0 //测试static#include<stdio.h>#include<math.h>static j;void f1(){static i = 0;i++;}void f2(){j = 0;j++;}void main(){int k = 0;for(k = 0;k < 10;k++){f1();f2();}}#endif#if 0//8.16#include<stdio.h>#include<math.h>void main(){void Change(int n);int m = 0;printf("Enter ox number:");scanf("%x",&m);// printf("%d\n",m);Change(m);}void Change(int n){//int m;printf("十进制是: %d\n",n); }#endif#if 0//8.17#include<stdio.h>#include<math.h>void main(){void Function(int n);int m;printf("Input a integer:");scanf("%d",&m);Function(m);}void Function(int n){int i;/* if(n!=0){ch[i] = n%10;temp = n/10;i++;Function(temp);}else{ch[i] = 0;}return ch;*/i = n/10;if(i != 0){Function(i);}putchar(n%10+'0');putchar(32);}#endif#if 0 //今天是今年的多少天#include<stdio.h>#include<math.h>void main(){int i = 1,d = 0,sum = 0;int year,month,day;printf("Enter year and month and day(2012/08/19):");scanf("%d/%d/%d",&year,&month,&day);sum = day;for(;i<month;i++){switch(i){case 1:case 3:case 5:case 7:case 8:case 10:case 12: d = 31; break;case 2:if(year%400 == 0 || year%4 == 0 && year%100!=0){d = 29;}else{d = 28;}break;case 4:case 6:case 9:case 11: d = 30;break;}sum += d;}printf("The %d of The year dates!\n",sum); }#endif#if 0 //8.15#include<stdio.h>#define N 10void main(){void Enter_massage(char staff_name[N][10],int staff_number[N]);void Sort(char staff_name[N][10],int staff_number[N]);int middle_search(int staff_number[N],int number);int number,temp = 0;char staff_name[N][10];int staff_number[N];Enter_massage(staff_name,staff_number);//输入信息Sort(staff_name,staff_number);printf("Enter search staff_number:");scanf("%d",&number);temp = middle_search(staff_number,number);printf("The staff number is : %d\n",staff_number[temp]);printf("The staff name is : %s\n",staff_name[temp]);}void Enter_massage(char staff_name[N][10],int staff_number[N]) {int i;for(i = 0;i < N;i++){printf("Enter Staff number No.:");scanf("%d",&staff_number[i]);fflush(stdin);printf("Input staff name:");gets(staff_name[i]);}}void Sort(char staff_name[N][10],int staff_number[N]) {int i,j;int temp1;char temp[N][10];for(i = 0;i < N - 1; i++){for(j = 0;j < N - 1 - i; j++){if(staff_number[j]>staff_number[j+1]){temp1 = staff_number[j];strcpy(temp[j],staff_name[j]);staff_number[j] = staff_number[j+1];strcpy(staff_name[j],staff_name[j+1]);staff_number[j+1] = temp1;strcpy(staff_name[j+1],temp[j]);}}/*if(staff_number[i]<staff_number[i+1]){temp1 = staff_number[i];staff_number[i] = staff_number[i+1];staff_number[i+1] = temp1;}*/}for(i = 0;i < N;i++){printf("staff number is : %d\n",staff_number[i]);printf("staff name is %s\n",staff_name[i]);}}int middle_search(int staff_number[N],int number) //折半查找法!{int min = 0,high = 0,mid = 0;int i = 0;min = 0;high = N - 1;while(min <= high){mid = (min+high)/2;if(staff_number[mid] > number){high = mid - 1;}else if(staff_number[mid] < number){min = mid + 1;}else{return mid;}}}#endif/*fasdfa the world helloint flag=0;flag=1;//letterflag=0;//space*/#if 0 //有问题输出的是不对的!为什么指针值temp+1的值不是指向的name[1]这一行的首地址而是++后指向的是name[0]下一列的地址呢?#include<stdio.h>void main(){char name[5][10];char temp[5][10];int i = 0;// temp = &name[0];for(i = 0;i < 5;i++){gets(name[i]);strcpy(temp[i],name[i]);}for(i = 0;i < 5;i++){//puts(name[i]);puts(temp[i]);}}#endif#if 0 //看不懂的程序8.15 #define N 10find(a,b)int a[],b[];{int i,j,s,t,c[N][2];for(i=0;i<N;i++){c[i][1]=a[i];c[i][1]=i;} for(i=0;i<N;i++)for(j=0;j<N-i-1;j++)if(c[i][0]>c[i+1][0]){t=c[i][0];c[i][0]=c[i+1][0];c[i+1][0]=t;s=c[i][1];c[i][1]=c[i+1][1];c[i+1][1]=s;}for(i=0;i<N;i++)b[i]=c[i][1];return;}lookfor(h,k)int h[],k;{int i,j;for(i=0;i<N;i++)if(h[i]-k==0) j=i;return j;}main(){int number[N],x[N],i,j,u,p;char name[N][20];for(i=0;i<N;i++){gets(name[i]);scanf("%d",&number[i]);} scanf("%d",&p);find(number,x);u=lookfor(number,p);for(i=0;i<N;i++){printf("%d",number[i]);puts(name[x[i]]);}puts(name[x[u]]);}#endif#if 1 //16进制转换成10进制#include<stdio.h>#include<math.h>#define N 10int flag = 1;void main(){extern int sum;int convert(char str[],int n);char str[N];int s = 0,n = 0;puts("Enter 16 OX number:");gets(str);n = strlen(str);s = convert(str,n);if(1 == flag){printf("10 d number is : %d\n",s);}else{printf("The number isn't OX \n");}}int convert(char str[],int n){int i,j = 0;int sum = 0;for(i = n - 1;i > -1;i--,j++){if(str[i] > 47 && str[i] < 58){sum = sum + (str[i] - 48) * (int)pow(16,j);}else if(str[i] > 64 && str[i] < 71){sum = sum + (str[i] - 55)* (int)pow(16,j);}else if(str[i] > 96 && str[i] < 103){sum = sum + (str[i] - 87) * (int)pow(16,j);}else{flag = 0;}}return sum;}#endif#if 0#include<stdio.h>#include<math.h>void main(){printf("%d\n",(int)pow(2,0)); }#endif。
C++面向对象程序设计答案1~8章_谭浩强
}
void change(int ,int )
{
int r1,r2,temp;
temp=r1;
r1=r2;
r2=temp;
}
13:
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
}
int main()
{Time t1;
t1.set_time();
t1.show_time();
return 0;
}
2:
#include <iostream>
using namespace std;
class Time
{public:
void set_time(void)
{cin>>hour;
cin>>minute;
int sec;
};
void Time::set_time() //在main函数之前定义
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time() //在main函数之前定义
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
cout<<endl<<endl;
}
void sort(float a[])
C++程序设计第三版(谭浩强)第八章习题答案
C++程序设计第三版(谭浩强)第八章习题答案8.1 题#includeusing namespace std;class Time{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();return 0;}8.2 题#includeusing namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;}void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;} private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}8.3 题#includeusing namespace std;class Time{public:void set_time(void);void show_time(void);private:int hour;int minute;int sec;};void Time::set_time(void){cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;} Time t;int main(){ t.set_time();t.show_time();return 0;}8.4 题//xt8-4.h(student.h)class Student{ public:void display( );void set_value();private:int num;char name[20];char sex ;};8.4 题//xt8-4-1.cpp(main.cpp)#includeusing namespace std;#include "xt8-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}8.4 题另一解//xt8-4-2.cpp(即 student.cpp)#include "xt8-4.h" //在此文件中进行函数的定义#includeusing 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();void show_value(); private:int array[10];int max;};8.5 题//xt8-5-1.cpp(file1.cpp)#include#include "xt8-5.h"int main(){Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0;}8.5 题//xt8-5-2.cpp(arraymax.cpp) #includeusing 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 题#includeusing namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};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<<"</volume()<<endl;}</max<<endl;</sex<<endl;</name<<endl;</num<<endl;</hour<<":"<<minute<<":"<<sec<<endl;}</hour<<":"<<minute<<":"<<sec<<endl;}</t.hour<<":"<<t.minute<<":"<<t.sec<<endl;。
C程序设计谭浩强(第三版) 函数参考答案
C语言程序设计(第三版)谭浩强第8章函数参考答案【知识要点】C语言的基本组成单位函数。
C是由一个且仅有一个主函数( main)和若干个子函数组成.子函数可有可无。
一个C语言源程序执行,从主函数开始,以主函数结束。
C语言称为函数式语言,即用户可根据解决问题的算法编成一个相对独立的函数模块,然后采用调用的方法来使用函数。
采用了函数模块式的结构的特点:程序的层次结构清晰,便于程序的编写、阅读和调试。
C语言提供了极为丰富的库函数,使用这些库函数时,在源程序的头部使用文件包含命令。
如:数学函数#include <math.h>字符函数#include <ctype.h>字符串函数#include <string.h>输入输出函数#include <stdio.h>动态存储分配函数#include <stdlib.h> #include <malloc.h>清屏函数#include <conio.h>日期和时间函数#include <time.h>函数可以嵌套调用,而不可嵌套定义。
函数的值:函数的值的数据类型由函数定义时所决定的。
调用函数时可有返回值和无返回值两种。
有返回值的函数在函数体内必有一条或多条return 语句,无返回值的函数定义为void类型,且在函数体无return 语句。
函数调用时参数的传递:主调函数和被调函数之间数据传递有两种,数值传递和地址传递。
【习题参考答案】8.1写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。
【解析】用碾转相除法求最大公约数。
最小公倍数=两个整数之积/最大公约数。
#include <stdio.h>int gcf(int x,int y) /*求两整数的最大公约数*/{int t,r;if (x<y){t=x;x=y;y=t;}while ((r=x%y)!=0){ x=y;y=r;}return y;}int lcm(int x,int y,int h) /*求两整数的最小公倍数*/{return(x*y/h);}void main ( ){ int a,b,df,sc; /* df:所求的最大公约数,sc:所求的最小公倍数*/printf("Input a,b\n");scanf(“%d%d”,&a,&b);df=gcf(a,b);printf(“G.C.F=%d\n”,df);sc=lcm(a,b,df);printf(“L.C.M=%d\n”,sc);}8.2 求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0,等于0和小于0时的根,并输出结果。
谭浩强C语言程序设计习题答案
谭浩强C语言程序设计习题参考答案第一章1.6main(){int a,b,c,max;printf("input three numbers:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf("max=%d",max);}第二章2.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)162.6aabb (8)cc (8)abc(7)AN2.7main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1+=4, c2+=4, c3+=4, c4+=4, c5+=4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}2.8main(){int c1,c2;c1=97;c2=98;printf("%c %c",c1,c2);}2.9(1)=2.5(2)=3.52.109,11,9,102.12(1)24 (2)10 (3)60 (4)0 (5)0 (6)0第三章3.4main(){int a,b,c;long int u,n;float x,y,z;char c1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf("\n");printf("a=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c'or %d(ASCII)\n",c1,c1);printf("c2='%c'or %d(ASCII)\n",c2,c2);}3.5575 767.856400,-789.12396267.856400,-789.12396267.86 -789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER, COM3.6a=3 b=7/x=8.5 y=71.82/c1=A c2=a/3.710 20Aa1.5 -3.75 +1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车3.8main(){float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("input r,h\n");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2f\n",l);printf("s=%6.2f\n",s);printf("sq=%6.2f\n",sq);printf("vq=%6.2f\n",sv);printf("vz=%6.2f\n",sz);}3.9main(){float c,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2f\n",c);}3.10#include"stdio.h"main(){char c1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("\n");printf("%c%c\n",c1,c2);}第四章4.3(1)0 (2)1 (3)1 (4)0 (5)1 4.4main(){int a,b,c;scanf("%d,%d,%d",&a,&b,&c); if(a<b)if(b<c)printf("max=%d\n",c);elseprintf("max=%d\n",b);else if(a<c)printf("max=%d\n",c);elseprintf("max=%d\n",a);}main(){int a,b,c,temp,max;scanf("%d,%d,%d",&a,&b,&c);temp=(a>b)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}4.5main(){int x,y;scanf("%d",&x);if(x<1)y=x;else if(x<10)y=2*x-1;else y=3*x-11;printf("y=%d",y);}4.6main(){int score,temp,logic;char grade;logic=1;while(logic){scanf("%d",&score);if(score>=0&&score<=100)logic=0; }if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){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("score=%d,grade=%c",score,grade);}4.7main(){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;scanf("%ld",&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;printf("place=%d\n",place);ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;switch(place){case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);break;case 3:printf("%d,%d,%d\n",hundred,ten,indiv);printf("%d,%d,%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d\n",ten,indiv);printf("%d,%d\n",indiv,ten);break;case 1:printf("%d\n",indiv);printf("%d\n",indiv);}}4.8main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);if(i<=1e5)bonus=i*0.1;else if(i<=2e5)bonus=bon1+(i-100000)*0.075; else if(i<=4e5)bonus=bon2+(i-200000)*0.05; else if(i<=6e5)bonus=bon4+(i-400000)*0.03; else if(i<=1e6)bonus=bon6+(i-600000)*0.015; else bonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){long i;float bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf("%ld",&i);branch=i/100000;if(branch>10)branch=10;switch(branch){case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break; case 2:case 3:bonus=bon2+(i-200000)*0.05;break; case 4:case 5:bonus=bon4+(i-400000)*0.03;break; case 6:case 7case 8:case 9:bonus=bon6+(i-600000)*0.015;break; case 10:bonus=bon10+(i-1000000)*0.01;}printf("bonus=%10.2f",bonus);}4.9main(){int t,a,b,c,d;scanf("%d,%d,%d,%d",&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("%d %d %d %d\n",a,b,c,d);}4.10main(){int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1>1&&d2>1&&d3>1&&d4>1)h=0;printf("h=%d",h);}第五章循环控制5.1main(){int a,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1<num2){temp=num1;num1=num2;num2=temp;} a=num1;b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("%d\n",a);printf("%d\n",num1*num2/a);}5.2#include"stdio.h"main(){char c;int letters=0,space=0,digit=0,other=0;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++;else other++;printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other); }5.3main(){int a,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n){tn+=a;sn+=tn;a*=10;++count;}printf("a+aa+aaa+...=%d\n",sn);}5.4main(){float n,s=0,t=1;for(n=1;n<=20;n++){t*=n;s+=t;}printf("s=%e\n",s);}5.5main(){int N1=100,N2=50,N3=10;float k;float s1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2f\n",s1+s2+s3);}5.6main(){int i,j,k,n;for(n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)printf("n=%d\n",n);}5.7#define M 1000main(){int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9; int i,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;switch(n){case 1:k0=i;break;case 2:k1=i;break;case 3:k2=i;break;case 4:k3=i;break;case 5:k4=i;break;case 6:k5=i;break;case 7:k6=i;break;case 8:k7=i;break;case 9:k8=i;break;case 10:k9=i;break;}}}if(s==0){printf("j=%d\n",j);if(n>1)printf("%d,%d",k0,k1);if(n>2)printf(",%d",k2);if(n>3)printf(",%d",k3);if(n>4)printf(",%d",k4);if(n>5)printf(",%d",k5);if(n>6)printf(",%d",k6);if(n>7)printf(",%d",k7);if(n>8)printf(",%d",k8);if(n>9)printf(",%d\n",k9);}}}main(){static int k[10];int i,j,n,s;for(j=2;j<=1000;j++) {n=-1;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("j=%d\n",j);for(i=0;i<n;i++)printf("%d,",k[i]);printf("%d\n",k[n]);}}}5.8main(){int n,t,number=20;float a=2;b=1;s=0;for(n=1;n<=number;n++) {s=s+a/b;t=a,a=a+b,b=t;}printf("s=%9.6f\n",s);}5.9main(){float sn=100.0,hn=sn/2; int n;for(n=2;n<=10;n++) {sn=sn+2*hn;hn=hn/2;}printf("sn=%f\n",sn);printf("hn=%f\n",hn);}5.10main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf("x1=%d\n",x1);}5.11#include"math.h"main(){float a,xn0,xn1;scanf("%f",&a);xn0=a/2;xn1=(xn0+a/xn0)/2;do{xn0=xn1;xn1=(xn0+a/xn0)/2;}while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); }5.12#include"math.h"main(){float x,x0,f,f1;x=1.5;do{x0=x;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);printf("x=%6.2f\n",x);}5.13#include"math.h"main(){float x0,x1,x2,fx0,fx1,fx2;do{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("x0=%6.2f\n",x0);}5.14main(){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");}}5.15main(){char i,j,k;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("\na--%c\tb--%c\tc--%c\n",i,j,k);}}}}第六章数组6.1#include <math.h>#define N 101main(){ int i,j,line,a[N];for (i=2;i<N;i++) a[i]=i; for (i=2;i<sqrt(N);i++)for (j=i+1;j<N;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }printf("\n");for (i=2,line=0;i<N;i++) { if(a[i]!=0){ printf("%5d",a[i]);line++; }if(line==10){ printf("\n");line=0; }}}6.2#define N 10main(){int i,j,min,temp,a[N];for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N-1;i++){min=i;for(j=i+1;j<N;j++)if(a[min]>a[j])min=j;temp=a[i];a[i]=a[min];a[min]=temp;}for(i=0;i<N;i++)printf("%5d",a[i]);}6.3main(){float a[3][3],sum;int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%f",&sum);a[i][j]=sum;}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%f",sum);}6.4main(){int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number, end,i,j;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;}}}for(i=0;i<11;i++)printf("%6d",a[i]);}6.5#define N 5main(){int a[N]={8,6,5,4,1},i,temp;for(i=0;i<N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++)printf("%4d",a[i]);}6.6#define N 11main(){int i,j,a[N][N];for(i=1;i<N;i++){a[i][i]=1;a[i][1]=1;}for(i=3;i<N;i++)for(j=2;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<N;i++){for(j=1;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}}6.7main(){int a[16][16],i,j,k,p,m,n;p=1;while(p==1){scanf("%d",&n);if((n!=0)&&(n<=15)&&(n%2!=0))p=0; }for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;j=n/2+1;a[1][j]=1;for(k=2;k<=n*n;k++){i=i-1;j=j+1;if((i<1)&&(j>n)){i=i+2;j=j-1;}else{if(i<1)i=n;if(j>n)j=1;}if(a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");}}6.8#define N 10#define M 10main(){int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; scanf("%d,%d",&n,&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);flag2=0;for(i=0;i<n;i++){max=a[i][0];for(j=0;j<m;j++)if(max<a[i][j]){max=a[i][j];maxj=j;}for(k=0,flag1=1;k<n&&flag1;k++)if(max>a[k][maxj])flag1=0;if(flag1){ printf("\na[%d][%d]=%d\n",i,maxj,max);flag2=1;}}if(!flag2) printf("NOT");}6.9#include<stdio.h>#define N 15main(){int i,j,number,top,bott,min,loca,a[N],flag;char c;for(i=0;i<=N;i++)scanf("%d",&a[i]);flag=1;while(flag){scanf("%d",&number);loca=0;top=0;bott=N-1;if((number<a[0])||(number>a[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("number=%d,loca=%d\n",number,loca+1);}else if(number<a[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("%d not in table\n",number);printf("continue Y/N or y/n\n");c=getchar();if(c=='N'||c=='n')flag=0;}}6.10main(){int i,j,uppn,lown,dign,span,othn;char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}6.11main(){static char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for(i=0;i<=5;i++){printf("\n");for(j=1;j<=3*i;j++)printf("%1c",space);for(k=0;k<=5;k++)printf("%3c",a[k]);}}6.12#include<stdio.h>main(){int i,n;char ch[80],tran[80];gets(ch);i=0;while(ch[i]!='\0'){if((ch[i]>='A')&&(ch[i]<='Z'))tran[i]=26+64-ch[i]+1+64;else if((ch[i]>='a')&&(ch[i]<='z'))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;}n=i;for(i=0;i<n;i++)putchar(tran[i]);}6.13main(){char s1[80],s2[40];int i=0,j=0;scanf("%s",s1);scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("s=%s\n",s1);}6.14#include<stdio.h>main(){int i,resu;char s1[100],s2[100];gets(s1);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("s1=%s,s2=%s,resu=%d\n",s1,s2,resu); }6.15#include"stdio.h"main(){char from[80],to[80];;int i;scanf("%s",from);for(i=0;i<=strlen(from);i++)to[i]=from[i];printf("%s\n",to);}第七章7.1hcf(u,v)int u,v;{int a,b,t,r;if(u>v){t=u;u=v;v=t;}a=u;b=v;while((r=b%a)!=0){b=a;a=r;}return(a);}lcd(u,v,h)int u,v,h;{return(u*v/h);}main(){int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);}7.2#include"math.h"float x1,x2,disc,p,q;greater_than_zero(a,b)float a,b;{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}equal_to_zero(a,b)flaot a,b;{x1=x2=-b/(2*a);}smaller_than_zero(a,b)float a,b;{p=-b/(2*a);q=sqrt(-disc)/(2*a);}main(){float a,b,c;scanf("%f,%f,%f",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-5){equal_to_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2); }else if(disc>0){greater_than_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q); }}7.3main(){int number;scanf("%d",&number);if(prime(number))printf("yes");elseprintf("no");}int prime(number)int number;{int flag=1,n;for(n=2;n<number/2&&flag==1;n++)if(number%n==0)flag=0;return(flag);}7.4#define N 3int array[N][N];convert(array)int array[3][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;}}main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}7.5main(){char str[100];scanf("%s",str);inverse(str);printf("%s\n",str);}inverse(str)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.6char concate(str1,str2,str)char str1[],str2[],str[];{int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}7.7main(){char str[80],c[80];void cpy();gets(str);cpy(str,c);printf("\n%s\n",c);}void cpy(s,c)char s[],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.8main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}7.9int alph,digit,space,others;main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others); }count(str)char str[];{int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;elseothers++;}7.10int alph(c)char c;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) return(1);elsereturn(0);}int longest(string)char string[];{int len=0,i,length=0,flag=1,place,point; for(i=0;i<=strlen(string);i++)if(alph(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>length){length=len;place=point;len=0;}}return(place);}main(){int i;char line[100];gets(line);for(i=longest(line);alph(line[i]);i++) printf("%c",line[i]);printf("\n");}7.11#define N 10char str[N];{int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");elseflag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);}sort(str)char str[N];{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<math.h>float solut(a,b,c,d)float a,b,c,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-5);return(x);}main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d); printf("x=%10.7f\n",solut(a,b,c,d)); }main(){int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(tn,tx)int tn,tx;{if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn); }7.14#define N 10#define M 5float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("\n number class 1 2 3 4 5 avr");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f",a_stu[i]);}printf("\nclassavr");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n\n%8.2f %d %d\n",h,r,c);printf("\n %8.2f\n",s_diff());input_stu(){int i,j;float x;for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N; }}float highest(r,c)int *r,*c;{float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;*c=j+1;}return(high);}float s_diff()float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }7.15#include<stdio.h>#define N 10void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){scanf("%d",&num[i]);gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]); }void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;bott=N-1;if((n<num[0])||(n>num[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continue?Y/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}7.16#include<stdio.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;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("\nnumber=%d\n",htoi(t));printf("continue?");c=getchar();if(c=='n'||c=='N')flag1=0;else{flag=0;i=0;}}}}htoi(s)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<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}7.18main(){int year,month,day;int days;scanf("\n%d,%d,%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3))days+=1;printf("days=%d\n",days);}static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} int sum_day(month,day)int month,day;{int i;for(i=1;i<month;i++)day+=day_tab[i];return(day);}int leap(year)int year;{int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}第八章编译预处理8.1#define SW AP(a,b) t=b;b=a;a=tmain(){int a,b,t;scanf("%d,%d",&a,&b);SWAP(a,b);printf("a=%d\tb=%d\n",a,b);}8.2#define SURPLUS(a,b) ((a)%(b))main(){int a,b;scanf("%d,%d",&a,&b);printf("%d",SURPLUS(a,b));}。
谭浩强c语言程序设计第二版答案
谭浩强c语言程序设计第二版答案谭浩强教授的《C语言程序设计》第二版是一本广受好评的C语言教材,它不仅详细介绍了C语言的基础知识,还通过大量的实例和习题来帮助读者加深理解。
以下是对该书中一些习题的答案解析,但请注意,这些答案仅供参考,实际编程时还需要根据具体问题进行调整。
第一章:C语言概述1. 问题1:为什么C语言既具有高级语言的特点,又具有低级语言的特点?- 答案:C语言具有高级语言的特点,因为它提供了丰富的数据类型、结构化编程和抽象能力。
同时,它又具有低级语言的特点,因为它允许直接访问内存地址,进行位操作,并且具有高效的运行速度。
第二章:数据类型、运算符与表达式1. 问题1:如何理解C语言中的变量?- 答案:变量是程序中存储数据的容器,它们具有特定的数据类型,用来存储整型、浮点型、字符型等数据。
变量在使用前需要声明,声明时需要指定变量的类型和名称。
2. 问题2:C语言中的运算符有哪些?- 答案:C语言中的运算符包括算术运算符(如+、-、*、/、%)、关系运算符(如<、>、==、!=)、逻辑运算符(如&&、||、!)、位运算符(如&、|、^、~、<<、>>)等。
第三章:控制结构1. 问题1:请解释if-else语句的工作原理。
- 答案:if-else语句是一种条件控制结构,它根据条件表达式的真假来决定执行哪一段代码。
如果条件表达式为真,则执行if语句块中的代码;如果为假,则执行else语句块中的代码。
2. 问题2:while循环和do-while循环有什么区别?- 答案:while循环先判断条件再执行循环体,如果条件为假,则不执行循环体。
do-while循环则先执行一次循环体,然后再判断条件,即使条件为假,循环体也会至少执行一次。
第四章:函数1. 问题1:函数的作用是什么?- 答案:函数是一段具有特定功能的代码块,可以被重复调用。
函数的使用可以提高代码的复用性,降低程序的复杂度,并且使程序结构更加清晰。
第四版谭浩强c语言课后答案_完整版
}
#include <stdio.h> #include <math.h> int main() {float d=300000,p=6000,r=0.01,m; m=log10(p/(p-d*r))/log10(1+r); printf("m=%6.2f\n",m); return 0; }
#include <stdio.h> 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; }
printf("%c,%c\n",c1,c2); printf("按 ASCII 码输出结果为:\n"); printf("%d,%d\n",c1,c2); return 0; }#include <stdio.h> int main() { char c1,c2; int i1,i2; printf("请输入两个字符 c1,c2:"); scanf("%c,%c",&c1,&c2); i1=c1; i2=c2; printf("按字符输出结果:\n"); printf("%c,%c\n",i1,i2); printf("按整数输出结果:\n"); printf("%d,%d\n",c1,c2); return 0; }#include <stdio.h> int main() { char c1,c2; //定义为字符型 //赋值给整型变量 //定义字符型变量 //定义整型变量
C程序设计习题答案(7-9章)谭浩强著
{ printf("\na[%d][%d]=%d\n",i,maxj,max);
flag2=1;
}
}
if(!flag2) printf("NOT");
for(k=2;k<=n*n;k++)
{i=i-1;
j=j+1;
if((i<1)&&(j>n))
{i=i+2;
j=j-1;
}
else
uppn++;
else if(text[i][j]>='a'&&text[i][j]<='z')
printf("\n");
}
}
7.7
main()
{int a[16][16],i,j,k,p,m,n;
p=1;
while(p==1)
{scanf("%d",&n);
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
c=getchar();
if(c=='N'||c=='n')flag=0;
}
}
7.10
main()
{int i,j,uppn,lown,dign,span,othn;
char text[3][80];
for(i=3;i<N;i++)
谭浩强C语言程序设计习题集
目录重要次重要重复或欲删除基础第一章C语言概述1.1 选择题1.2 填空题第二章数据类型、运算符与表达式2.1 选择题2.2 填空题第三章最简单的C程序设计3.1 选择题3.2 填空题第四章逻辑运算和判断选取控制4.1 选择题4.2 填空题4.3 编程题第五章循环控制5.1 选择题5.2 填空题5.3 编程题第六章数组6.1 选择题6.2 填空题6.3 编程题第七章函数7.1 选择题7.2 填空题7.3 编程题第八章编译预处理8.1 选择题8.2 填空题8.3 编程题第九章指针9.1 选择题9.2 填空题第一章C语言概述1.1 选择题*1.1一个C程序的执行是从。
A)本程序的main函数开始,到main函数结束B)本程序文件的第一个函数开始,到本程序文件的最后一个函数结束C)本程序的main函数开始,到本程序文件的最后一个函数结束D)本程序文件的第一个函数开始,到本程序main函数结束参考答案:A参考分析:C语言总是从main函数开始,main函数结束。
但是C语言中存在一个exit(0)函数,它可以使得程序在任何时候、任何位置结束程序的运行。
如果不考虑exit(0)等函数的特殊作用,C则总是在main函数结束。
*1.2以下叙述正确的是。
A)在C程序中,main函数必须位于程序的最前面B)在C程序的每一行只能写一条语句C)C语言本身没有输入输出语句D)在对一个C程序进行编译的过程中,可发现注释中的拼写错误参考答案:C参考分析:C程序对main函数的位置没有任何要求;其书写格式自由,一行可以写多条语句,一条语句(多关键字语句)可以写在多行;C语言忽略注释,把注释看作是一个空格,不会对注释中的内容进行语法检查。
因此,如果注释中存在错误,系统是不可能发现的。
另外,C语言的I/O操作均通过函数实现,系统本身未提供相应的语句。
1.3以下叙述不正确的是。
A)一个C源程序可由一个或多个函数组成B)一个C源程序必须包含一个main函数C)C程序的基本组成单位是函数D)在C程序中,注释说明只能位于一条语句的后面参考答案:D参考分析:C语言中,注释语句的位置是任意的,当然,它不能破坏标识符的完整性。
2024年度谭浩强《C程序设计》第八章习题解析
选择排序的时间复杂度也为O(n^2)。
47
2024/2/3
插入第一个元素
将数组的第一个元素视为已排序序列,从第二个元素开始遍历数组。
插入当前元素
将当前元素插入到已排序序列中的正确位置,即找到已排序序列中第一个大于当前元素的元素,并将当前元素插入到该元素之前。
重复插入操作
重复上述插入操作,直到整个数组都插入到已排序序列中。
给定一个3x3的矩阵A和一个整数k,求矩阵A的k次幂。解析:可以使用递归或迭代的方式来实现矩阵的幂运算。递归方式较为简洁但效率较低,迭代方式需要手动实现矩阵乘法和结果矩阵的初始化等操作,但效率较高。在实现时需要注意矩阵乘法的规则和结果矩阵的大小。
给定一个n阶方阵A,求其转置矩阵B。解析:首先声明两个n阶方阵A和B,然后通过嵌套循环遍历A中的每个元素,并将元素值交换后赋值给B中对应位置的元素即可。在实现时需要注意方阵的阶数n需要通过参数传递或动态分配内存来确定。
22
2024/2/3
max = a[i];
23
2024/2/3
}
if (a[i] < min) {
24
2024/2/3
min = a[i];
25
2024/2/3
1
2
3
}
}
printf("数组中的最大值为:%dn", max);
26
2024/2/3
printf("数组中的最小值为:%d", min);
38
2024/2/3
分析
遍历字符串中的每个字符,根据字符类型进行计数。
分析
分别遍历两个字符串的首尾字符,比较是否相等,若全部相等则为逆序串。
谭浩强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语言完整详细答案(第8章)
谭浩强C语言答案8.1//最大公约数与最小公倍数#include<stdio.h>int gys(int a,int b){int m,n,t,r;if(a<b){t=a;a=b;b=t;}n=a;m=b;while((r=m%n)!=0){m=a;n=r;}return(n);}int gbs(int a,int b){int k;k=a*b/gys(a,b);return k;}void main(){int a,b;printf("请输入两个整数:\n");scanf("%d%d",&a,&b);printf("最大公约数是:%d\n",gys(a,b));printf("最小公倍数是:%d\n",gbs(a,b));}8.2//一元二次方程根的情况#include<stdio.h>#include<math.h>void gen(float a,float b,float c){float k1,k2;if(b*b-4*a*c<0)printf("该函数没有实数根!\n");else if(b*b-4*a*c==0){k1=-b+sqrt(b*b-4*a*c);printf("该函数有两个相同的根为:%3.2f",k1/2);printf("\n");}else{k1=(-b+sqrt(b*b-4*a*c))/(2*a);k2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("该函数有两个不同的根为:%3.2f,%3.2f",k1,k2);printf("\n");}}void main(){float a,b,c;printf("请输入三个实数:\n");scanf("%f%f%f",&a,&b,&c);gen(a,b,c);}8.3//是否为素数#include<stdio.h>#include<math.h>void sushu(int a){int i;for(i=2;i<=a-1;i++)if(a%i==0) {printf("%d不是素数",a);break;}else {printf("%d是素数",a);break;}}void main(){int a;printf("请输入一个实数:\n");scanf("%d",&a);sushu(a);}8.4//矩阵转置#include<stdio.h>#include<math.h>#define N 3int array[N][N];convert(array)int array[3][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;}}void main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5//字符串反向输出#include<stdio.h>//#include<string.h>#include<math.h>void string(char a[]){int i,k;char b[10];for(i=0;a[i]!=0;i++);k=i;for(i=0;i<k;i++)b[i]=a[k-i-1];b[i]=0;//结束字符串标志printf("%s",b);}void main(){char a[10];printf("请输入一个字符串:\n");scanf("%s",a);printf("倒序后的字符串为:\n");string(a);}8.6//连接两个字符串#include<stdio.h>#include<string.h>#include<math.h>char concate(char str1[],char str2[],char str[]) {int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7//找出字符串中的元音字母#include<stdio.h>#include<string.h>#include<math.h>char find(char str1[],char str2[]){int i,j=0;for(i=0;str1[i]!=0;i++){if(str1[i]=='a'||str1[i]=='A'||str1[i]=='e'||str1[i]=='E'||str1[i]=='i'||str1[i]=='I'||str1[i]=='o'||str1[i]=='O'||str1[i]=='u'||str1[i]=='U')str2[j++]=str1[i];else continue;}str2[j]='\0';printf("%s中元音字母是%s\n",str1,str2);}main(){char str1[100],str2[100];scanf("%s",str1);find(str1,str2);}8.8//输入数字1990输出1 9 9 0#include<stdio.h>#include<string.h>void main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9//统计字母,数字,空格,和其他字符的个数#include<stdio.h>#include<string.h>int alph,digit,space,others;void main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others);}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'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;else others++;}8.10//输出最长单词#include<stdio.h>#include<string.h>int zimu(char c){if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(char string[]){int i,point,point1,num=0,nummax=0,flag=1;for(i=0;i<=strlen(string);i++)if(zimu(string[i]))//是字母则记下起始位置和长度if(flag){point=i;flag=0;}elsenum++;else //不是字母则比较长度重新记下初始位置{flag=1;if(num>nummax){nummax=num;point1=point;}}return point1;}void main(){int i;char line[100];gets(line);for(i=longest(line);zimu(line[i]);i++)printf("%c",line[i]);printf("\n");}8.11//冒泡法字符串排序#include<stdio.h>#include<string.h>#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");else flag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);sort(char str[N]){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;}}8.12//找根的值,一元三次方程牛顿迭代公式法#include<stdio.h>#include<string.h>#include<math.h>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-5);return(x);}void main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13//递归法#include<stdio.h>#include<string.h>#include<math.h>void main()int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(int tn,int tx){if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn);}8.14//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差#include<stdio.h>#include<string.h>#include<math.h>#define N 4#define M 4float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();input_stu();avr_stu();avr_cor();printf("\n人数科目 1 2 3 4 平均分");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);//每个学生的各门成绩printf("%8.2f",a_stu[i]); //每个学生的平均成绩}printf("\n课程平均成绩");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n最高分为%8.2f在第%d行第%d列\n",h,r,c);printf("\n平均分方差为%8.2f\n",s_diff());}/*输入是个学生的成绩*/input_stu(){int i,j;float x;for(i=0;i<N;i++){printf("请输入第%d个学生的成绩:\n",i+1);for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}/*输出每个学生的平均成绩*/avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}/*输出每门课的平均成绩*/avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}/*求最高分和它属于哪个学生,哪门课*/float highest(int *r,int *c){float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;//用指针实现双向传递很经典,也可该用全局变量*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N));}8.15//输入10个职工的姓名和职工号按职工号由小到大排列,姓名顺序也随之调整//输入一个职工号用折半法找出该职工的姓名#include<stdio.h>#include<string.h>#include<math.h>#define N 3void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){printf("\ninput No.");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]); }void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1])) loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min]) bott=min-1;else top=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continueY/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16//输入16进制输出10进制#include<stdio.h>#include<string.h>#include<math.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;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("\nnumber=%d\n",htoi(t));printf("continue");c=getchar();if(c=='n'||c=='N')flag1=0;else {flag=0; i=0; }}}}htoi(s)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);}8.17//递归法#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}8.18//年月日具体是多少天#include<stdio.h>#include<string.h>#include<math.h>void main(){int year,month,day;int days;int sum_day();int leap();scanf("\n%d%d%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3)){days+=1;printf("%d年共有366天该天是其中的%d天",days);}elseprintf("%d年共有365天该天是其中的%d天",days); }static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; /*总天数*/int sum_day(int month,int day){int i;for(i=1;i<month;i++)day+=day_tab[i];return(day);}/*是否为闰年*/int leap(int year){int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}。
c程序设计第四版(谭浩强)第八章答案
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>/*int main(){int *p1,*p2,*p3,*t,a,b,c;printf("enter three number:");scanf("%d%d%d",&a,&b,&c);p1=&a;p2=&b;p3=&c;if(a>b){t=p1;p1=p2;p2=t;}if(a>c){t=p1;p1=p3;p3=t;}if(b>c){t=p2;p2=p3;p3=t;}printf("sort number:%d %d %d\n",*p1,*p2,*p3); }*//*#define N20int main(){char *p1,*p2,*p3,*t,a[N],b[N],c[N];printf("enter three strings:");scanf("%s%s%s",a,b,c);p1=a;p2=b;p3=c;if(strcmp(a,b)>0){t=p1;p1=p2;p2=t;}if(strcmp(a,c)>0){t=p1;p1=p3;p3=t;}if(strcmp(b,c)>0){t=p2;p2=p3;p3=t;}printf("sort string:%s%s%s\n",p1,p2,p3);}*//*void input(int*p){int i;printf("enter10number:");for(i=0;i<10;i++)scanf("%d",p+i);void output(int*p){int i;printf("new10number:");for(i=0;i<10;i++)printf("%-2d",*(p+i));}void sort(int*p,int n){int i,t;for(i=0;i<n-1;i++){if(*(p+i)>*(p+i+1)){t=*(p+i);*(p+i)=*(p+i+1);*(p+i+1)=t;} }for(i=n-2;i>0;i--){if(*(p+i-1)>*(p+i)){t=*(p+i);*(p+i)=*(p+i-1);*(p+i-1)=t;} }}int main(){int a[10],*p=a;input(p);sort(p,10);output(p);putchar('\n');}*//*#define N10void houyi(int*p,int m){int i,b[N]={0};for(i=0;i<N-m;i++)b[i+m]=*(p+i);for(;i<N;i++)b[i-N+m]=*(p+i);for(i=0;i<N;i++)printf("%3d",b[i]);}int main()int a[N],i,m,*p=a;printf("enter number:");for(i=0;i<N;i++)scanf("%d",&a[i]);printf("enter m:");scanf("%d",&m);printf("new number:");houyi(p,m);putchar('\n');}*//*int main(){int count(int*p,int n);int n,*p,i;printf("enter number:");scanf("%d",&n);p=(int *)calloc(n,sizeof(int));for(i=0;i<n;i++)*(p+i)=0;printf("the last number is %d\n",count(p,n)+1);free(p);}int count(int*p,int n){int i,j=1,k=0;for(i=0;i<n;i++){if(*(p+i)!=3){*(p+i)=j;if(*(p+i)==3)j=1; //数到3 时将下一个重置为1elsej+=1;if(i==(n-1))i=-1; //轮到到最后一个人报数时,将i 重置为-1,执行完for 语句后自动加一变为0,从头开始k+=1; //报完一个数后加一}else{if(i==(n-1))i=-1; //检查到最后一个人是3时,将i 重置为-1,执行完for 语句后自动加一变为0,从头开始continue; //检查到这个人是3 时,跳过报数}if(k==3*(n-1)) //报数的总个数达到3*(n-1)时,就只剩一个人了break;elsecontinue;}for(i=0;i<n;i++)if(*(p+i)==3)continue;else return i;}*//*#define N20int length(char*p){int n=0;while(*p!=0){p++;n+=1;}return n;}int main(){char a[N],*p=a;printf("enter string:");gets(a);printf("字符串长度为:%d\n",length(p));}*//*#define N10int main(){void copyn(char*p,int m);char *p="abcdefghij";int m;printf("%s\nenter m:",p);scanf("%d",&m);copyn(p,m);}void copyn(char*p,int m){char b[N],i;for(i=m-1;*(p+i);i++)b[i-m+1]=*(p+i);b[i-m+1]='\0';printf("%s\n",b);}*//*#define N50void count(char*p){int d=0,x=0,k=0,s=0,o=0;while(*p) //不能写*(p++),否则计算出错,无法计算第一个字符{if(*p>='a'&&*p<='z')x+=1;else if(*p>='A'&&*p<='Z')d+=1;else if(*p>='0'&&*p<='9')s+=1;else if(*p==' ')k+=1;else o+=1;p++;}printf("大写字母:%d\n小写字母:%d\n空格:%d\n数字:%d\n 其他:%d\n",d,x,k,s,o);}int main(){char a[N],*p=a;printf("enter string:");gets(a);count(p);}*//*void zhuangzhi(int(*p)[3]){int i,j,t;for(i=0;i<3;i++)for(j=0;j<3;j++)if(i<j){t=*(*(p+i)+j); //*(*(p+i)+j)代表a[i][j]*(*(p+i)+j)=*(*(p+j)+i);*(*(p+j)+i)=t;}}int main(){int (*p)[3],a[3][3],i,j;p=a;printf("enter array:\n");for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);zhuangzhi(p);printf("new array:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){printf("%-2d",a[i][j]);if(j==2)putchar('\n');}}*//*void central(int (*p)[5]){int i,j,l=0,r=0,t;//r 代表行,l 代表列for(i=0;i<5;i++)for(j=0;j<5;j++)if(*(*(p+i)+j)>*(*(p+r)+l)){r=i;l=j;}t=*(*(p+2)+2);*(*(p+2)+2)=*(*(p+r)+l);*(*(p+r)+l)=t; //最大元素放在中心l=0;r=0;for(i=0;i<5;i++)//四个角放最小元素,每次从第二个元素开始对比for(j=1;j<5;j++)if(*(*(p+i)+j)<*(*(p+r)+l)){r=i;l=j;//找出最小元素}t=*(*p);*(*p)=*(*(p+r)+l);*(*(p+r)+l)=t; //将最小元素与左上角元素交换,下同l=4;r=0;//换完重置r 和l,下同for(i=0;i<5;i++)for(j=1;j<5;j++)if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*p+4);*(*p+4)=*(*(p+r)+l);*(*(p+r)+l)=t;r=4;l=0;for(i=0;i<5;i++)for(j=1;j<5;j++)if(i==0&&j==4) //遇到右上角,跳过对比continue;else if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*(p+4));*(*(p+4))=*(*(p+r)+l);*(*(p+r)+l)=t;r=4;l=4;for(i=0;i<5;i++)for(j=1;j<5;j++)if(i==0&&j==4||i==4&&j==0) //遇到右上角和左下角,跳过对比continue;else if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*(p+4)+4);*(*(p+4)+4)=*(*(p+r)+l);*(*(p+r)+l)=t;}int main(){int a[5][5],(*p)[5]=a,i,j;printf("enter array:\n");for(i=0;i<5;i++)for(j=0;j<5;j++)scanf("%d",*(p+i)+j);central(p);printf("new array:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%-3d",*(*(p+i)+j));putchar('\n');}}*//*void sort(char*p[],int n) //指针数组处理{int i,j;char *k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(p[i-1],p[j])>0){k=p[i-1];p[i-1]=p[j];p[j]=k;}}int main(){char*p[10]={"gutsy","glory","gonad","girth","gaudy","gypsy","gusto","guppy","group","grope"}; //测试数据gutsy glory gonad girth gaudy gypsy gusto guppy group gropeint i;printf("enter strings:");for(i=0;i<10;i++)printf("%s ",p[i]);sort(p,10);printf("\nnew strings:");for(i=0;i<10;i++)printf("%s ",p[i]);putchar('\n');}*//*#define N20void sort(char*p,int n) //指针处理{int i,j;char k[N];for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(p+20*(i-1),p+20*j)>0) //不能写为p+i-1,否则出错{strcpy(k,p+20*(i-1));strcpy(p+20*(i-1),p+20*j);strcpy(p+20*j,k);}}int main(){char a[10][N];char *p=a[0]; //不能写为*p=a,这等价于是把行向量赋值给列向量int i;printf("enter strings:\n");for(i=0;i<10;i++)scanf("%s",&a[i]); //可写为gets(a[i]),这样写的话,每次输入字符串后按enter,或写为scanf("%s",&a[i]),每输一个字符后按空格即可;sort(p,10);printf("new strings:\n");for(i=0;i<10;i++)printf("%s ",a[i]);putchar('\n');}*//*#define n 1000 //矩形法求定积分,n 为区间分割数目double ding(double a,double b,double(*fun)(double)){double s=0;int i;for(i=1;i<n;i++)s+=(*fun)(i*(b-a)/n);return s*=(b-a)/n;}int main(){double a,b,s1,s2,s3;//定义积分下限,上限,用double 精度较高printf("输入积分下限,上限,分割区间数目:");scanf("%lf%lf",&a,&b);s1=ding(a,b,sin);s2=ding(a,b,cos);s3=ding(a,b,exp);printf("sin(x)从%f 到%f 的积分为%f\n",a,b,s1);printf("cos(x)从%f 到%f 的积分为%f\n",a,b,s2);printf("exp(x)从%f 到%f 的积分为%f\n",a,b,s3);}*//*#define N10void nixu(int*p){int i,m=N/2,t;for(i=0;i<m;i++){t=*(p+i);*(p+i)=*(p+N-1-i);*(p+N-1-i)=t;}}int main(){int a[N],*p=a,i;printf("enter %d numbers:\n",N);for(i=0;i<N;i++)scanf("%d",&a[i]);nixu(p);printf("new numbers:\n");for(i=0;i<N;i++)printf("%d ",a[i]);putchar('\n');}*//*void average1(float(*p)[6],int n){int i;float ave=0;for(i=0;i<n;i++)ave+=*(*(p+i)+1)/4;printf("第一门课程的平均分为:%4.2f\n",ave);}float average(float(*p)[6]){int i;float ave=0;for(i=1;i<6;i++)ave+=*(*p+i)/5;return ave;}void out(float(*p)[6],int n){int i,j,k,l,s=0;printf("两门课以上不及格的学生:\n");for(i=0;i<n;i++){k=0;for(j=1;j<6;j++){if(*(*(p+i)+j)<60)k+=1;else continue;}if(k>=2){s=1;printf("%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));printf(" 平均分为:%4.2f\n",average(p+i));}}if(s==0) //没有符合条件的学生时输出“无”printf("无!\n");}void in(float(*p)[6],int n){int i,j,l;for(i=0;i<n;i++){for(j=0;j<6;j++){if(average(p+i)>=90){printf("平均分在90 分以上的学生:%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));putchar('\n');break;}if(*(*(p+i)+j)>=85)continue;elsebreak;}if(j==6){printf("全部课程成绩在85 分以上的学生:%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));putchar('\n');}}}int main(){int i,j;floata[4][6]={{1001,65,67,55,60,45},{1002,80,87,90,81,85},{1003,90,99,100,98,95},{1004,86,89,90,91, 85}};float (*p)[6]=a;for(i=0;i<4;i++){for(j=0;j<6;j++)printf("%4.0f ",a[i][j]);putchar('\n');}average1(p,4);out(p,4);in(p,4);}*//*#define N10 //方法一int num1(char*p) //将数字字符串转为整形数字的函数{int n=strlen(p),i,a=0;for(i=0;i<n;i++)a+=(int)((*(p+i)-48)*pow(10,n-1-i));return a;}void number(char*p){int i,num=0,j=1,k=-1,n=strlen(p); //j 用于计算数字长度,k 用于计算数字个数int a[N];//最多容纳N 个整数for(i=0;i<=n;i++){if(*p<'0'||*p>'9'){if(num==1){*p='\0';a[k]=num1(p-j);}num=0;}else if(num==0){num=1;j=1; //找到新数字时重置jk+=1;}elsej+=1;p++;}for(i=0;i<k+1;i++)printf("%d ",a[i]);putchar('\n');}*//*#define N10void number(char*p) //方法二{int i,num=0,j=1,k=-1,n=strlen(p); //j 用于计算数字长度,k 用于计算数字个数char a[N][10];//最多容纳N 个整数,每个整数最多十位数for(i=0;i<=n;i++){if(*p<'0'||*p>'9'){if(num==1){*p='\0';strcpy(a[k],p-j);}num=0;}else if(num==0){num=1;j=1; //找到新数字时重置jk+=1;}elsej+=1;p++;}for(i=0;i<k+1;i++)printf("%s ",a[i]);putchar('\n');}int main(){char a[100],*p=a; //最多可输入100个字符printf("enter strings:\n");gets(p);number(p);}*//*//字符串比较函数#define N20int str_cmp(char*p1,char*p2){int i,n=strlen(p1);for(i=0;i<=n;i++)if(*(p1+i)==*(p2+i))continue;else if(*(p1+i)>*(p2+i)){return*(p1+i)-*(p2+i);break;}else{return*(p1+i)-*(p2+i);break;}if(i==n+1)return0;}int main(){char a[N],b[N],*p1=a,*p2=b;printf("enter string1:");gets(p1);printf("enter string2:");gets(p2);printf("%d\n",str_cmp(p1,p2));}*//*void month(int*p){char*a[12]={"January","February","March","April","May","June","July","August","September","Octob er","Novenber","December"};int i;for(i=0;i<12;i++)if(*p==i+1)printf("英文:%s\n",a[i]);else continue;}int main(){int a,*p=&a;printf("enter month:");scanf("%d",p);month(p);}*//*#define SIZE1000char newbuf[SIZE]; //定义字符数组newbufchar *newp=newbuf;//定义指针变量newp,指向可存区的始端char *mynew(int n) //定义开辟存区的函数new,开辟存储区后返回指针{if(newp+n<=newbuf+SIZE) // 开辟区未超过newbuf数组的大小{newp+=n; // newp 指向存储区的末尾return(newp-n);} // 返回一个指针,它指向存区的开始位置elsereturn(NULL); // 当存区不够分配时,返回一个空指针}void free(char*p){if(p>=newbuf&&p<newbuf+SIZE)newp=p; // newp 指向存储区的开始位置}int main(){char *p=mynew(6);strcpy(p,"hello");printf("%s\n",p);free(p);}*//*#define N10 //定义每个字符串的最大字符数void sort(char*a[],int n){char **p=a;int i,j;char *k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(*(p+i-1),*(p+j))>0){k=*(p+i-1);*(p+i-1)=*(p+j);*(p+j)=k;}}int main(){char a[5][N];char *aa[5];int i;printf("enter strings:\n",);for(i=0;i<5;i++){scanf("%s",&a[i]);aa[i]=*(a+i);}sort(aa,5);printf("new strings:\n");for(i=0;i<5;i++)printf("%s ",aa[i]); //因为sort 函数改变的是aa[5]的排序,a[5][10]顺序并未改变,故输出的是aa[i],若写为a[i],输出顺序不变putchar('\n');}*/#define N5 //定义排序整数的个数void sort(int*a[],int n){int **p=a;int i,j,k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(**(p+i-1)>**(p+j)){k=**(p+i-1);**(p+i-1)=**(p+j);**(p+j)=k;}}int main(){int a[N];int *aa[N];int i;printf("enter numbers:\n");for(i=0;i<N;i++){scanf("%d",&a[i]);aa[i]=a+i;}sort(aa,N);printf("new number:\n");for(i=0;i<N;i++)printf("%d ",a[i]); //因为sort 函数改变的是aa[N]的排序,也是a[N]的排序, 故输出的是a[i],也可写为*aa[i]putchar('\n');}。
c语言程序设计第五版课后答案谭浩强第八章课后答案
c语⾔程序设计第五版课后答案谭浩强第⼋章课后答案c语⾔程序设计第五版课后答案谭浩强习题答案第⼋章善于利⽤指针本章习题均要求使⽤指针⽅法处理。
1. 输⼊3个整数,要求按由⼩到⼤的顺序输出。
解题思路:先获取到三个变量的地址,然后获取三个数据,通过指针进⾏⽐较转换即可答案:#include <stdio.h>void swap(int *p_a, int *p_b){int temp = *p_a;*p_a = *p_b;*p_b = temp;}int main(){int a, b, c, *p_a = &a, *p_b = &b, *p_c = &c; // 获取每个变量空间的地址printf("Please enter three numbers:");scanf_s("%d%d%d", p_a, p_b, p_c);if (*p_a > *p_b) {swap(p_a, p_b);//通过指针进⾏指向空间内的数据交换}if (*p_a > *p_c) {swap(p_a, p_c);}if (*p_b > *p_c) {swap(p_b, p_c);}printf("%d %d %d\n", *p_a, *p_b, *p_c);system("pause");return 0;}2. 输⼊3个字符串,要求按由⼩到⼤的顺序输出。
解题思路:字符串的⽐较可以使⽤strcmp函数,返回值>0表⽰⼤于,返回值⼩于0表⽰⼩于,返回追等于0表⽰相同。
其他的⽐较排序思路与数字的排序交换没有区别,逐个进⾏⽐较先找出最⼤的,然后找出第⼆⼤的。
答案:#include <stdio.h>int main(){char str[3][32];char *p[3];printf("Please enter three strings:");for (int i = 0; i < 3; i++) {p[i] = str[i];scanf_s("%s", p[i], 32);//后边的数字限制缓冲区边界,防⽌缓冲区溢出访问越界}//让p[0]和p[1]/p[2]分别进⾏⽐较,找出最⼤的字符串,i+1之后,则让p[1]和p[2]进⾏⽐较,找出第⼆⼤//i循环总个数-1次,最后⼀个是不需要⽐较的for (int i = 0; i < 2; i++) {for (int j = i + 1; j < 3; j++) {if (strcmp(p[i], p[j]) > 0) {char *tmp = p[i]; p[i] = p[j]; p[j] = tmp;}}}printf("%s %s %s\n", p[0], p[1], p[2]);system("pause");return 0;}3. 输⼊10个整数,将其中最⼩的数与第⼀个数对换, 把最⼤的数与最后⼀个数对换。
C语言程序设计_第三版_谭浩强主编_课后习题答案_完整版6-8章
}
上面的程序是从最后一天计算,一直到第一天。当while第一次循环,既day=9的时候,X1此时计算的是第9天的桃子数。而X2既是X1前一天的桃子数。计算出第9天的桃子数后,把X1的值赋值给X2,那么X2此时X2就表示第9天的桃子数,再通过while的循环,可以逐步求出第7天,第6天...一直到第一天的桃子数. 这里X2=X1,就是一个赋值的过程,为的是求前一天的桃子数.
return 0;
}
6.10猴子吃桃问题
#include <stdio.h>
int main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2;
x2=x1;
day--;
}
printf("桃子总数=%d\n",x1);
{
case 1: k0=i; break;
case 2: k1=i; break;
case 3: k2=i; break;
case 4: k3=i; break;
case 5: k4=i; break;
case 6: k5=i; break;
case 7: k6=i; break;
return 0;
}
6.3求s(n)=a+aa+aaa+…+aa…a之值,其中a是一个数字,n表示a的位数。
解:
int main()
{
int a,n,count=1,sn=0,tn=0;
printf("请输入a和n的值:\n");
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
谭浩强C语言答案8.1//最大公约数与最小公倍数#include<stdio.h>int gys(int a,int b){int m,n,t,r;if(a<b){t=a;a=b;b=t;}n=a;m=b;while((r=m%n)!=0){m=a;n=r;}return(n);}int gbs(int a,int b){int k;k=a*b/gys(a,b);return k;}void main(){int a,b;printf("请输入两个整数:\n");scanf("%d%d",&a,&b);printf("最大公约数是:%d\n",gys(a,b));printf("最小公倍数是:%d\n",gbs(a,b));}8.2//一元二次方程根的情况#include<stdio.h>#include<math.h>void gen(float a,float b,float c){float k1,k2;if(b*b-4*a*c<0)printf("该函数没有实数根!\n");else if(b*b-4*a*c==0){k1=-b+sqrt(b*b-4*a*c);printf("该函数有两个相同的根为:%3.2f",k1/2);printf("\n");}else{k1=(-b+sqrt(b*b-4*a*c))/(2*a);k2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("该函数有两个不同的根为:%3.2f,%3.2f",k1,k2);printf("\n");}}void main(){float a,b,c;printf("请输入三个实数:\n");scanf("%f%f%f",&a,&b,&c);gen(a,b,c);}8.3//是否为素数#include<stdio.h>#include<math.h>void sushu(int a){int i;for(i=2;i<=a-1;i++)if(a%i==0) {printf("%d不是素数",a);break;}else {printf("%d是素数",a);break;}}void main(){int a;printf("请输入一个实数:\n");scanf("%d",&a);sushu(a);}8.4//矩阵转置#include<stdio.h>#include<math.h>#define N 3int array[N][N];convert(array)int array[3][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;}}void main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5//字符串反向输出#include<stdio.h>//#include<string.h>#include<math.h>void string(char a[]){int i,k;char b[10];for(i=0;a[i]!=0;i++);k=i;for(i=0;i<k;i++)b[i]=a[k-i-1];b[i]=0;//结束字符串标志printf("%s",b);}void main(){char a[10];printf("请输入一个字符串:\n");scanf("%s",a);printf("倒序后的字符串为:\n");string(a);}8.6//连接两个字符串#include<stdio.h>#include<string.h>#include<math.h>char concate(char str1[],char str2[],char str[]) {int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7//找出字符串中的元音字母#include<stdio.h>#include<string.h>#include<math.h>char find(char str1[],char str2[]){int i,j=0;for(i=0;str1[i]!=0;i++){if(str1[i]=='a'||str1[i]=='A'||str1[i]=='e'||str1[i]=='E'||str1[i]=='i'||str1[i]=='I'||str1[i]=='o'||str1[i]=='O'||str1[i]=='u'||str1[i]=='U')str2[j++]=str1[i];else continue;}str2[j]='\0';printf("%s中元音字母是%s\n",str1,str2);}main(){char str1[100],str2[100];scanf("%s",str1);find(str1,str2);}8.8//输入数字1990输出1 9 9 0#include<stdio.h>#include<string.h>void main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9//统计字母,数字,空格,和其他字符的个数#include<stdio.h>#include<string.h>int alph,digit,space,others;void main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others);}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'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;else others++;}8.10//输出最长单词#include<stdio.h>#include<string.h>int zimu(char c){if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(char string[]){int i,point,point1,num=0,nummax=0,flag=1;for(i=0;i<=strlen(string);i++)if(zimu(string[i]))//是字母则记下起始位置和长度if(flag){point=i;flag=0;}elsenum++;else //不是字母则比较长度重新记下初始位置{flag=1;if(num>nummax){nummax=num;point1=point;}}return point1;}void main(){int i;char line[100];gets(line);for(i=longest(line);zimu(line[i]);i++)printf("%c",line[i]);printf("\n");}8.11//冒泡法字符串排序#include<stdio.h>#include<string.h>#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");else flag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);sort(char str[N]){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;}}8.12//找根的值,一元三次方程牛顿迭代公式法#include<stdio.h>#include<string.h>#include<math.h>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-5);return(x);}void main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13//递归法#include<stdio.h>#include<string.h>#include<math.h>void main()int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(int tn,int tx){if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn);}8.14//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差#include<stdio.h>#include<string.h>#include<math.h>#define N 4#define M 4float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();input_stu();avr_stu();avr_cor();printf("\n人数科目 1 2 3 4 平均分");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);//每个学生的各门成绩printf("%8.2f",a_stu[i]); //每个学生的平均成绩}printf("\n课程平均成绩");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n最高分为%8.2f在第%d行第%d列\n",h,r,c);printf("\n平均分方差为%8.2f\n",s_diff());}/*输入是个学生的成绩*/input_stu(){int i,j;float x;for(i=0;i<N;i++){printf("请输入第%d个学生的成绩:\n",i+1);for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}/*输出每个学生的平均成绩*/avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}/*输出每门课的平均成绩*/avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}/*求最高分和它属于哪个学生,哪门课*/float highest(int *r,int *c){float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;//用指针实现双向传递很经典,也可该用全局变量*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N));}8.15//输入10个职工的姓名和职工号按职工号由小到大排列,姓名顺序也随之调整//输入一个职工号用折半法找出该职工的姓名#include<stdio.h>#include<string.h>#include<math.h>#define N 3void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){printf("\ninput No.");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]); }void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1])) loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min]) bott=min-1;else top=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continueY/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16//输入16进制输出10进制#include<stdio.h>#include<string.h>#include<math.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;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("\nnumber=%d\n",htoi(t));printf("continue");c=getchar();if(c=='n'||c=='N')flag1=0;else {flag=0; i=0; }}}}htoi(s)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);}8.17//递归法#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}8.18//年月日具体是多少天#include<stdio.h>#include<string.h>#include<math.h>void main(){int year,month,day;int days;int sum_day();int leap();scanf("\n%d%d%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3)){days+=1;printf("%d年共有366天该天是其中的%d天",days);}elseprintf("%d年共有365天该天是其中的%d天",days); }static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; /*总天数*/int sum_day(int month,int day){int i;for(i=1;i<month;i++)day+=day_tab[i];return(day);}/*是否为闰年*/int leap(int year){int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}。