C how to program; 习题3.17 编写一个程序:读入行驶的总公里数和每次加满油后使用的燃料数,计算并显示每次加油后的每加仑平均行驶的里程数,然后还要计算并显示所有燃料的每加仑平均行驶里程数。输入输出样例如图:
1 #include<stdio.h> 2 int main(void) 3 { 4 float gallons=0,miles,TotGallons=0,TotMiles=0; 5 while (gallons!=-1) 6 { 7 printf("Enter the gallons used (-1 to end):"); 8 scanf("%f",&gallons); 9 10 if(gallons!=-1) 11 { 12 printf("Enter the miles driven:"); 13 scanf("%f",&miles); 14 printf("The miles / gallon for this tank was: %f\n",miles/gallons); 15 TotGallons+=gallons; 16 TotMiles+=miles; 17 } 18 } 19 printf("\n"); 20 printf("The overall average miles/gallon was : %f\n",TotMiles/TotGallons); 21 return 0; 22 }