8. Modify assumption a. in exercise 7 so that the program presents a menu of pay rates from which to choose. Use a switch to select the pay rate. The beginning of a run should look something like this:
Enter the number corresponding to the desires pay rate or action:
1) $8.75 /hr 2) $9.323/hr
3) $10.00/hr 4) $11.20/hr
5) quit
If choices 1 through 4 are selected, the program should request the hours worked. The program should recycle until 5 is entered. If something other than choices are and then recycle. Use #defined constants for the carious earning rates and tax rates.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#include <stdio.h> #define OVERHOUR 40 /* 超過多少小時就要開始每小時工資打折 */ #define ADDITIONAL 0.5 /* 工資的折扣 */ #define TAX1 0.15 /* 前 300 元 */ #define TAX2 0.20 /* 接下來的 150 元 */ #define TAX3 0.25 /* 超出 $450 元 */ #define STATE1 300 /* 前三百元 */ #define STATE2 150 /* 接下來的一百五十元 */ bool check_input(int choose) { if( choose <=5 && choose >= 1) return true; else return false; } int main(void) { double hour; /* 每小時工作時數 */ double money; /* 總收入 */ double net; /* 淨收入 */ double taxes; /* 稅金 */ double base = 10; /* 基本報酬率 10 元/hr */ int choose = 0 ; /* 選項 */ while(choose != 5) { printf("請選擇報酬率\n"); printf("1) $8.75 /hr 2) $9.33/hr\n"); printf("3) $10.00/hr 4) $11.20/hr\n"); printf("5) quit\n"); scanf("%d", &choose); while(!check_input(choose)) { printf("請輸入 1-5 的數字:"); scanf("%d", &choose); /*再輸入一次*/ } switch(choose) { case 1: hour = 8.75; break; case 2: hour = 9.33; break; case 3: hour = 10.00; break; case 4: hour = 11.20; break; case 5: printf("程式結束\n"); return 0; default: break; } printf("請輸入每小時工作時數"); scanf("%lf", &hour); if (hour <= OVERHOUR) money = hour * base; else money = OVERHOUR * base + (hour - OVERHOUR) * base * ADDITIONAL; if (money <= STATE1) taxes = money * TAX1; else if (money <= STATE1 + STATE2) taxes = STATE1 * TAX1 + (money - STATE1) * TAX2; else taxes = STATE1 * TAX1 + STATE2 * TAX2 + (money - STATE1 - STATE2) * TAX3; net = money - taxes; printf("總收入: $%.2f; 稅金: $%.2f; 淨收入: $%.2f\n", money, taxes, net); } return 0; } |