運行下面的代碼,我希望收到這樣的輸出:setprecision和浮點
Desired output:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
但結果出來如下:
Actual output:
Car Hours Charge
1 2 2
2 4 2.5
3 2e+01 10
,可以引導我使用的任何建議正確浮點數比較和使用setprecision正確的是w迎閱讀。
int main()
{
double hour[3];
double charge[3];
double sum_hour = 0;
double sum_charge = 0;
for (int i = 0; i < 3; i++)
{
cout<<"Enter the hours for car No. "<<i<<": ";
cin>>hour [i];
if (hour [i] <= 3)
{charge [i] = 2.00;}
if (hour [i] > 3 && hour [i] < 24)
{charge [i] = (2.00 + (ceil(hour [i] -3))*0.5);}
if (hour [i] == 24)
{charge [i] = 10.00;}
sum_hour = sum_hour + hour [i];
sum_charge = sum_charge + charge [i];
}
cout<<"Car"<<setw(10)<<"Hours"<<setw(10)<<"Charge"<<endl;
for (int i = 0; i < 3; i++)
{
cout<<i+1<<setw(10)<<setprecision(1)<<hour[i]<<setw(10)<<setprecision(2)<<charge[i]<<endl;
}
cout<<"TOTAL"<<setw(6)<<sum_hour<<setw(10)<<sum_charge<<endl;
}
我跑的碼塊中的代碼。我只是在學習浮點和setprecision的調整 – SRYZDN 2015-01-21 12:39:01