c语言程序设计科学出版社课后习题解答文档格式.docx
《c语言程序设计科学出版社课后习题解答文档格式.docx》由会员分享,可在线阅读,更多相关《c语言程序设计科学出版社课后习题解答文档格式.docx(20页珍藏版)》请在冰豆网上搜索。
![c语言程序设计科学出版社课后习题解答文档格式.docx](https://file1.bdocx.com/fileroot1/2023-1/24/58aa7841-4d0e-4382-bdd2-b3117b766bb0/58aa7841-4d0e-4382-bdd2-b3117b766bb01.gif)
c=a*b;
printf("
%d*%d=%d"
a,b,c);
第3章
(1)b
(2)b
(3)d
(4)a
(5)b
2.
(1)&
b
(2)l,s
3.printf(“x=%.2f,y=%.2f\n”,x,y);
intnum1,num2,num3,sum;
floataverage;
scanf("
%d%d%d"
num1,&
num2,&
num3);
sum=num1+num2+num3;
average=sum/3.0;
printf("
sum=%d,average=%.2f\n"
sum,average);
inthour,minute,second,total;
/*定义变量代表时、分、秒和总秒数*/
%d"
total);
hour=total/3600;
minute=total%3600/60;
second=total%3600%60;
%dhours:
%dminutes:
%dseconds\n"
hour,minute,second);
第4章
(1)a
(3)b
(4)b
2.0
(1)6
(2)4
(3)1
(4)1
intx,y;
x);
if(x>
-5&
&
x<
0)
y=5*x;
if(x==0)
y=-1;
0&
10)
y=2*x+1;
x=%d,y=%d\n"
x,y);
intscore,rank;
/*score表示成绩,rank表示级别*/
Pleaseinputscore:
"
);
score);
rank=score/10;
switch(rank)
{
case10:
case9:
成绩等级为:
A\n"
break;
case8:
B\n"
case7:
C\n"
case6:
D\n"
default:
E\n"
}
6.
voidmain()
intn;
Pleaseinputthedigit:
n);
switch(n)
case1:
Jan\n"
break;
case2:
Feb\n"
case3:
Mar\n"
case4:
Apr\n"
case5:
May\n"
Jun\n"
Jul\n"
Agu\n"
Sep\n"
Oct\n"
case11:
Nov\n"
case12:
Dec\n"
第5章
(4)d
2.7,1
3.y=-1
4.m=6
5.10,14
6.3
7.5
8.ABABCDCD
9.
#include<
main()
inti;
longintsum=0;
for(i=2;
i<
=200;
i+=2)
sum+=i;
2+4+6+...+200=%ld"
sum);
10.
longintn;
intsum=0;
Pleaseinputthenber:
%ld"
while(n!
=0)
sum+=n%10;
n/=10;
%d\n"
sum);
第6章
(1)d
(4)c
(5)a
2.11
3.3
4.5689
5.12,6
6.
(1)i--
(2)n
7.
(1)chark;
(2)i<
j
8.
floata[10];
floatsum=0,average;
for(i=0;
10;
i++)
a[%d]=?
i);
%f"
a[i]);
sum+=a[i];
average=sum/10;
averageis%.2f\n"
average);
#defineN10
inta[N],i,target,found;
N;
Pleaseinputanumber:
target);
i=0;
while(i<
N&
target!
=a[i])
i++;
found=i<
N?
i:
-1;
found);
#defineN80
charstr[N];
intlen=0,i=0;
Pleaseinputastring:
gets(str);
while(str[i++]!
=0)len++;
thelengthofstringis%d.\n"
len);
第7章
1.21
2.136
3.16
4.
(1)func
(2)m++
5.9
6.1,6,3,2,3
7.2,5,1,2,3,-2
intfun(inta,intb,intc);
inta,b,c,max;
Pleaseinputthreenumbers:
b,&
c);
max=fun(a,b,c);
Maxis%d.\n"
max);
intfun(inta,intb,intc)
intmax;
max=a>
b?
(a>
c?
a:
c):
(b>
b:
returnmax;
longintsum(intn);
n=?
Sum=%ld.\n"
sum(n));
longintsum(intn)
if(n==1)return1;
elsereturnsum(n-1)+n;
voidfun(n);
fun(n);
voidfun(n)
if(n==0)return;
else
fun(n/2);
%-2d"
n%2);
第8章
(2)d
(5)c
2.8
3.123456789
4.23456
5.345
6.123456
7.b[i]
8.bcdefgha
9.p=sum
string.h>
charstr[80];
char*p1,*p2;
gets(str);
p1=str;
p2=str+strlen(str)-1;
while(p1<
p2&
*p1++==*p2--);
puts(p1<
p2?
"
不是回文"
:
是回文"
);
第9章
a.#defineF(x)(x)*(x)*(x)
b.#defineF(x)(x)%4
c.#defineF(x,y)(x)*(y)<
100?
1:
0
a.4
b.4
c.#defineDOUBLE(x)2*(x)
3.d
4.-20
5.Nisundefined
6.7.5
7.y=6
#defineSWAP(a,b){inttemp;
temp=a;
a=b;
b=temp;
x=?
y=?
y);
SWAP(x,y)
x=%d,y=%d.\n"
第10章
structstudent{
intsno;
charsname[10];
charsex;
}stu1,stu2;
2.12
3.合法的有a,b,d
c.改成s.u.rectangle.length=25;
e.改成s.u.circle.radius=5;
f.改成s.u.circle.radius=5;
4.a,b,c,d(说明:
变量b有确定的值之后,b++是合法的。
)
5.改为
typedefstructproduct
charname[10];
floatprice;
}PRODUCT;
PRODUCTproducts[10];
(1)structemployee
(2)printemp(emp)
7.24398
structtime_struct
inthour;
intminute;
intsecond;
};
structtime_structtime;
Inputtime?
\n(Example18:
28:
38)\n"
%d:
time.hour,&
time.minute,&
time.second);
Timeis%d:
time.hour,time.minute,time.second);
}time;
voidenter_time();
voiddisplay_time();
enter_time();
display_time();
voidenter_time()
Enterthetime(example18:
38)?
voiddisplay_time()
%d.\n"
}\
#defineN3
structhotel
charname[31];
/*旅馆名称*/
charaddress[31];
/*旅馆地址*/
intgrade;
/*旅馆级别*/
floataverage_charge;
/*平均房价*/
intnumber;
/*房间数量*/
}h[N]={
{"
h1"
"
上海路"
5,500.00,80},
h2"
北京大街"
5,480.00,70},
h3"
南京大街"
3,300.50,100}
intgrade,i;
请输入级别(3-5)?
grade);
if(h[i].grade==grade)
名称:
%s\n地址:
%s\n级别:
%d\n平均房价%.2f\n房间数量:
h[i].name,h[i].address,h[i].grade,h[i].average_charge,h[i].number);
第11章
2.Basican
3.fgetc(fp)
4.“record.dat”,“w”
stdlib.h>
typedefstructEmployee
intid;
charname[20];
chargender[20];
intage;
charaddress[20];
}Employee;
intmain(void)
FILE*fp;
charanother,choice;
Employeeemp;
longintrecsize;
fp=fopen("
employee.dat"
rb+"
if(fp==NULL)
fp=fopen("
wb+"
Can'
tOpenFile"
exit(0);
recsize=sizeof(emp);
while
(1)
1.添加记录2.显示男员工3.退出\n"
Enteryourchoice(1-3):
fflush(stdin);
%c"
choice);
switch(choice)
case'
1'
:
fseek(fp,0,SEEK_END);
another='
Y'
;
while(another=='
||another=='
y'
输入信息(id姓名性别年龄住址):
\n"
%d%s%s%d
%s"
emp.id,&
emp.name,&
emp.gender,&
emp.age,&
emp.address);
fwrite(&
emp,recsize,1,fp);
是否继续(Y/N):
another=getchar();
case'
2'
学号\t姓名\t性别\t年龄\t住址\n"
rewind(fp);
while(fread(&
emp,recsize,1,fp)==1)
if(strcmp(emp.gender,"
男"
)==0)
%d\t%s\t%s\t%d\t%s\n"
emp.id,emp.name,emp.gender,emp.age,emp.address);
3'
fclose(fp);
#include"
stdio.h"
stdlib.h"
#defineM2
#definestustructstudent
stu
intnum;
floats1;
floats2;
floats3;
floatavg;
stust[M];
请输入5名同学生的成绩,按照学号,姓名,成绩1,成绩2,成绩3的顺序,中间用空格隔
开:
for(i=0;
M;
%d%s%f%f%f"
st[i].num,st[i].name,&
st[i].s1,&
st[i].s2,&
st[i].s3);
st[i].avg=(st[i].s1+st[i].s2+st[i].s3)/3;
}if((fp=fopen("
stud.rec"
wb"
))==NULL)
cannotopenfile\n"
if(fwrite(&
st[i],sizeof(stu),1,fp)!
=1)
filewriteerror\n"
rb"
num\tname\tscore1\tscore2\tscore3\taverage\n"
fread(&
st[i],sizeof(stu),1,fp);
%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n"
st[i].num,st[i].name,st[i].s1,st[i].s2,st[i].s3,st[i].avg);
第12章
a.2
c.11
d.4
e.-15
f.28
g.-36
程序设计分析:
先将整数x右移4位,将该整数机内码的第4到7位移至第0到3位,
然后与0x000f(0000000000001111)进行位与运算,所保留的低4位就是所要的结果
intx,y;
y=x>
>
4;
y=y&
0x000f;
4~7位的十六进制数:
y=%#x\n"
y);
4.
(1)p!
=NULL
(2)p=p->
next5.p=p->
next
6.略