我不能为我的生活弄清楚我在腻子这些简单的方程式有什么问题。我认为一切都是正确的,但我的教授样本输出的输出是错误的。
//This program is used to calculate total cost of painting.
#include <iostream>
#include <iomanip>
using namespace std;
//These are the function prototypes that will be used for the main function.
void displayInstructions(int &feet, float &price);
float paintprice (int feet, float price);
float laborprice (int feet);
float totalcost(float paint, float labor);
void displayoutput (float paint, float labor, float total, int feet);
int main()
{
int feet=0;
float price=0;
float paint=0;
float labor=0;
float total=0;
displayInstructions(feet, paint);
paint=paintprice(feet, paint);
labor=laborprice(feet);
total=totalcost(labor, paint);
displayoutput(paint, labor, total, feet);
return 0;
}
void displayInstructions(int &feet, float &price)
{
cout<<setw(35)<<"==================================="<<endl;
cout<<setw(30)<<"Painting Cost Calculator" <<endl;
cout<<setw(35)<<"===================================" <<endl;
cout<<"This program will compute the costs (paint, labor, total)\nbased on th\
e square feet of wall space to be painted \
and \nthe price of paint." <<endl;
cout<<"How much wall space, in square feet, is going to be painted?" <<endl;
cin>>feet;
cout<<"How much is the price of a gallon of paint?" <<endl;
cin>>price;
}
float paintprice (int feet, float price)
{
float paint;
paint=((feet/115)*price);
return paint;
}
float laborprice (int feet)
{
float labor;
labor=((feet/115)*18*8);
return labor;
}
float totalcost (float paint, float labor)
{
float total;
total=(paint+labor);
return total;
}
void displayoutput (float paint, float labor, float total, int feet)
{
cout<<"Square feet:" <<feet <<endl;
cout<<"Paint cost:" <<paint <<endl;
cout<<"Labor cost:" <<labor <<endl;
cout<<"Total cost:" <<total <<endl;
}
基于输入为脚= 12900,价格= 12.00 涂料成本的最终产出应为1346.09美元 劳动力成本的最终产出应为16153.04美元
我分别得到:$ 1344.00,$ 16128.00
如果你可以帮助我,那将是一个救生员。
答案 0 :(得分:1)
labor=(((float)feet/115)*18*8);
应该修正你的正确性问题。与paint =
这可行的原因是因为方式C ++计算工作。计算像a + b这样的表达式时,两者都自动转换为最准确的常见类型。
然而,当您将整数除以整数时,如feet/115
所示,在分配给float
值之前,结果计算为int labor
。这意味着计算中的小数位丢失,因此您将失去准确性。
例如,如果脚= 120,feet/115
的答案为1,而不是1.04。
解决此问题的另一种方法是通过编写115.0f 将115转换为浮动
答案 1 :(得分:0)
你的脚是一个整数,当整数除以时,它会忽略小数点后的数字
例如int a = 10;
a / 3 = 3而不是3.33333333。 (float)将你的int转换为float,以便它可以工作。