曹计昌《C程序设计》课后习题答案解析Word文档下载推荐.docx
《曹计昌《C程序设计》课后习题答案解析Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《曹计昌《C程序设计》课后习题答案解析Word文档下载推荐.docx(57页珍藏版)》请在冰豆网上搜索。
(3)2.89e+12L
(4)”String+\”FOO\””
(5)x,*,*,2
(6)”X?
?
/”
(7)a,?
b
(8)x,--,+=,y
(9)intx,=,+,10
(10)”String”,“FOO”
2.3
不是表识符的如下:
4th首字母为数字
sizeof关键字
x*y*不是字母、数字、下划线
temp-2-不是字母、数字、下划线
isn’t
’不是字母、数字、下划线
enum关键字
2.4
合法常数:
.12
0.L
1.E-5
3.F
浮点型常量
2L
33333
0377UL0x9cfU
整型常量
“a”
“”
字符串常量
‘\45’
‘\0’
‘\a’
字符常量
非法常数:
‘‘’必须用转义序列
0x1ag十六进制没有g
E20没有尾数部分
‘\18’
要用八进制数
‘\0xa’格式错误,可以是’\xa’
“3’4””
需要转义序列
‘”’
2.5
(1)inta,b=5;
(2)doubleh;
(3)intx=2.3;
0.3会被截取。
(4)constlongy=1;
必须赋初值
(5)floata=2.5*g;
g没有定义。
(6)inta=b=2;
在turboC中编译出错:
未定义的符号’b’在main函数中。
2.6
(1)4
(2)0
(3)1
(4)6
(5)8
(6)0
(7)3.00
(8)1
(9)108
(10)0
2.7
答案不确定
(1)a==b==c
c未定义
(2)正确
(3)正确
(4)正确
(5)a*++-b
表达式缺值
(6)a||b^i
^运算的操作数必须是整型,而i不是
(7)i*j%a
%运算的操作数必须是整型,而a不是
(8)正确
(9)正确
(10)int(a+b)
应该改成(int)(a+b)
2.9
(1)0
(2)-2
(3)65535
(4)5
(5)60
(6)113
(7)-2
(8)-1
(9)65532
(10)3
2.10
unsignedlongencrypt(unsignedlongx)
{
unsignedlongx0,x1,x2,x3,x4,x5,x6,x7;
x0=(x&
0x0000000F)<
<
8;
x1=(x&
0x000000F0);
x2=(x&
0x00000F00)<
x3=(x&
0x0000F000);
x4=(x&
0x000F0000)<
x5=(x&
0x00F00000);
x6=(x&
0x0F000000)>
>
24;
x7=(x&
0xF0000000);
return(x0|x1|x2|x3|x4|x5|x6|x7);
}
2.11
#include<
stdio.h>
voidmain()
unsignedlongin;
unsignedlonga,b,c,d;
scanf("
%ld"
&
in);
//in=1563;
a=(in&
0xff000000)>
24;
b=(in&
0x00ff0000)>
16;
c=(in&
0x0000ff00)>
8;
d=in&
0x000000ff;
printf("
%d.%d.%d.%d"
a,b,c,d);
2.15
((k>
8)&
0xFF00)|((p&
0x00FF)<
8)
2.16
max=a>
b?
a>
c?
a:
c:
b>
b:
c;
max=a>
b?
((a>
c)?
a:
c):
((b>
b:
c);
2.17
X=y>
n
2.18
(c>
=’0’&
&
c<
=’9’)?
c–‘0’:
c
2.19
(a%3==0)&
(a%10==5)?
0;
第三章习题
3.1函数原型是指对函数的名称、返回值类型、参数的数目和参数类型的说明。
其规定了调用该函数的语法格式,即调用形式。
putchar函数的原型为:
intputchar(intc);
puts函数的原型为:
intputs(constchar*s);
printf函数的原型为:
intprintf(constchar*format,…);
getchar函数的原型为:
intgetchar_r(void);
gets函数的原型为:
char*gets_r(char*s);
scanf函数的原型为:
intscanf(constchar*format,…);
3.2不同点:
①puts为非格式输出函数,printf为格式输出函数;
②puts函数的参数类型和数目一定(一个字符串),printf函数的参数类型和数目不固定;
③puts函数输出后会自动换行,printf函数没有这一功能。
相同点:
①二者都向标准设备输出;
②二者返回值类型都为int。
3.3x1=-1,177777,ffff,65535
x2=-3,177775,fffd,65533
y1=123.456703,
123.457,123.457,123.457
(注意对齐)
y2=123.449997,1.23450e+02,123.45
x1(%4d)=
-1
3.4⑴%c;
⑵%c;
⑶%f;
⑷%f;
⑸%lu;
⑹%d;
⑺%d;
⑻%d;
⑼%f;
⑽%Lf
3.5⑴错误,运行提示为divideerror
⑵正确,结果为b
⑶正确,结果为
*
⑷正确
⑸正确,但无法正常从结果中退出
⑹正确
⑺正确,结果为82,63
⑻编译错误,提示cannotmodifyaconstobject
⑼正确
⑽正确
3.6-6.70000
-6
177601
123
-2
0
3.8
charc;
c=getchar_r();
if((c>
='
0'
c<
9'
)||(c>
A'
F'
a'
f'
))
{
printf("
%d\n"
c-'
);
}
elseif((c>
+10);
else
putchar(c);
3.9
shortnum,high,low;
Pleaseinputashortnumber:
\n"
scanf("
%hd"
num);
low=0x00ff&
num;
high=0x00ff&
(num>
8);
Thehighbyteis:
%c\n"
high);
Thelowbyteis:
low);
3.10
#include"
stdafx.h"
intmain(intargc,char*argv[])
unsignedshortintx;
unsignedshortinthigh,low;
inputainteger:
%d"
x);
high=(x>
12)&
0x000f;
low=(x<
0xf000;
x=x&
0x0ff0;
x=x|high|low;
x);
return0;
3.11
unsignedshortintx,m,n;
unsignedshortintresult;
%hu%hu%hu"
x,&
m,&
n);
result=(x>
(m-n+1))<
(15-n+1);
%hu\n"
result);
3.12
floatf,c;
%f"
f);
c=(5*(f-32))/9;
%.0f(F)=%.2f(C)\n"
f,c);
或者
intf;
floatc;
%d(F)=%.2f(C)\n"
3.13
#include<
#definePI(3.1415926)
doubler,h;
doubles,v;
Pleaseinputtherandh."
%lf,%lf"
&
r,&
h);
s=2*PI*r*h+2*PI*r*r;
v=PI*r*r*h;
sis%lf,vis%lf"
s,v);
3.14
chara[4]="
编"
;
机内码:
%x%x\t\n"
a[0]&
0xff,a[1]&
0xff);
区位码:
%x\t\n"
0xff<
8+a[1]&
0xff-0x2020-0x8080);
国际码:
0xff-0x8080);
第四章习题
4.1
voidmain(void)
floata,b,c;
PleaseenterthescoreofA:
a);
PleaseenterthescoreofB:
b);
PleaseenterthescoreofC:
c);
if((a-b)*(a-c)<
0)
Agetsthescore%.1f"
a);
if((b-a)*(b-c)<
Bgetsthescore%.1f"
b);
if((c-a)*(c-b)<
Cgetsthescore%.1f"
c);
4.3
intmdays(inty,intm)
if(m==2)return(y%4==0&
(y%100==0||y%400==0))?
29:
28;
elseif(m==4||m==6||m==9||m==11)return30;
elsereturn31;
main()
inty,m,d,days;
Enteryear:
"
y);
Entermonth:
m);
Enterday:
d);
days=d;
while(m>
1){days+=mdays(y,m-1);
m--;
days);
4.4
if方法:
floatx=0;
inputthesalary\n"
if(x<
wrong\n"
elseif(x>
0&
x<
1000)
0\n"
elseif(x<
2000)
%f\n"
x*0.05);
3000)
x*0.1);
4000)
x*0.15);
5000)
x*0.2);
x*0.25);
Case方法:
floatx;
intxCase=0;
xCase=(int)(x/1000.0);
switch(xCase)
case0:
break;
case1:
case2:
case3:
case4:
default:
4.7
char*sa;
inti=0,j=0,k=0;
do
sa[i++]=c;
}while(c!
='
\r'
for(i=0;
sa[i+1];
i++)
for(j=i+1;
sa[j];
j++)
if(sa[i]==sa[j]&
sa[j]=='
'
)
for(k=j;
sa[k];
k++)
sa[k]=sa[k+1];
j--;
for(k=0;
%2c"
sa[k]);
4.8
Inputthecharstobecopy:
c=getchar();
while(c!
=EOF)
if(c==32)
{
c=getchar();
continue;
}
else
putchar(c);
c=getchar();
4.10
#defineEPS1e-5
ints=1;
floatn=1.0,t=1.0,pi=0;
while(1.0/n>
=EPS)
pi=pi+t;
n=n+2;
s=s*(-1);
t=s/n;
pi=pi*4;
pi=%10.6f\n"
pi);
4.11
intmain()
inta,b,num1,num2,temp;
Inputa&
b:
%d%d"
num1,&
num2);
if(num1>
num2)
temp=num1;
num1=num2;
num2=