课后题答案-C语言程序设计(第2版)

合集下载

C语言程序设计(第二版)习题参考答案

C语言程序设计(第二版)习题参考答案

C语言程序‎设计习题参‎考答案习题 1一、判断题1.在计算机中‎,小数点和正‎负号都有专‎用部件来保‎存和表示。

2.二进制是由‎0和1两个‎数字组成的‎进制方式。

3.二进制数的‎逻辑运算是‎按位进行的‎,位与位之间‎没有进位和‎借位的关系‎。

4.在整数的二‎进制表示方‎法中,0的原码、反码都有两‎种形式。

5.有符号数有‎三种表示法‎:原码、反码和补码‎。

6.常用字符的‎A S CII‎码值从小到‎大的排列规‎律是:空格、阿拉伯数字‎、大写英文字‎母、小写英文字‎母。

解:1.F2.T 3.T 4.T 5.T 6.T二、单选题1.在计算机中‎,最适合进行‎数值加减运‎算的数值编‎码是。

A. 原码B. 反码C. 补码D. 移码2.已知英文小‎写字母m的‎A SCII‎码为十进制‎数109,则英文小写‎字母y的A‎S CII码‎为十进制数‎。

A. 112B. 120C. 121D. 1223.关于ASC‎II码,在计算机中‎的表示方法‎准确地描述‎是。

A. 使用8位二‎进制数,最右边一位‎为1B. 使用8位二‎进制数,最左边一位‎为1C. 使用8位二‎进制数,最右边一位‎为0D. 使用8位二‎进制数,最左边一位‎为04.设在机器字‎长4位,X=0111B‎,Y=1011B‎,则下列逻辑‎运算中,正确的是_‎_____‎_____‎。

A. X∧Y=1000B. X∨Y=1111C. X⊕Y=0011D. ¯Y=10005.下列叙述中‎正确的是()。

A.高级语言就‎是机器语言‎B.汇编语言程‎序、高级语言程‎序都是计算‎机程序,但只有机器‎语言程序才‎是计算机可‎以直接识别‎并执行的程‎序C.C语言因为‎具有汇编语‎言的一些特‎性,所以是汇编‎语言的一种‎D.C源程序经‎过编译、连接,若正确,执行后就能‎得到正确的‎运行结果6.用C语言编‎写的源程序‎经过编译后‎,若没有产生‎编译错误,则系统将()。

C语言程序设计(第二版)答案

C语言程序设计(第二版)答案

