C程序设计答案修订.docx

上传人:b****1 文档编号:10288420 上传时间:2023-05-24 格式:DOCX 页数:35 大小:22.47KB
下载 相关 举报
C程序设计答案修订.docx_第1页
第1页 / 共35页
C程序设计答案修订.docx_第2页
第2页 / 共35页
C程序设计答案修订.docx_第3页
第3页 / 共35页
C程序设计答案修订.docx_第4页
第4页 / 共35页
C程序设计答案修订.docx_第5页
第5页 / 共35页
C程序设计答案修订.docx_第6页
第6页 / 共35页
C程序设计答案修订.docx_第7页
第7页 / 共35页
C程序设计答案修订.docx_第8页
第8页 / 共35页
C程序设计答案修订.docx_第9页
第9页 / 共35页
C程序设计答案修订.docx_第10页
第10页 / 共35页
C程序设计答案修订.docx_第11页
第11页 / 共35页
C程序设计答案修订.docx_第12页
第12页 / 共35页
C程序设计答案修订.docx_第13页
第13页 / 共35页
C程序设计答案修订.docx_第14页
第14页 / 共35页
C程序设计答案修订.docx_第15页
第15页 / 共35页
C程序设计答案修订.docx_第16页
第16页 / 共35页
C程序设计答案修订.docx_第17页
第17页 / 共35页
C程序设计答案修订.docx_第18页
第18页 / 共35页
C程序设计答案修订.docx_第19页
第19页 / 共35页
C程序设计答案修订.docx_第20页
第20页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C程序设计答案修订.docx

《C程序设计答案修订.docx》由会员分享,可在线阅读,更多相关《C程序设计答案修订.docx(35页珍藏版)》请在冰点文库上搜索。

C程序设计答案修订.docx

C程序设计答案修订

习题参考答案

第1章

1.算法是对数据结构的操作过程。

算法的特征有:

有穷性、确定性、可行性、有零个或多个输入、有一个或多个输出。

算法通常由两种基本要素组成:

一是对数据对象的运算和操作,二是算法的控制结构。

2.采用模块化结构、自上而下、逐步求精、限制使用GOTO语句

3.顺序结构、选择结构、循环结构

4.略

第2章

1.略

2.非法标识符有:

a*b、a[i]、f(x)、\0、'Y'、"C"、12345、123a、for、{abc}、S.Johnson、break、123_、$25.5

3.合法常量及其类型有:

int:

1、01、0xabcd、0x123

long:

1L、123456789

float:

1F

double:

1.、1.0、1E3、-1E-3、1E3.5

char:

'\\'、'\*'、'\"'、'\55'

字符串:

"\*"

4.略

5.

(1)(a+b)*(a*a-a*b+b*b)

(2)3.14*r*r(3)1/3*s*h

(4)(x-y/z)/(a+b+c)(5)n*n*(n+1)*(n+1)/4(6)(a*x+(a+x)/(4*a))/2

6.

(1)

(2)(3)(4)不正确,原因略

7.

(1)5

(2)31(3)-24(4)7

(5)12(6)10(7)2.500000(8)3.500000

8.略

第3章

1.D2.C3.A4.C

5.略

6.

(1)14,16,e,14

(2)235,3.14159

(3)3.141590,3.14

(4)*

**

***

****

(5)ThisisaCprogram.

(6)97\b

x=’61’,’62’

7.编写程序题(参考答案)

(1)

main()

