C语言详解(第五版)程序设计项目答案第二章1.编写一个程序,以每英里0.35的费率计算一个售货员的里程偿还金。
程序中要求与用户进行如下方式的交互。
#include<stdio.h>#include<stdlib.h>int main(void){float mile_1,mile_2,miles,consume,payment;printf("MILEAGE REIMBURSEMENT CALCULATOR\n");printf("Enter begainning odometer reading>\n");scanf("%f",&mile_1);printf("Enter endding odometer reading>\n");scanf("%f",&mile_2);printf("Enter consume per mile>\n");scanf("%f",&consume);miles=mile_2-mile_1;payment=miles*consume;printf("You traveled%f miles.At%f per mile,your reimbursementis%f",miles,consume,payment);system("pause");return(0);}2.编写一个程序,用于辅助设计水力发电大坝。
提示用户输入大坝高度,以及水流量。
如果水由重力所做的功有90%转化为电能,测算会有多少M瓦的功率。
#include<stdio.h>#include<stdlib.h>int main(void){double density,weight,gravitational_acceleration,efficiency,height,water_flow_rate,work,electricity_power;printf("Please type in the density of water>");scanf("%lf",&density);printf("Please type in the gravitational_acceleration>");scanf("%lf",&gravitational_acceleration);printf("Please type in the height of the water>");scanf("%lf",&height);printf("Please type in the efficiency of the transformation\nfrom water to electricity>");scanf("%lf",&efficiency);printf("Please type in the water_flow_rate>");scanf("%lf",&water_flow_rate);weight=density*water_flow_rate;work=weight*height*gravitational_acceleration;electricity_power=work*efficiency/1000000;printf("The electric energy production is%fW",electricity_power);system("pause");return0;}3.编写一个程序,用于预测冰箱内的温度,断电后经过的时间是给定的。
温度公式:。
要求程序提示用户输入改时间,它以整数小时和分钟表示。
需要注意的是,经历的世间应该转化为小时。
#include<stdio.h>#include<stdlib.h>int main(void){double time,temperature;int hour,minute;printf("Please type in the time,include hour and minute.For example230(2void space 30)>");scanf("%d%d",&hour,&minute);time=hour+minute/60;temperature=time*time*4/(time+2)-20;printf("The temperature of the refrigerator is%f℃?",temperature);system("pause");return0;}4.将华氏温度转换成摄氏温度。
#include<stdio.h>#include<stdlib.h>int main(void){int fahrenheit;double centigrade;printf("Please type in temperature in fahrenheit>");scanf("%d",&fahrenheit);centigrade=(fahrenheit-32)*5/9;printf("The temperature%d in fahrenheit equals to the temperature%f in centigrade",fahrenheit,centigrade);system("pause");return0;}5.编写一个程序,将两个数作为输入数据,并显示它们的和差积商;#include<stdio.h>#include<stdlib.h>int main(void){double x,y;double sum;double difference;double product;double quotient;printf("Please type in x and y>");scanf("%lf%lf",&x,&y);sum=x+y;difference=x-y;product=x*y;quotient=x/y;printf("the sum of x and y is%.2f",sum);printf("the difference of x and y is%.2f",difference);printf("the product of x and y is%.2f",product);printf("the quotient of x and y is%.2f",quotient);system("pause");return0;}6.如果要使一门课程获得所希望的分数等级没那么预测一下,在期末考试中所需要的分数。
编写一个程序实现该目标,程序与用户按如下方式交互:#include<stdio.h>#include<stdlib.h>int main(void){char rank;double minimum_average,current_average,score,percentage;printf("Please enter the desired rank>");scanf("%c",&rank);printf("Please enter the minimum average required>");scanf("%lf",&minimum_average);printf("Please enter the current average in course>");scanf("%lf",¤t_average);printf("Please enter how much the final counts as a percentage of the course grade>"); scanf("%lf",&percentage);score=minimum_average/(percentage/100)-current_average*(100-percentage)/percentage ;printf("You need a score of%.2f on the final to get a%c",score,rank);system("pause");return0;}7.编写一个程序,计算在给定燃油的加仑数和房屋内燃油的效率后,可以给房屋释放多少热量。
#include<stdio.h>#include<stdlib.h>int main(void){double volume,energy,efficiency;printf("Please enter the volume of oil>");scanf("%lf",&volume);printf("Please enter the efficiency of oil>");scanf("%lf",&efficiency);energy=volume/42*5800000*efficiency;printf("The oil provide%.2fBtu energy for the house",energy);system("pause");return0;}9.计算剪草坪的时间#include<stdio.h>#include<stdlib.h>int main(void){double length,width,velocity;double area,time;printf("Please enter the length and width>");scanf("%lf%lf",&length,&width);area=length*width;printf("Please enter the velocity of mowing>");scanf("%lf",&velocity);time=area/velocity;printf("The time consumed for mowing lawns is%.2fs",time);system("pause");return0;}10.编写一个程序,显示两个分数乘积后的分子与分母,并且显示该乘积的百分比。