参考答案习题二(P33)一.单选题1.C2.B3.D4.C5.A6.D7.D8.B9.B 10.D 11.D 12.C 13.C . 15. A 14题最后一句应为printf("%f\n",d*y);结果为2.2二.填空题1. 182. int float double3. 10 114. 八十六十5. %三.阅读程序题1.10,10,9,102.j=1,i=2k=3,i=3j=3,i=2k=1,i=1习题三(P52)一.单选题1.D2.C3.D4.B5.A6.B7.C8.A9.C 10.B 11. -1,37777777777,4294967295 二.填空题5. L6. -1三.阅读程序题1. 6 6 6.00 6.002. x=127,x= 127,x=177,x=7fY=123.4567 , y= 123.46 , y=123.456703. 2,14. 12345. 4,36. -6,-6四.程序设计题1.#include "stdio.h"#include "math.h"main(){float a,b,c,d,x1,x2;a=2;b=-3;c=-5;d=b*b-4*a*c;x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);printf("x1=%.2f,x2=%.2f\n",x1,x2);}2.#include <stdio.h>main(){ float a,v,s;scanf("%f",&a);v=a*a*a;s=6*a*a;printf("v=%.2f,s=%.2f\n",v,s);}3.#include <stdio.h>main(){ int a,b,c,t;scanf("%d%d%d",&a,&b,&c);printf("a=%d,b=%d,c=%d\n",a,b,c);t=c;c=b;b=a;a=t;printf("a=%d,b=%d,c=%d\n",a,b,c);}4.#include <stdio.h>main(){ char s1,s2;s1=getchar();s2=s1-32;printf("%c\n",s2);}习题四(P70)一.单选题1.C2.D3.D4.B5.A6.D7.D8.B9.C 10.A 11.B 12.D 13.passwarnerror .14.C 15. C 16.B 17. B 18. C二.填空题1. 非0 02. k==03. n%7==0 && n%8==0 else三.阅读程序题1. a=1,b=02. c=1四.程序设计题1.#include <stdio.h>main(){ int a,b;char c;printf("INPUT A+(-*/)B\n");scanf("%d%c%d",&a,&c,&b);switch(c){case'+':printf("%d+%d=%d\n",a,b,a+b);break;case'-':printf("%d-%d=%d\n",a,b,a-b);break;case'*':printf("%d*%d=%d\n",a,b,a*b);break;case'/':printf("%d/%d=%d\n",a,b,a/b);break;default:printf("INPUT ERROR!");}}2.#include <stdio.h>main(){ float x,y;scanf("%f",&x);if(x==0||x==2)y=0;else if(x>0) y=(x+1)/(x-2);else y=(x-1)/(x-2);printf("y=%f\n",y);}3.#include <stdio.h>main(){ int g;printf("请输入学生成绩:");scanf("%d",&g);printf("g=%d:",g);switch(g/10){case 10:printf("A\n");break;case 9:printf("B\n");break;case 8:printf("C\n");break;case 7:printf("D\n");break;case 6:printf("E\n");break;default:printf("F\n");}}4.#include <stdio.h>main(){ int x,n=0;scanf("%d",&x);if(x<=0||x>=10000)printf("Data Error!\n");else{if(x<10)n=1;else if(x<100)n=2;else if(x<1000)n=3;else n=4;}printf("n=%d\n",n);}习题五(P87)一.单选题1.B2.D3.C4.C5.B6.C7.D8.C9.A 10.D 11.B 12.B 13.B 14.D 15. C 二.填空题1. for语句while语句do-while语句2. a=14,y=263. k=14,n= -14. 395. s=196. 222227. 108. 79. 810. 5,5三.改错题应改为for(i=0;i<5;i++)j++;应改为int j=0;while(j<10){j++;i=j;} 注while(j<10)后不应有分号应改为while(j<10); 注while(j<10)后分号不能少s*=i;i++;应改为{s*=i;i++;}continue 应改为break四.程序设计题1.#include <stdio.h>main(){ int i,j,s=0;for(i=1,j=1;i<=100;i++,j=j*(-1))s=s+i*j;printf("s=%d\n",s);}1.(另一做法)#include<stdio.h>main(){int i,s;for(i=1,s=0;i<=100;i++)if(i%2==0)s=s-i;else s=s+i;printf ("%d\n",s);}2.#include <stdio.h>main(){ int i,j,s1=0,s2=0;for(i=0;i<10;i++){scanf("%d",&j);if(j>0)s1=s1+j;else s2=s2+j;}printf("s1=%d,s2=%d\n",s1,s2);}3.#include<stdio.h>main(){int i,s;for(i=6,s=0;i<=96;i=i+1)if(i%10==6||i/10==6)s=s+i;printf ("%d\n",s);}4.#include<stdio.h>main(){int i,a,b,c;for(i=100;i<=999;i++){a=(i%10);b=(i/10%10);c=(i/100);if(i==a*a*a+b*b*b+c*c*c)printf ("%d\n",i);}}5.#include <stdio.h>main( ){int i,j,k=0;for (i=1;i<=4;i++){for (j=1;j<=i;j++){k++;printf("%d",k%10);}printf("\n");}}6.#include <stdio.h>main( ){int i,j,k=0;for (i=-3;i<=3;i++){if(i<0)k=-i;else k=i;for(j=1;j<=k;j++)printf(" ");for(j=1;j<=7-2*k;j++)printf("*");printf("\n");}}习题六(P111)一.单选题1.D2.B3.C4.C5.C6.D7.A8.B二.填空题1. 20 0 192. 数组名3. 越界4. 65. j==k a[j][k]=1; a[j][k]=0;三.阅读程序题1. 6 5 43 2 12.aaabbbccc ddd3.2,2,1四.程序设计题1.#include<stdio.h>main(){ int a[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int i,j,s=0;for(i=0;i<4;i++)for(j=0;j<4;j++)if(i==j||i+j==3)s+=a[i][j];printf("%d",s);}2.#include <stdio.h>{ char a[80];int i,j=5; /*假设删除位置为5*/gets(a);for(i=j-1;a[i]!='\0';i++)a[i]=a[i+1];a[i]='\0';puts(a);}3.#include <stdio.h>#include <string.h>main(){ char a[80];int i,j=5; /*假设插入位置为5*/char s='t'; /*假设插入字符为t*/gets(a);for(i=strlen(a);i>j;i--)a[i+1]=a[i];a[j]='t';puts(a);}4.#include<stdio.h>main(){ int a[3][5]={1,3,5,7,9,2,4,6,8,10,3,5,8,7,6}; int i,j,s1[3]={0},s2[5]={0};for(i=0;i<3;i++)for(j=0;j<5;j++)s1[i]+=a[i][ j];for(i=0;i<5;i++)for(j=0;j<3;j++)s2[i]+=a[ j][i];for(i=0;i<3;i++){for(j=0;j<5;j++)printf("%6d",a[i][j]);printf (" |%6d\n",s1[i]);}for(i=0;i<33;i++) printf("-");printf("\n");for(i=0;i<5;i++) printf("%6d",s2[i]);printf("\n");}5.#include<stdio.h>{ char s[3][80];int a=0,b=0,c=0,d=0,e=0,i,j;for(i=0;i<3;i++) gets(s[i]);for(i=0;i<3;i++)for(j=0;s[i][j]!='\0';j++)if(s[i][j]>='A'&&s[i][j]<='Z')a++;else if(s[i][j]>='a'&&s[i][j]<='z')b++;else if(s[i][j]>='0'&&s[i][j]<='9')c++;else if(s[i][j]==32)d++;else e++;printf("%3d%3d%3d%3d%3d",a,b,c,d,e);}习题七(P145)一.单选题1.A2.B3.C4.A5.C6.D7.C8.B9.B 10.C 11.D 二.填空题1. 该函数内局部2. 整型3. k<=breturn y;4. x[i]return (ave);fun(a,20)5. 1;add(n-1);add(n);6. n*f(n-1)0;f(i)三.阅读程序,写出运行结果1.10,20,302.643.84. hlo5. sum=55四.程序设计题1.#include <stdio.h>main(){float add(float, float), sub(float, float);float aver(float, float),a,b;scanf("%f,%f",&a,&b);printf("add=%f, sub=%f\n ", add(a,b), sub(a,b)); printf("aver =%f", aver(a,b));}float add(float x, float y) {return(x+y);}float sub(float x, float y) {return(x-y);}float aver(float x, float y) {return((x+y)/2);} 2.#include<stdio.h>main(){ int a,b,c,abmax(int,int);scanf("%d%d%d",&a,&b,&c);printf("max=%d\n",abmax(abmax(a,b),c)); }int abmax(int a,int b){if(a>b)return a;else return b;}3.#include <stdio.h>main(){float x,fun(float, int);int n;scanf("%f%d",&x,&n);printf("%f\n", fun(x,n));}float fun(float x, int n){float y;if(n==0)y=1;else y=x*fun(x,n-1);return y;}4.#include <stdio.h>main(){long f(int);int n,k;scanf("%d",&n);for(k=1;k<=n;k++)printf("%ld,",f(k));printf("\n");}long f(int n){long y;if(n<=2)y=1;else y=f(n-1)+f(n-2); return y;}5.#include <stdio.h>#include <string.h> main(){char a[50];int n;void fun(char x[ ],int n); gets(a);n=strlen(a); fun(a,n);puts(a);}void fun(char a[ ],int n) {int k,s;for(k=0;k<n/2;k++) {s=a[k];a[k]=a[n-k-1];a[n-k-1]=s;}}5. (另一做法)#include <stdio.h>#include <string.h> main(){char a[50];int n;void fun(char x[ ],int n); gets(a);n=strlen(a); fun(a,n);puts(a);}void fun(char a[ ],int n) {int k;char b[50];for(k=0;k<n;k++)b[n-1-k]=a[k];b[k]=a[k];strcpy(a,b);}6.#include <stdio.h>#define KK 100main(){char a[KK];long sjz(char a[]);int i=0,f1=0;printf("input a data:");gets(a);for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9'||a[i]>='A'&&a[i]<='F'||a[i]>='a'&&a[i]<='f') continue;else {f1=1;break;}}a[i]='\0';if(f1==1)printf("Data Error!\n");else printf("result is :%d\n",sjz(a));}long sjz(char a[]){long n=0,i;;for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9')n=n*16+a[i]-'0';if(a[i]>='A'&&a[i]<='F')n=n*16+a[i]-'A'+10;if(a[i]>='a'&&a[i]<='f')n=n*16+a[i]-'a'+10;}return n;}6.(另一做法)#include <stdio.h>#define KK 100main(){char str[KK],c;long sjz(char a[]);int i=0,f1=0;printf("input a data:");while((c=getchar())!='\n'&&i<KK){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f')str[i++]=c;else f1=1;}str[i]='\0';if(f1==1)printf("Data Error!\n");else printf("result is :%d\n",sjz(str));}long sjz(char a[]){long n=0,i;;for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9')n=n*16+a[i]-'0';if(a[i]>='A'&&a[i]<='F')n=n*16+a[i]-'A'+10;if(a[i]>='a'&&a[i]<='f')n=n*16+a[i]-'a'+10;}return n;}习题八(P181) (注:无答案的题不在本课程讲授范围内)一.单选题1.D2.A3.C4.D5.C6.B7.C8.C9.C 13.D 14.B 15.A 16.C 17.A 二.填空题1. 指向取地址2. 2 +23. 286. *(p+5)7. ABCD A三.阅读程序题1.102.103.04. 3,65. 1 2 3 4四.程序填空题1.a,b,c或者&x,&y,&z max=*b或者max=y max=*c或者max=z2. ++ =*q ++ ++4. int *a,int *b b[j]=a[i] b[i++]5. *str+=3 *str>’z’&&*str<’a’||*str>’z’ a6. else 0 t[2*j+1]五.程序设计题1.#include <stdio.h>main(){int a[10],*p,*q,t;p=a;for(q=a;q<a+10;q++)scanf("%d",q);for(q=a;q<a+10;q++)printf("%5d",*q);printf("\n");q--;for(;p<q;p++,q--){t=*p;*p=*q;*q=t;}for(p=a;p<a+10;p++)printf("%5d",*p);printf("\n");}2.#include <stdio.h>#include <string.h>void main( ){ char a[50],*p1,*p2,t,n;gets(a);n=strlen(a);p1=a; p2=a+n-1;for(; p1<p2; p1++, p2--){t=*p1; *p1=*p2; *p2=t;}puts(a);}另一做法#include <stdio.h>main(){char str[20],*p=str;gets(str);while(*p)p++;p--;while(p>=str){printf("%c",*p);p--;}printf("\n");}3.#include <stdio.h>main(){int a[10],*p,*max,*min,t;for(p=a;p<a+10;p++)scanf("%d",p); for(p=a;p<a+10;p++)printf("%5d",*p); printf("\n");max=min=a;for(p=a+1;p<a+10;p++){if(*p>*max)max=p;if(*p<*min)min=p;}t=a[0];a[0]=*min;*min=t;t=a[9];a[9]=*max;*max=t;for(p=a;p<a+10;p++)printf("%5d",*p); printf("\n");}5.#include <stdio.h>int length(char *s){int n=0;while(*s){n++;s++;}return n;}main(){char str[20];int n;gets(str);n=length(str);printf("The string length is %d\n",n);}6.#include <stdio.h>main(){char str[81],*p=str,*q,t;gets(str);printf("The origenal string:\n");puts(str);for(p=str;*(p+1);p++)for(q=p+1;*q;q++)if(*q<*p){t=*p;*p=*q;*q=t;}printf("The result string:\n");puts(str);}习题九(P222) (注:无答案的题不在本课程讲授范围内)一.单选题1.D2.A3.B4.D 8.B 9.B 10.C二.填空题1. 结构体成员结构体指针指向2. 343. 224.ex三.阅读程序题1.92.10,x3.134. 46 40 415. 06. 3839。

