为什么我的计算在我的程序中搞砸了

why are my calculations messed up in my program?

本文关键字:我的 程序 计算 为什么      更新时间:2023-10-16

我不太确定我的代码中哪里导致了导致错误计算的问题。当我运行程序时,出现以下警告:

C4305:"参数":从"双精度"到"浮点数"的截断。

税额(ta(和总成本(tc(似乎有问题,

Current Output:
Cost before Tax: $30.20
Tax Amount: $30.20     
Total Cost: $-107374144.00
ground beef is ex-sponged
Press any key to continue . .

What it **should** be:
Your item name:ground beef
Cost before Tax: $30.20
Tax Amount: $2.64
Total Cost: $32.84
ground beef is ex-sponged

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class item
{
public:
    item(char* = " " ,float=0.0,int=0,float=0.0);
    ~item();
    void print();
    void calc(int);
private:
    char name[20];
    int quan;
    float cost, tp, cbt, tax, tc;
};
item::~item()
{
    cout << name << " is ex-sponged"<<endl;
    system("pause");
    }
item::item(char *w,float x, int y, float z)
{
    strcpy(name, w);
    cost = x;
    quan=y;
    tp = z;
    tax=cost*quan;
    tc=cbt+tax;
    cbt = cost*quan;
}
void item::print()
{
    cout << "Your item name:" << name << endl;
    cout << "Cost before Tax: $" << cbt << endl;
    cout << "Tax Amount: $" << tax << endl;
    cout << "Total Cost: $" << tc << endl;
}
void item::calc(int n)
{
    quan += n;
    cbt = cost*quan;
     tax = cbt*tp/100;
     tc = cbt + tax;
}
int main()
{
    item i("ground beef", 7.55, 4, 8.75);
    cout << setprecision(2) << showpoint << fixed;
    i.print();
}

在构造函数中,您在初始化之前使用它cbt

tc=cbt+tax;
cbt = cost*quan;

未初始化变量的值本质上是随机的。


不相关的建议:

  • 使用 std::string 而不是 C 样式字符串(char数组(。

  • 在浮动文本上使用f后缀,为它们提供类型 float 而不是 double(从而删除警告(:7.55f而不是7.550.0f(或0.f(而不是0.0等等。

  • 不要使用浮点格式来赚钱,而要使用固定精度格式。货币应用中的舍入误差和不准确是不好的。

  • 在声明中命名参数,它用作自文档代码。

  • 通常,最好在构造函数中使用 mem-initialiser-lists,而不是分配给构造函数主体中的成员。这对于具有非平凡默认构造函数的类类型的成员尤其重要(对于无法默认初始化的成员来说完全是必需的(。由于数据成员始终按类中的声明顺序初始化,因此必须对它们重新排序。

我不知道定点格式,但是通过其他建议,您的代码将如下所示:

class item
{
public:
    item(std::string name = " " , float cost = 0.0, int quant = 0, float tp = 0.0);
    ~item();
    void print();
    void calc(int);
private:
    std::string name;
    float cost;
    int quan;
    float tp, tax, cbt, tc;
};
item::~item()
{
    cout << name << " is ex-sponged" << endl;
    system("pause");
}
item::item(std::string name, float cost, int quant, float tp)
  : name(name),
    cost(cost),
    quan(quant),
    tp(tp),
    tax(cost * quant),
    cbt(cost * quant),
    tc(cbt + tax)
{
}
void item::print()
{
    cout << "Your item name:" << name << endl;
    cout << "Cost before Tax: $" << cbt << endl;
    cout << "Tax Amount: $" << tax << endl;
    cout << "Total Cost: $" << tc << endl;
}
void item::calc(int n)
{
    quan += n;
    cbt = cost*quan;
    tax = cbt*tp/100;
    tc = cbt + tax;
}

你在初始化之前使用的是cbt

tc=cbt+tax;
cbt = cost*quan;

交换这两行,它至少应该可以工作。

首先你计算tax

tax=cost*quan;

其中成本 == x == 7.55 和泉 == y == 4。所以 7.55 * 4 是 30.2。这就是你得到的输出。如果你期望有别的东西,可以修复你的计算。

第二:

tc=cbt+tax;
cbt = cost*quan;

使用未初始化的cbt计算tc,然后将值分配给cbt 。所以你会得到垃圾tc

警告原因:

默认情况下,7.55, 4, 8.75是双精度。应将它们指定为浮点数:7.55f, 4.0f, 8.75f

计算错误的原因:

查看@Angew和@PaulEvans的答案

相关文章: