当前位置:文档之家› c语言详解(第五版)第三章程序设计项目答案

c语言详解(第五版)第三章程序设计项目答案

c语言详解(第五版)第三章程序设计项目答案1.假设买一辆车首付为500dollar。

请计算月供。

#include<stdio.h>#include<math.h>#include<stdlib.h>int main(void){double capital_sum,monthly_interest_rate,initial_payment,temp,payment,terms; printf("Please enter the price of the car>>");scanf("%lf",&capital_sum);printf("Please enter the monthly interest rate>>");scanf("%lf",&monthly_interest_rate);printf("Please enter the terms duration of loan>>");scanf("%lf",&terms);printf("Please enter the initial_payment>>");scanf("%lf",&initial_payment);temp=1+monthly_interest_rate;payment=(capital_sum-500)*monthly_interest_rate/(1-pow(temp,-terms));printf("The monthly contribution is %.2f dollars.",payment);printf("The capital sum is %.2f dollars.",capital_sum);system("pause");return 0;}2.编写两个函数,一个显示三角形,另一个显示矩形。

#include<stdio.h>#include<math.h>#include<stdlib.h>void draw_triangle(void);void draw_rectangle(void);int main(void){draw_triangle();draw_rectangle();system("pause");return 0;}/*Draw an triangle.*/void draw_triangle(void){ printf(" /\\ \n");printf(" / \\ \n");printf(" / \\ \n");printf(" / \\ \n");printf(" -------- \n");}void draw_rectangle(void){ printf(" ------------ \n");printf("| |\n| |\n| |\n");printf(" ------------ \n");}4.编写一个计算机程序,用于计算导弹飞行的实践和它击中目标时距离地面的高度。

#include<stdio.h>#include<math.h>#include<stdlib.h>#define G 32.17double time(); /*必须要有*/double height(); /*必须要有*/int main(void){double time(double,double,double); /*必须要有*/double height(double,double,double); /*必须要有*//double theta,distance,velocity,a,b;printf("The theta of elevation is>>");scanf("%lf",&theta);printf("The distance to target is>>");scanf("%lf",&distance);printf("The projectile velocity (ft/sec) is>>");scanf("%lf",&velocity);a=time(theta,distance,velocity); /*赋值将函数转化为变量*/b=height(a,theta,velocity); /*赋值将函数转化为变量*/printf("The time is %f seconds and the height is %f ft.",a,b);system("pause");return 0;}double time(double theta,double distance,double velocity) /*数据类型一定要写好*/ {double time;time=distance/(velocity*cos(theta));return(time);}double height(double a,double theta,double velocity) /*数据类型一定要写好*/ {double height;height=velocity*sin(theta)*a-G*pow(a,2)/2;return(height);}5.编写一个四舍五入的函数#include<stdio.h>#include<math.h>#include<stdlib.h>double scale(double x,int n);int main(void){double scale(double,int);int n=2;printf("Please enter x>>");scanf("%lf",&num_1);printf("The rounded x is %.2f.",scale(num_1,n));system("pause");return 0;}double scale(double num_1,int n){double y,rounded_x;int z;y=num_1*100;z=(int)(y+0.5);rounded_x=(double)(z);return(rounded_x/100);}6.计算1km路程中的速度#include<stdio.h>#include<math.h>#include<stdlib.h>double mile_velocity(double miles,double time);double meter_velocity(double kilometers,double time);int main(void){ double mile_velocity(double,double);double meter_velocity(double,double);double time,kilometers,minutes,seconds;printf("please enter the length of race>>");scanf("%lf",&kilometers);printf("please enter the time of race>>");scanf("%lf%lf",&minutes,&seconds);time=minutes*60+seconds;printf("the velocity of candidate is %.2f mps.\n",meter_velocity(kilometers,time)); /*一个函数只能返回一个数值。

*/printf("the velocity of candidate is %.2f fps.\n",mile_velocity(kilometers,time)); system("pause");return 0;}double mile_velocity(double kilometers,double time){double velocity,miles;miles=kilometers*3282/5280;velocity=miles/time;return(velocity);}double meter_velocity(double kilometers,double time)velocity=kilometers/time;return(velocity);}7.买房子,5年的开销。

#include<stdio.h>#include<math.h>#include<stdlib.h>double feul_cost(double x);double property_tax(double initial_payment,double tax_rate);int main(void){double x,tax_rate,initial_payment;printf("Please enter the feul cost per year>>");scanf("%lf",&x);printf("Please enter the property rate per year>>");scanf("%lf",&tax_rate);printf("Please enter the initial payment>>");scanf("%lf",&initial_payment);printf("The feul cost of five years is %.2f dollars\n",feul_cost(x));printf("The property tax of five years is %.2fdollars\n",property_tax(initial_payment,tax_rate));system("pause");return 0;}double feul_cost(double x){return(x*5);}double property_tax(double initial_payment,double tax_rate){return(initial_payment*tax_rate*5);}8.计算一个人骑着自行车的加速度和降速所需要的时间#include<stdio.h>#include<stdlib.h>#include<math.h>double deceleration(double initial_velocity,double final_velocity,double time); double deceleration_time(double initial_velocity,double a);int main(void){double initial_velocity,final_velocity,time,a,t;printf("Please enter the initial velocity>>");scanf("%lf",&initial_velocity);printf("Please enter the final velocity>>");scanf("%lf",&final_velocity);printf("Please enter the deceleration time>>");scanf("%lf",&time);a=deceleration(initial_velocity,final_velocity,time);t=deceleration_time(initial_velocity,a);printf("The deceleration is %.2f unit.",a);printf("The deceleration time is %.2f unit.",t);system("pause");return 0;}double deceleration(double initial_velocity,double final_velocity,double time){double a;a=(final_velocity-initial_velocity)/time;return(a);}double deceleration_time(double initial_velocity,double a){double t;t=fabs(initial_velocity/a);return(t);}9.计算每个容器的开销和所有容器总共的开销。

相关主题