课后题答案-C语言程序设计(第2版)

课后题答案-C语言程序设计(第2版)

《C语言程序设计能力教程(第二版)》课后作业及实训题参考答案第1章进入C语言程序世界二、1. I love China!printf("we are students.\n")2. 6项目实训题参考答案1.编写一个C程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *I am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf("********************\n");printf(" I am a student!\n ");printf("********************\n");}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。

解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf("v=%d",v);}本程序运行结果为:v=3000第2章编制C程序的基础知识一选择题C B A B A C C二操作题,2,-8,23.000000,2.500000,-8.0000002. ABC DEFGHwhy is 21+35 equal 523.3 14 32 31 24. aa bb cc abcA N项目实训题1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。

#define M 5main(){ int n,c;n=2; c=M*n;printf("%d\n",c); }2.编程求下面算术表达式的值。

(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。

C语言程序设计(第二版)习题参考答案

C语言程序设计(第二版)习题参考答案

printf("%d*%d=%d\n",a,a,z);
}
*3.仿照例 1.3 编程,输入两个数后,输出其中较小值。
解:#include<stdio.h>
float min(float x, float y)
{ float m;
if (x<y) m=x;
else m=y;
return m;
}
void main()
6.常用字符的 ASCII 码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、
小写英文字母。
解:1.F 2.T 3.T 4.T 5.T
6.T
二、单选题
1.在计算机中,最适合进行数值加减运算的数值编码是

A. 原码
B. 反码
C. 补码
D. 移码
2.已知英文小写字母 m 的 ASCII 码为十进制数 109,则英文小写字母 y 的 ASCII 码为
面积 s(s x)(s y)(s z)
其中 s
x yz 2
程序如下: #include <stdio.h>
#include <math.h>
void main()
{
double x,y,z,s,dime;
scanf("%lf%lf%lf",&x,&y,&z);
dime=sqrt(s*(s-x)*(s-y)*(s-z));
程序如下:
#include <stdio.h>
void main()
{
int a,b,c,s,z;
printf("Please input a b c:\n");

C语言程序设计答案——清华大学出版社(第二版)

C语言程序设计答案——清华大学出版社(第二版)

答案整理:林子雨 E‐mail:ziyulin@ 主页:/linziyu 第 4 页/共 8 页
厦门大学本科生公共课 《C 程序设计基础》 第二版教材习题答案 第八章 数组
}
6.输入一字符串,统计出某自定字符在字符串中出现的次数。 #include<stdio.h> #include<string.h> main() { char x,a[100],c=0; int i; puts("输入一串字符:"); gets(a); puts("查哪个字符个数?"); x=getchar(); for(i=0;i<strlen(a);i++) { if(a[i]==x) c++; } printf("共%d个。",c); }
厦门大学本科生公共课 《C 程序设计基础》 第二版教材习题答案 第八章 数组
【教材】 《C 语言程序设计(第 2 版) 》清华大学出版社,黄保和,江弋 编著。2011 年 10 月第 二版。ISBN:978‐7‐302‐26972‐4。售价:35 元。 【答案版本】本习题答案为 2012 年 2 月修订版本。 一 、选择题 1、以下数组定义中,错误的是:C)int a[3]={1,2,3,4}; A.int a[ ] = {1,2,3}; B. int a[5] = {1,2,3}; C. int a[3] = {1,2,3,4}; D. int a[5] , b; 2、以下数组定义中,正确的是:B) int a[][2]={1,2,3,4}; A. int n = 4, a[n] = {1,2,3,4}; B. int a[][2] = {1,2,3,4}; C. int a[2][] = {1,2,3,4}; D. int a[][] = {{1,2},{3,4}}; 3、设有定义“int a[8][10];”,在VC中一个整数占用4字节,设a的起始地址为1000,则a[1][1] 的地址是:D)1044 A. 1000 B. 1004 C. 1036 D. 1044 4、已知有数组定义“int a[][3]={1,2,3,4,5,6,7,8,9};”,则a[1][2]的值是:C)6 A. 2 B. 5 C. 6 D. 8 5、在以下字符串定义、初始化和赋值运算中,错误的是:A) char str[10];str="String"; A. char str[10];str="String"; B. char str[10] = "String"; C. char str[10] = {'S','t','r','i,'n','g'}; D. char str[ ] = {'S','t','r','i,'n','g',0}; 6、设有以下字符串定义, char s1[]={‘S’,’t’,’r’,’i’,’n’,’g’}; char s2[]=”String”; 则s1和s2: C)长度不同,但内容相同。 A. 长度相同,内容也相同 B. 长度不同,但内容相同 C. 长度不同,但内容相同 D. 长度不同,内容也不同 7、设有定义“int a[10]={0};”,则说法正确的是:A)数组a有10个元素,各元素的值为0. A. 数组a有10个元素,各元素的值为0 B. 数组a有10个元素,其中a[0]的值为0,其他元素的值不确定 C. 数组a有1个元素,其值为0 D. 数组初始化错误,初值个数少于数组元素个数 8、设已定义“char str[6]={‘a’,’b’,’\0’,’c’,’d’,’\0’};”,执行语句“printf(“%s”, str)”后,输出结果为:B)ab A. a B. ab C. abcd D. ab\0cd\0 9、引用数组元素时,数组元素下标不可以是:C)字符串 A.字符常量 B.整型常量 C.字符串 D.算术表达式 10、已定义字符串S1和S2,以下错误的输入语句是:C)gets(s1,s2); A. scanf("%s%s", s1, s2); B. scanf("%s%s", &s1, &s2); C. gets( s1, s2); D. gets(s1); gets(s2);

c语言课后答案

c语言课后答案

《C程序设计》课外作业及参考答案说明:(1)章节顺序按照PowerPoint电子教案;(2)教材上的作业注明了章节和题号;(3)所用教材是《C程序设计(第二版)》谭浩强清华大学出版社;第一章C语言概述1.1 简答题:写出C语言的主要特点(教材习题1.1)。

1.2填空题:1.C语言与操作系统并称“栾生兄弟”。

2.C语言俗称为形式。

3.C源程序文件经过处理后产生目标文件,目标文件经处理后生成可执行文件。

1.3 单项选择题1. 以下说法中正确的是。

(A)C语言程序总是从第一个定义的函数开始执行(B)在C语言程序中,要调用的函数必须在main函数中定义(C)C语言程序总是main函数开始(D)C语言程序中的main函数必须放在程序的开始部分2. 以下正确的C标识符是。

(A)_125 (B)C# (C)C++ (D)A$1.4 多项选择题1. 以下关于C语言的说法中,错误的是。

(A)俗称为“低级语言的高级形式”,因此功能简单(B)不是结构化程序设计语言(C)可能直接访问内存(D)语法限制不严2. 以下标识符属于C语言关键字的有。

