编写一个C++程序来计算利润或亏损,并附带一个示例。此程序允许用户输入产品的实际成本和销售价格。
C++ 计算利润或亏损程序
我们使用Else If语句来检查实际成本是否低于销售金额。如果为True,则产品盈利;否则为亏损。如果以上两个条件都不满足,我们使用了最后的else语句(无利润,无亏损)。
#include<iostream>
using namespace std;
int main()
{
float unitPrice, salesAmount, amount;
cout << "\nPlease Enter the Actual Product Cost = ";
cin >> unitPrice;
cout << "\nPlease Enter the Sale Price (or Selling Price) = ";
cin >> salesAmount;
if (salesAmount > unitPrice)
{
amount = salesAmount - unitPrice;
cout << "\nProfit Amount = " << amount;
}
else if(unitPrice > salesAmount)
{
amount = unitPrice - salesAmount;
cout << "\nLoss Amount = " << amount;
}
else
cout << "\nNo Profit No Loss!";
return 0;
}

我将使用销售价格低于实际价格作为示例。
Please Enter the Actual Product Cost = 3200
Please Enter the Sale Price (or Selling Price) = 2750
Loss Amount = 450