{floata1,a2,a3,a4,a5,a6,sum=0,ave=0;

printf("pleaseinput6grade:

\n");

scanf("%f%f%f%f%f%f",&a1,&a2,&a3,&a4,&a5,&a6);

sum=a1+a2+a3+a4+a5+a6;

ave=sum/6.0;

printf("sum=%5.2f;ave=%5.2f",sum,ave);

}

(2)

main()

{floatk,m;

printf("pleaseinputkilometer:

\n");

scanf("%f",&k);

m=k*1000;

printf("m=%f\n",m);

}

(3)

main()

{floatprice,rate,pp;

printf("pleaseinputprice,rate:

\n");

scanf("%f%f",&price,&rate);

pp=price*rate/100.0;

printf("pp=%f\n",pp);

}

第4章

1.C2.B3.B4.B5.D6.C

7.

(1)1

(2)0

(3)1

(4)1

8.

(1)(x%2==0)||(y%2==0)

(2)

(2)(x<0)||(x>10)

(3)(3)(x>=0)&&(x<=60)

(4)(x%3==0)&&(x%7==0)

9.略

10.

(1)5

(2)-1

(3)a=2,b=1

(4)a=10,b=30,c=10

11.编写程序题(参考答案)

(1)

main()

{floata,b,c,min;

printf("pleaseinput3num:

\n");

scanf("%f%f%f",&a,&b,&c);

min=a;

if(min>b)min=b;

if(min>c)min=c;

printf("min=%f\n",min);

}

(2)

#include

main()

{floata,b,c,t,x1,x2;

printf("pleaseinput3num:

\n");

scanf("%f%f%f",&a,&b,&c);

t=b*b-4*a*c;

if(t<0)printf("无实根!

\n");

else

{if(t>0)

{x1=(-b+sqrt(t))/(2*a);

x2=(-b-sqrt(t))/(2*a);

}

if(t==0)x1=x2=-b/(2*a);

printf("x1=%f;x2=%f;\n",x1,x2);

}

}

(3)

#include

main()

{intselect;

floatleng,r=2.54;

printf("Pleasechoose(1:

inchtocm;2:

cmtoinch):

");

scanf("%d",&select);

printf("Entertheleng:

");

scanf(“%f”,&leng);

switch(select)

{case1:

printf("%finch=%fcm\n",leng,leng*r);break;

case2:

printf("%fcm=%finch\n",leng,leng/r);break;

default:

printf("Entererror!

\n");

}

}

第5章

1.略

2.略

3.

(1)3

6

9

12

15

18

(2)0sum=0

1sum=1

2sum=3

(3)4

(4)#1

#2

#3

#4

#5

(5)1

4

9

16

25

36

49

64

81

100

totalis385

(6)i=8

i=9

i=10

(7)25

(8)x=0;y=5

(9)i=1

i=3

(10)****

****

****

****

****

****

****

****

****

4.

(1)

main()

{inti,j;

for(i=0;i<6;i++)

{for(j=0;j<6-i;j++)

printf(“*”);

printf(“\n”);}

}

(2)

main()

{inti=0,j=0,x;

printf(“pleaseinputainteger(0:

end):

”);

scanf(“%d”,&x);

while(x!

=0)

{if(x>0)i+=1;

elsej+=1;

printf(“pleaseinputainteger(0:

end):

”);

scanf(“%d”,&x);

}

printf(“%d,%d\n”,i,j);

}

5.编写程序题(参考答案)

(1)

main()

{intz=0,f=0;

floatnum,max;

printf("pleaseinputnum:

(-999toend)\n");

scanf("%f",&num);

max=num;

while(num!

=-999)

{if(num>max)max=num;

if(num>=0)z++;

elsef++;

printf("pleaseinputnum:

(-999toend)\n");

scanf("%f",&num);

}

printf("max=%f;z=%d;f=%d;\n",max,z,f);

}

(2)

main()

{inti,j=0;

for(i=100;i<=200;i++)

if(i%5==0&&i%7==0)

{printf("%d",i);

j++;

}

printf("\n");

printf("totalis%d\n",j);

}

(3)

#defineN10

main()

{inti,a=0,b=0,c=0,d=0,e=0;

floatscore;

printf("pleaseinput%dscore:

",N);

for(i=1;i<=N;i++)

{scanf("%f",&score);

if(score>=90)a++;

elseif(score>=80)b++;

elseif(score>=70)c++;

elseif(score>=60)d++;

elsee++;

}

printf("A=%d;B=%d;C=%d;D=%d;E=%d;\n",a,b,c,d,e);

}

(4)

main()

{inti,j;

for(i=1;i<=6;i++)

{for(j=1;j<=i;j++)

printf("*");

printf("\n");

}

printf("\n");

}

main()

{inti,j;

for(i=1;i<=6;i++)

{for(j=6;j>=i;j--)

printf("*");

printf("\n");

}

printf("\n");

}

(5)

main()

{inti,j;

for(i=1;i<=9;i++)

{for(j=1;j<=i;j++)

printf("%1d*%1d=%2d",i,j,i*j);

printf("\n");

}

printf("\n");

}

第6章

1.A2.D3.C4.B

5.

s=6a=6

s=7a=6

s=8a=6

s=9a=6

s=10a=6

6.

i=1,j=1

(i)=2

last(i)=10

new(i+j)=12

i=3,j=2

(i)=4

last(i)=9

new(i+j)=15

7.编写程序题(参考答案)

(1)

#include

voidfac(intx,chary)

{inti;

for(i=1;i<=x;i++)

printf("%c",y);

}

main()

{intn;

charc;

printf("inputchar:

");

getchar(c);

printf("inputnumber:

");

scanf("%d",&n);

fac(n,c);

}

(2)

main()

{inta,b,m,n;

printf("inputaandb:

");

scanf("%d%d",&a,&b);

m=f1(a,b);

n=f2(a,b,m);

printf("m=%d,n=%d\n",m,n);

}

intf1(intx,inty)

{intr;

if(x

while((r=x%y)!

=0)

{x=y;y=r;}

return(r);

}

intf2(intx,inty,intz)

{return(x*y/z);

}

(3)

age(intn)

{intc;

if(n==1)c=10;

elsec=age(n-1)+2;

return(c);

}

main()

{printf("%d\n",age(6));

}

第7章

1.A2.C3.B

4.

(1)a=0,b=7

(2)-5-12-7

(3)5,18

(4)26

(5)输入r值,求得面积

5.编写程序题(参考答案)

(1)

fun(intx,inty,int*p1,int*p2)

{*p1=x+y;

*p2=x*y;

}

main()

{inta,b,he,ji;

scanf("%d,%d",&a,&b);

fun(a,b,&he,&ji);

printf("%d,%d\n",he,ji);

}

(2)

main()

{inta,b,c,t,*p1,*p2,*p3;

scanf("%d,%d,%d",&a,&b,&c);

p1=&a;p2=&b;p3=&c;

if(a>b)

{t=*p1;*p1=*p2;*p2=t;}

if(a>c)

{t=*p1;*p1=*p3;*p3=t;}

if(b>c)

{t=*p2;*p2=*p3;*p3=t;}

printf("%d,%d,%d\n",*p1,*p2,*p3);

}

第8章

1.A2.A3.D4.D5.B6.C7.D8.C

9.

(1)*(++p)或*++p

(2)①p++

②w[i-1]

(3)①r+b[k]

②*x

(4)①’\0’

②*sptr++

10.

(1)Porm

(2)15

(3)ga

(4)6

11.编写程序题(参考答案)

(1)

#defineN10

main()

{inti,j,t,a[N];

for(i=0;i

scanf("%d",&a[i]);

for(i=0;i

printf("%5d",a[i]);

printf("\n");

for(i=0,j=N-1;i

{t=a[i];a[i]=a[j];a[j]=t;}

for(i=0;i

printf("%5d",a[i]);

}

(2)

main()

{inti,j,s=0,a[3][3];

printf("inputnumbers:

");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf("%d",&a[i][j]);

for(i=0;i<3;i++)

{for(j=0;j<3;j++)

{if(i==j)s=s+a[i][j];

printf("%5d",a[i][j]);

}

printf("\n");

}

printf("s=%d\n",s);

}

(3)

main()

{inti,j,a[10][10];

for(i=0;i<10;i++)

{for(j=0;j

{if(i==j||j==0)

a[i][j]=1;

else

a[i][j]=a[i-1][j-1]+a[i-1][j];

printf("%5d",a[i][j]);

}

printf("\n");

}

}

(4)

#include

main()

{intn=0;

charstr[80];

gets(str);

while(str[n]!

='\0')

n++;

printf("strlen=%d\n",n);

}

(5)

main()

{inti,j,t,a[11];

printf("input10numbers:

");

for(i=1;i<11;i++)

scanf("%d",&a[i]);

printf("\n");

for(i=1;i<10;i++)

for(j=1;j<11-i;j++);

if(a[j]>a[j+1])

{t=a[j];a[j]=a[j+1];a[j+1]=t;}

printf("thesortednumbers:

\n");

for(i=1;i<11;i++)

printf("%5d",a[i])

}

(6)

#include

main()

{charstr[80],*s=str;

inti,j;

gets(s);

for(i=j=0;s[i];i++)

if(s[i]!

='')

s[j++]=s[i];

s[j]='\0';

puts(s);

}

(7)

#include

#include

main()

{charstr[80],*s=str,ch;

inti,j;

gets(s);

for(i=0,j=strlen(s)-1;i

ch=s[i],s[i]=s[j],s[j]=ch;

puts(s);

}

(8)略

第9章

1.选择题

(1)B

(2)A(3)C(4)A(5)A,D

2.填空题

(1)p2->next,ma

(2)structliab*,structliab,structliab*,structliab,p3

3.程序设计题(参考答案)

(1)

#include"stdio.h"

structstudent

{charnum[9];

charname[20];

floatscore[3];

};

voidprint(structstudents[],intn)

{inti;

printf("\nnumnamescore[0]score[1]score[2]\n");

for(i=0;i

{printf("%-9s%-20s%9.1f%9.1f%9.1f\n",s[i].num,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2]);;}

}

main()

{structstudentstu[5];

inti;

floatsc[3];/*定义一个数组、便于数据输入*/

for(i=0;i<5;i++)

{printf("pleaseinputstudentnum(format:

xxxxxxxxx):

");

scanf("%s",stu[i].num);

printf("pleaseinputstudentname(format:

xxxxxxxxxxxxxxxxxxxx):

");

scanf("%s",stu[i].name);

printf("pleaseinputstudentscore(format:

xx.x,xx.x,xx.x):

");

scanf("%f,%f,%f",sc,sc+1,sc+2);/*用数组sc接收数据、然后再赋给学生成绩数组*/

stu[i].score[0]=sc[0];

stu[i].score[1]=sc[1];

stu[i].score[2]=sc[2];

}

print(stu,5);/*调用函数输出*/

}

(2)

#include"stdio.h"

#defineNUM10

structstudent

{charNo[9];

charname[20];

floatenglish,politics,computer;

floatscoresum;

};

main()

{structstudentstu[NUM];

inti,loc;

floatsc[3];

floatsum=0,maxscore;

for(i=0;i

{printf("pleaseinputstudentNo.(xxxxxxxxx):

");

scanf("%s",stu[i].No);

printf("pleaseinputstudentname(xxxxxxxxxxxxxxxxxxxx):

");

scanf("%s",stu[i].name);

printf("pleaseinputstudentenglishscore(xx.x):

");

scanf("%f",sc);

printf("pleaseinputstudentpoliticsscore(xx.x):

");

scanf("%f",sc+1);

printf("pleaseinputstudentcomputerscore(xx.x):

");

scanf("%f",sc+2);

stu[i].english=sc[0];

stu[i].politics=sc[1];

stu[i].computer=sc[2];

stu[i].scoresum=stu[i].english+stu[i].politics+stu[i].computer;

sum=sum+stu[i].scoresum;

}

printf("\nAllscoreaverage=%.2f",sum/(3*NUM));

maxscore=stu[0].scoresum;

loc=0;

for(i=1;i

{if(maxscore

{maxscore=stu[i].scoresum;

loc=i;

}

}

printf("Theinfomationof1st\nNo.:

%s,name:

%s,English:

%.1f,Politics:

%.1f,Computer:

%.1f",

stu[loc].No,stu[loc].name,stu[loc].english,stu[loc].politics,stu[loc].computer);

}

(3)

#include

#include"stdlib.h"

#defineNULL0

structysf

{intnum;

structysf*next;

};

intcnt(structysf*h)

{intsum=0;

structysf*p;

p=h->next;

while(p->next!

=h->next)

{sum++;p=p->next;}

returnsum+1;

}

main()

{intnum,sum=0;

intnn=10;

structysf*head,*h,*q,*p,*n;

head=(structysf*)malloc(sizeof(structysf));/*建立链表*/

head->next=NULL;

h=p=head;

printf("\npleaseenternum(format:

xxx):

");

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2