用c++编程实现:已知A商品单价为7.3元,购买数量20个及以上时打八折。从键盘输入商品数量,若商品数量不为负数,则计算出商品总金额并输出,再输入付款金额,计算出找零金额并输出。要求所有输出均保留两位小数
时间: 2023-05-12 15:05:17 浏览: 175
以下是用 C++ 实现的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double unitPrice = 7.3;
const int discountQuantity = 20;
const double discountRate = 0.8;
int quantity;
double totalPrice, payment, change;
cout << "请输入商品数量:";
cin >> quantity;
if (quantity < 0)
{
cout << "商品数量不能为负数!" << endl;
return 1;
}
if (quantity >= discountQuantity)
{
totalPrice = quantity * unitPrice * discountRate;
}
else
{
totalPrice = quantity * unitPrice;
}
cout << "商品总金额为:" << fixed << setprecision(2) << totalPrice << "元" << endl;
cout << "请输入付款金额:";
cin >> payment;
if (payment < totalPrice)
{
cout << "付款金额不足!" << endl;
return 1;
}
change = payment - totalPrice;
cout << "找零金额为:" << fixed << setprecision(2) << change << "元" << endl;
return 0;
}
如果您有更多的问题,请继续提问。
阅读全文