第5章C语言程序设计习题答案

申明敬告: 本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。

文档介绍

第5章C语言程序设计习题答案

《C语言程序设计(VisualC++6.0环境)》习题答案习题五一、思考题1,编写两个函数,分别用来求两个整数的和和差,用主函数调用这两个函数,并输出结果。#include"stdio.h"intf1(intx,inty){intz;z=x+y;returnz;}intf2(intx,inty){intc;c=x-y;returnc;}main(){intm,n,sum,dec;scanf("%d%d",&m,&n);sum=f1(m,n);dec=f2(m,n);printf("sum=%d,,dec=%dn",sum,dec);}2、编写一个将两个字符串连接的函数。① #includecvs(charstr1[],charstr2[],charstr[]){inti,j;for(i=0;str1[i]!='';i++)str[i]=str1[i];for(j=0;str2[j]!='';j++)str[i+j]=str2[j];str[i+j]='';}main(){chars1[100],s2[100],s[100];printf("输入2个字符串n");scanf("%s%s",s1,s2);cvs(s1,s2,s);printf("两字符串连接后:%sn",s);}②#include"stdio.h"#include"string.h"voidlj(chars1[],chars2[])n{inti,j;for(i=strlen(s1),j=0;s2[j]!='';i++,j++)s1[i]=s2[j];s1[i]='';}main(){chars1[200],s2[100];printf("请输入第一个字符串");gets(s1);printf("请输入第二个字符串");gets(s2);printf("输入的字符串是:");puts(s1);puts(s2);lj(s1,s2);printf("连接后的字符串是:");puts(s1);}3、编写一个函数,输入一个十进制数,输出相应的二进制数。数组:#include"stdio.h"voidzh(intnumber,inta[]){intys;inti=0,n;while(number/2!=0){a[i++]=number%2;number/=2;}a[i]=number%2;for(n=i;n>=0;n--)printf("%5d",a[n]);}main(){intnumber;inta[10];printf("Pleaseinputnunber:");scanf("%d",&number);zh(number,a);}n递归(1)#include"stdio.h"voidzh(intnumber){intys;ys=number%2;number=number/2;if(number==1)printf("%d",number);elsezh(number);printf("%d",ys);}main(){intnumber;printf("Pleaseinputnunber:");scanf("%d",&number);zh(number);}(2)#include"stdio.h"voidzh(intm){intf;f=m%2;if(m/2==0){printf("%d",m%2);return;}elsezh(m/2);printf("%d",f);}main(){intm;printf("请输入一个十进制数:");scanf("%d",&m);zh(m);}4、编写一个函数,使输入的字符串反序存放,在主函数中输入和输出字符串。n①#include#includeinver(charstr[]){chart;inti,j;for(i=0,j=strlen(str);i0)dyl(a,b,c,d);elseif(d<0)xyl(a,b,c,d);elsexdl(a,b,c,d);}main(){inta,b,c;printf("请输入一元二次方程的三个系数a,b,c:");scanf("%d,%d,%d",&a,&b,&c);qg(a,b,c);}② #include"math.h"#include"stdio.h"voidfun1(floata,floatb,floatc){floatx1,x2;x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x1=%f,x2=%fn",x1,x2);}nvoidfun2(floata,floatb,floatc){floatx1,x2;x1=x2=(-b)/(2*a);printf("x1=x2=%fn",x1);}voidfun3(){printf("该方程无解!n");}main(){floata,b,c,d;scanf("%f%f%f",&a,&b,&c);d=b*b-4*a*c;if(d>0)fun1(a,b,c);elseif(d==0)fun2(a,b,c);elsefun3();}6、输入两个整数,求它们相除的余数。用带参数的宏来实现。#include"stdio.h"#defineA(x,y)((x)%(y))main(){intm,n;printf("请输入两个整数:");scanf("%d,%d",&m,&n);printf("%d",A(m,n));}7、给年份year定义一个宏,以判别该年份是否闰年。#include"stdio.h"#defineYEAR(y)(y%4==0)&&(y%100!=0)||(y%400==0)main(){intyear;scanf("%d",&year);if(YEAR(year))printf("%d年是闰年.n",year);elseprintf("%d年不是闰年.n",year);}8、分别用带参数的宏和函数,从三个数中找出最大的数。(1)用带参数的宏实现#include“stdio.h”#defineMAX(a,b)((a)>(b)?(a):(b))main(){ninta,b,c;scanf(“%d%d%d”,&a,&b,&c);printf(“max=%dn”,MAX(MAX(a,b),c));}(2)用函数实现#include"stdio.h"intmax(intx,inty){intt;t=x>y?x:y;return(t);}main(){inta,b,c;scanf("%d%d%d",&a,&b,&c);printf("max=%dn",max(max(a,b),c));}二、选择题1D2C3A4D5C6D7A8A9C10B11D12A三、填空题1a=1;b=1;s=1;2、inti;k=sqrt(pm);for(i=2;i<=k;i++)if(pm%i==0)break;if(i>k)return1;elsereturn0;3for(i=0;i<3;i++)for(j=0;j<3;j++)scanf(“%d”,&a[i][j];)for(i=0;i<3;i++)sum=sum+a[i][i];returnsum;
查看更多

相关文章

您可能关注的文档