CC++程序设计(吴国风 宣善立主编)部分课后题答案(自己写的)(原创).doc_第1页
CC++程序设计(吴国风 宣善立主编)部分课后题答案(自己写的)(原创).doc_第2页
CC++程序设计(吴国风 宣善立主编)部分课后题答案(自己写的)(原创).doc_第3页
CC++程序设计(吴国风 宣善立主编)部分课后题答案(自己写的)(原创).doc_第4页
CC++程序设计(吴国风 宣善立主编)部分课后题答案(自己写的)(原创).doc_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C/C+程序设计(吴国风 宣善立主编)第三章1.求解满足条件1+2+3+n1000的最小n值及其和s。源程序:#includevoid main()int s=0,n=0;while(s1000)n+;s+=n;printf(最小n值:%dt和s:%dn,n,s);运行结果:最小n值:45 和s:1035Press any key to continue2.输入n个实数,并分别统计正数的和及负数的和,最后输出统计结果。源程序:#includevoid main()char c;float zsum=0,fsum=0,x;doscanf(%f,&x);if(x=0) zsum+=x;else fsum+=x;while(c=getchar()!=n);printf(正数的和:%ft负数的和:%fn,zsum,fsum);运行结果:1 -2 0 3.4 -4.5 5.66 -6.89正数的和:10.059999 负数的和:-13.389999Press any key to continue3.求出10100之内能同时被2、3、7整除的数,并输出。源程序:#includevoid main()int i;for(i=10;i=100;i=i+2)if(i%3=0&i%7=0)printf(%dt,i);else continue;printf(n);运行结果:42 84Press any key to continue4.利用循环语句求1-1/3+1/5-1/7+1/(2n-1)(直到第100项的和)源程序:#includevoid main()float n=1,t=1,s=1,sum=0;while(n=100)sum+=t;s=-t*(2*n-1);n+;t=s/(2*n-1);printf(直到第100项的和:%fn,sum);运行结果:直到第100项的和:0.782898Press any key to continue5.计算并输出1!+2!+3!+10!。一、源程序:#includevoid main()float y=0.0,x;int i,n;for(n=1;n=10;n+)x=1;for(i=1;i=n;i+)x=x*i;y=y+x;printf(%fn,y);运行结果:4037913.000000Press any key to continue二、源程序:#includevoid main()float n,s=0,t=1; for(n=1;n=10;n+) t*=n;s+=t;printf(1!+2!+3!.+10!=%en,s); 运行结果:1!+2!+3!.+10!=4.037913e+006Press any key to continue6.计算并输出1!+2!+3!+n!一、源程序:#includevoid main()float y=0.0,x;int i,j,n;printf(请输入n值:);scanf(%d,&n);for(j=1;j=n;j+)x=1;for(i=1;i=j;i+)x=x*i;y=y+x;printf(%fn,y);运行结果:请输入n值:104037913.000000Press any key to continue二、源程序:#includevoid main()float s=0,t=1;int n,i;printf(请输入n值:n);scanf(%d,&n);for(i=1;i=n;i+) t*=i;s+=t;printf(1!+2!+3!.+%d!=%en,n,s); 运行结果:请输入n值:101!+2!+3!.+10!=4.037913e+006Press any key to continue7.求1100之间的奇数之和及偶数之和,并输出。源程序:#includevoid main()int i,jsum=0,osum=100;for(i=1;i100;i=i+2)jsum+=i;osum+=(i-1);printf(奇数之和:%dt偶数之和:%dn,jsum,osum);运行结果:奇数之和:2500 偶数之和:2550Press any key to continue8.用键盘输入一段文字,以“*”作为结束标志,试编写一个对行、单词和字符计数的程序。其中,单词为一串不含空格、制表符或换行符的字符串。源程序:#include#define YES 1#define NO 0void main()int nl,nw,nc,inword;char c;inword=NO;nl=nw=nc=0;while(c=getchar()!=*)+nc;if(c=n) +nl;if(c= |c=t|c=n)inword=NO;elseif(inword=NO)inword=YES;+nw;nc-;nl+;nc+;printf(行数:%dt单词数:%dt字符数:%dn,nl,nw,nc);运行结果:i am why.what is your name?*行数:2 单词数:7 字符数:22Press any key to continue9.编写一个程序,要求输出如下图案。 * * * * * *源程序:#includevoid main() int i,j,k;for(i=0;i=3;i+)for(j=0;j=2-i;j+)printf( );for(k=0;k=2*i;k+)printf(*);printf(n);for(i=0;i=2;i+)for(j=0;j=i;j+)printf( );for(k=0;k=4-2*i;k+)printf(*);printf(n);10.编写一个程序,要求输出如下图案。 * * * *一、源程序:#includevoid main() int i,j;for(i=0;i=2;i+)for(j=0;j2-i;j+)printf( );for(j=2-i;j=2+i;j+)printf(*);printf(n);for(i=3;i=4;i+)for(j=0;j2-i%2;j+)printf( );for(j=2-i%2;j=2+i%2;j+)printf(*);printf(n);二、源程序#includevoid main() int i,j;for(i=0;i=2;i+)for(j=0;j=2-i&j=2+i) printf(*);else printf( );printf(n);for(i=3;i=4;i+)for(j=0;j=2-i%2&j=2+i%2) printf(*);else printf( );printf(n);11.编写一个程序,要求输出如下图案。 1 1*1 2*2 3*3 4*4 5*5$源程序:#includevoid main() int i,j;/*输出第一行*/for(j=0;j=12;j+)if(j=6) printf(1);else printf( );printf(n);/*输出第二行到第六行*/for(i=1;i=5;i+)for(j=0;j6-i&j6+i) printf(*);else if(j=6-i|j=6+i) printf(%d,i);else printf( );printf(n);/*输出最后一行*/for(j=0;j=12;j+)printf($);printf(n);12.编写一个程序,模拟选举过程,共有n个人参加选举,候选人有4位,分别用字符A、B、C、D表示,选某位候选人时,直接键入其代号:若键入除A、B、C、D以外其他字符时为无效票。选举结束后按得票多少的顺序输出候选人代号所得票数。源程序:#include void main()char c;int n=0,Asum,Bsum,Csum,Dsum,Wsum;Asum=Bsum=Csum=Dsum=Wsum=0;while(c=getchar()!=n)n+;switch(c)case A:case a:Asum+;break;case B:case b:Bsum+;break;case C:case c:Csum+;break;case D:case d:Dsum+;break;default:Wsum+;break;printf(A所得票数:%dnB所得票数:%dnC所得票数:%dnD所得票数:%dn无效票数:%dn参加选举的人数:%dt,Asum,Bsum,Csum,Dsum,Wsum,n);运行结果:abcdABCDwhy AbCdA所得票数:3B所得票数:3C所得票数:3D所得票数:3无效票数:4参加选举的人数:16Press any key to continue第四章习题1.下列程序执行后的输出结果。源程序:#include#includevoid main()char arr25;strcpy(arr0,love);/改为strcpy(arr,love)出错了strcpy(arr1,you);arr04=$;printf(%s n,arr);运行结果:love$youPress any key to continue2.下列程序执行后的输出结果。源程序:#include#includevoid main()char str=abn012n,s110=1230abc;printf(数组str的字符串长度:%dn,strlen(str);printf(数组s1的字符串长度:%dn,strlen(s1);运行结果:数组str的字符串长度:6数组s1的字符串长度:3Press any key to continue3.写出下列程序运行结果。源程序:#includevoid main()int a5=1,1,1,0,i,k=2;for(i=0;i=k;i+)ai=ai+1;printf(%dn,ak);运行结果:2Press any key to continue4.若输入ABC,写出下列程序运行结果。源程序:#include#includestring.hvoid main()char a10=a,b,c,d,e;gets(a);strcat(a,6789);printf(%sn,a);运行结果:ABCABC6789Press any key to continueGet()函数功能:从标准输入设备(键盘)上输入一个字符串到字符数组中,并自动在末尾加字符串结束标志符0。Strcat()函数功能:将两个字符串连接起来形成一个新的字符串。他将第一个字符串结束标志符0删除,然后把第二个字符串的全部内容,包括它的结束标识符0,都连接到第一个字符串的后面,形成新的字符串并存放到第一个字符串变量中。需要注意的是,必须保证第一个字符串(即字符串数组)的长度足以容纳合并过来的第二个字符串的全部内容。5.写出下列程序运行结果。源程序:#includevoid main()char a=11223344;a3=0;printf(%sn,a);运行结果:112Press any key to continue6.输入I am why,写出下列程序的运行结果。一:源程序#include#includestring.hvoid main()char str19;/str18亦可,个人认为str19比较好gets(str1);puts(str1);printf(用gets()输入字符串数组的长度:%dn,strlen(str1);printf(*n);scanf(%s,str1);printf(%sn,str1);printf(用scanf()输入字符串数组的长度:%dn,strlen(str1);运行结果:I am whyI am why用gets()输入字符串数组的长度:8*I am whyI用scanf()输入字符串数组的长度:1Press any key to continue二:源程序#include#includestring.hvoid main()char str18;/若改为str19,长度为:15int i;for(i=0;i8;i+)scanf(%c,&str1i);for(i=0;i8;i+)printf(%c,str1i);printf(n);printf(用循环语句输入字符串数组的长度:%dn,strlen(str1);运行结果:I am whyI am why用循环语句输入字符串数组的长度:11Press any key to continue7.输入12345678,写出下列程序的运行结果。一:源程序:#includevoid main()char str19;int i;for(i=0;i9;i+)scanf(%c,&str1i);for(i=0;i9;i+)printf(%c,str1i);printf(*n);/利用循环语句输入字符串,不可用其他方式输出printf(%sn,str1);printf(*n);puts(str1);运行结果:1234567812345678*12345678烫虉*12345678烫虉Press any key to continue二:源程序:#includevoid main()char str19;int i;scanf(%s,str1);printf(%sn,str1);printf(*n);/利用scanf()语句输入字符串,可以用其他方式输出for(i=0;i9;i+)printf(%c,str1i);printf(n*n);puts(str1);运行结果:1234567812345678*12345678*12345678Press any key to continue三:源程序:#includevoid main()char str19;int i;gets(str1);puts(str1);printf(*n);/利用gets()输入字符串,可以用其他语句输出for(i=0;i9;i+)printf(%c,str1i);printf(n*n);printf(%sn,str1);运行结果:1234567812345678*12345678*12345678Press any key to continue8.将一个字符串的内容颠倒过来。源程序:#include#includestring.hvoid main()char str80;int i,j,k;gets(str);j=strlen(str)-1;for(i=0;ij;i+,j-)k=stri;stri=strj;strj=k;puts(str);运行结果:12345*I am whyyhw ma I*54321Press any key to continue9.对输入的字符串统计其中数字字符出现的次数。源程序:#include#includestring.hvoid main()char ch80;int i,a10;for(i=0;i=0&chi=9)achi-0+;for(i=0;i=9;i+)printf(%d ,%dn,i,ai);运行结果:123456789100 ,11 ,22 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,1Press any key to continue10.将字符数组str2中的全部字符复制到字符数组str1中。不使用strcpy函数。复制时,0也要复制进去,0后面的字符不复制。一:源程序:#includevoid main()char str110,str210;int i;gets(str1);gets(str2);printf(复制前:n);puts(str1);puts(str2);for(i=0;str2i!=0;i+)str1i=str2i;str1i=0;printf(复制后:n);puts(str1);puts(str2);运行结果:I am why!Are you?复制前:I am why!Are you?复制后:Are you?Are you?Press any key to continue二:源程序:#include#includestring.hvoid main()char str110,str210;int i;gets(str1);gets(str2);printf(复制前:n);puts(str1);puts(str2);for(i=0;i=strlen(str2);i+)/若改为istrlen(str2)会如何呢?str1i=str2i;printf(复制后:n);puts(str1);puts(str2);运行结果:123456789abcd复制前:123456789abcd复制后:abcdabcdPress any key to continue二.1源程序:#include#includestring.hvoid main()char str110,str210;int i;gets(str1);gets(str2);printf(复制前:n);puts(str1);puts(str2);for(i=0;istrlen(str2);i+)str1i=str2i;printf(复制后:n);puts(str1);puts(str2);运行结果:123456789abcd复制前:123456789abcd复制后:abcd56789abcdPress any key to continue11.找出一个二维数组中的鞍点,即该位置上的元素在该行上最大、在该列上最小。数组中也可能没有鞍点。任意输入一个45的整数矩阵。源程序:#includevoid main()int a45,i,j,k,m,max,min,t,b;m=k=t=0;for(i=0;i=3;i+)for(j=0;j=4;j+)scanf(%d,&aij);for(i=0;i=3;i+)b=i;max=ai0;for(j=1;j=max)max=aij;k=j;/行的最大值的列坐标/printf(max=%dnk=%dn,max,k);min=aik;for(i=0;i=3;i+)if(aikmin)/if(aik=min)亦可min=aik;m=i;/该行最大值列坐标的最小值的行坐标/printf(m=%d,i=%dn,m,i);i=b;/printf(i=%dn,i);if(m=i)printf(鞍点的位置为: %d行%d列n,m,k);t=1;if(t=0)printf(没有鞍点!n);printf(n array a:n);for(i=0;i4;i+)for(j=0;j5;j+)printf(%4d,aij);printf(n);一:运行结果:鞍点的位置为: 1行2列1 9 6 12 5 1 3 6 2 5 1 4 6 10 0 1 12 6 11 15 array a: 1 9 6 12 5 1 3 6 2 5 1 4 6 10 0 1 12 6 11 15Press any key to continue二:运行结果:1 9 6 12 5 1 3 6 2 5 1 4 7 10 0 1 12 8 11 15鞍点的位置为: 1行2列 array a: 1 9 6 12 5 1 3 6 2 5 1 4 7 10 0 1 12 8 11 15Press any key to continue三:运行结果:1 4 3 64 2 2 6 7 89 56 11 2 34 65 3 9 10 29 13 12没有鞍点! array a: 1 4 3 64 2 2 6 7 89 56 11 2 34 65 3 9 10 29 13 12Press any key to continue12.输入一个长度不超过80的字符串,编写程序,删除其中所有的数字字符。源程序:#include#includestring.hvoid main()char ch80;int i;gets(ch);for(i=0;chi!=0;i+)if(chi=0&chi=9)chi= ;puts(ch);运行结果:I123am 12why!I am why!Press any key to continue13.编程,将任意一个整数x插入到一个有序的数组a中。假设数组a是有序排列。源程序#includevoid main()int a9=1,3,5,7,9,11,13,15,i,x,t;printf(插入的x值:);scanf(%d,&x);for(i=0;i=ai&x=ai+1)t=ai+1;ai+1=x;x=t;ai=x;printf(n新的数组:n);for(i=0;i9;i+)printf(%3d,ai);printf(n);运行结果:插入的x值:4新的数组: 1 3 4 5 7 9 11 13 15Press any key to continue14.编程,打印杨辉三角前8行8列的值。源程序:#includevoid main()int a89=0,i,j;for(i=0;i8;i+)ai0=1;for(i=0;i7;i+)for(j=0;j7;j+)ai+1j+1=aij+aij+1;for(i=0;i8;i+)for(j=0;aij!=0;j+)printf(%3d,aij);printf(n);运行结果: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1Press any key to continue第五章1.静态类型局部变量与自动变量。源程序一:#includevoid row(void);void main()int b;for(b=1;b=9;b+)row();void row(void)static int a=1;int b;for(b=1;b=9;b+)printf(%5d,a*b);printf(n);a+;运行结果: 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81Press any key to continue源程序二:#includevoid row(void);void main()int b;for(b=1;b=9;b+)row();void row(void)int a=1;int b;for(b=1;b=9;b+)printf(%5d,a*b);printf(n);a+;运行结果: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9Press any key to continue2.计算输出x的平方值。源程序:#includesquare(int);void main()int x=7;x=square(x);printf( the square is %dn,x);square(int x)return(x*x);运行结果:the square is 49Press any key to continue3.计算矩形的面积。源程序:#includefloat mul(int,int);void main()float x,y;scanf(%f%f,&x,&y);printf(%fn,mul(x,y);float mul(int a,int b)return(a*b);运行结果:2 36.000000Press any key to continue4.输入两个整数,求他们差的绝对值。源程序:#include#includevoid main()int x,y,z;scanf(%d%d,&x,&y);z=(int)fabs(x-y);printf(|%d-%d|=%dn,x,y,z);运行结果:4 6|4-6|=2Press any key to continue5.分别用函数和带参数的宏,求三个数中的最大值。源程序一:#includefloat imax(float,float,float);void main()float a,b,c,d;scanf(%f%f%f,&a,&b,&c);d=imax(a,b,c);printf(a,b,c中的最大值为:%fn,d);float imax(float x,float y,float z)float max;if(xy)max=x;else max=y;if(zmax)max=z;return(max);运行结果:3 6 2a,b,c中的最大值为:6.000000Press any key to continue源程序二:#include#define MAX(x,y) (xy?x:y)void main()float a,b,c,d;scanf(%f%f%f,&a,&b,&c);d=MAX(a,b);d=MAX(d,c);printf(a,b,c中的最大值为:%fn,d);运行结果:3 6 2a,b,c中的最大值为:6.000000Press any key to continue6编写求x!的函数,通过调用该函数求出能使等式x!=x3-10成立的10以内的所有正整数。源程序:#includefloat fact(int);void main()int i;float y;for(i=1;i=10;i+)if(fact(i)=i*i*i-10*i)printf(%dn,i);float fact(int a)int i;float z=1;/包含了a为0的情况for(i=1;i=a;i+)z=z*i;return z;运行结果:4Press any key to continue7.求组合数,编程计算:C(m,n)=m!(n!(m-n)!)源程序:#includedouble fact(int);void main()int m,n;double c;scanf(%d%d,&m,&n);c=fact(m)/(fact(n)*fact(m-n);printf(C(%d,%d)=%fn,m,n,c);double fact(int a)int i;double z=1;for(i=1;iy)return x;else return y;/*fa2.c程序*/float max2(float x,float y,float z)float m;m=max1(max1(x,y),z);return m;/*fa3.c程序*/#include#includefa1.c#includefa2.cvoid main()float a,b,c,max;scanf(%f,%f,%f,&a,&b,&c);max=max2(a,b,c);printf(max(%f,%f,%f)=%fn,a,b,c,max);编译正确后保存完毕后关闭工作空间重新打开fa3.c编译(compile)BuildBuildExecute运行结果:4,6,9max(4.000000,6.000000,9.000000)=9.000000Press any key to continue第六章1.输入两个整数i1和i2,利用指针将大数存放到i1中,小数存放到i2中,最后按i1、i2的顺序输出。源程序一:#includevoid main(void)int i1,i2,*p1,*p2,t;p1=&i1;p2=&i2;printf(Enter Two Numbers:n);scanf(%d%d,p1,p2);if(i1i2)t=*p1;*p1=*p2;*p2=t;/利用指针变量的指向操作交换i1,i2的值printf(i1=%d,i2=%dn,i1,i2);运行结果:Enter Two Numbers:12 23i1=23,i2=12Press any key to continue源程序二:#includevoid main(void)int i1,i2,max,min,*p1,*p2,*p;p1=&i1;p2=&i2;printf(Enter Two Numbers:n);scanf(%d%d,p1,p2);if(i1i2)p=p1;p1=p2;p2=p;/利用临时变量p交换指针变量p1、p2存放的地址值printf(i1=%d,i2=%dn,i1,i2);printf(max=%d,min=%dn,*p1,*p2);运行结果:Enter Two Numbers:12 23i1=12,i2=23max=23,min=12Press any key to continue源程序三:#include#define NULL 0void main(void)int i1,i2,max,min,*p1,*p2,*p=NULL;/指针变量p没有存放普通变量的地址,因此错误!p1=&i1;p2=&i2;printf(Enter Two Numbers:n);scanf(%d%d,p1,p2);if(i1i2)*p=*p1;*p1=*p2;*p2=*p;printf(i1=%d,i2=%d

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论