以下是用 C++ 实现的计算邮费的代码: ```cpp #include <iostream> using namespace std; int calculatePostage(int weight, char isUrgent) { int postage = 8; // 1000 克以内基本费 8 元 if (weight > 1000) { // 超过 1000 克 int extraWeight = weight - 1000; // 计算超重的重量 postage += (extraWeight / 500 + (extraWeight % 500!= 0)) * 4; // 计算超重费用,不足 500 克按 500 克计算 } if (isUrgent == 'y') { // 如果加急 postage += 5; // 加收 5 元 } return postage; } int main() { int weight; char isUrgent; cin >> weight >> isUrgent; // 输入重量和是否加急 int postage = calculatePostage(weight, isUrgent); cout << postage << endl; // 输出邮费 return 0; } ``` 额外需要注意的逻辑点: 1. 输入的重量和是否加急的字符需要进行有效性判断,比如重量不能是负数,字符只能是 'y' 或 'n' 。 2. 对于超重部分的计算,要注意处理好余数部分,确保不足 500 克也按照 500 克计算费用。 3. 在计算加急费用时,要确保只有输入 'y' 时才加收 5 元。 [2024-12-08 13:54:57 | AI写代码神器 | 384点数解答]