-5

你好,我正在做一个任务。我花了很多时间,无法弄清楚为什么在我的程序结束时“总佣金”和“总到期”没有加起来。我是一个初学者,所以任何可以引导我走向正确方向的东西都将不胜感激。谢谢。

原始代码

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
             PC_COMMISSION = 0.12,
             PRINTER_BASE_SALES = 2500.00,
             PRINTER_COMMISSION = 0.10,
             ACCESSORIE_BASE_SALES = 2000.00,
             ACCESSORIE_COMMISSION = 0.10,
             MAINTENANCE_BASE_SALES = 1500.00,
             MAINTENANCE_COMMISSION = 0.06;

double       baseSalary = 0.00,
             pcSales = 0.00,
             printerSales = 0.00,
             accessorieSales = 0.00,
             maintenanceSales = 0.00,
             totalCommission = 0.00,
             totalDue = 0.00,
             noCommission = 0.00,
             pcCommission = baseSalary * PC_COMMISSION,
             printerCommission = baseSalary * PRINTER_COMMISSION,
             accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION,
             maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;

int          idNumber; //salesman identification number

do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;

if (idNumber == -999)
    return 0;
else

cout << "Please Enter Salesman Base Salary\t\t\t  ";
cin >> baseSalary;

cout << "Please Enter Personal Computer Sales\t\t\t  ";
cin >> pcSales;

cout << "Please Enter Printer Sales\t\t\t\t  ";
cin >> printerSales;

cout << "Please Enter Accessories Sales\t\t\t\t   ";
cin >> accessorieSales;

cout << "Please Enter Maintenance Sales\t\t\t\t  ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;

cout << "\t\t\t  My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t  Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t   ********************************\n";
cout << endl;

cout << fixed;
cout << setprecision(2);

cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;

cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;

while (pcSales > PC_BASE_SALES)
    {
        double pcCommission = baseSalary * PC_COMMISSION;
        cout << right << setw(20) << pcCommission;
        cout << "\n" << endl;
        pcCommission += totalCommission;
        break;
    }
    while (pcSales < PC_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    break;
    }

cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
    while (printerSales > PRINTER_BASE_SALES)
    {
        double printerCommission = baseSalary * PRINTER_COMMISSION;
        cout << right << setw(20) << printerCommission;
        cout << "\n" << endl;
        printerCommission += totalCommission;
        break;
    }
    while (printerSales < PRINTER_BASE_SALES)
    {
        cout << right << setw(20) << noCommission;
        cout << "\n" << endl;
        break;
    }

cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
    while (accessorieSales > ACCESSORIE_BASE_SALES)
    {
        double accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION;
        cout << right << setw(20) << accessorieCommission;
        cout << "\n" << endl;
        accessorieCommission += totalCommission;
        break;
    }
    while (accessorieSales < ACCESSORIE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    break;
    }

cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
    while (maintenanceSales > MAINTENANCE_BASE_SALES)
    {
        double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
        cout << right << setw(20) << maintenanceCommission;
        cout << "\n" << endl;
        maintenanceCommission += totalCommission;
        break;
    }
    while (maintenanceSales < MAINTENANCE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    cout << endl;
    break;
    }

cout << endl;

      totalCommission =  pcCommission + printerCommission + accessorieCommission +   
      maintenanceCommission;
      totalDue = baseSalary + totalCommission;

cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;

      cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;

cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;

    } while ( idNumber != -999);

system ("PAUSE");
return 0;

更新代码

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
             PC_COMMISSION = 0.12,
             PRINTER_BASE_SALES = 2500.00,
             PRINTER_COMMISSION = 0.10,
             ACCESSORIE_BASE_SALES = 2000.00,
             ACCESSORIE_COMMISSION = 0.10,
             MAINTENANCE_BASE_SALES = 1500.00,
             MAINTENANCE_COMMISSION = 0.06;

double       baseSalary = 0.00,
             pcSales = 0.00,
             printerSales = 0.00,
             accessorieSales = 0.00,
             maintenanceSales = 0.00,
             totalCommission = 0.00,
             totalDue = 0.00,
             noCommission = 0.00,
             pcCommission = baseSalary * PC_COMMISSION,
             printerCommission = baseSalary * PRINTER_COMMISSION,
             accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION,
             maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;

int          idNumber; //salesman identification number

do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;

if (idNumber == -999)
    return 0;
else