(A)integer (B)else (C)include (D)while第二章C语言基本数据类型、变量、常量与表达式2.1简答题1.求下面算术表达式的值。

(教材习题3.9)a)x+a%3*(int)(x+y)%2/4设x=2.5, a=7, y=4.7b)(float)(a+b)/2+(int)x%(int)y设a=2, b=3, x=3.5, y=2.52.写出下面程序的运行结果。

(教材习题3.10)main(){ int i, j, m, n;i=8; j=10; m=++i; n=j++;printf("%d,%d,%d,%d", i, j, m, n);}3.写出下面表达式运算后a的值,设原来a=12。

设a和n已定义为整型变量。

(教材习题3.12)a)a+=a b) a-=2 c) a*=2+3b)d) a/=a+a e) a%=(n%=2), n的值等于5 f) a+=a-=a*=a4.C语言中如何表示“真”和“假”?系统如何判断一个量的“真”和“假”?2.2 填空题1. 写出下面各逻辑表达式的值。

c语言程序设计现代方法(第二版)习题答案

c语言程序设计现代方法(第二版)习题答案

Chapter 2Answers to Selected Exercises2. [was #2] (a) The program contains one directive (#include) and four statements (three calls of printf and one return).(b)Parkinson's Law:Work expands so as to fill the timeavailable for its completion.3. [was #4]#include <stdio.h>int main(void){int height = 8, length = 12, width = 10, volume;volume = height * length * width;printf("Dimensions: %dx%dx%d\n", length, width, height);printf("Volume (cubic inches): %d\n", volume);printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166);return 0;}4. [was #6] Here's one possible program:#include <stdio.h>int main(void){int i, j, k;float x, y, z;printf("Value of i: %d\n", i);printf("Value of j: %d\n", j);printf("Value of k: %d\n", k);printf("Value of x: %g\n", x);printf("Value of y: %g\n", y);printf("Value of z: %g\n", z);return 0;}When compiled using GCC and then executed, this program produced the following output:Value of i: 5618848Value of j: 0Value of k: 6844404Value of x: 3.98979e-34Value of y: 9.59105e-39Value of z: 9.59105e-39The values printed depend on many factors, so the chance that you'll get exactly these numbers is small.5. [was #10] (a) is not legal because 100_bottles begins with a digit.8. [was #12] There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.Answers to Selected Programming Projects4. [was #8; modified]#include <stdio.h>int main(void){float original_amount, amount_with_tax;printf("Enter an amount: ");scanf("%f", &original_amount);amount_with_tax = original_amount * 1.05f;printf("With tax added: $%.2f\n", amount_with_tax);return 0;}The amount_with_tax variable is unnecessary. If we remove it, the program is slightly shorter:#include <stdio.h>int main(void){float original_amount;printf("Enter an amount: ");scanf("%f", &original_amount);printf("With tax added: $%.2f\n", original_amount * 1.05f);return 0;}Chapter 3Answers to Selected Exercises2. [was #2](a) printf("%-8.1e", x);(b) printf("%10.6e", x);(c) printf("%-8.3f", x);(d) printf("%6.0f", x);5.[was #8] The values of x, i, and y will be 12.3, 45, and .6, respectively. Answers to Selected Programming Projects1. [was #4; modified]#include <stdio.h>int main(void){int month, day, year;printf("Enter a date (mm/dd/yyyy): ");scanf("%d/%d/%d", &month, &day, &year);printf("You entered the date %d%.2d%.2d\n", year, month, day);return 0;}3. [was #6; modified]#include <stdio.h>int main(void){int prefix, group, publisher, item, check_digit;printf("Enter ISBN: ");scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item,&check_digit);printf("GS1 prefix: %d\n", prefix);printf("Group identifier: %d\n", group);printf("Publisher code: %d\n", publisher);printf("Item number: %d\n", item);printf("Check digit: %d\n", check_digit);/* The five printf calls can be combined as follows:printf("GS1 prefix: %d\nGroup identifier: %d\nPublishercode: %d\nItem number: %d\nCheck digit: %d\n",prefix, group, publisher, item, check_digit);*/return 0;}Chapter 4Answers to Selected Exercises2.[was #2] Not in C89. Suppose that i is 9 and j is 7. The value of (-i)/j could be either –1 or –2, depending on the implementation. On the other hand, the value of -(i/j) is always –1, regardless of the implementation. In C99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).9. [was #6](a) 63 8(b) 3 2 1(c) 2 -1 3(d) 0 0 013. [was #8] The expression ++i is equivalent to (i += 1). The value of both expressions is i after the increment has been performed.Answers to Selected Programming Projects2. [was #4]#include <stdio.h>int main(void){int n;printf("Enter a three-digit number: ");scanf("%d", &n);printf("The reversal is: %d%d%d\n", n % 10, (n / 10) % 10, n / 100);return 0;}Chapter 5Answers to Selected Exercises2. [was #2](a) 1(b) 1(c) 1(d) 14. [was #4] (i > j) - (i < j)6. [was #12] Yes, the statement is legal. When n is equal to 5, it does nothing, since 5 is not equal to –9.10. [was #16] The output isonetwosince there are no break statements after the cases.Answers to Selected Programming Projects2. [was #6]#include <stdio.h>int main(void){int hours, minutes;printf("Enter a 24-hour time: ");scanf("%d:%d", &hours, &minutes);printf("Equivalent 12-hour time: ");if (hours == 0)printf("12:%.2d AM\n", minutes);else if (hours < 12)printf("%d:%.2d AM\n", hours, minutes);else if (hours == 12)printf("%d:%.2d PM\n", hours, minutes);elseprintf("%d:%.2d PM\n", hours - 12, minutes);return 0;}4. [was #8; modified]#include <stdio.h>int main(void){int speed;printf("Enter a wind speed in knots: ");scanf("%d", &speed);if (speed < 1)printf("Calm\n");else if (speed <= 3)printf("Light air\n");else if (speed <= 27)printf("Breeze\n");else if (speed <= 47)printf("Gale\n");else if (speed <= 63)printf("Storm\n");elseprintf("Hurricane\n");return 0;}6. [was #10]#include <stdio.h>int main(void){int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total;printf("Enter the first (single) digit: ");scanf("%1d", &d);printf("Enter first group of five digits: ");scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);printf("Enter second group of five digits: ");scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5);printf("Enter the last (single) digit: ");scanf("%1d", &check_digit);first_sum = d + i2 + i4 + j1 + j3 + j5;second_sum = i1 + i3 + i5 + j2 + j4;total = 3 * first_sum + second_sum;if (check_digit == 9 - ((total - 1) % 10))printf("VALID\n");elseprintf("NOT VALID\n");return 0;}10. [was #14]#include <stdio.h>int main(void){int grade;printf("Enter numerical grade: ");scanf("%d", &grade);if (grade < 0 || grade > 100) {printf("Illegal grade\n");return 0;}switch (grade / 10) {case 10:case 9: printf("Letter grade: A\n");break;case 8: printf("Letter grade: B\n");break;case 7: printf("Letter grade: C\n");break;case 6: printf("Letter grade: D\n");break;case 5:case 4:case 3:case 2:case 1:case 0: printf("Letter grade: F\n");break;}return 0;}Chapter 6Answers to Selected Exercises4.[was #10] (c) is not equivalent to (a) and (b), because i is incremented before the loop body is executed.10. [was #12] Consider the following while loop:while (…) {…continue;…}The equivalent code using goto would have the following appearance:while (…) {…goto loop_end;…loop_end: ; /* null statement */}12. [was #14]for (d = 2; d * d <= n; d++)if (n % d == 0)break;The if statement that follows the loop will need to be modified as well:if (d * d <= n)printf("%d is divisible by %d\n", n, d);elseprintf("%d is prime\n", n);14. [was #16] The problem is the semicolon at the end of the first line. If we remove it, the statement is now correct:if (n % 2 == 0)printf("n is even\n");Answers to Selected Programming Projects2. [was #2]#include <stdio.h>int main(void){int m, n, remainder;printf("Enter two integers: ");scanf("%d%d", &m, &n);while (n != 0) {remainder = m % n;m = n;n = remainder;}printf("Greatest common divisor: %d\n", m);return 0;}4. [was #4]#include <stdio.h>int main(void){float commission, value;printf("Enter value of trade: ");scanf("%f", &value);while (value != 0.0f) {if (value < 2500.00f)commission = 30.00f + .017f * value;else if (value < 6250.00f)commission = 56.00f + .0066f * value;else if (value < 20000.00f)commission = 76.00f + .0034f * value;else if (value < 50000.00f)commission = 100.00f + .0022f * value;else if (value < 500000.00f)commission = 155.00f + .0011f * value;elsecommission = 255.00f + .0009f * value;if (commission < 39.00f)commission = 39.00f;printf("Commission: $%.2f\n\n", commission);printf("Enter value of trade: ");scanf("%f", &value);}return 0;}6. [was #6]#include <stdio.h>int main(void){int i, n;printf("Enter limit on maximum square: ");scanf("%d", &n);for (i = 2; i * i <= n; i += 2)printf("%d\n", i * i);return 0;}8. [was #8]#include <stdio.h>int main(void){int i, n, start_day;printf("Enter number of days in month: ");scanf("%d", &n);printf("Enter starting day of the week (1=Sun, 7=Sat): "); scanf("%d", &start_day);/* print any leading "blank dates" */for (i = 1; i < start_day; i++)printf(" ");/* now print the calendar */for (i = 1; i <= n; i++) {printf("%3d", i);if ((start_day + i - 1) % 7 == 0)printf("\n");}return 0;}Chapter 7Answers to Selected Exercises3. [was #4] (b) is not legal.4.[was #6] (d) is illegal, since printf requires a string, not a character, as its first argument.10.[was #14] unsigned int, because the (int) cast applies only to j, not j * k.12. [was #16] The value of i is converted to float and added to f, then the result is converted to double and stored in d.14. [was #18] No. Converting f to int will fail if the value stored inf exceeds the largest value of type int.Answers to Selected Programming Projects1.[was #2] short int values are usually stored in 16 bits, causing failure at 182. int and long int values are usually stored in 32 bits, with failure occurring at 46341.2. [was #8]#include <stdio.h>int main(void){int i, n;char ch;printf("This program prints a table of squares.\n");printf("Enter number of entries in table: ");scanf("%d", &n);ch = getchar();/* dispose of new-line character following number of entries *//* could simply be getchar(); */for (i = 1; i <= n; i++) {printf("%10d%10d\n", i, i * i);if (i % 24 == 0) {printf("Press Enter to continue...");ch = getchar(); /* or simply getchar(); */}}return 0;}5. [was #10]#include <ctype.h>#include <stdio.h>int main(void){int sum = 0;char ch;printf("Enter a word: ");while ((ch = getchar()) != '\n')switch (toupper(ch)) {case 'D': case 'G':sum += 2; break;case 'B': case 'C': case 'M': case 'P':sum += 3; break;case 'F': case 'H': case 'V': case 'W': case 'Y': sum += 4; break;case 'K':sum += 5; break;case 'J': case 'X':sum += 8; break;case 'Q': case 'Z':sum += 10; break;default:sum++; break;}printf("Scrabble value: %d\n", sum);return 0;}6. [was #12]#include <stdio.h>int main(void){printf("Size of int: %d\n", (int) sizeof(int));printf("Size of short: %d\n", (int) sizeof(short));printf("Size of long: %d\n", (int) sizeof(long));printf("Size of float: %d\n", (int) sizeof(float));printf("Size of double: %d\n", (int) sizeof(double));printf("Size of long double: %d\n", (int) sizeof(long double));return 0;}Since the type of a sizeof expression may vary from one implementation to another, it's necessary in C89 to cast sizeof expressions to a known type before printing them. The sizes of the basic types are small numbers, so it's safe to cast them to int. (In general, however, it's best to cast sizeof expressions to unsigned long and print them using %lu.) In C99, we can avoid the cast by using the %zu conversion specification.Chapter 8Answers to Selected Exercises1.[was #4] The problem with sizeof(a) / sizeof(t) is that it can't easily be checked for correctness by someone reading the program. (The reader would have to locate the declaration of a and make sure that its elements have type t.)2. [was #8] To use a digit d (in character form) as a subscript into the array a, we would write a[d-'0']. This assumes that digits have consecutive codes in the underlying character set, which is true of ASCII and other popular character sets.7. [was #10]const int segments[10][7] = {{1, 1, 1, 1, 1, 1},{0, 1, 1},{1, 1, 0, 1, 1, 0, 1},{1, 1, 1, 1, 0, 0, 1},{0, 1, 1, 0, 0, 1, 1},{1, 0, 1, 1, 0, 1, 1},{1, 0, 1, 1, 1, 1, 1},{1, 1, 1},{1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 1, 0, 1, 1}};Answers to Selected Programming Projects2. [was #2]#include <stdio.h>int main(void){int digit_count[10] = {0};int digit;long n;printf("Enter a number: ");scanf("%ld", &n);while (n > 0) {digit = n % 10;digit_count[digit]++;n /= 10;}printf ("Digit: ");for (digit = 0; digit <= 9; digit++)printf("%3d", digit);printf("\nOccurrences:");for (digit = 0; digit <= 9; digit++)printf("%3d", digit_count[digit]);printf("\n");return 0;}5. [was #6]#include <stdio.h>#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0]))) #define INITIAL_BALANCE 100.00int main(void){int i, low_rate, month, num_years, year;double value[5];printf("Enter interest rate: ");scanf("%d", &low_rate);printf("Enter number of years: ");scanf("%d", &num_years);printf("\nYears");for (i = 0; i < NUM_RATES; i++) {printf("%6d%%", low_rate + i);value[i] = INITIAL_BALANCE;}printf("\n");for (year = 1; year <= num_years; year++) {printf("%3d ", year);for (i = 0; i < NUM_RATES; i++) {for (month = 1; month <= 12; month++)value[i] += ((double) (low_rate + i) / 12) / 100.0 * value[i]; printf("%7.2f", value[i]);}printf("\n");}return 0;}8. [was #12]#include <stdio.h>#define NUM_QUIZZES 5#define NUM_STUDENTS 5int main(void){int grades[NUM_STUDENTS][NUM_QUIZZES];int high, low, quiz, student, total;for (student = 0; student < NUM_STUDENTS; student++) {printf("Enter grades for student %d: ", student + 1);for (quiz = 0; quiz < NUM_QUIZZES; quiz++)scanf("%d", &grades[student][quiz]);}printf("\nStudent Total Average\n");for (student = 0; student < NUM_STUDENTS; student++) {printf("%4d ", student + 1);total = 0;for (quiz = 0; quiz < NUM_QUIZZES; quiz++)total += grades[student][quiz];printf("%3d %3d\n", total, total / NUM_QUIZZES);}printf("\nQuiz Average High Low\n");for (quiz = 0; quiz < NUM_QUIZZES; quiz++) {printf("%3d ", quiz + 1);total = 0;high = 0;low = 100;for (student = 0; student < NUM_STUDENTS; student++) {total += grades[student][quiz];if (grades[student][quiz] > high)high = grades[student][quiz];if (grades[student][quiz] < low)low = grades[student][quiz];}printf("%3d %3d %3d\n", total / NUM_STUDENTS, high, low); }return 0;}Chapter 9Answers to Selected Exercises2. [was #2]int check(int x, int y, int n){return (x >= 0 && x <= n - 1 && y >= 0 && y <= n - 1);}4. [was #4]int day_of_year(int month, int day, int year){int num_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day_count = 0, i;for (i = 1; i < month; i++)day_count += num_days[i-1];/* adjust for leap years, assuming they are divisible by 4 */if (year % 4 == 0 && month > 2)day_count++;return day_count + day;}Using the expression year % 4 == 0 to test for leap years is not completely correct. Centuries are special cases: if a year is a multiple of 100, then it must also be a multiple of 400 in order to be a leap year. The correct test isyear % 4 == 0 && (year % 100 != 0 || year % 400 == 0)6. [was #6; modified]int digit(int n, int k){int i;for (i = 1; i < k; i++)n /= 10;return n % 10;}8. [was #8] (a) and (b) are valid prototypes. (c) is illegal, since it doesn't specify the type of the parameter. (d) incorrectly specifies that f returns an int value in C89; in C99, omitting the return type is illegal.10. [was #10](a)int largest(int a[], int n){int i, max = a[0];for (i = 1; i < n; i++)if (a[i] > max)max = a[i];return max;}(b)int average(int a[], int n){int i, avg = 0;for (i = 0; i < n; i++)avg += a[i];return avg / n;}(c)int num_positive(int a[], int n){int i, count = 0;for (i = 0; i < n; i++)if (a[i] > 0)count++;return count;}15. [was #12; modified]double median(double x, double y, double z) {double result;if (x <= y)if (y <= z) result = y;else if (x <= z) result = z;else result = x;else {if (z <= y) result = y;else if (x <= z) result = x;else result = z;}return result;}17. [was #14]int fact(int n){int i, result = 1;for (i = 2; i <= n; i++)result *= i;return result;}19. [was #16] The following program tests the pb function:#include <stdio.h>void pb(int n);int main(void){int n;printf("Enter a number: ");scanf("%d", &n);printf("Output of pb: ");pb(n);printf("\n");return 0;}void pb(int n){if (n != 0) {pb(n / 2);putchar('0' + n % 2);}}pb prints the binary representation of the argument n, assuming that n is greater than 0. (We also assume that digits have consecutive codes in the underlying character set.) For example:Enter a number: 53Output of pb: 110101A trace of pb's execution would look like this:pb(53) finds that 53 is not equal to 0, so it callspb(26), which finds that 26 is not equal to 0, so it calls pb(13), which finds that 13 is not equal to 0, so it calls pb(6), which finds that 6 is not equal to 0, so it callspb(3), which finds that 3 is not equal to 0, so it callspb(1), which finds that 1 is not equal to 0, so it callspb(0), which finds that 0 is equal to 0, so it returns, causingpb(1) to print 1 and return, causingpb(3) to print 1 and return, causingpb(6) to print 0 and return, causingpb(13) to print 1 and return, causingpb(26) to print 0 and return, causingpb(53) to print 1 and return.Chapter 10Answers to Selected Exercises1. [was #2] (a) a, b, and c are visible.(b) a, and d are visible.(c) a, d, and e are visible.(d) a and f are visible.Answers to Selected Programming Projects3. [was #4]#include <stdbool.h> /* C99 only */#include <stdio.h>#include <stdlib.h>#define NUM_CARDS 5#define RANK 0#define SUIT 1/* external variables */int hand[NUM_CARDS][2];/* 0 1____ ____0 |____|____|1 |____|____|2 |____|____|3 |____|____|4 |____|____|rank suit*/bool straight, flush, four, three;int pairs; /* can be 0, 1, or 2 *//* prototypes */void read_cards(void);void analyze_hand(void);void print_result(void);/********************************************************** * main: Calls read_cards, analyze_hand, and print_result * * repeatedly. * **********************************************************/ int main(void){for (;;) {read_cards();analyze_hand();print_result();}}/********************************************************** * read_cards: Reads the cards into the external variable * * hand; checks for bad cards and duplicate * * cards. * **********************************************************/ void read_cards(void){char ch, rank_ch, suit_ch;int i, rank, suit;bool bad_card, duplicate_card;int cards_read = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");rank_ch = getchar();switch (rank_ch) {case '0': exit(EXIT_SUCCESS);case '2': rank = 0; break;case '3': rank = 1; break;case '4': rank = 2; break;case '5': rank = 3; break;case '6': rank = 4; break;case '7': rank = 5; break;case '8': rank = 6; break;case '9': rank = 7; break;case 't': case 'T': rank = 8; break;case 'j': case 'J': rank = 9; break;case 'q': case 'Q': rank = 10; break;case 'k': case 'K': rank = 11; break;case 'a': case 'A': rank = 12; break;default: bad_card = true;}suit_ch = getchar();switch (suit_ch) {case 'c': case 'C': suit = 0; break;case 'd': case 'D': suit = 1; break;case 'h': case 'H': suit = 2; break;case 's': case 'S': suit = 3; break;default: bad_card = true;}while ((ch = getchar()) != '\n')if (ch != ' ') bad_card = true;if (bad_card) {printf("Bad card; ignored.\n");continue;}duplicate_card = false;for (i = 0; i < cards_read; i++)if (hand[i][RANK] == rank && hand[i][SUIT] == suit) { printf("Duplicate card; ignored.\n");duplicate_card = true;break;}if (!duplicate_card) {hand[cards_read][RANK] = rank;hand[cards_read][SUIT] = suit;cards_read++;}}}/********************************************************** * analyze_hand: Determines whether the hand contains a * * straight, a flush, four-of-a-kind, * * and/or three-of-a-kind; determines the * * number of pairs; stores the results into * * the external variables straight, flush, * * four, three, and pairs. * **********************************************************/ void analyze_hand(void){int rank, suit, card, pass, run;straight = true;flush = true;four = false;three = false;pairs = 0;/* sort cards by rank */for (pass = 1; pass < NUM_CARDS; pass++)for (card = 0; card < NUM_CARDS - pass; card++) {rank = hand[card][RANK];suit = hand[card][SUIT];if (hand[card+1][RANK] < rank) {hand[card][RANK] = hand[card+1][RANK];hand[card][SUIT] = hand[card+1][SUIT];hand[card+1][RANK] = rank;hand[card+1][SUIT] = suit;}}/* check for flush */suit = hand[0][SUIT];for (card = 1; card < NUM_CARDS; card++)if (hand[card][SUIT] != suit)flush = false;/* check for straight */for (card = 0; card < NUM_CARDS - 1; card++)if (hand[card][RANK] + 1 != hand[card+1][RANK])straight = false;/* check for 4-of-a-kind, 3-of-a-kind, and pairs bylooking for "runs" of cards with identical ranks */card = 0;while (card < NUM_CARDS) {rank = hand[card][RANK];run = 0;do {run++;card++;} while (card < NUM_CARDS && hand[card][RANK] == rank); switch (run) {case 2: pairs++; break;case 3: three = true; break;case 4: four = true; break;}}}/********************************************************** * print_result: Prints the classification of the hand, * * based on the values of the external * * variables straight, flush, four, three, * * and pairs. * **********************************************************/ void print_result(void){if (straight && flush) printf("Straight flush");else if (four) printf("Four of a kind");else if (three &&pairs == 1) printf("Full house");else if (flush) printf("Flush");else if (straight) printf("Straight");else if (three) printf("Three of a kind");else if (pairs == 2) printf("Two pairs");else if (pairs == 1) printf("Pair");else printf("High card");printf("\n\n");}5. [was #6]#include <stdbool.h> /* C99 only */#include <stdio.h>#include <stdlib.h>#define NUM_RANKS 13#define NUM_SUITS 4#define NUM_CARDS 5/* external variables */int num_in_rank[NUM_RANKS];int num_in_suit[NUM_SUITS];bool straight, flush, four, three;int pairs; /* can be 0, 1, or 2 *//* prototypes */void read_cards(void);void analyze_hand(void);void print_result(void);/********************************************************** * main: Calls read_cards, analyze_hand, and print_result * * repeatedly. * **********************************************************/ int main(void){for (;;) {read_cards();analyze_hand();print_result();}}/********************************************************** * read_cards: Reads the cards into the external * * variables num_in_rank and num_in_suit; * * checks for bad cards and duplicate cards. * **********************************************************/void read_cards(void){bool card_exists[NUM_RANKS][NUM_SUITS];char ch, rank_ch, suit_ch;int rank, suit;bool bad_card;int cards_read = 0;for (rank = 0; rank < NUM_RANKS; rank++) { num_in_rank[rank] = 0;for (suit = 0; suit < NUM_SUITS; suit++) card_exists[rank][suit] = false;}for (suit = 0; suit < NUM_SUITS; suit++)num_in_suit[suit] = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");rank_ch = getchar();switch (rank_ch) {case '0': exit(EXIT_SUCCESS); case '2': rank = 0; break;case '3': rank = 1; break;case '4': rank = 2; break;case '5': rank = 3; break;case '6': rank = 4; break;case '7': rank = 5; break;case '8': rank = 6; break;case '9': rank = 7; break;case 't': case 'T': rank = 8; break;case 'j': case 'J': rank = 9; break;case 'q': case 'Q': rank = 10; break; case 'k': case 'K': rank = 11; break; case 'a': case 'A': rank = 12; break; default: bad_card = true;}suit_ch = getchar();switch (suit_ch) {case 'c': case 'C': suit = 0; break;。

c语言程序设计教程第二版课后习题答案

c语言程序设计教程第二版课后习题答案

c语言程序设计教程第二版课后习题答案【篇一:c语言程序设计教程_李含光_郑关胜_清华大学出版社习题答案习题答案[完美打印版]】1.单项选择题(1)a (2)c(3)d (4)c (5)b 2.填空题(1)函数(2)主函数(main)(3)printf() , scanf()第2章习题参考答案1.单项选择题1-5 cbccc 6-10 cdcdc 11-13 dbb 2.填空题(1)1(2)26 (3)6 , 4 , 2 (4)10 , 6(5)3.000000 (6)双精度(double)(7)9 (8)字母,数字,下划线(9)13.700000 (10)11(11)((m/10)%10)*100+(m/100)*10+m%10(12)0 (13)10 ,9 ,11(15)(x0y0)||(x0z0)||(y0||z0)(16)double (17)x==0(18)sqrt(fabs(a-b))/(3*(a+b))(19)sqrt((x*x+y*y)/(a+b))第3章习题参考答案1.单项选择题1-5 cccdd 6-10 bcdbc11-15 bcbbb16 a 2.填空题(1)用;表示结束(2){ }(3)y=x0?1:x==0?0:-1(4)y%4==0y%100!=0||y%400==0(5)上面未配对(6)default 标号(7)while , do while , for(8)do while(9)本次(10)本层 3.阅读程序,指出结果(1)yes(2)*(3)abother(4)28 70(5)2,0(6)8(7)36 (8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12(11)i=6,k=4 (12)1,-2 4.程序填空(1)x:y , u:z(2)m=n , m!=0,m=m/10(3)teps , t*n/(2*n+1) , printf(“%lf\n”,2*s) (4)m%5==0 ,printf(“%d\n”,k) (5)cx=getchar() , cx!=front , cx(6)double s=0, 1.0/k , %lf (7)s=0 , sgmin, 5.编程题(1). #include stdio.h int main() {double x,y; scanf(%lf,x); if(x1) y=x;else if(x=1.0x10) y=2*x-11; elsey=3*x-11;printf(%lf\n,y); return 0; } (2).#include stdio.h int main() {double x,y,z,min;scanf(%lf%lf%lf,x,y,z); if(xy) min=y; else min=x; if(minz)min=z;printf(min=%lf\n,min); return 0; } (3).#include stdio.h int main() {int y,m,d,flag,s=0,w,i;scanf(%d%d%d,y,m,d);flag=(y%4==0y%100!=0||y%400==0);w=((y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400)%7;for(i=1;i=m;i++) {switch(i) {case 1:s=d;break; case 2:s=31+d;break; case 3:s=59+d;break; case 4:s=90+d;break; case 5:s=120+d;break; case6:s=151+d;break; case 7:s=181+d;break; case8:s=212+d;break; case 9:s=243+d;break; case10:s=273+d;break; case 11:s=304+d;break; case12:s=334+d;break;} }s=(w+s)%7; if(s==0)printf(星期日\n); elseprintf(星期%d\n,s); return 0; }(4).#include stdio.h int main() {float p,r;scanf(%f,p); if(p=10) r=p*0.1;else if(p10p=20) r=10*0.1+(p-10)*0.075; else if(p20p=40)r=10*0.1+10*0.075+(p-20)*0.05; else if(p40p=60)r=10*0.1+10*0.075+20*0.05+(p-40)*0.03;else if(p60p=100)r=10*0.1+10*0.075+20*0.05+20*0.03+(p-60)*0.015; else if(p100)r=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(p-100)*0.01; printf(%f\n,r); return 0; } (5).#include stdio.h int main() {char c;while((c=getchar())!=\n) {if(c=ac=z) c=c-32; putchar(c);}return 0; } (6).#includestdio.h int main() {int m,k=2;printf(输入一个正整数:\n); scanf(%d,m); while(km) if(m%k==0) {printf(%4d,k); m=m/k; } else k++;printf(%4d\n,m); return 0; } (7).#includestdio.h int main() {int a,n,s=0,p=0,i;scanf(%d %d,n,a); for(i=1;i=n;i++) {p=p*10+a; s=s+p; }printf(%d\n,s); return 0; } (8).#includestdio.h int main(){int i,j,k;for(i=1;i=9;i++) for(j=0;j=9;j++) for(k=0;k=9;k++)printf(%5d,100*i+10*j+k); return 0; }(9).#includestdio.h #includemath.h int main() {float a=-10,b=10,x,f1,f2,f; f1=(((2*a-4)*a+3)*a)-6; f2=(((2*b-4)*b+3)*b)-6; do {x=(a+b)/2;f=(((2*x-4)*x+3)*x)-6; if(f*f10) { b=x; f2=f; } else { a=x;f1=f; }}while(fabs(f)=1e-6); printf(%6.2f\n,x); return 0; }(10).#includestdio.h#includemath.h int main() {int n=2;double eps,t,s=0,x;scanf(%lf %lf,x,eps); t=x; s=t;while(fabs(t)=eps) {t=-t*(2*n-3)*x*x/(2*n-2); s=s+t/(2*n); n++; }printf(%d,%lf\n,n,s); return 0; }(11).#includestdio.h int main() {unsigned long s,t=0,p=1; scanf(%u,s); while(s!=0) {if((s%10)%2!=0) {t=t+(s%10)*p; p=p*10; }s=s/10; }printf(%u\n,t); return 0; }第4章习题参考答案1.单项选择题1-5 dddbd 6-10 badcd 11-14 bdab 2.填空题(1)2(2)嵌套,递归(3)全局变量,局部变量,静态变量,动态变量(4)auto , static , register , extern (5)外部变量(6)编译,运行 3.阅读程序,指出结果(1)15(2)5(3)5,4,3 (4)i=5 i=2 i=2 i=4 i=2(5)求水仙花数(6)-5*5*5(7)30 (8)0 10 1 11 2 124.程序填空(1)float fun(float , float) , x+y,x-y, z+y,z-y (2)x , x*x+1 (3)s=0 , a=a+b 5.编程题(1).while(s!=0) #includestdio.h { unsigned int fun(unsigned int);p=p+s%10; int main() s=s/10; { } unsigned int s; return p; scanf(%u,s); } printf(%u\n,fun(s)); (2). return 0;#includestdio.h } #includestdlib.h unsigned int fun(unsignedint s) #includemath.h { void f1(float,float,float,float); unsigned int p=0; void f2(float,float,float,float);【篇二:《c语言程序设计》课后习题答案(第四版)谭浩强】t>1.1什么是计算机程序11.2什么是计算机语言11.3c语言的发展及其特点31.4最简单的c语言程序51.4.1最简单的c语言程序举例61.4.2c语言程序的结构101.5运行c程序的步骤与方法121.6程序设计的任务141-5 #include stdio.hint main ( ){ printf (**************************\n\n);printf( very good!\n\n);printf (**************************\n);return 0;}1-6#include stdio.hint main(){int a,b,c,max;printf(please input a,b,c:\n);scanf(%d,%d,%d,a,b,c);max=a;if (maxb)max=b;if (maxc)max=c;printf(the largest number is %d\n,max);return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用n?s流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的c程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3c语句573.3.1c语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include stdio.h#include math.hint main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf(p=%f\n,p);return 0;}3-2-1#include stdio.h#include math.hint main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5);// 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期 p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次 p5=p*pow(1+r0/4,4*5); // 存活期存款。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《C语言程序设计能力教程(第二版)》课后作业及实训题参考答案第1章进入C语言程序世界二、1. I love China!printf("we are students.\n")2. 6项目实训题参考答案1.编写一个C程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *I am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf("********************\n");printf(" I am a student!\n ");printf("********************\n");}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。

解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf("v=%d",v);}本程序运行结果为:v=3000第2章编制C程序的基础知识一选择题C B A B A C C二操作题,2,-8,23.000000,2.500000,-8.0000002. ABC DEFGHwhy is 21+35 equal 523.3 14 32 31 24. aa bb cc abcA N项目实训题1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。

#define M 5main(){ int n,c;n=2; c=M*n;printf("%d\n",c); }2.编程求下面算术表达式的值。

(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。

(1)main(){ int a=7;float x=2.5,y=4.7;printf("%f\n",x+a%3*(int)(x+y)%2/4); }(2)main(){ int a=2,b=3;float x=3.5,y=2.5;printf("%f\n",(float)(a+b)/2+(int)x%(int)y); }第三章顺序结构程序设计一选择题A C D C C二操作题1. x=3,a=2,b=32. z=12.7000003. 1 2 1a2 1 2三.编程题编程题1. 某工种按小时计算工资,每月劳动时间(小时)×每小时工资=总工资,总工资中扣除10%公积金,剩余的为应发工资。

编写一个程序从键盘输入劳动时间和每小时工资,打印出应发工资。

解:#include <stdio.h>main(){float sj,gz,yfgz;printf("time,salary:");scanf("%f,%f",&sj,&gz);yfgz=sj*gz*0.9;printf("total salary:%f\n",yfgz);}本程序运行结果为:time,salary:4,3<CR>total salary:10.8000002.编写一个程序求出任意一个输入字符的ASCII码解:#include <stdio.h>main(){char c;printf("Input a string:");scanf("%c",&c);printf("%c ASCII is %d\n",c,c);}本程序运行结果为:Input a string:a<CR>a ASCII is 973、编写一个程序用于水果店售货员算帐:已知苹果每斤2.50元,鸭梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求输入各类水果的重量,打印出应付解:main(){float p,y,x,j,ys,g,fk;printf("apple,pear,banana,orange(weight)=");scanf("%f,%f,%f,%f",&p,&y,&x,&j);ys=2.5*p+1.8*y+2*x+1.6*j;printf("fu kuan=");scanf("%f",&g);fk=g-ys;printf("result:\n");printf("fukuan=%6.2fyuan\nshoukuan=%6.2fyuan\nzhaohui=%6.2fyuan\n",g ,ys,fk);}本程序运行结果为:apple,pear,banana,orange(weight)=1,2,3,4fu kuan=100result:fukuan=100.00yuanshoukuan= 18.50yuanzhaohui= 81.50yuan项目实训1.假设银行定期存款的年利率rate为2.25%,并已知存款期为n年,存款本金为capital元,试编程计算n年后可得到本利之和deposit。

#include<math.h>main(){ int n;float rate=0.0225,capital,deposit;scanf("%d,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit); }2.将一个三位数整数,正确分离出它的个位、十位和百位数字,并分别在屏幕上输出。

main(){ int n,a,b,c;scanf("%3d",&n);a=n/100;b=n%100/10;c=n%100%10/1;printf("a=%d,b=%d,c=%d\n",a,b,c); }第四章选择结构程序设计一、略二、B B A B C B A三、1. 1 02. 2 3 2 23. 10 20 04. ch>=’A’&&ch<=’Z’||ch>=’a’&&ch<=’z’ch>=’0’&&ch<=’9’ch==’’5. -1四、上机操作1. 从键盘输入一个英文字母,如果是大写字母,则将它变为小写字母输出;如果是小写字母,则将其变为大写字母输出。

#include<stdio.h>main(){char ch;ch=getchar();if(ch>='A'&&ch<='Z') ch+=32;else if(ch>='a'&&ch<='z') ch-=32;putchar(ch);putchar('\n'); }2. 根据输入的x值依据下列表达式,计算y的值。

2x (x>-1)y = 3 (x=-1)4+x (x<-1)解:main(){float x,y;scanf("%f",&x);y=2*x;else if(x==1)y=3;else y=4+x;printf("y=%f",y);}本程序运行结果为:-2<CR>y=2.0000003.编写程序,输入一个整数,判断它是奇数还是偶数,若是奇数,输出“Is Odd“;若是偶数,输出“Is Even“。

main(){ int x;scanf("%d",&x);if(x%2==0) printf("Is Even\n");else printf("Is Odd\n"); }4.设计应用程序,求二次方程ax2+bx+c=0的解。

#include<math.h>main(){ float a,b,c,disc,x1,x2,p,q;scanf("%f,%f,%f",&a,&b,&c);if(fabs(a)<=1e-6) printf(" The equation is not a quadratic\n");else{ disc=b*b-4*a*c;if(fabs(disc)< 1e-6) printf("x1=x2=%8.4f\n",-b/(2*a));else if(disc>1e-6){x1=(-b+sqrt(disc)/(2*a));x2=(-b-sqrt(disc)/(2*a));printf("x1=%8.4f,x2=%8.4f\n",x1,x2); }else{ p=-b/(2*a);q=sqrt(-disc/(2*a));printf("%8.4f+%x8.4fi\n",p,q);printf("%8.4f-%8.4fi\n",p,q);} } }5.按托运规则,行李不超过50公斤时,运费为0.15元/公斤,如超过50公斤,超过部分的运费为0.22元/公斤,现有行李w公斤,编写一个程序计算运费。

解:#include <stdio.h>main(){float w,f,x;printf("weight:");scanf("%f",&w);if(w<=50)x=0.15*w;elsex=0.15*50+0.22*(w-50);printf("money:%6.2f yuan\n",x);}本程序运行结果为:weight:20<CR>money:3.00 yuanweight:60<CR>money:9.70 yuan6. 某商场给与顾客购物的折扣率如下:购物金额<200元不打折500元>购物金额>=200元 9折1000元>购物金额>=500元 8折购物金额>=1000元 7.5折输入一个购物金额,输出打折率、购物实际付款金额。

相关文档
最新文档