《C Primer Plus》第六版 第十二章编程练习答案.docx
- 文档编号:6030626
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:15
- 大小:17.26KB
《C Primer Plus》第六版 第十二章编程练习答案.docx
《《C Primer Plus》第六版 第十二章编程练习答案.docx》由会员分享,可在线阅读,更多相关《《C Primer Plus》第六版 第十二章编程练习答案.docx(15页珍藏版)》请在冰豆网上搜索。
《CPrimerPlus》第六版第十二章编程练习答案
1.
#include
intcritic(void);
intmain(intargc,char*argv[])
{
intnum=56;
intunits;
printf("Howmanypoundstoafirkinofbutter?
\n");
scanf("%d",&units);
while(units!
=num)
{
units=critic();
}
getchar();
return0;
}
intcritic()
{
intn;
printf("Noluck,myfriend.Tryagain.\n");
scanf("%d",&n);
returnn;
}
2.
//***********pe12-2a.h*******//
#include
voidset_mode(intmode);
voidget_info();
voidshow_info();
//**********pe12-2a.c*************//
#include
#include"pe12-2a.h"
intmode;
floatdistance,fuel;
voidset_mode(intm)
{
if(m!
=0&&m!
=1)
{
printf("Invalidmodespecified.Mode1(US)used.\n");
m=1;
}
mode=m;
}
voidget_info()
{
if(0==mode)
{
printf("Enterdistancetraveledinkilometers:
");
scanf("%f",&distance);
printf("Enterfuelconsumedinliters:
");
scanf("%f",&fuel);
}
else
{
printf("Enterdistancetraveledinmiles:
");
scanf("%f",&distance);
printf("Enterfuelconsumedingallons:
");
scanf("%f",&fuel);
}
}
voidshow_info()
{
floatunits;
if(0==mode)
{
units=100*(fuel/distance);
printf("Fuelconsumedinliters:
%.1fper100km\n",units);
}
else
{
units=distance/fuel;
printf("Fuelconsumedis%.1fmilespergallon\n",units);
}
}
3.
//***********pe12-2a.h*******//
#include
voidset_mode(intmode);
voidget_info(intmode);
voidshow_info(intmode,floatdistance,floatfuel);
//***********pe12-2a.c*******//
#include
#include"pe12-2a.h"
voidset_mode(intmode)
{
if(mode!
=0&&mode!
=1)
{
printf("Invalidmodespecified.Mode1(US)used.\n");
mode=1;
}
get_info(mode);
}
voidget_info(intmode)
{
floatdistance,fuel;
if(0==mode)
{
printf("Enterdistancetraveledinkilometers:
");
scanf("%f",&distance);
printf("Enterfuelconsumedinliters:
");
scanf("%f",&fuel);
}
else
{
printf("Enterdistancetraveledinmiles:
");
scanf("%f",&distance);
printf("Enterfuelconsumedingallons:
");
scanf("%f",&fuel);
}
show_info(mode,distance,fuel);
}
voidshow_info(intmode,floatdistance,floatfuel)
{
floatunits;
if(0==mode)
{
units=100*(fuel/distance);
printf("Fuelconsumedinliters:
%.1fper100km\n",units);
}
else
{
units=distance/fuel;
printf("Fuelconsumedis%.1fmilespergallon\n",units);
}
}
//***********pe12-2b.c*******//
#include
#include"pe12-2a.h"
intmain(void)
{
intmode;
printf("Enter0formetricmode,1forUSmode:
");
scanf("%d",&mode);
while(mode>=0)
{
set_mode(mode);
printf("Enter0formetricmode,1forUSmode:
");
printf("(-1toquit):
");
scanf("%d",&mode);
}
printf("Done,\n");
getchar();
return0;
}
4.
#include
intcount=0;
intfun();
intmain(void)
{
inti=0;
intcnt;
while(i<20)
{
cnt=fun();
printf("Thefunctionhasbeencalled%dtimes:
\n",cnt);
i++;
}
getchar();
return0;
}
intfun()
{
count++;
returncount;
}
5.
//*******main.c***************//
#include
#include"rand0.h"
intmain(void)
{
intStr[100];
intcount;
for(count=0;count<100;count++)
{
Str[count]=rand0();
}
sort(Str);
for(count=0;count<100;count++)
{
if(count%10==0)
{
putchar('\n');
}
printf("%3d",Str[count]);
}
getchar();
return0;
}
//*********fun.h***********//
#include
#include
intrand0(void);
voidsort(int*str);
//************fun.c***************//
#include
#include"rand0.h"
intrand0(void)
{
intnum;
num=rand()%10+1;
returnnum;
}
voidsort(int*str)
{
inti,j,temp;
for(i=0;i<100;i++)
{
for(j=0;j<100-i-1;j++)
{
if(str[j] { temp=str[j]; str[j]=str[j+1]; str[j+1]=temp; } } } } 6. #include #include intrand0(); intmain(void) { intStr[10]={0}; intcount; intvalue; for(count=0;count<1000;count++) { value=rand0(); Str[value-1]++; } for(count=0;count<10;count++) { printf("%3d",Str[count]); } getchar(); return0; } intrand0() { intnum; num=rand()%10+1; returnnum; } 7. //****************main.c*************// #include #include #include #include"diceroll.h" intmain(void) { intdice,roll; intsides,sets; intstatus; inti; srand((unsignedint)time(0)); printf("Enterthenumberofsets;enterqtostop.\n"); while(scanf("%d",&sets)==1&&sets>0) { if(sets=='q') { break; } printf("Enterthenumberofsidesperdie,0tostop.\n"); if(scanf("%d",&sides)==1&&sides>0) { printf("Howmanydice? \n"); if((status=scanf("%d",&dice))! =1) { if(status==EOF) { break; } else { printf("youshouldhaneenteredaninteger."); printf("Let'sbeginagain.\n"); while(getchar()! ='\n') { continue; } printf("Howmanyside? enter0tostop.\n"); continue; } } printf("Hereare%dsetsof%d%d-sidedthrows.\n",sets,dice,sides); for(i=0;i { roll=roll_n_dice(dice,sides); printf("%d",roll); } putchar('\n'); printf("Enterthenumberofsets;enterqtostop.\n"); } } printf("Therollem()functionwascalled%dtimes.\n",roll_count); printf("GOODFORTUNETOYOU! \n"); getchar(); return0; } //**************diceroll.h**************// externintroll_count; introll_n_dice(intdice,intsides); //*************diceroll.c**************// #include"diceroll.h" #include #include introll_count=0; staticintrollem(intsides) { introll; roll=rand()%sides+1; roll_count++; returnroll; } introll_n_dice(intdice,intsides) { intd; inttotal=0; if(sides<2) { printf("Needatleast2sides.\n"); return-2; } if(dice<1) { printf("Needatleast1die.\n"); return-1; } for(d=0;d { total+=rollem(sides); } returntotal; } 8. #include #include int*make_array(intelem,intval); voidshow_array(constintar[],intn); intmain(void) { int*pa; intsize; intvalue; printf("enterthenumberodelements: "); while(scanf("%d",&size)==1&&size>0) { printf("entertheinitializationvalue: "); scanf("%d",&value); pa=make_array(size,value); if(pa) { show_array(pa,size); free(pa); } printf("enterthenumberofelements(<1toquit): "); } printf("Done.\n"); getchar(); return0; } int*make_array(intelem,intval) { int*ptd; inti; ptd=(int*)malloc(elem*sizeof(double)); for(i=0;i { ptd[i]=val; } returnptd; } voidshow_array(constintar[],intn) { inti; for(i=0;i { if(i%8==0) { putchar('\n'); } printf("%3d",ar[i]); } putchar('\n'); } 9. #include #include #include intmain(void) { intnum,i; char**ptd; char*temp; printf("Howmanywordsyouwishtoenter? "); while(scanf("%d",&num)==1&&num! =0) { ptd=(char**)malloc(num*sizeof(char*)); printf("Enter%dwordsnow: \n",num); temp=(char*)malloc(100*sizeof(char)); for(i=0;i { scanf("%s",temp); *(ptd+i)=temp; printf("%s\n",*(ptd+i)); } free(ptd); free(temp); printf("Howmanywordsyouwishtoenter? "); } getchar(); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C Primer Plus C Primer Plus第六版 第十二章编程练习答案 Plus 第六 第十二 编程 练习 答案