C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx

上传人:b****1 文档编号:15209989 上传时间:2023-07-02 格式:DOCX 页数:18 大小:26.18KB
下载 相关 举报
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第1页
第1页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第2页
第2页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第3页
第3页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第4页
第4页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第5页
第5页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第6页
第6页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第7页
第7页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第8页
第8页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第9页
第9页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第10页
第10页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第11页
第11页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第12页
第12页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第13页
第13页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第14页
第14页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第15页
第15页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第16页
第16页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第17页
第17页 / 共18页
C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx

《C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx》由会员分享,可在线阅读,更多相关《C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx(18页珍藏版)》请在冰点文库上搜索。

C语言程序设计实用教程习题参考答案和上机实验参考源程序.docx

C语言程序设计实用教程习题参考答案和上机实验参考源程序

C语言程序设计实用教程习题参考答案和上机实验参考源程序

第1章

一、选择题:

1.B2.C3.B

二、问答题:

2.

第2章

一、选择题:

1.D2.B3.A4.C5.C6.A7.C8.D

9.D10.D11.C12.C13.D14.C15.B

二、填空题:

1.—32.33.64.205.double6.20

第3章

一、选择题:

1.B2.C3.B4.C5.A6.D7.D8.B

9.C10.A11.B12.D13.C14.C15.C16.B

17.A

二、填空题:

1.112.2521373.214.35.0

上机实验

1.参考源程序:

#include

main()

