c语言详解第五版第四章程序设计项目答案.docx
- 文档编号:11733718
- 上传时间:2023-03-31
- 格式:DOCX
- 页数:22
- 大小:18.87KB
c语言详解第五版第四章程序设计项目答案.docx
《c语言详解第五版第四章程序设计项目答案.docx》由会员分享,可在线阅读,更多相关《c语言详解第五版第四章程序设计项目答案.docx(22页珍藏版)》请在冰豆网上搜索。
c语言详解第五版第四章程序设计项目答案
C语言详解(第五版)第四章程序设计项目答案
1.Keith的SheetMusic需要一个程序来实现其为音乐教师定制的折扣政策。
程序提示用户输入购买总额并且表明购买者是不是教师。
#include
#include
#definediscount_10.12
#definediscount_20.1
#definetax_rate0.05
voidinstruct_purchase(void);
doubletotal(doubletotal_purchase,chara[1]);
intmain(void)
{voidinstruct_purchase(void);
doubletotal(double,char);
doubletotal_purchase,total_payment,total1;
chara;
instruct_purchase();
printf("Pleaseenterthecreerteacher(entert)ornot(entern).>>>\n");
scanf("%c",&a);
printf("Pleaseenterthetotalpurchase.>>>\n");
scanf("%lf",&total_purchase);
total1=total(total_purchase,a);
printf("Thetotalpaymentis%.2f.",total1);
system("pause");
return(0);}
voidinstruct_purchase(void)
{printf("Thisprogramfigureatotalbill.");
printf("Thetaxrateis$%.2f.\n",tax_rate);
printf("Thediscountoftotalpurchasewhichismorethan100dollarsis$%.2f.\n",discount_1);
printf("Thediscountoftotalpurchasewhichislessthan100dollarsis$%.2f.\n",discount_2);
printf("Pleasepress
}
doubletotal(doubletotal_purchase,chara)
{doublediscount_total,total_payment,tax;
if(a=='t')
if(total_purchase>100)
{discount_total=total_purchase*discount_1;
tax=total_purchase*tax_rate;
total_payment=total_purchase+tax-discount_total;
}
else
{discount_total=total_purchase*discount_2;
tax=total_purchase*tax_rate;
total_payment=total_purchase+tax-discount_total;
}
else
{tax=total_purchase*tax_rate;
total_payment=total_purchase+tax;
}
return(total_payment);
}
3.罗盘的度数换算为罗盘的方位。
#include
#include
voiddirection(doubleangle_degree);
intmain(void)
{doubleangle_degree;
printf("Pleaseentertheangleofthetotalstation.>>>\n");
scanf("%lf",&angle_degree);
printf("Theangleofthetotalstationis%.2f°?
.\n\n\n",angle_degree);
direction(angle_degree);
system("pause");
return(0);
}
voiddirection(doubleangle_degree)
{doubleangle;
if(angle_degree==0)
{printf("Thedirectioniseast(E).\n\n");
}
if(0 {printf("Thedirectionisnorth(N)%.2feast(E).\n\n",angle_degree); } if(angle_degree==90) {printf("Thedirectionisnorth(N)."); } if(90 {angle=180-angle_degree; printf("Thedirectionisnorth(N)%.2fwest(W).\n\n",angle); } if(angle_degree==180) {printf("Thedirectioniswest(W).\n\n"); } if(180 {angle=angle_degree-180; printf("Thedirectionissouth(S)%.2fwest(W).\n\n",angle); } if(angle_degree==270) {printf("Thedirectionissouth(S).\n\n"); } if(270 {angle=angle_degree-270; printf("Thedirectionisnorth(N)%.2feast(E).\n\n",angle); } } 4.编写一个程序根据压缩气囊颜色的第一个字母确定压缩罐的内容。 #include #include voidcolor(charfa); intmain(void) {charfa; printf("Pleaseenterthefirstalphabetofthecontainer.>>\n"); scanf("%c",&fa); color(fa); system("pause"); return(0); } voidcolor(charfa) {switch(fa) {case'O': case'o': printf("Thecolorsuggestthecontentisammoniagas.\n\n"); break; case'B': case'b': printf("ThecolorsuggestthecontentisCO.\n\n"); break; case'Y': case'y': printf("Thecolorsuggestthecontentishydrogen.\n\n"); break; case'G': case'g': printf("Thecolorsuggestthecontentisoxygen.\n\n"); break; default: printf("Thereisnotthecontainer."); }} 5.对地震的理氏数进行描述。 #include #include voidRichter_scale(doublea); intmain(void) {doublea; printf("PleaseentertheRichterscaleoftheearthquake.>>>"); scanf("%lf",&a); Richter_scale(a); system("pause"); return(0); } voidRichter_scale(doublea) {if(a>0&&a<5.0) printf("破? 坏¦Ì很¨¹小? 或¨°者? 没? 有®D.\n"); elseif(a<5.5) printf("一°? 些? 破? 坏¦Ì.\n"); elseif(a<6.5) printf("严? 重? 破? 坏¦Ì;ê? 墙? 会¨¢倒Ì1塌¨².\n"); elseif(a<7.5) printf("灾? 难? ;ê? 房¤? 屋Y或¨°者? 建¡§筑t物? 会¨¢倒Ì1塌¨².\n"); elseif(a>=7.5) printf("大䨮灾? 难? ;ê? 大䨮部? 分¤? 建¡§筑t物? 被À? 破? 坏¦Ì.\n"); else printf("Thereisnotthismagnititudeofearthquake.\n"); } (2)switch函数编写 #include #include charRichter_scale(doublea); voidsf(charb); intmain(void) {doublea; charb; printf("PleaseentertheRichterscaleoftheearthquake.>>>"); scanf("%lf",&a); b=Richter_scale(a); sf(b); system("pause"); return(0); } charRichter_scale(doublea) {charc; if(a>0&&a<5.0) {c='A'; return(c); } elseif(a<5.5) {c='B'; return(c); } elseif(a<6.5) {c='C'; return(c); } elseif(a<7.5) {c='D'; return(c); } elseif(a>=7.5) {c='E'; return(c); } elseif(a<=0) {c='F'; return(c); } } voidsf(charb) {switch(b) {case'A': printf("破? 坏¦Ì很¨¹小? 或¨°者? 没? 有®D。 ¡ê\n"); break; case'B': printf("一°? 些? 破? 坏¦Ì。 ¡ê\n"); break; case'C': printf("严? 重? 破? 坏¦Ì;ê? 墙? 会¨¢倒Ì1塌¨²。 ¡ê\n"); break; case'D': printf("灾? 难? ;ê? 房¤? 屋Y和¨ª建¡§筑t物? 会¨¢倒Ì1塌¨²。 ¡ê\n"); break; case'E': printf("大䨮灾? 难? ;ê? 大䨮部? 分¤? 建¡§筑t物? 被À? 破? 坏¦Ì。 ¡ê\n"); break; case'F': printf("无T该? 地Ì? 震e等̨¨级? 。 ¡ê\n"); break; }} 6.判断一个点在哪个象限 #include #include voidquadrant(doublea,doubleb); intmain(void) {doublea,b; printf("Pleaseenterthecoordinateofthedot.>>"); scanf("%lf%lf",&a,&b); quadrant(a,b); system("pause"); return(0); } voidquadrant(doublea,doubleb) {if(a==0&&b! =0) {printf("(%.2f,%.2f)isontheyaxis.\n",a,b); } if(a! =0&&b==0) {printf("(%.2f,%.2f)isonthexaxis.\n",a,b); } if(a>0&&b>0) {printf("(%.2f,%.2f)isinquadrantⅠ.\n",a,b); } if(a>0&&b<0) {printf("(%.2f,%.2f)isinthequadrantⅣ.\n",a,b); } if(a<0&&b>0) {printf("(%.2f,%.2f)isinthequadrantⅡ.\n",a,b); } if(a<0&&b<0) {printf("(%.2f,%.2f)isinthequadrantⅢ.\n",a,b); } } 7.一年中的某一天问题。 #include #include voidorder(intyear,intmonth,intday,inte); intleap(intyear); intmain(void) {voidorder(int,int,int,int); intleap(int); intyear,month,day,e; printf("Pleaseenterthedate(yearmonthday).>>"); scanf("%d%d%d",&year,&month,&day); e=leap(year); order(year,month,day,e); system("pause"); return(0); } intleap(intyear) {inta,b,c,d; a=year%4; b=year%100; c=year%400; if((a==0&&b==0)||c==0) {d=1;} else {d=0;} return(d); } voidorder(intyear,intmonth,intday,inte) {intdayth; if(e==1) {if(month==1) {dayth=day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==2) {dayth=31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==3) {dayth=31+29+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==4) {dayth=31+29+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==5) {dayth=31+29+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==6) {dayth=31+29+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==7) {dayth=31+29+31+30+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==8) {dayth=31+29+31+30+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==9) {dayth=31+29+31+30+31+30+31+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==10) {dayth=31+29+31+30+31+30+31+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==11) {dayth=31+29+31+30+31+30+31+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==12) {dayth=31+29+31+30+31+30+31+31+30+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } } else {if(month==1) {dayth=day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==2) {dayth=31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==3) {dayth=31+28+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==4) {dayth=31+28+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==5) {dayth=31+28+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==6) {dayth=31+28+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==7) {dayth=31+28+31+30+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==8) {dayth=31+28+31+30+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==9) {dayth=31+28+31+30+31+30+31+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==10) {dayth=31+28+31+30+31+30+31+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==11) {dayth=31+28+31+30+31+30+31+31+30+31+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } if(month==12) {dayth=31+28+31+30+31+30+31+31+30+31+30+day; printf("Thisdayisthe%ddayof%dyear.\n",dayth,year); } } } 9.居民用电 #include #include #defineR_price0.052 #defineC_price0.045 #defineI_price_P0.065 #defineI_price_NP0.028 voidtype_consumption(chartype,doublee_consumption); voidR_consumption(doublee_consumption); voidC_consumption(doublee_consumption); voidI_consumption(doublee_consumption); intmain(void) {doublee_consumption; chartype; printf("ThealphabetRmeansreside,Cmeanscommercial,Imeansindustry.>>\n"); printf("Pleaseentertheapplicationtype(R,CorI).>>\n"); scanf("%c",&type); printf("Pleaseentertheconsumptionofelectricity.>>\n"); scanf("%lf",&e_consumption); type_consumption(type,e_consumption); system("pause"); return0; } voidtype_consumption(chartype,doublee_consumption) { if(type=='R') {R_consumption(e_consumption);} elseif(type=='C') {C_consumption(e_c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 详解 第五 第四 章程 设计 项目 答案