c程序设计语言第二版答案.docx
《c程序设计语言第二版答案.docx》由会员分享,可在线阅读,更多相关《c程序设计语言第二版答案.docx(21页珍藏版)》请在冰点文库上搜索。
c程序设计语言第二版答案
c程序设计语言第二版答案
【篇一:
c语言程序设计现代方法(第二版)习题答案】
answerstoselectedexercises
2.[was#2](a)theprogramcontainsonedirective(#include)andfourstatements(threecallsofprintfandonereturn).
(b)
parkinsonslaw:
workexpandssoastofillthetime
availableforitscompletion.
3.[was#4]
#includestdio.h
intmain(void)
{
intheight=8,length=12,width=10,volume;
volume=height*length*width;
printf(dimensions:
%dx%dx%d\n,length,width,height);
printf(volume(cubicinches):
%d\n,volume);
printf(dimensionalweight(pounds):
%d\n,(volume+165)/166);
return0;
}
4.[was#6]heresonepossibleprogram:
#includestdio.h
intmain(void)
{
inti,j,k;
floatx,y,z;
printf(valueofi:
%d\n,i);
printf(valueofj:
%d\n,j);
printf(valueofk:
%d\n,k);
printf(valueofx:
%g\n,x);
printf(valueofy:
%g\n,y);
printf(valueofz:
%g\n,z);
return0;
}
whencompiledusinggccandthenexecuted,thisprogramproducedthefollowingoutput:
valueofi:
5618848
valueofj:
0
valueofk:
6844404
valueofx:
3.98979e-34
valueofy:
9.59105e-39
valueofz:
9.59105e-39
thevaluesprinteddependonmanyfactors,sothechancethatyoullgetexactlythesenumbersissmall.
5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.
8.[was#12]thereare14tokens:
a,=,(,3,*,q,-,p,*,p,),/,3,and;.
answerstoselectedprogrammingprojects
4.[was#8;modified]
#includestdio.h
intmain(void)
{
floatoriginal_amount,amount_with_tax;
printf(enteranamount:
);
scanf(%f,original_amount);
amount_with_tax=original_amount*1.05f;
printf(withtaxadded:
$%.2f\n,amount_with_tax);
return0;
}
theamount_with_taxvariableisunnecessary.ifweremoveit,theprogramisslightlyshorter:
#includestdio.h
intmain(void)
{
floatoriginal_amount;
printf(enteranamount:
);
scanf(%f,original_amount);
printf(withtaxadded:
$%.2f\n,original_amount*1.05f);
return0;
}
chapter3
answerstoselectedexercises
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]thevaluesofx,i,andywillbe12.3,45,and.6,respectively.answerstoselectedprogrammingprojects
1.[was#4;modified]
#includestdio.h
intmain(void)
{
intmonth,day,year;
printf(enteradate(mm/dd/yyyy):
);
scanf(%d/%d/%d,month,day,year);
printf(youenteredthedate%d%.2d%.2d\n,year,month,day);
return0;
}
3.[was#6;modified]
#includestdio.h
intmain(void)
{
intprefix,group,publisher,item,check_digit;
printf(enterisbn:
);
scanf(%d-%d-%d-%d-%d,prefix,group,publisher,item,check_digit);
printf(gs1prefix:
%d\n,prefix);
printf(groupidentifier:
%d\n,group);
printf(publishercode:
%d\n,publisher);
printf(itemnumber:
%d\n,item);
printf(checkdigit:
%d\n,check_digit);
/*thefiveprintfcallscanbecombinedasfollows:
printf(gs1prefix:
%d\ngroupidentifier:
%d\npublishercode:
%d\nitemnumber:
%d\ncheckdigit:
%d\n,
prefix,group,publisher,item,check_digit);
*/
return0;
}
chapter4
answerstoselectedexercises
2.[was#2]notinc89.supposethatiis9andjis7.thevalueof(-i)/jcouldbeeither–1or–2,dependingontheimplementation.ontheotherhand,thevalueof-(i/j)isalways–1,regardlessoftheimplementation.inc99,ontheotherhand,thevalueof(-i)/jmustbeequaltothevalueof-(i/j).
9.[was#6]
(a)638
(b)321
(c)2-13
(d)000
13.[was#8]theexpression++iisequivalentto(i+=1).thevalueofbothexpressionsisiaftertheincrementhasbeenperformed.answerstoselectedprogrammingprojects
2.[was
#4]
#includestdio.h
intmain(void)
{
intn;
printf(enterathree-digitnumber:
);
scanf(%d,n);
printf(thereversalis:
%d%d%d\n,n%10,(n/10)%10,n/100);
return0;
}
chapter5
answerstoselectedexercises
2.[was#2]
(a)1
(b)1
(c)1
(d)1
4.[was#4](ij)-(ij)
6.[was#12]yes,thestatementislegal.whennisequalto5,itdoesnothing,since5isnotequalto–9.
10.[was#16]theoutputis
onetwo
sincetherearenobreakstatementsafterthecases.
answerstoselectedprogrammingprojects
2.[was#6]
【篇二:
c语言与程序设计-第2章课后习题参考答案】
txt>关键字(是)
注释
空白符
八进制常量(是)
三字符序列
字符串常量(是)
括号(是)
2.2c编译器可将下列每一个源字符串分解为哪些记号?
(不必考虑记号组合是否合法)
(1)x+++yx,++,+,y
(2)-0xabl-,0xabl
(3)2.89e+12l2.89e+12l
(4)string+\foo\string+\foo\
(5)x**2x,*,*,2
(6)x?
?
/x?
?
/
(7)a?
ba,?
b
(8)x--+=yx,--,+=,y
(9)intx=+10intx,=,+,10
(10)stringfoostring,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合法,longdouble长双精度浮点型
e20不合法,缺少尾数部分
0377ul合法,unsignedlong无符号长整型
\18不合法,存在非8进制位
\0xa不合法,不符合十六进制字符码表示规则\xhh
0x9cfu合法,unsignedint无符号整形
\45合法,char字符型
1.e-5合法,double双精度浮点型
\0合法,char字符型
3.f合法,float浮点型
34不合法,缺少转义符
合法,char字符型(p35,双引号作为字符常量时既可用图形符号也可用转义序列表示)
\a合法,char字符型
2.6以下的变量声明语句中有什么错误?
(1)inta;b=5;第一个分号改为逗号inta,b=5;
(2)doubelh;关键字错误doubleh;
(3)intx=2.3;类型错误floatx=2.3;
(4)constlongy;需要赋初值constlongy=0;
(5)floata=2.5*g;g未定义变量intg=1;floata=2.5*g;
(6)inta=b=2;b未定义变量inta=2,b=2;
2.7设变量说明为:
inta=1,b=2,c=3,d;
doublex=2.0;y=7.7;
请给出下列表达式的值。
(1)++a*b--4
(2)!
a+b/c0
(3)a==-b+ctrue
(4)d=a++,a*=b+1d为1,a为6
(5)d=y+=1/xy为8.2,d为8
(6)abx==yfalse
(7)x=(int)y/b++x为3.0
(8)a--?
++a:
++aa为1
(9)a+\xa+a108
(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下面代码的执行结果是什么?
chara=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设变量说明为:
unsignedshortx=1,y=2,z=4,mask=0xc3,w;
shortv;
请给出下列表达式的值。
(1)~xx0
(2)v=~x-2
(3)w=~x^x65535
(4)x|yx|z5
(5)w=y|z,(w3)+(w1)60
(6)w=x|yx|zy^maskx113
(7)v=-1,v=1-2
(8)v=~x|x-1
(9)w=x^~y65532
(10)x|y|z23
2.11写一个表达式,将整数k的高字节作为结果的低字节,整数p的低字节作为结果的高字节,拼成一个新的整数。
表达式为:
32位
k24|(k2558)8|(p25516)8|p24
16位
(k8)|(p8)
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)((unsignedshort)~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
intmain(void)
{
unsignedchardata,backup,t=0;
intparity=0;
data=getchar();
backup=data;
while(data)
{
t++;
data=(data-1);
}
data=backup|((parity7)^(t7));
printf(thedatais%#x\n,backup);
printf(parity-checkcodeis%#x\n,data);
return0;
}
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定义一个枚举类型enummonth,用来描述一年12个月:
一月(jan)、二月(feb)、……、十二月(dec),并编写一个程序,根据用户输入的年份,输出该年各月的英文名及天数。
#includestdio.h
enumyear{jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
intmain(void)
}
2.20设变量说明为:
floata;doubleb;charc;intx;试将下列表达式中隐含的类型转换改为用强制类型转换运算符转换的表达式。
(1)x=(int)(a-(float)(int)c+a)
(2)b*(double)x+(double)((int)c-(int)0)
(3)(x0)?
(double)a:
b
enumyaermonth;intyear_num,year_days=365;char*month_name[]={january,february,march,april,may,june,july,august,september,october,november,december};intmonth_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]);return0;
【篇三:
c语言程序设计学习指导第二版答案】
.c4.c5.d6.a7.b8.c9.a10.d11.b12.c13.a
14.d15.b16.b
第二章
一、
1.d2.a3.c4.d5.a6.b7.a8.b9.a10.d11.b12.c13.c
14.d15.c16.b17.d18.a19.c20.b21.b22.d23.a24.b25.b26.b
27.b
第三章
一、
1.d2.c3.a4.c5.a6.c7.c8.d9.a10.d11.a
13.c14.c15.a16.b17.a18.a19.c20.a21.b22.d23.b24.b25.d
26.d27.c28.c
第四章
一、
1.b2.c3.d4.d5.d6.c7.b8.d9.c10.d11.b12.a13.c
14.c15.b16.ab17.d
二、
1.doublefun(intm)
{doubley=0;
y=sin(m)*10;
return(y);
}
2.floatfun(floath)
{return(long)(h*100+0.5)/100.0;}
3.doublefun(doublem)
{floatn;
n=(5.0/9.0)*(m-32);
returnn;}
4.charfun(charc)
{c=c+32;
returnc;}
(146f)12.b
一、
1.d2.c3.c4.c5.a6.a7.c8.a9.b10.b11.a12.b13.c
14.c15.d16.a17.d18.a19.a20.a21.b22.d23.c24.b25.d
二、
1.intfun(intn)
{
intbw,sw,gw;
bw=n/100;sw=(n-bw*100)/10;gw=n%10;
if(n==bw*bw*bw+sw*sw*sw+gw*gw*gw)return1;
elsereturn0;}
2.floatfun(floatx)
{floaty;
if(x0x!
=-3.0)
y=x*x+x+6;
elseif(x=0x10.0x!
=2.0x!
=3.0)
y=x*x-5*x+6;
elsey=x*x-x-1;
returny;}
3.doubley(floatx)
{doublez;
if(x10)z=exp(x);
elseif(x-3)z=log(x+3);
elsez=sin(x)/(cos(x)+4);
return(z);}
4.intfun(intx)
{intk;