我是初学者,写了很多基础程序。我的新时间表程序有些问题,我想计算总停车费并将其与汽车号码和小时数一起显示在屏幕上。这个序列必须是汽车,小时,收费。 在我的程序中,只有scanf()向用户询问小时数。用户输入小时并回车,程序就会得到新的一行。我希望输出结果像这样:
汽车 小时 收费
Car Hours Charge
1 5 3.00
但是程序的输出结果如下:
Car Hours Charge
1 5
3.00
这是我的程序源代码:
#include<stdio.h>
double calculateCharges ( double time1 );
int main( void )
{ //open main
double time;
int i;
double TotalCharges=0, TotalTime=0;
printf("Car\tHours\tCharge\t\n");
for(i=1;i<=3;i++) //there is 3 cars checkin
{ //open for
printf("%d\t",i);
scanf("%lf", &time);
printf("\t");
TotalTime+=time;
printf("%lf",calculateCharges(time) ); // fonks calculate
TotalCharges+=calculateCharges(time); // for total charge
puts("");
} // end for
} // end main
double calculateCharges ( double time1 )
{ //open fonk
double totalC=0;
if( time1<=3) // untill 3 hours, for 2 dolars
{ //open if
totalC+=2.00;
} //end if
else if(time1>3) // after 3 hours, each hours cost 0.5 dolars
{ //open else if
totalC+=2+(time1-3)*0.5;
} //end else if
return totalC;
} // end fonk
printf("\t");
更改为printf("%lf\t", time);
,对我来说它可以正常工作。 - Thirupathi Thangavel