华科周纯杰版c语言答案 联系客服

发布时间 : 星期一 文章华科周纯杰版c语言答案更新完毕开始阅读f651c36a561252d380eb6ebd

第七章课后习题参考答案:编程题

1. 输入两个整数,调用函数squSum()求两数平方和,返回主函数显示结果。 #include \ int squSum(int a,int b); void main() { int a,b; int sm; printf(\ scanf(\ %d\ m=squSum(a,b); printf(\ } int squSum(int a,int b) //计算两数平方和 { int c; c=a*a+b*b; return c; }

2. 写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。 #include \ int funCommonDivisor(int a,int b); int funCommonMultiple(int a,int b); void main() { int a,b; int commonDivisor; int commonMultiple; printf(\ scanf(\ %d\

commonDivisor=fun1(a,b); commonMultiple=fun2(a,b); printf(\ } int funCommonDivisor(int a,int b) { int r; //求公约数算法 while(a%b) { r=a%b; a=b; b=r; } return b; } int funCommonMultiple(int a,int b) { int c,d; c=funCommonDivisor(a,b); d=a*b/c; return d; }

3. 求方程ax2 + bx + c = 0的根,用三个函数分别求不相等实根、相等实根、共轭复根,并在函数中输出结果,a、b、c从主函数输入。 #include \ #include \ void twoRealRoot(float a,float b,float c); void equaRoot(float a,float b,float c); void twoImRoot(float a,float b,float c); void main() { float a,b,c; printf(\ scanf(\ %f %f\

if(b*b==4*a*c) equaRoot(a,b,c); else if (b*b>4*a*c) twoRealRoot(a,b,c); else twoImRoot(a,b,c); } void twoRealRoot(float a,float b,float c) { float root1,root2; float d; d=b*b-4*a*c; d=(float)sqrt(d); root1=(-b-d)/(2*a); root2=(-b+d)/(2*a); printf(\ printf(\ } void equaRoot(float a,float b,float c) { printf(\ printf(\ } void twoImRoot(float a,float b,float c) { float real,im; float d; d=4*a*c-b*b; d=(float)sqrt(d); real=-b/(2*a); im=d/(2*a); printf(\ \ printf(\ printf(\}

4. 编写一个函数,删除给定字符串中的指定字符,如给定字符串“abcdfrc”,删除指定字符‘c’后,字符串变为“abdfr”;主函数完成给定字符串和指定字符的输入,调用所编函数,输出处理后的字符串。 #include \ #include \

void deleteChar(char *pChar,char delChar); void main() { char string[]=\ char delChar='c'; printf(\ scanf(\ printf(\ printf(\ deleteChar(string,delChar); printf(\ } void deleteChar(char *pChar,char delChar) { int i=0; char c; int length,pos; length=strlen(pChar); while(*(pChar+i)!='\\0') //找到删除字符的位置 { if(*(pChar+i)==delChar) { pos=i; length--; //将后面的字符向前移 if(pos==length) { *(pChar+i)='\\0'; break; } else { for(i=pos;i