如何使用switch实现下面要求

img


13题的第2问怎么做

问题的解决代码如下:

#include <iostream>
using namespace std;
class Box
{
public:
    void set();
    double calculate();
    void display();
private:
    double profit;
    double bonus;
};
void Box::set()
{
    cout << "请输入利润为(单位:万元):";
    cin >> profit;
}
double Box::calculate()
{
    bonus = 0;
    int c = profit/10;
    switch (c)
    {
    case 10:
        bonus = bonus + (profit - 100) * 0.01;
        profit = 100;
    case 6:
        bonus = bonus + (profit - 60) * 0.015;
        profit = 60;
    case 4:
        bonus = bonus + (profit - 40) * 0.03;
        profit = 40;
    case 2:
        bonus = bonus + (profit - 20) * 0.05;
        profit = 20;
    case 1:
        bonus = bonus + (profit - 10) * 0.075;
        profit = 10;
    default:
        bonus = bonus + profit * 0.1;
    }
    return bonus;
}
void Box::display()
{
    cout << "该利润对应的奖金为(单位:万元):" << calculate() << endl;
}
int main()
{
    Box a;
    a.set();
    a.display();
    return 0;
}

运行结果如下:

img