2

私は一生、パテにあるこれらの単純な方程式の何が問題なのかを理解できません。すべてが正しく設定されていると思いますが、私の出力は私の教授のサンプル出力から少し間違っています。

//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

あなたが私を助けることができれば、それは命の恩人になるでしょう.

4

2 に答える 2

1

labor=(((float)feet/115)*18*8);正確性の問題を修正する必要があります。同様にpaint =

これが機能する理由は、C++計算の機能によるものです。a + bのような式を計算する場合、両方が最も正確な共通の型に自動的にキャストされます

ただし、整数を整数で割るとfeet/115、結果は値に割り当てる前にintとして計算されます。これは、その計算の小数点以下の桁数が失われるため、精度が失われることを意味します。floatlabor

たとえば、feet = 120の場合、答えfeet/115は1.04ではなく1になります。

これを修正する別の方法は、115.0fと記述して115をfloatに変換することです。

于 2012-11-07T01:33:08.083 に答える
0

あなたの足は整数です。整数を除算すると、小数点以下の数値は無視されます。たとえばint a = 10; 、3.33333333ではなくa / 3=3です。(float)intをfloatにキャストして、機能するようにします。

于 2012-11-07T01:32:23.647 に答える