首页 > 解决方案 > c ++初学者问题:我的代码是错误的还是我误解了说明?

问题描述

我有一个今晚要交的学校作业。这些是说明:

小结
Linda 正在开始一项新的化妆品和服装业务,并希望在支付所有费用后获得约 10% 的净利润,包括商品成本、商店租金、员工工资和商店的电费。

她想知道商品应该加价多少,以便在年底支付所有费用后,她可以获得大约 10% 的商品成本净利润。

请注意,在标记 > 一件商品的价格后,她希望将该商品以 15% 的价格出售。

说明
编写一个提示 Linda 输入的程序:

商品总成本
员工工资(包括她自己的工资)
年租金
预估电费。
然后程序输出应该标记多少商品(以百分比表示),以便琳达获得所需的利润。

由于您的程序处理货币,请确保使用可以存储小数精度为 2 的小数的数据类型。

评分
完成课程后,单击提交按钮记录您的分数。

这是我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

//Create constants for the percentage off when items are on sale and profit desired (as a percentage of total merchandise costs)

const double DISCOUNT_PERCENTAGE = 0.15;
const double PROFIT_DESIRED = 0.10;

int main(void) 
{
    // Define variables for total cost of merchandise, salary of employees, yearly rent, and estimated electricity cost
    double totalCostOfMerchandise;
    double salaryOfEmployees;
    double yearlyRent;
    double estimatedElectricityCost;

    //Define variables for total costs desired gross income and markup percentage
    double totalCosts;
    double desiredGrossIncome;
    double markupPercentage;

    ////Set the formatting for the screen output of decimal values to 2 decimal places
    cout << fixed << setprecision(2);

    //Prompt user to enter the total cost of the merchandise
    cout << "Enter total cost of merchandise: " << endl;
    cin >> totalCostOfMerchandise;

   //Output a blank line
   cout << endl;

    //Prompt user to enter the salary of the employees (including own salary)
    cout << "Enter salary of employees (including your own): " << endl;
    cin >> salaryOfEmployees;

    //Output a blank line
    cout << endl;

    //Prompt user to enter the yearly rent
    cout << "Enter yearly rent: " << endl;
    cin >> yearlyRent;

    //Output a blank line
    cout << endl;

    //Prompt user to enter the estimated electricity cost
    cout << "Enter estimated electricity cost: " << endl;
    cin >> estimatedElectricityCost;

    //Output a blank line
    cout << endl;

    //Assign total costs and desiredGrossIncome
    totalCosts = totalCostOfMerchandise + salaryOfEmployees + yearlyRent + estimatedElectricityCost;
    desiredGrossIncome = totalCostOfMerchandise + (totalCostOfMerchandise * PROFIT_DESIRED) + (totalCosts - totalCostOfMerchandise);

    //Assign markup percentage
    markupPercentage = (((desiredGrossIncome/totalCostOfMerchandise)-1)*100) * (1 + DISCOUNT_PERCENTAGE);

    //Output markup percentage
    cout << "Merchandise should be marked up " << markupPercentage << "%" << endl;

    return 0;
}

但是,我得到了以下值来输入我的程序的“运行检查”:

total cost of merchandise: 540000
salary of employees: 80000
yearly rent: 50000
estimated electricity costs: 4000

当我输入这些值时,输出应该是 158.61,但我得到的是 40.04。

在我的程序上运行检查的另一组值:

total cost of merchandise: 600432.50
salary of employees: 99400
yearly rent: 65400
estimated electricity costs: 4356.36

当我输入这些值时,输出应该是 162.56,但我得到的是 43.90。

我不知道我的代码是否真的错了,或者我只是误解了说明。

我理解它的方式,输出应该是一个百分比,指的是产品应该被标记多少。例如,如果商品成本为 100,而其余业务成本加起来为 100,那么期望的总收入将是 110+100(即 210)。210 是原始成本 (100) 加上 110% 的加价。

编辑:我改变了我的代码。之前读到的那一行:

//Assign markup percentage
markupPercentage = (((desiredGrossIncome/totalCostOfMerchandise)-1)*100) * (1 + DISCOUNT_PERCENTAGE);

现在阅读:

//Assign markup percentage
markupPercentage = ((desiredGrossIncome/totalCostOfMerchandise)*100) * (1 + DISCOUNT_PERCENTAGE);

我在“*100”之前去掉了“-1”。

这意味着如果商品“加价 100%”,就不会加价。如果价格为 100 的商品以 200 的价格出售,那将是“200% 的加价”。我不确定这是对标记的正确定义。

无论如何,在此更改之后,我的输出更接近所需的输出,但它仍然关闭。在第一个示例中,我的输出现在是 155.04(所需的输出是 158.61)。

在第二个示例中,我的输出现在是 158.90(所需的输出是 162.56)。

同样,我不确定我的代码是否错误,或者我只是误解了说明。

标签: c++

解决方案


推荐阅读