运费计算问题

运输公司对用户计算运输费用。路程(s/km)越远, 每吨·千米运费越低。标准如下。

路程 折扣
s<250 没有折扣
250≤s<500 2%折扣
500≤s<1000 5%折扣
1000≤s<2000 8%折扣
2000≤s<3000 10%折扣
3000≤s 15%折扣

设每吨每千米货物的基本运费为P, 货物重为W, 距离为S, 折扣为D, 则总运费F的计算公式为F = P × W × S × (1 – D)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int main() {
    int c, S;
    float P, W, D, F;
    printf("Please enter Basic Freight(P) ,Cargo Weight(W) and Distance(S): P= ,W= ,S= \n");
    scanf("P=%f,W=%f,S=%d", &P, &W, &S);
    if(S >= 3000) {
        D = 0.15;
    } else {
        c = S / 250;
        switch(c) {
            case 0: D = 0; break;
            case 1: D = 0.02; break;
            case 2:
            case 3: D = 0.05; break;
            case 4:
            case 5:
            case 6:
            case 7: D = 0.08; break;
            case 8:
            case 9:
            case 10:
            case 11: D = 0.10; break;
            case 12: D = 0.15; break;
        }
    }
    F = P * W * S * (1 - D);
    printf("The total freight is: %8.2f\n", F);
    return 0;
}