C primer plus 第六版 第八章 第七題 程式設計練習答案

Aeron-A發表於2018-05-31

Github 地址:這裡這裡φ(>ω<*)

/*   本程式修改習題七建立 :
設計一個選單,並讓使用者選擇工資型別。
如使用者輸入錯誤則再顯示選單讓使用者輸入。
使用 switch 完成工資等級選擇。


要求使用者輸入一週工作時間,然後列印工資總額、稅金和淨收入。
計算要求:
基本工資 = ( 使用者選擇  X/hr )
加班(超過40小時)= 1.5倍的時間
稅率 : 前 300 美元為 15%
續 150 美元為 20%
餘下為 25%
*/
#include<stdio.h> 


#define MONEY1 300.0
#define MONEY2 150.0


#define SHUI1 0.15
#define SHUI2 0.2
#define SHUI3 0.25


#define TIME1 40.0


#define SM1 /* 基本工資1 -- START MONEY */   8.75   // X/hr 基本工資/小時 
#define SM2 /* 基本工資2 -- START MONEY */   9.33   // X/hr 基本工資/小時   
#define SM3 /* 基本工資3 -- START MONEY */   10.00  // X/hr 基本工資/小時 
#define SM4 /* 基本工資4 -- START MONEY */   11.2   // X/hr 基本工資/小時 






int main(void)
{
char choices = 0;//迴圈用。
int i = 0;//迴圈用。 

double SM = 0;//儲存選項對應的基本工資。 
double time = 0;//儲存上班時間時間。 
double money = 0;//儲存資金總額。
double shui = 0;//儲存稅金。 


char n = '\n';


void MENU(void);


MENU: //選單部分。 
for (i = 0; i < 40; i++)
{
printf("*");
}
printf("\n");
printf("Enter the number correspounding to the desired pay rate or action:\n");
printf(" a) $8.75/hr                    b) $9.33/hr \n");
printf(" c) $10.00/hr                   d) $11.20/hr \n");
printf(" q) $quit\n");
printf("\n");
for (i = 0; i < 40; i++)
{
printf("*");
}
printf("\n\n");
printf("Please input your choices:");


for (time = 0; choices = getchar(); /* 空語句*/)
{
putchar(n);


//正題。
switch (choices)
{
case 'a': 
printf("Please input your work time( for a weak ):");
scanf_s("%lf", &time);
putchar(n);
money = time * SM1;


printf("Input is loading. Please whaiting some time.\n");


if (money <= MONEY1) //計算稅收。 
{
shui = money * SHUI1; //當錢沒有超過 300  美元時; 
}
else if (money <= MONEY1 + MONEY2)
{
shui = (MONEY1 * SHUI1) + (money - MONEY1) * SHUI2; //當錢沒有超過 450 美元時; 
}
else
{
shui = (MONEY1 * SHUI1) + (MONEY2 * SHUI2) + (money - MONEY1 - MONEY2) * SHUI3; // 當錢超過 450 美元時; 
}


printf("Total wages is %f.(工資總額)\n", money);
printf("Tax is %f.(稅金)\n", shui);
printf("Net income is %f.(淨收入)\n", money - shui);
printf("Don't worry about it, my dear. You can be well then now.\n\n");
goto MENU;


case 'b':
printf("Please input your work time( for a weak ):");
scanf_s("%lf", &time);
putchar(n);
money = time * SM2;


printf("Input is loading. Please whaiting some time.\n");


if (money <= MONEY1) //計算稅收。 
{
shui = money * SHUI1; //當錢沒有超過 300  美元時; 
}
else if (money <= MONEY1 + MONEY2)
{
shui = (MONEY1 * SHUI1) + (money - MONEY1) * SHUI2; //當錢沒有超過 450 美元時; 
}
else
{
shui = (MONEY1 * SHUI1) + (MONEY2 * SHUI2) + (money - MONEY1 - MONEY2) * SHUI3; // 當錢超過 450 美元時; 
}


printf("Total wages is %f.(工資總額)\n", money);
printf("Tax is %f.(稅金)\n", shui);
printf("Net income is %f.(淨收入)\n", money - shui);
printf("Don't worry about it, my dear. You can be well then now.\n\n");
   
goto MENU;


case 'c':
printf("Please input your work time( for a weak ):");
scanf_s("%lf", &time);
putchar(n);
money = time * SM3;


printf("Input is loading. Please whaiting some time.\n");


if (money <= MONEY1) //計算稅收。 
{
shui = money * SHUI1; //當錢沒有超過 300  美元時; 
}
else if (money <= MONEY1 + MONEY2)
{
shui = (MONEY1 * SHUI1) + (money - MONEY1) * SHUI2; //當錢沒有超過 450 美元時; 
}
else
{
shui = (MONEY1 * SHUI1) + (MONEY2 * SHUI2) + (money - MONEY1 - MONEY2) * SHUI3; // 當錢超過 450 美元時; 
}


printf("Total wages is %f.(工資總額)\n", money);
printf("Tax is %f.(稅金)\n", shui);
printf("Net income is %f.(淨收入)\n", money - shui);
printf("Don't worry about it, my dear. You can be well then now.\n\n");
   goto MENU;


case 'd':
printf("Please input your work time( for a weak ):");
scanf_s("%lf", &time);
putchar(n);
money = time * SM4;


printf("Input is loading. Please whaiting some time.\n");


if (money <= MONEY1) //計算稅收。 
{
shui = money * SHUI1; //當錢沒有超過 300  美元時; 
}
else if (money <= MONEY1 + MONEY2)
{
shui = (MONEY1 * SHUI1) + (money - MONEY1) * SHUI2; //當錢沒有超過 450 美元時; 
}
else
{
shui = (MONEY1 * SHUI1) + (MONEY2 * SHUI2) + (money - MONEY1 - MONEY2) * SHUI3; // 當錢超過 450 美元時; 
}


printf("Total wages is %f.(工資總額)\n", money);
printf("Tax is %f.(稅金)\n", shui);
printf("Net income is %f.(淨收入)\n", money - shui);
printf("Don't worry about it, my dear. You can be well then now.\n\n");
   goto MENU;


case 'q': printf("The Program is quit now.\n");
break;


default: printf("Reading your input is default ! Please input 'a'-'d' ");
printf("or 'q' to quit !");
goto MENU;
}
break;
}


printf(" Bye !\n");


return 0;

}

相關文章