c++已知苹果每斤8.5元,香蕉每斤2.5元,橘子每斤4元,要求输入各类水果的重量各2斤,输出应付金额,再输入顾客所付金额,打印出找顾客的金额。
时间: 2025-07-06 21:49:53 浏览: 11
在C++中,你可以通过创建一个简单的控制台应用程序来实现这个功能。首先,我们需要定义变量存储每个水果的价格以及用户输入的数量和总金额。然后,计算总价并找出找回的零钱。下面是一个简化的示例代码:
```cpp
#include <iostream>
using namespace std;
int main() {
double apple_price = 8.5;
double banana_price = 2.5;
double orange_price = 4.0;
// 输入水果重量
int apple_weight, banana_weight, orange_weight;
cout << "请输入苹果的重量(单位:斤): ";
cin >> apple_weight;
cout << "请输入香蕉的重量(单位:斤): ";
cin >> banana_weight;
cout << "请输入橘子的重量(单位:斤): ";
cin >> orange_weight;
// 计算总价
double total_cost = (apple_price * apple_weight) + (banana_price * banana_weight) + (orange_price * orange_weight);
// 输入顾客支付金额
double customer_payment;
cout << "请输入顾客付款金额: ";
cin >> customer_payment;
// 找零
double change = customer_payment - total_cost;
if (change >= 0) {
cout << "应付金额: " << total_cost << " 元\n";
cout << "找零: " << change << " 元" << endl;
} else {
cout << "无需找零,实际消费: " << total_cost << " 元" << endl;
}
return 0;
}
```
阅读全文