c语言程序设计科学出版社课后习题解答.docx
《c语言程序设计科学出版社课后习题解答.docx》由会员分享,可在线阅读,更多相关《c语言程序设计科学出版社课后习题解答.docx(20页珍藏版)》请在冰豆网上搜索。
c语言程序设计科学出版社课后习题解答
第3章习题解答
第1章
1.C语言程序主要由预处理命令、函数、注释等组成。
2.填空
(1)分号
(2)main
(3)stdio.h
3.
源程序:
#include
main()
{
printf(“*************************\n”);
printf(“HelloWorld!
\n”);
printf(“*************************”);
}
4.
源程序:
#include
main()
{
inta,b,c;/*定义变量*/
scanf(“%d”,&a);/*输入第一个整数*/
scanf(“%d”,&b);/*输入第二个整数*/
c=a-b;/*计算差*/
printf(“%d-%d=%d”,a,b,c);/*输出结果*/
}
5.
(1)
(2)x=10;
(3)printf(“s=%d\n”,s);
第2章
1.
(1)c
(2)a
(3)bg
(4)ade
(5)d
2.a.5b.295c.4d.29e.9
3.a.x=4,y=6
b.x=4,y=3
f.x=3,y=6
4.16
5.
#include
main()
{
inta,b,c;
scanf("%d%d",&a,&b);
c=a*b;
printf("%d*%d=%d",a,b,c);
}
第3章
1.
(1)b
(2)b
(3)d
(4)a
(5)b
2.
(1)&a,&b
(2)l,s
3.printf(“x=%.2f,y=%.2f\n”,x,y);
4.
#include
main()
{
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);
}
5.
#include
main()
{
inthour,minute,second,total;/*定义变量代表时、分、秒和总秒数*/
scanf("%d",&total);
hour=total/3600;
minute=total%3600/60;
second=total%3600%60;
printf("%dhours:
%dminutes:
%dseconds\n",hour,minute,second);
}
第4章
1.
(1)a
(2)b
(3)b
(4)b
(5)b
2.0
3.
(1)6
(2)4
(3)1
(4)1
4.
#include
main()
{
intx,y;
scanf("%d",&x);
if(x>-5&&x<0)
y=5*x;
if(x==0)
y=-1;
if(x>0&&x<10)
y=2*x+1;
printf("x=%d,y=%d\n",x,y);
}
5.
#include
main()
{
intscore,rank;/*score表示成绩,rank表示级别*/
printf("Pleaseinputscore:
");
scanf("%d",&score);
rank=score/10;
switch(rank)
{
case10:
case9:
printf("成绩等级为:
A\n");break;
case8:
printf("成绩等级为:
B\n");break;
case7:
printf("成绩等级为:
C\n");break;
case6:
printf("成绩等级为:
D\n");break;
default:
printf("成绩等级为:
E\n");break;
}
}
6.
#include
voidmain()
{
intn;
printf("Pleaseinputthedigit:
");
scanf("%d",&n);
switch(n)
{
case1:
printf("Jan\n");break;
case2:
printf("Feb\n");break;
case3:
printf("Mar\n");break;
case4:
printf("Apr\n");break;
case5:
printf("May\n");break;
case6:
printf("Jun\n");break;
case7:
printf("Jul\n");break;
case8:
printf("Agu\n");break;
case9:
printf("Sep\n");break;
case10:
printf("Oct\n");break;
case11:
printf("Nov\n");break;
case12:
printf("Dec\n");break;
}
}
第5章
1.
(1)b
(2)a
(3)b
(4)d
(5)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;
printf("2+4+6+...+200=%ld",sum);
}
10.
#include
main()
{
longintn;
intsum=0;
printf("Pleaseinputthenber:
");
scanf("%ld",&n);
while(n!
=0)
{
sum+=n%10;
n/=10;
}
printf("%d\n",sum);
}
第6章
1.
(1)d
(2)b
(3)d
(4)c
(5)a
2.11
3.3
4.5689
5.12,6
6.
(1)i--
(2)n
7.
(1)chark;
(2)i8.
#include
main()
{
floata[10];
inti;
floatsum=0,average;
for(i=0;i<10;i++)
{
printf("a[%d]=?
",i);
scanf("%f",&a[i]);
sum+=a[i];
}
average=sum/10;
printf("averageis%.2f\n",average);
}
9.
#include
#defineN10
main()
{
inta[N],i,target,found;
for(i=0;iscanf("%d",&a[i]);
printf("Pleaseinputanumber:
");
scanf("%d",&target);
i=0;
while(i=a[i])
i++;
found=ii:
-1;
printf("%d\n",found);
}
10.
#include
#defineN80
main()
{
charstr[N];
intlen=0,i=0;
printf("Pleaseinputastring:
");
gets(str);
while(str[i++]!
=0)len++;
printf("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
8.
#include
intfun(inta,intb,intc);
main()
{
inta,b,c,max;
printf("Pleaseinputthreenumbers:
");
scanf("%d%d%d",&a,&b,&c);
max=fun(a,b,c);
printf("Maxis%d.\n",max);
}
intfun(inta,intb,intc)
{
intmax;
max=a>b?
(a>c?
a:
c):
(b>c?
b:
c);
returnmax;
}
9.
#include
longintsum(intn);
main()
{
intn;
printf("n=?
");
scanf("%d",&n);
printf("Sum=%ld.\n",sum(n));
}
longintsum(intn)
{
if(n==1)return1;
elsereturnsum(n-1)+n;
}
10.
#include
voidfun(n);
main()
{
intn;
printf("n=?
");
scanf("%d",&n);
fun(n);
}
voidfun(n)
{
if(n==0)return;
else
{
fun(n/2);
printf("%-2d",n%2);
}
}
第8章
1.
(1)b
(2)d
(3)b
(4)c
(5)c
2.8
3.123456789
4.23456
5.345
6.123456
7.b[i]
8.bcdefgha
9.p=sum
10.
#include
#include
main()
{
charstr[80];
char*p1,*p2;
gets(str);
p1=str;
p2=str+strlen(str)-1;
while(p1puts(p1"不是回文":
"是回文");
}
第9章
1.
a.#defineF(x)(x)*(x)*(x)
b.#defineF(x)(x)%4
c.#defineF(x,y)(x)*(y)<100?
1:
0
2.
a.4
b.4
c.#defineDOUBLE(x)2*(x)
3.d
4.-20
5.Nisundefined
6.7.5
7.y=6
8.
#include
#defineSWAP(a,b){inttemp;temp=a;a=b;b=temp;}
main()
{
intx,y;
printf("x=?
");
scanf("%d",&x);
printf("y=?
");
scanf("%d",&y);
SWAP(x,y)
printf("x=%d,y=%d.\n",x,y);
}
第10章
1.
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];
6.
(1)structemployee
(2)printemp(emp)
7.24398
8.
#include
structtime_struct
{
inthour;
intminute;
intsecond;
};
main()
{
structtime_structtime;
printf("Inputtime?
\n(Example18:
28:
38)\n");
scanf("%d:
%d:
%d",&time.hour,&time.minute,&time.second);
printf("Timeis%d:
%d:
%d\n",time.hour,time.minute,time.second);
}
9.
#include
structtime_struct
{
inthour;
intminute;
intsecond;
}time;
voidenter_time();
voiddisplay_time();
main()
{
enter_time();
display_time();
}
voidenter_time()
{
printf("Enterthetime(example18:
28:
38)?
");
scanf("%d:
%d:
%d",&time.hour,&time.minute,&time.second);
}
voiddisplay_time()
{
printf("Timeis%d:
%d:
%d.\n",time.hour,time.minute,time.second);
}\
10.
#include
#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}
};
main()
{
intgrade,i;
printf("请输入级别(3-5)?
");
scanf("%d",&grade);
for(i=0;iif(h[i].grade==grade)
printf("名称:
%s\n地址:
%s\n级别:
%d\n平均房价%.2f\n房间数量:
%d\n",
h[i].name,h[i].address,h[i].grade,h[i].average_charge,h[i].number);
}
第11章
1.
(1)c
(2)d
(3)b
(4)b
(5)b
2.Basican
3.fgetc(fp)
4.“record.dat”,“w”
5.
#include
#include
#include
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("employee.dat","wb+");
if(fp==NULL)
{
printf("Can'tOpenFile");
exit(0);
}
}
recsize=sizeof(emp);
while
(1)
{
printf("1.添加记录2.显示男员工3.退出\n");
printf("Enteryourchoice(1-3):
");
fflush(stdin);
scanf("%c",&choice);
switch(choice)
{
case'1':
fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y'||another=='y')
{
printf("输入信息(id姓名性别年龄住址):
\n");
scanf("%d%s%s%d
%s",&emp.id,&emp.name,&emp.gender,&emp.age,&emp.address);
fwrite(&emp,recsize,1,fp);
printf("是否继续(Y/N):
");
fflush(stdin);
another=getchar();
}
break;
case'2':
printf("学号\t姓名\t性别\t年龄\t住址\n");
rewind(fp);
while(fread(&emp,recsize,1,fp)==1)
if(strcmp(emp.gender,"男")==0)
printf("%d\t%s\t%s\t%d\t%s\n",
emp.id,emp.name,emp.gender,emp.age,emp.address);
break;
case'3':
fclose(fp);
exit(0);
}
}
}
6.
#include"stdio.h"
#include"stdlib.h"
#defineM2
#definestustructstudent
stu
{
intnum;
charname[20];
floats1;
floats2;
floats3;
floatavg;
};
main()
{
stust[M];
FILE*fp;
inti;
printf("请输入5名同学生的成绩,按照学号,姓名,成绩1,成绩2,成绩3的顺序,中间用空格隔
开:
\n");
for(i=0;i{
scanf("%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)
{
printf("cannotopenfile\n");
}
for(i=0;i{
if(fwrite(&st[i],sizeof(stu),1,fp)!
=1)
printf("filewriteerror\n");
}
fclose(fp);
fp=fopen("stud.rec","rb");
printf("num\tname\tscore1\tscore2\tscore3\taverage\n");
for(i=0;i{
fread(&st[i],sizeof(stu),1,fp);
printf("%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);
}
fclose(fp);
}
第12章
1.
a.2
b.4
c.11
d.4
e.-15
f.28
g.-36
2.
程序设计分析:
先将整数x右移4位,将该整数机内码的第4到7位移至第0到3位,
然后与0x000f(0000000000001111)进行位与运算,所保留的低4位就是所要的结果
#include
voidmain()
{
intx,y;
scanf("%d",&x);
y=x>>4;y=y&0x000f;
printf("4~7位的十六进制数:
y=%#x\n",y);
}
3.d
4.
(1)p!
=NULL
(2)p=p->next5.p=p->next
6.略