一道C程序题

来源:百度知道 编辑:UC知道 时间:2024/05/27 21:04:22
机械工业出版社的C程序设计教程的习题
题目是这样的:
由于汽油价格很高,司机们非常关心汽车所走的路.每个司机都通过记录公里数和每个油罐耗油的加仑数来了解几个油罐的使用率.编一个C程序,输入每一个油罐使汽车所行驶的公里数和消耗汽油的加仑数,计算并显示每加仑汽油使汽车行驶的路程.处理完所有数据后,计算并打印出对所有油罐来说平均每加仑汽油使汽车行驶的公里数.
输出形式为:
Enter the gallons used:12.8
Enter the miles driven:287
The miles/gallon for this tank was 22.421875

Enter the gallons used:10.3
Enter the miles driven:200
The miles/gallon for this tank was 19.417475

Enter the gallons used:5
Enter the miles driven:120
The miles/gallon for this tank was 24.000000

Enter the gallons used :-1

The overall average miles/gallon was 21.601423

这是我打的程序
#include<stdio.h>
void main()
{
float miles,gallon,counter=1,average,total=0;
printf("Enter the gallons used :");
scanf("%f",&gallon);
printf("Enter the miles driven :");
scanf("%f",&miles);
printf("The

#include<stdio.h>
void main()
{
float miles,gallon,counter=1,average,total=0;
printf("Enter the gallons used :");
scanf("%f",&gallon);

//你在这个地方输入gallon,程序只会执行下一步,而不会直接跳到WHILE那步,所以判断应该在输入gallon之后立即判断,如果gallon!=-1才允许输入miles.

不知道我这个行不行:
#include<stdio.h>
void main()
{
float miles,gallon,counter=0,average,total=0;
start:
printf("Enter the gallons used :");
scanf("%f",&gallon);
if(gallon!=-1)
{
printf("Enter the miles driven :");
scanf("%f",&miles);
printf("The miles/gallon for this tank was :%f",miles/gallon);
printf("\n\n");
counter++;
total=total+miles/gallon;
goto start;
}

printf("The overall average miles/gallon was %f",total/counter);
getchar();
getchar();
}

什么啊