当前位置:文档之家› c程序设计语言第二版答案

c程序设计语言第二版答案

c程序设计语言第二版答案

【篇一:c语言程序设计现代方法(第二版)习题答案】answers to selected exercises

2. [was #2] (a) the program contains one directive (#include) and four statements (three calls of printf and one return).

(b)

parkinsons law:

work expands so as to fill the time

available 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] heres 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: 5618848

value of j: 0

value of k: 6844404

value of x: 3.98979e-34

value of y: 9.59105e-39

value of z: 9.59105e-39

the values printed depend on many factors, so the chance that youll 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 projects

4. [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 3

answers to selected exercises

2. [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 projects

1. [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\npublisher

code: %d\nitem number: %d\ncheck digit: %d\n,

prefix, group, publisher, item, check_digit);

*/

return 0;

}

chapter 4

answers to selected exercises

2. [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 0

13. [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 projects

2. [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 5

answers to selected exercises

2. [was #2]

(a) 1

(b) 1

(c) 1

(d) 1

4. [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 is

onetwo

since there are no break statements after the cases.

answers to selected programming projects

2. [was #6]

【篇二:c语言与程序设计-第2章课后习题参考答案】txt>关键字(是)

注释

空白符

八进制常量(是)

三字符序列

字符串常量(是)

括号(是)

2.2 c编译器可将下列每一个源字符串分解为哪些记号?(不必考

虑记号组合是否合法)

(1) x+++y x, ++, +, y

(2) -0xabl -, 0xabl

(3) 2.89e+12l 2.89e+12l

(4) string+\foo\ string+ \foo\

(5) x**2 x, *, *, 2

(6) x??/ x??/

(7) a?ba, ?, b

(8) x--+=y x, --, +=, y

(9) intx=+10 intx, =, +, 10

(10) stringfoo string, foo

(这道题当时改的时候有几个小题改得有错误,注意!)

2.3 下列哪些不是标识符,为什么?

标识符由字母、数字和下划线组成,但首字符必须是字母或下划线。 4th不是,以数字开头;

sizeof 不是(标准c的关键字)

_limit 是

_is2是

xyshould 是

x*y不是,* 非法

o_no_o_no 是

temp-2 不是,- 非法

isnt不是,非法

enum 不是(标准c的关键字。注:关键字也称为保留字,是被系统赋予特定含义并有专门用途的标识符。关键字不能作为普通标识符,但可以作为宏名。所有预处理均发生在识别这些关键字之前。)

2.4 在下列表示中,哪些是合法常数,哪些是非法常数?对于合法

常数,指出其类型;对于非法常数,说明其错误原因。

2l 合法,long长整型

不合法,单引号组中的单引号前需要转义字符

.12 合法,double双精度浮点型

0x1ag 不合法,g不是16进制数中的符号,也不表示任何类型

33333 合法,int整形

a 合法,字符串常量

合法,字符串常量

0.l 合法,long double长双精度浮点型

e20 不合法,缺少尾数部分

0377ul 合法,unsigned long无符号长整型

\18 不合法,存在非8进制位

\0xa 不合法,不符合十六进制字符码表示规则\xhh

0x9cfu 合法,unsigned int无符号整形

\45 合法,char字符型

1.e-5 合法,double双精度浮点型

\0 合法,char字符型

3.f 合法,float浮点型

34 不合法,缺少转义符

合法,char字符型(p35,双引号作为字符常量时既可用图形符号也可用转义序列表示)

\a 合法,char字符型

2.6 以下的变量声明语句中有什么错误?

(1) int a; b = 5; 第一个分号改为逗号 int a, b=5;

(2) doubel h; 关键字错误double h;

(3) int x = 2.3; 类型错误float x = 2.3;

(4) const long y; 需要赋初值const long y = 0;

(5) float a = 2.5*g; g未定义变量 int g = 1; float a = 2.5*g;

(6) int a = b = 2; b未定义变量int a = 2, b = 2;

2.7 设变量说明为:

int a = 1, b = 2, c = 3, d;

double x = 2.0; y = 7.7;

请给出下列表达式的值。

(1) ++a*b-- 4

(2) !a+b/c 0

(3) a==-b+c true

(4) d=a++,a*=b+1d为1, a为6

(5) d=y+=1/x y为8.2, d为8

(6) abx==yfalse

(7) x=(int)y/b++x为3.0

(8) a--?++a:++aa为1

(9) a+\xa+a 108

(10) a=0,--a,a+=(a++)-a 表达式结果为-1,a的值为0

2.8 设i和j是int类型,a和b是double类型,下列表达式哪些是错误的,为什么?

(1) a==b==c 错误,c未定义且逻辑错误

(2) a^045 正确

(3) 7+i*--j/3 正确

(4) 39/-++i-+29%j正确

(5) a*++-b 错误,++需要左值

(6) a||b^i 错误,^号左侧类型为double

(7) i*j%a 错误,%右侧类型为double

(8) i/j2 正确

(9) a+=i+=1+2正确

(10) int(a+b) 正确,vc++下可运行

2.9 下面代码的执行结果是什么?

char a = 1, b = 2, c = 3;

printf(%d,%d,%d,%d\n,sizeof(c), sizeof(a), sizeof(c=a),

sizeof(a+b+7.7));

结果:1,4,1,8

2.10 设变量说明为:

unsigned short x = 1, y = 2, z = 4, mask = 0xc3, w;

short v;

请给出下列表达式的值。

(1) ~xx 0

(2) v=~x -2

(3) w=~x^x 65535

(4) x|yx|z 5

(5) w=y|z,(w3)+(w1) 60

(6) w=x|yx|zy^maskx 113

(7) v=-1,v=1-2

(8) v=~x|x -1

(9) w=x^~y 65532

(10) x|y|z2 3

2.11写一个表达式,将整数k的高字节作为结果的低字节,整数p 的低字节作为结果的高字节,拼成一个新的整数。

表达式为:32位

k 24 | (k 2558)8 | (p 25516)8 | p 24

16位

(k 8) | (p 8)

2.12 写一个表达式,将整数x向右循环移位n位。

表达式为:32位

x(32-(n%32)) | x(n%32)

16位

x(16-(n%16)) | x(n%16)

2.13 写一个表达式,将整数x从第p位开始的向右n位(p从右至左编号为0~15)翻转(即1变0,0变1),其余各位保持不变。

表达式为:

x^((~0)(p+1-n) ((unsigned short)~0)(16-p-1))

或:x^(~0(16-n)(p+1-n))

2.15 表达式v = (v-1)能实现将v最低位的1翻转。比如v=108,其二进制表示为01101100,则v = (v-1)的结果是01101000。用这一方法,可以实现快速统计v的二进制中1的位数,只要不停地翻转v 的二进制数的最低位的1,直到v等于0即可。请用该方法重写例

2-18。 #includestdio.h

int main(void)

{

unsigned char data, backup, t = 0;

int parity = 0;

data = getchar();

backup = data;

while(data)

{

t++;

data = (data -1);

}

data = backup | ((parity7)^(t7));

printf(the data is %#x\n, backup);

printf(parity-check code is %#x\n, data);

return 0;

}

2.16写一个表达式,其结果是a、b和c这3个数中最大的一个。表达式为:

ab?ac?a:c:bc?b:c 或

(ab)?(ac?a:c):(bc?b:c) 或

(ab)?((ac)?a:c):((bc)?b:c)

2.18 写一个表达式,如果整数a能被3整除且个位数字是5,则结

果为非0,否则为0。表达式为:

a%3 ? 0 : (a%10==5 ? 1 : 0)

2.19 定义一个枚举类型enum month,用来描述一年12个月:一

月(jan)、二月(feb)、……、十二月(dec),并编写一个程序,根据用户输入的年份,输出该年各月的英文名及天数。

#includestdio.h

enum year { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};

int main(void)

}

2.20 设变量说明为:float a; double b; char c; int x; 试将下列表

达式中隐含的类型转换改为用强制类型转换运算符转换的表达式。

(1) x = (int)(a - (float)(int)c + a)

(2) b * (double)x + (double)( (int)c - (int)0 )

(3) ( x 0 ) ? (double)a : b

enum yaer month; int year_num, year_days=365; char

*month_name[] ={january, february, march, april, may, june, july, august, september, october, november, december}; int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; scanf(%d, year_num); if(!(year_num%4) year_num%100

|| !(year_num%400)) month_days[1]=29, year_days=366;

printf(\n%d\n, year_days); for(month=jan; month = dec;

month++) printf(%s,%d\n,

month_name[month],month_days[month]); return 0;

【篇三:c语言程序设计学习指导第二版答案】

.c4.c5.d6.a7.b8.c9.a10.d 11.b 12.c 13.a

14.d 15.b 16.b

第二章

一、

1.d

2.a

3.c

4.d

5.a

6.b

7.a

8.b

9.a 10.d 11.b 12.c 13.c

14.d 15.c 16.b 17.d 18.a 19.c 20.b 21.b 22.d 23.a 24.b

25.b 26.b

27.b

第三章

一、

1.d

2.c

3.a

4.c

5.a

6.c

7.c

8.d

9.a 10.d 11.a

13.c 14.c 15.a 16.b 17.a 18.a 19.c 20.a 21.b 22.d 23.b 24.b

25.d

26.d 27.c 28.c

第四章

一、

1.b

2.c

3.d

4.d

5.d

6.c

7.b

8.d

9.c 10.d 11.b 12.a 13.c

14.c 15.b 16.ab 17.d

二、

1.double fun(int m)

{ double y=0;

y=sin(m)*10;

return(y);

}

2.float fun ( float h )

{return (long)( h * 100 + 0.5 )/ 100.0;}

3.double fun(double m)

{ float n;

n=(5.0/9.0)*(m-32);

return n; }

4. char fun(char c)

{c=c+32;

return c;}

(146f) 12.b

一、

1.d

2.c

3.c

4.c

5.a

6.a

7.c

8.a

9.b 10.b 11.a 12.b 13.c

14.c 15.d 16.a 17.d 18.a 19.a 20.a 21.b 22.d 23.c 24.b 25.d 二、

1.int fun(int n)

{

int bw,sw,gw;

bw=n/100;sw=(n-bw*100)/10;gw=n%10;

if(n==bw*bw*bw+sw*sw*sw+gw*gw*gw) return 1;

else return 0;}

2.float fun(float x)

{float y;

if (x0 x!=-3.0)

y=x*x+x+6;

else if(x=0 x10.0 x!=2.0 x!=3.0)

y=x*x-5*x+6;

else y=x*x-x-1;

return y;}

3.double y(float x)

{double z;

if(x10) z=exp(x);

else if(x-3) z=log(x+3);

else z=sin(x)/(cos(x)+4);

return(z);}

4.int fun(int x)

{ int k;

k=x*x;

if((k%10==x)||(k%100==x))

return 1;

else

return 0;}

第六章

一、

1.c

2.c

3.d

4.b

5.c

6.a

7.a

8.a

9.d 10.a 11.d 12.c 13.c

14.c 15.c 16.a 17.a 18.b 19.a 20.d 21.b 22.c 23.c 24.d 25.b

26.b

27.c 28.a

二、

1.位置 1:r!=0 【或】 0!=r 【或】 r

位置 2:r=m%n 【或】 r=m-m/n*n

位置 3:n

位置 4:gcd,lcm 【或】 n,lcm

2.位置 1:k=0

位置 2:n%10 【或】 n-n/10*10 【或】 n-10*(n/10)

位置 3:while(n0) 【或】 while(0n) 【或】 while(n!=0) 【或】

while(0!=n) 位置 4:printf(\n)

3.位置 1:x!=0 【或】 x

位置 2:else 【或】 else if(x%2==1) 【或】 else if(x%2!=0) 【或】if(x%2) 位置 3:scanf(%d,x)

位置 4:av2=s2/j

4.位置 1:n=0

位置 2:i=300 【或】 i300 【或】 300=i 【或】 300i

位置 3:i%7==0||i%17==0 【或】 !(i%7)||!(i%17)

【或】 !(i%17)||!(i%7) 【或】 !(i%7i%17)

【或】 i%17==0||i%7==0

位置 4:n%5==0 【或】 !(n%5) 【或】 n/5*5==n

5.位置 1:s=0

位置 2:i+=2 【或】 i=i+2 【或】 i=2+i 【或】 i++,i++

位置 3:j=i 【或】 i=j 【或】 ji+1 【或】 i+1j 【或】 j1+i 【或】

1+ij 位置 4:f=f*j 【或】 f=j*f

三、

1.位置 1:#include math.h 【或】 #include math.h

位置 2:float s=0,t=1,p=1; 【或】 float s=0,p=1,t=1; 【或】 float p=1,s=0,t=1; 【或】 float p=1,t=1,s=0; 【或】 float t=1,p=1,s=0; 【或】 float t=1,s=0,p=1;

位置 3:while(fabs(t)1e-4) 【或】 while(0.0001fabs(t)) 【或】

while(1e-4fabs(t))

【或】 while(fabs(t)0.0001)

位置 4:printf(pi=%f\n,s*4); 【或】 printf(pi=%f\n,4*s);

2.位置 1:printf(%8.0f,f1); 【或】 printf(%f,f1); 【或】

printf(%8f,f1);

位置 2:for(i=1;i20;i++) 【或】 for(i=1;20i;i++) 【或】

for(i=2;i=20;i++) 【或】 for(i=2;20=i;i++) 【或】 for(i=1;i=19;i++) 【或】 for(i=1;19=i;i++)

位置 3:f1=f2;

位置 4:f2=f3;

3.位置 1:long k=1;

位置 2: scanf(%ld,n);

位置 3: n/=10; 【或】 n=n/10;

4.位置 1:scanf(%d,n);

位置 2:for(i=1;i=n;i++) 【或】 for(i=1;n=i;i++) 【或】

for(i=1;in+1;i++) 【或】 for(i=1;n+1i;i++)

位置 3:s+=1.0/t; 【或】 s=s+1.0/(float)t; 【或】 s=1.0/(float)t+s; 【或】 s=s+1.0/t;

【或】 s=1.0/t+s; 【或】 s+=1.0/(float)t; 【或】 s+=1.0/(double)t; 【或】 s=s+1.0/(double)t; 【或】 s=1.0/(double)t+s;

5.位置 1:sum=1.0; 【或】 sum=1;

位置 2:s2=1.0; 【或】 s2=1;

位置 3:for(k=4;k=n;k++) 【或】 for(k=4;n=k;k++) 【或】for(k=4;kn+1;k++) 【或】 for(k=4;k1+n;k++) 【或】

for(k=4;n+1k;k++) 【或】 for(k=4;1+nk;k++)

6.位置 1:t=1; 【或】 t=1.0;

位置 2:t=t*j; 【或】 t=j*t; 【或】 t*=j;

位置 3:s=s+t; 【或】 s=t+s; 【或】 s+=t;

位置 4:printf(jiecheng=%f\n,s);

四、

1.int fun(int n)

{ int d,s=0;

while (n0)

{d=n%10;

s+=d*d*d;

n/=10;

}

return s;

}

2.int fun(int n)

{

int i,s=0;

for (i=2;in;i++)

if (n%i==0) s=s+i;

return (s);}

3.float sum(int n)

{

float s=0;

int i;

for(i=1;i=n;i=i+2)

s=s+i*i;

return(s);}

4.double fun(int n)

{

int i,s=0;

for (i=1;in;i++)

if (i%3==0 i%7==0) s=s+i;

return (sqrt(s));}

5.gcd(int n,int m)

{

int r,t;

if(nm) { t=n;n=m;m=t;} r=n%m;

while(r!=0)

{ n=m;m=r;r=n%m;}

return(m);}

6.double fun(int n)

{

int i,j=0;

double s=0;

for (i=3;i=n;i++)

{ for (j=2;ji;j++)

if (i%j==0) break;

if (j==i) s=s+sqrt(i);

}

return s;}

7.long fun (long s,long t) {

long sl=10;

t = s % 10;

while(s 0)

{ s = s/100;

t = s%10*sl + t;

sl = sl * 10;

}

}

8.double fun(int n)

{

double m=1.0;

int i;

double p=1.0;

for(i=1;i=n;i++)

{p=p*i;

m=m+1.0/p;

}

return (m);}

9.int fun(int n)

{int i,k;

for(i=n+1;;i++){

for(k=2;ki;k++)

if(i%k==0)

break;

if(k==i)

return(i);}

10.int fun(int s)

int x1=0,x2=1,m=0;

while (sm)

{

m=x1+x2;

x1=x2;

x2=m;

}

return m;}

第七章

一、

1.a

2.d

3.c

4.b

5.b

6.d

7.b

8.c

9.b 10.b 11.d 12.b 13.b

14.a 15.c 16.a 17.c 18.a 19.b 20.c 21.d 22.a 23.a 24.d 25.a

26.d

27.a 28.d 29.b 30.c 31.a 32.d 33.d 34.c 35.a 36.b 37.c 38.a

39.a

40.b 41.d 42.b 43.a 44.c 45.a

二、

1.位置 1: break;

位置2:i-1;

位置3:c;

相关主题
文本预览
相关文档 最新文档