标准C语言程序设计及应用周纯杰版习题参考答案Word下载.docx
《标准C语言程序设计及应用周纯杰版习题参考答案Word下载.docx》由会员分享,可在线阅读,更多相关《标准C语言程序设计及应用周纯杰版习题参考答案Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
(3)9
(4)0xFFC6or65478
(5)104
(6)2
2、
(1)6
(2)50
(3)1
(4)-16or0xFFF0
(5)1
(6)20
3、表达式值a的值b的值
(1)053
(2)153
(3)313
(4)15104
(5)883
(6)153
4、表达式值x的值y的值
(1)12252
(2)0243
(3)1243
(4)27243
(5)1243
(6)663
(7)242424(y<
<
=3)
(8)27243
(9)-29or0xffe3243
以上类型均为int
5、表达式值x的值y的值
(1)001
(2)121
(3)100
(4)-3-33
(5)424
五、程序分析题
b=20(注:
中间4个空格)a=3600回车
习题三
一、填空题
1、s=6回车
2、s=96回车
3、
(1)c=getchar()orscanf(“%c”,&
c)
(2)c-32orc-‘a’+’A’
习题四
一、写出程序运行结果:
1、no1a=1no1a=0no1a=1no1a=0(注:
教材中关于func函数的原型有错,应该改为:
voidfunc();
)
2、a=0b=0c=0
a=1b=0c=0
a=2b=0c=0
3、main:
x=5,y=1,n=1
fun:
x=6,y=21,n=11
main:
x=5,y=1,n=11
x=8,y=31,n=21
习题五
一、选择题和填空题
1、D
2、D
3、C
4、6
5、CDABC
6、
(1)j+=2
(2)a[j]>
a[i]
(注:
教材中for语句有错,应该去掉表达式3后面的分号“;
”)
最后输出为unalggace
7、
(1)s[j]
(2)s[j]
8、D
9、B
10、6
11、isdigit(s[i])该带参数的宏用来判断字符s[i]是不是数字字符,该宏的定义在头文件ctype.h中
12、‘\0’和str1[i]-str2[i]
二、1、第7行sum=0;
第10行改为:
scanf(“%d”,&
a[i][j]);
习题六
3、D
4、C
5、CD
6、D
7、B
二、填空题
1、*(p+3)222
2、cdefg
bcdefg
abcdefg
7
3、6385
三、程序分析题
9、第5行改为:
p=&
s[0];
orp=s;
10、第4,5行合为:
doublex,y,*p;
11、第4行改为:
intx,*p=&
x;
否则p为“野指针”或者在给p赋值之前定义一个int变量y,然后p=&
y;
12、第4,5行交换书写次序。
习题七
1、A
3、B
4、D
5、C
6、A
二、程序填空题
1、a[k]a[k]a[k]
2、a[i]j6ori+1
三、改错题
1、第3行最后加分号“;
”
第4行改为:
voidmain()
第7行去掉三个&
运算符
第8,9行改为:
if(strcmp(a,b)>
0)swap(a,b);
if(strcmp(b,c)>
0)swap(b,c);
swap函数改为:
voidswap(char*pstr1,char*pstr2)
{
charp[80];
//千万不能用char*p;
没有赋初值即为“野指针”,危险!
strcpy(p,pstr1);
strcpy(pstr1,pstr2);
strcpy(pstr2,p);
}
2、第1行后面加入:
floatprocess(float*,int,int*);
intm;
定义后加入:
floatmax;
process定义改为:
floatprocess(…)
…
floattemp=p1[0];
或改为:
=*p1
*p2=p1[0];
//插入
for(x=1;
x<
n;
x++)
四、编程题
//P.230第七章习题
//编程题4参考程序答案
//
#include<
stdio.h>
voiddelete_char(char*s,charc);
voidmain()
charstr[80],ch;
puts("
请输入一个不足79个字符的串:
"
);
scanf("
%s"
str);
getchar();
请输入待删除的字符:
%c"
&
ch);
delete_char(str,ch);
printf("
删除特定字符%c后的字符串为%s\n"
ch,str);
voiddelete_char(char*s,charc)
char*p;
p=s;
for(;
*s;
s++)
if(*s!
=c)*p++=*s;
*p='
\0'
;
//编程题8参考程序答案
/*
stdlib.h>
string.h>
//#include<
alloc.h>
voidreverse(char*s);
intlen;
//全局变量使得reverse函数通用性降低
char*str;
str=(char*)malloc(80*sizeof(char));
if(!
str)//if(str==NULL)
puts("
内存空间不够!
请输入待翻转的字符串:
//gets(str);
len=strlen(str);
reverse(str);
翻转后的字符串为:
%s\n"
free(str);
//很重要,必须与前面的malloc配对,编程时,最好习惯性地在写
//完malloc后马上先把free写上,免得遗忘
voidreverse(char*s)
chartemp;
if(len==2||len==3)
{
temp=*s;
*s=*(s+len-1);
*(s+len-1)=temp;
}
else
len-=2;
reverse(s+1);
}*/
//----------------------------------------------------------------
//下面是通用性强的reverse函数
voidreverse(char*s,intlen);
intlength;
length=strlen(str);
reverse(str,length);
voidreverse(char*s,intlen)
len-=2;
reverse(s+1,len);
//编程题15参考程序答案
//本例可以采用结构体数组
voidinput(int[],char[][20],int[],int[][4]);
voidcalcu(int[][4],float[],float*);
voidsort(int[],char[][20],int[],int[][4],float[]);
voidoutput(int[],char[][20],int[],int[][4],float[],float);
intscore[30][4];
floatavg[30],total_avg;
charname[30][20];
intid[30],sex[30];
input(id,name,sex,score);
calcu(score,avg,&
total_avg);
sort(id,name,sex,score,avg);
output(id,name,sex,score,avg,total_avg);
voidsort(intstu_id[],charstu_name[][20],intstu_sex[],intstu_score[][4],floatstu_avg[])
inti,j,k,m,tmp;
chartemp[20];
floataverage;
for(i=0;
i<
29;
i++)
m=i;
for(j=i+1;
j<
4;
j++)
if(stu_avg[j]>
stu_avg[m])
m=j;
if(m!
=i)
tmp=stu_id[m];
stu_id[m]=stu_id[i];
stu_id[i]=tmp;
strcpy(temp,stu_name[m]);
strcpy(stu_name[m],stu_name[i]);
strcpy(stu_name[i],temp);
tmp=stu_sex[m];
stu_sex[m]=stu_sex[i];
stu_sex[i]=tmp;
for(k=0;
k<
k++)
{
tmp=stu_score[m][k];
stu_score[m][k]=stu_score[i][k];
stu_score[i][k]=tmp;
}
average=stu_avg[m];
stu_avg[m]=stu_avg[i];
stu_avg[i]=average;
voidcalcu(intstu_score[][4],floatstu_avg[],float*stu_total_avg)
inti,j;
*stu_total_avg=0;
=29;
stu_avg[i]=0;
for(j=0;
{
stu_avg[i]+=stu_score[i][j];
*stu_total_avg+=stu_score[i][j];
}
stu_avg[i]/=4;
*stu_total_avg/=30*4;
voidinput(intstu_id[],charstu_name[][20],intstu_sex[],intstu_score[][4])
请按学号依次输入学生的学号、姓名、性别(1:
男生,0:
女生)"
和数学、物理、英语、计算机的课程成绩:
%6s%6s%6s%6s%6s%6s%6s\n"
"
学号"
姓名"
性别"
数学"
物理"
英语"
计算机"
scanf("
%d%s%d"
stu_id[i],stu_name[i],&
stu_sex[i]);
scanf("
%6d"
stu_score[i][j]);
-----------------------------------------------------------------------"
voidoutput(intstu_id[],charstu_name[][20],intstu_sex[],intstu_score[][4],floatstu_avg[],floattotal_avg)
inti;
%-8s%-10s%-6s%-6s%-6s%-6s%-6s\n"
if(stu_avg[i]>
total_avg&
&
stu_sex[i]==1)
printf("
%-8d%-10s%-6d%-6d%-6d%-6d%-6d\n"
stu_id[i],stu_name[i],stu_sex[i],stu_score[i][0],stu_score[i][1],stu_score[i][2],stu_score[i][3]);