cout << "Please Enter Salesman Base Salary\t\t\t  ";
cin >> baseSalary;

cout << "Please Enter Personal Computer Sales\t\t\t  ";
cin >> pcSales;

cout << "Please Enter Printer Sales\t\t\t\t  ";
cin >> printerSales;

cout << "Please Enter Accessories Sales\t\t\t\t   ";
cin >> accessorieSales;

cout << "Please Enter Maintenance Sales\t\t\t\t  ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;

cout << "\t\t\t  My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t  Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t   ********************************\n";
cout << endl;

cout << fixed;
cout << setprecision(2);

cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;

cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;

if (pcSales > PC_BASE_SALES)
    {
        double pcCommission = baseSalary * PC_COMMISSION;
        cout << right << setw(20) << pcCommission;
        cout << "\n" << endl;
        totalCommission += pcCommission;
    }
if (pcSales < PC_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    }

cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;

if (printerSales > PRINTER_BASE_SALES)
    {
        double printerCommission = baseSalary * PRINTER_COMMISSION;
        cout << right << setw(20) << printerCommission;
        cout << "\n" << endl;
        totalCommission += printerCommission;
    }
if (printerSales < PRINTER_BASE_SALES)
    {
        cout << right << setw(20) << noCommission;
        cout << "\n" << endl;
    }

cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;

if (accessorieSales > ACCESSORIE_BASE_SALES)
    {
        double accessorieCommission =  baseSalary * ACCESSORIE_COMMISSION;
        cout << right << setw(20) << accessorieCommission;
        cout << "\n" << endl;
        totalCommission += accessorieCommission;
    }
if (accessorieSales < ACCESSORIE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    }

cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
if (maintenanceSales > MAINTENANCE_BASE_SALES)
    {
        double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
        cout << right << setw(20) << maintenanceCommission;
        cout << "\n" << endl;
        totalCommission += maintenanceCommission;
    }
if (maintenanceSales < MAINTENANCE_BASE_SALES)
    {
    cout << right << setw(20) << noCommission;
    cout << "\n" << endl;
    cout << endl;
    }

cout << endl;

totalCommission =  pcCommission + printerCommission + accessorieCommission + maintenanceCommission;
totalDue = baseSalary + totalCommission;

cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;

cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;

cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;

    } while ( idNumber != -999);

system ("PAUSE");
return 0;

}

4

2 回答 2

3

您是否有意为混淆的 C++ 竞赛编写代码?

这(作为几个类似的例子):

while (printerSales > PRINTER_BASE_SALES)
{
    double printerCommission = baseSalary * PRINTER_COMMISSION;
    cout << right << setw(20) << printerCommission;
    cout << "\n" << endl;
    printerCommission += totalCommission;
    break;
}

完全一样:

if (printerSales > PRINTER_BASE_SALES)
{
    double printerCommission = baseSalary * PRINTER_COMMISSION;
    cout << right << setw(20) << printerCommission;
    cout << "\n" << endl;
    printerCommission += totalCommission;
}

因为在break循环内是无条件的,因此使循环只运行一次。这可能是您最初想要的。

此外,由于printerCommission是循环内的局部变量,因此这不会做任何有用的事情。

    printerCommission += totalCommission;

你的意思:

    totalCommission += printerCommission;

此外,正如评论所说,使用doublefloat计算金钱最终会出错,因为浮点值有时会不正确地四舍五入。例如,在二进制浮点表示中,值 0.1、0.2 和 0.6 和 0.7 将分别为 0.099999999999999、0.199999999999999、0.59999999999999 和 0.699999999999。这与试图用小数表示 1/3 相同,0.333333333 - 无论使用多少位,它都不能“正好是 1/3”。如果您使用这些值进行足够的数学运算,最终将“错误”。但是你应该得到大约正确的价值,给予或接受一分或任何货币的一小部分。

于 2013-06-29T17:52:52.483 回答
0

这是我发现的一个问题:

else没有{并且}只会执行下 一条语句的语句:

do
{
    cout << "Please Enter Salesman Identification or -999 to Terminate ";
    cin >> idNumber;

    if (idNumber == -999)
        return 0;
    else // <---  This else will cause only the next line to be executed
         //       since there is no '{' following it.

    cout << "Please Enter Salesman Base Salary\t\t\t  ";
    cin >> baseSalary;

您是否使用调试器单步执行了代码?

于 2013-06-29T19:06:54.593 回答