##薪资计算程序
##计算税前工资、应交所得税、税后工资
##代码中的税率不代表真实税率
##请勿盲目相信
#include#define OVERTIME 1.5 //working time over40, time =1.5*time #define RATE_LESS_300 0.15 //tax rate #define RATE_MORE_300_LESS_450 0.15 #define RATE_MORE_450 0.25 //this function is used to give user choices of salary per hour //and return the basic salary per hour //if user input number not 1~4 //whole program would over float basic_salary(void); //function declaration int main(void) { float wage = 0, tax = 0, hours = 0,BASIC=0; //BASIC--- basic salary/wage BASIC = basic_salary(); if (BASIC==-1) return -1; printf("Please input your working hours(q to quit): "); while (scanf("%f", &hours) == 1 && hours > 0) // judge the hours of your working time if (hours > 40) hours = 40 + (hours - 40) * OVERTIME; else ; wage = hours * BASIC; // judge the number of your salary if (wage > 450) tax += (wage - 450) * RATE_MORE_450 + 150 * RATE_MORE_300_LESS_450 + 300 * RATE_LESS_300; else if (wage > 300) tax += (wage - 300) * RATE_MORE_300_LESS_450 + 300 * RATE_LESS_300; else tax += wage * RATE_LESS_300; //initialize BASIC wage for next computing //print the answer printf("Your wage total is $%.2f, but you " "should pay tax $%.2f, so you could get $%.2f in the end.nnn", wage, tax, wage - tax); BASIC = basic_salary(); if (BASIC == -1) return -1; printf("Please input your working hours(q to quit): "); tax = 0; // initialize tax for next computing } printf("Done!n"); return 0; } float basic_salary(void) { float BASIC = 0; int choice = 0; printf("******************************************************************************************n"); printf("Enter the number(1~4,5 and other to quit) corresponding to the desired pay rate or action:n"); printf("1) %-5.2f/hr%20c2)$ %-5.2f/hrn", 8.75, ' ', 9.33); printf("3) %-5.2f/hr%20c4)$ %-5.2f/hrn", 10.00, ' ', 11.20); printf("5) quitn"); printf("******************************************************************************************n"); scanf("%d", &choice); switch (choice) { case 1: BASIC = 8.75; break; case 2: BASIC = 9.33; break; case 3: BASIC = 10.00; break; case 4: BASIC = 11.20; break; default: //besides 1~4,quit printf("quit!n"); return -1; // end the program break; } return BASIC; }
运行示例:
Enter the number(1~4,5 and other to quit) corresponding to the desired pay rate or action:
- 8.75 /hr 2)$ 9.33 /hr
- 10.00/hr 4)$ 11.20/hr
- quit
4
Please input your working hours(q to quit): 55
Your wage total is $700.00, but you should pay tax $130.00, so you could get $570.00 in the end.
Enter the number(1~4,5 and other to quit) corresponding to the desired pay rate or action:
- 8.75 /hr 2)$ 9.33 /hr
- 10.00/hr 4)$ 11.20/hr
- quit
0
quit!
本程序是一个C语言练手项目
程序中最突出的地方是是判断何时退出程序以及
将基础工资选项这代码行数较多的部分封装成一个函数
通过返回值判断用户输入是否妥当,不妥当就退出程序



