酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

C++实现精准邮费计算:兼顾重量、加急及输入规范

```cpp #include <iostream> using namespace std; int calculatePostage(int weight, bool isExpedited) { int baseFee = 8; // 基本费用 int extraFee = 0; // 超重费用 int expeditedFee = isExpedited ? 5 : 0; // 加急费用 if (weight > 1000) { int extraWeight = weight - 1000; // 计算超重部分 extraFee = (extraWeight / 500) * 4; // 每500克加收4元 if (extraWeight % 500 != 0) { extraFee += 4; // 不足500克按500克计算 } } return baseFee + extraFee + expeditedFee; // 总费用 } int main() { int weight; bool isExpedited; cout << "请输入邮件重量(克): "; cin >> weight; cout << "是否选择加急(1表示是,0表示否): "; cin >> isExpedited; int totalFee = calculatePostage(weight, isExpedited); cout << "邮费总计: " << totalFee << " 元" << endl; return 0; } ``` 额外需要注意的逻辑点: 1. 确保输入的重量是正整数,避免负数或非数字输入。 2. 加急费用的计算是基于布尔值,确保用户输入的是0或1。 3. 超重费用的计算需要正确处理不足500克的情况,确保费用计算的准确性。 ######[AI写代码神器 | 343点数解答 | 2025-02-20 20:19:00]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]