第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、编写一个将两个字符串连接的函数。① #include
cvs(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);i