欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOCX文档下载  

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

    • 资源ID:3171068       资源大小:38.50KB        全文页数:12页
    • 资源格式: DOCX        下载积分:6.99金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要6.99金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    C Primer Plus第六 第十二章编程练习答案1. #include<stdio.h> 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); return n; 2. /*pe12-2a.h*/ #include<stdio.h> voidset_mode(int mode); voidget_info; voidshow_info; /*pe12-2a.c*/ #include<stdio.h> #include"pe12-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("Enter 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* (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<stdio.h> voidset_mode(int mode); voidget_info(int mode); voidshow_info(int mode, float distance, float fuel); /*pe12-2a.c*/ #include<stdio.h> #include"pe12-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 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); printf("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<stdio.h> #include"pe12-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); 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<stdio.h> int count=0; int fun; int main(void) inti=0; intcnt; while (i<20) cnt=fun; printf("The function has been called %d times:n", cnt); i+; getchar; return 0; int fun count+; return count; 5. /*main.c*/ #include<stdio.h> #include"rand0.h" int main(void) intStr100; int count; for(count=0; count<100; count+) Strcount=rand0; sort(Str); for (count=0; count<100; count+) if (count%10=0) putchar('n'); printf("%3d ", Strcount); getchar; return 0; /*fun.h*/ #include<stdio.h> #include<stdlib.h> int rand0(void); void sort(int *str); /*fun.c*/ #include<stdio.h> #include"rand0.h" int rand0(void) intnum; num=rand%10+1; returnnum; void sort(int *str) inti, j, temp; for (i=0; i<100; i+) for (j=0; j<100-i-1; j+) if (strj<strj+1) temp=strj; strj=strj+1; strj+1=temp; 6. #include<stdio.h> #include<stdlib.h> int rand0; int main(void) intStr10=0; int count; int value; for(count=0; count<1000; count+) value=rand0; Strvalue-1+; for (count=0; count<10; count+) printf("%3d ", Strcount); getchar; return 0; int rand0 intnum; num=rand%10+1; returnnum; 7. /*main.c*/ #include<stdio.h> #include<stdlib.h> #include<time.h> #include"diceroll.h" int main(void) int dice, roll; int sides, sets; int status; inti; srand( (unsignedint) time(0) ); printf("Enter the number of sets; enter q to stop.n"); while(scanf("%d", &sets)=1 && sets>0) if (sets='q') break; printf("Enter the number of sides per die, 0 to stop.n"); if(scanf("%d", &sides)=1 && sides>0) printf("How many dice?n"); if( (status =scanf("%d", &dice)!=1) if (status=EOF) break; else printf("you should hane entered an integer."); printf(" Let's 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; i<sets; i+) roll=roll_n_dice(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*/ #include"diceroll.h" #include<stdio.h> #include<stdlib.h> 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(sides<2) printf("Need at least 2 sides.n"); return -2; if (dice<1) printf("Need at least 1 die.n"); return -1; for (d=0; d<dice; d+) total +=rollem(sides); return total; 8. #include<stdio.h> #include<stdlib.h> 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 && size>0) printf("enter the initialization value: "); 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; i<elem; i+) ptdi=val; returnptd; voidshow_array(constintar, intn) inti; for (i=0; i<n; i+) if (i%8=0) putchar('n'); printf("%3d ", ari); putchar('n'); 9. #include<stdio.h> #include<stdlib.h> #include<string.h> 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; i<num; 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;

    注意事项

    本文(《C Primer Plus》第六 第十二章编程练习答案.docx)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开