{

intyear;

floatmoney,rate,total;/*money:

本金rate:

月利率total:

本利合计*/

printf("Pleaseinputmoneyandyears:

");

scanf("%f%d",&money,&year);/*输入本金和存款年限*/

if(year==1)rate=0.00315;/*根据年限确定利率*/

elseif(year==2)rate=0.00330;

elseif(year==3)rate=0.00345;

elseif(year==5)rate=0.00375;

elseif(year==8)rate=0.00420;

elserate=0.0;

total=money+money*rate*12*year;/*计算到期的本利合计*/

printf("Total=%.2f\n",total);

}

2.参考源程序:

#include

main()

{

intletter=0,space=0,digit=0,other=0;

charc;

printf("Pleaseinputastring:

\n");

while((c=getchar())!

='\n')

if(c>='A'&&c<='Z'||c>='a'&&c<='z')

letter++;

elseif(c=='')

space++;

elseif(c>='0'&&c<='9')

digit++;

else

other++;

printf("Theletternumberis%d\n",letter);

printf("Thespacenumberis%d\n",space);

printf("Thedigitnumberis%d\n",digit);

printf("Theothernumberis%d\n",other);

}

第4章

一、选择题:

1.A2.D3.C4.B5.C6.B7.B8.D

二、填空题:

1.30252.43.1000104.16

上机实验

1.参考源程序:

#include

floatfun(floath);

main()

{

floata;

printf("Pleaseinputarealnumber:

");

scanf("%f",&a);

printf("Theoriginaldatais:

");

printf("%f\n",a);

printf("Theresult:

%6.2f\n",fun(a));

}

floatfun(floath)

{

longt;

h=h*1000;

t=(h+5)/10;

return(float)t/100;

}

2.参考源程序:

#include

intc(intx,inty);

main()

{

inti,j,n=13;

printf("Pleaseinputthennumberofrows(1-12):

");/*最大输入值不能大于12*/

while(n>12)

scanf("%d",&n);

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

{

for(j=1;j

printf("%6d",c(i,j));/*输出第i行的第j个值*/

printf("\n");

}

}

intc(intx,inty)/*求杨辉三角形中第x行第y列的值*/

{

intz;

if((y==1)||(y==x+1))

z=1;/*若为x行的第1或第x+1列,则输出1*/

else/*否则,其值为前一行中第y-1列与第y列值之和*/

z=c(x-1,y-1)+c(x-1,y);

return(z);

}

第5章

一、选择题1.A2.A3.B4.D5.D6.A7.A8.A

9.B10.A

二、填空题1.922.243.58

上机实验

1.参考源程序:

#include

main()

{

inti,j,num,a[10];

printf("Pleaseinput10numbers:

");

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

{

scanf("%d",&num);

for(j=i-1;j>=0&&a[j]>num;j--)

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

a[j+1]=num;

}

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

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

}

2.参考源程序:

#include

#include

main()

{

inti,j,a[30];

randomize();/*初始化随机数发生器*/

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

{

a[i]=random(1000);/*产生0-999的随机数*/

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

}

printf("\n");

for(i=1,j=0;i<30;i++)/*确定最大值的下标*/

if(a[i]>a[j])

j=i;

for(;j<29;j++)/*删除最大值*/

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

for(i=0;i<29;i++)/*打印剩下的29个数*/

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

}

3.参考源程序:

#include

main()

{

inti,j;

inta[10][10];

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

{

a[i][0]=1;

a[i][i]=1;

}

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

for(j=1;j

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

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

{

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

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

printf("\n");

}

}

4.参考源程序:

#include

intstrcmp(charastr1[],charastr2[])

{

inti=0,m;

while(astr1[i]!

='\0')

if(astr1[i]==astr2[i])i++;

elsebreak;

m=astr1[i]-astr2[i];

returnm;

}

main()

{

charstr1[50],str2[50];

intrst;

printf("\nPleaseinputstring1:

\n");

gets(str1);

printf("\nPleaseinputstring2:

\n");

gets(str2);

rst=strcmp(str1,str2);/*实现函数调用*/

printf("\nthecomparedresultis%d.",rst);

}

第6章

一、选择题:

1.A2.C3.D4.D5.C6.D7.D8.C

二、填空题:

1.802.34123.84.05.06.ARRAYa,b,c;

上机实验

1.参考源程序:

#include

#include

structpoint

{

floatx,y,z;

};

floatdistance(structpointapnt1,structpointapnt2);

floatsqr(floatx);

main()

{

structpointpnt1,pnt2;

printf("Pleaseinputx1:

");

scanf("%f",&pnt1.x);

printf("Pleaseinputy1:

");

scanf("%f",&pnt1.y);

printf("Pleaseinputz1:

");

scanf("%f",&pnt1.z);

printf("Pleaseinputx2:

");

scanf("%f",&pnt2.x);

printf("Pleaseinputy2:

");

scanf("%f",&pnt2.y);

printf("Pleaseinputz2:

");

scanf("%f",&pnt2.z);

printf("Thedistanceis%.2f",distance(pnt1,pnt2));

}

floatdistance(structpointapnt1,structpointapnt2)

{

floatsquare;

square=sqr(apnt1.x-apnt2.x)+sqr(apnt1.y-apnt2.y)+sqr(apnt1.z-apnt2.z);

returnsqrt(square);/*sqrt为求开方函数*/

}

floatsqr(floatx)/*求平方函数*/

{

returnx*x;

}

2.参考源程序:

#include

#defineN5

structexaminee

{

charnum[20];

charname[20];

intage;

union

{

floatscore;

charrank[2];

}grade;

};

main()

{

inti,count=0;

floatm;

structexamineeexm[N];

for(i=0;i

{

printf("Pleaseinputnumber,name,age:

");

scanf("%s%s%d",exm[i].num,exm[i].name,&exm[i].age);

if(30>exm[i].age)

{

printf("Pleaseinputscore:

");

scanf("%f",&exm[i].grade.score);

}

else

{

printf("Pleaseinputrank:

");

scanf("%c",&exm[i].grade.rank);

}

}

for(i=0;i

{

if(exm[i].age<30&&exm[i].grade.score>=60)

count++;

elseif(exm[i].age>=30&&exm[i].grade.rank!

='D')

count++;

}

printf("%dexamineeshavepassed.\n",count);

printf("numbernameagegrade\n");

for(i=0;i

{

if(exm[i].age<30&&exm[i].grade.score>=60)

printf("%s%s%d%.1f\n",

exm[i].num,exm[i].name,exm[i].age,exm[i].grade.score);

elseif(exm[i].age>=30&&exm[i].grade.rank!

='D')

printf("%s%s%d%c\n",

exm[i].num,exm[i].name,exm[i].age,exm[i].grade.rank);

}

}

第7章

一、选择题:

1.C2.D3.D4.B5.D6.C7.B8.A

9.C10.C11.D12.B13.D

二、填空题:

1.cdeab2.gae3.void(*p)()或void(*p)(int*,int*)4.*(++p)或*++p

上机实验

1.参考源程序:

#include

#definenmax50

main()

{

inti,k,m,n,num[nmax],*p;

printf("Pleaseinputthetotalofnumbers:

");

scanf("%d",&n);

p=num;

for(i=0;i

*(p+i)=i+1;

i=0;k=0;m=0;

while(m

{

if(*(p+i)!

=0)k++;

if(k==3)

{

*(p+i)=0;

k=0;

m++;

}

i++;

if(i==n)i=0;

}

while(*p==0)p++;

printf("%disleft\n",*p);

}

2.参考源程序:

#include

intlength(char*p);

main()

{

intlen;

charstr[50];

printf("Pleaseinputastring:

\n");

scanf("%s",str);

len=length(str);

printf("thestringhas%dcharacters.",len);

}

intlength(char*p)

{

intn=0;

while(*p!

='\0')

{

n++;

p++;

}

returnn;

}

3.参考源程序:

#include

#include

main()

{

charstr1[20],str2[20],*p1,*p2;

intsum=0;

printf("Pleaseinputthestring:

\n");

gets(str1);

printf("Pleaseinputthesubstring:

\n");

gets(str2);

p1=str1;p2=str2;

while(*p1!

='\0')

{

if(*p1==*p2)

{

while(*p1==*p2&&*p2!

='\0')

{

p1++;

p2++;

}

}

else

p1++;

if(*p2=='\0')

sum++;

p2=str2;

}

printf("%d",sum);

}

第8章

一、选择题:

1.B2.C3.D4.B5.A

二、填空题:

1.Hell2.12303.3

上机实验

1.参考源程序:

#include

#include

structstudent

{

charnum[6];

charname[8];

intscore[3];

floatavr;

}stu[5];

main()

{

inti,j,sum;

FILE*fp;

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

{

printf("PleaseinputNo.%dscore:

\n",i);

printf("stuNo:

");

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

printf("name:

");

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

sum=0;

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

{

printf("score%d.",j+1);

scanf("%d",&stu[i].score[j]);

sum+=stu[i].score[j];

}

stu[i].avr=sum/3.0;

}

fp=fopen("stud","w");

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

if(fwrite(&stu[i],sizeof(structstudent),1,fp)!

=1)

printf("filewriteerror\n");

fclose(fp);

}

2.参考源程序:

#include

main()

{

intc,d;

charfname[20];

FILE*fp;

printf("Pleaseinputthefilename:

");

gets(fname);

if((fp=fopen(fname,"r"))==NULL)

exit(0);

while((c=fgetc(fp))!

=EOF)

if(c=='/')/*如果是字符注释的起始字符'/'*/

if((d=fgetc(fp))=='*')/*则判断下一个字符是否为'*'*/

{

c=fgetc(fp);

d=fgetc(fp);

while(c!

='*'||d!

='/')

{/*连续的两个字符不是*和/则继续处理注释*/

c=d;

d=fgetc(fp);

}

}

else/*否则原样输出读入的两个字符*/

{

putchar(c);

putchar(d);

}

else

putchar(c);

}

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

当前位置:首页 > 经管营销 > 经济市场

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

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