《C Primer Plus》第六 第十二章编程练习答案.docx

上传人:牧羊曲112 文档编号:3171068 上传时间:2023-03-11 格式:DOCX 页数:12 大小:38.50KB
返回 下载 相关 举报
《C Primer Plus》第六 第十二章编程练习答案.docx_第1页
第1页 / 共12页
《C Primer Plus》第六 第十二章编程练习答案.docx_第2页
第2页 / 共12页
《C Primer Plus》第六 第十二章编程练习答案.docx_第3页
第3页 / 共12页
《C Primer Plus》第六 第十二章编程练习答案.docx_第4页
第4页 / 共12页
《C Primer Plus》第六 第十二章编程练习答案.docx_第5页
第5页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《《C Primer Plus》第六 第十二章编程练习答案.docx》由会员分享,可在线阅读,更多相关《《C Primer Plus》第六 第十二章编程练习答案.docx(12页珍藏版)》请在三一办公上搜索。

1、C Primer Plus第六 第十二章编程练习答案1. #include int critic(void ); int main(intargc, char *argv) intnum=56; int units; printf(How many pounds to a firkin of butter?n); scanf(%d,&units); while (units!=num) units=critic; getchar; return 0; int critic int n; printf(No luck, my friend. Try again.n); scanf(%d, &n)

2、; return n; 2. /*pe12-2a.h*/ #include voidset_mode(int mode); voidget_info; voidshow_info; /*pe12-2a.c*/ #include #includepe12-2a.h int mode; float distance, fuel; voidset_mode(intm) if (m !=0 &m !=1) printf(Invalid mode specified. Mode 1(US) used.n); m=1; mode=m; voidget_info if (0=mode) printf(Ent

3、er distance traveled in kilometers: ); scanf(%f, &distance); printf(Enter fuel consumed in liters: ); scanf(%f, &fuel); else printf(Enter distance traveled in miles: ); scanf(%f, &distance); printf(Enter fuel consumed in gallons: ); scanf(%f, &fuel); voidshow_info float units; if (0=mode) units=100*

4、 (fuel/distance); printf(Fuel consumed in liters: %.1f per 100 kmn, units); else units=distance/fuel; printf(Fuel consumed is %.1f miles per gallonn, units); 3. /*pe12-2a.h*/ #include voidset_mode(int mode); voidget_info(int mode); voidshow_info(int mode, float distance, float fuel); /*pe12-2a.c*/ #

5、include #includepe12-2a.h voidset_mode(intmode) if (mode !=0 &mode !=1) printf(Invalid mode specified. Mode 1(US) used.n); mode=1; get_info(mode); voidget_info(intmode) float distance, fuel; if (0=mode) printf(Enter distance traveled in kilometers: ); scanf(%f, &distance); printf(Enter fuel consumed

6、 in liters: ); scanf(%f, &fuel); else printf(Enter distance traveled in miles: ); scanf(%f, &distance); printf(Enter fuel consumed in gallons: ); scanf(%f, &fuel); show_info(mode, distance, fuel); voidshow_info(intmode, floatdistance, floatfuel) float units; if (0=mode) units=100* (fuel/distance); p

7、rintf(Fuel consumed in liters: %.1f per 100 kmn, units); else units=distance/fuel; printf(Fuel consumed is %.1f miles per gallonn, units); /*pe12-2b.c*/ #include #includepe12-2a.h int main(void) int mode; printf(Enter 0 for metric mode, 1 for US mode: ); scanf(%d, &mode); while(mode=0) set_mode(mode

8、); printf(Enter 0 for metric mode, 1 for US mode: ); printf(-1 to quit): ); scanf(%d, &mode); printf(Done,n); getchar; return 0; 4. #include int count=0; int fun; int main(void) inti=0; intcnt; while (i20) cnt=fun; printf(The function has been called %d times:n, cnt); i+; getchar; return 0; int fun

9、count+; return count; 5. /*main.c*/ #include #includerand0.h int main(void) intStr100; int count; for(count=0; count100; count+) Strcount=rand0; sort(Str); for (count=0; count100; count+) if (count%10=0) putchar(n); printf(%3d , Strcount); getchar; return 0; /*fun.h*/ #include #include int rand0(voi

10、d); void sort(int *str); /*fun.c*/ #include #includerand0.h int rand0(void) intnum; num=rand%10+1; returnnum; void sort(int *str) inti, j, temp; for (i=0; i100; i+) for (j=0; j100-i-1; j+) if (strjstrj+1) temp=strj; strj=strj+1; strj+1=temp; 6. #include #include int rand0; int main(void) intStr10=0;

11、 int count; int value; for(count=0; count1000; count+) value=rand0; Strvalue-1+; for (count=0; count10; count+) printf(%3d , Strcount); getchar; return 0; int rand0 intnum; num=rand%10+1; returnnum; 7. /*main.c*/ #include #include #include #includediceroll.h int main(void) int dice, roll; int sides,

12、 sets; int status; inti; srand( (unsignedint) time(0) ); printf(Enter the number of sets; enter q to stop.n); while(scanf(%d, &sets)=1 & sets0) if (sets=q) break; printf(Enter the number of sides per die, 0 to stop.n); if(scanf(%d, &sides)=1 & sides0) printf(How many dice?n); if( (status =scanf(%d,

13、&dice)!=1) if (status=EOF) break; else printf(you should hane entered an integer.); printf( Lets begin again.n); while(getchar!=n) continue; printf(How many side? enter 0 to stop.n); continue; printf(Here are %d sets of %d %d-sided throws.n, sets, dice, sides); for (i=0; isets; i+) roll=roll_n_dice(

14、dice, sides); printf(%d , roll ); putchar(n); printf(Enter the number of sets; enter q to stop.n); printf(The rollem function was called %d times.n, roll_count); printf(GOOD FORTUNE TO YOU!n); getchar; return 0; /*diceroll.h*/ externintroll_count; introll_n_dice(int dice, int sides); /*diceroll.c*/

15、#includediceroll.h #include #include introll_count=0; staticintrollem(intsides) int roll; roll=rand%sides+1; roll_count+; return roll; introll_n_dice(intdice, intsides) int d; int total=0; if(sides2) printf(Need at least 2 sides.n); return -2; if (dice1) printf(Need at least 1 die.n); return -1; for

16、 (d=0; ddice; d+) total +=rollem(sides); return total; 8. #include #include int *make_array(intelem, intval); voidshow_array(constintar, int n); int main(void) int *pa; int size; int value; printf(enter the number od elements: ); while(scanf(%d, &size)=1 & size0) printf(enter the initialization valu

17、e: ); scanf(%d, &value); pa=make_array(size, value); if (pa) show_array(pa, size); free(pa); printf(enter the number of elements (1 to quit): ); printf(Done.n); getchar; return 0; int *make_array(intelem, intval) int *ptd; inti; ptd=(int *) malloc(elem* sizeof(double); for (i=0; ielem; i+) ptdi=val;

18、 returnptd; voidshow_array(constintar, intn) inti; for (i=0; in; i+) if (i%8=0) putchar(n); printf(%3d , ari); putchar(n); 9. #include #include #include int main(void) intnum, i; char *ptd; char *temp; printf(How many words you wish to enter? ); while ( scanf(%d, &num) =1 &num!=0) ptd=(char*)malloc(num*sizeof(char *); printf(Enter %d words now:n, num); temp=(char*) malloc(100 * sizeof(char); for (i=0; inum; i+) scanf(%s, temp); *(ptd+i)=temp; printf(%sn, *(ptd+i); free(ptd); free(temp); printf(How many words you wish to enter? ); getchar; return 0;

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号