【问题标题】:Function calculate and return (switch)函数计算并返回(switch)
【发布时间】:2022-12-06 09:28:17
【问题描述】:

我有

float AddVat(float price, int category)
float total_price=0;

我需要编写函数来计算和返回包括增值税在内的总价(使用switch/case而不使用scanf,适合初学者)

VAT:
1 category 20%
2 - 20%
3 - 20%
4 - 15%
5 - 8%
6 - 0%

我尝试将价格乘以一个百分比。

我想了解如何找到解决方案。

【问题讨论】:

  • “我尝试将价格乘以一个百分比”--> 发布该代码。

标签: c


【解决方案1】:

如果你想退回价格+增值税,你可以这样做:

float addVat(float price, int category) {
    switch(category) {
        case 1:
        case 2:
        case 3:
            return price * 1.20;
        case 4:
            return price * 1.15;
        case 5:
            return price * 1.08;
        case 6:
            return price;
        default:
            // error
            return -1;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多