#include<stdio.h>
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
void show_menu(void);
void cal_salary(float base_salary,float hours);
int main(void)
{
float hours = 0;
char selected;
do
{
show_menu();
scanf("%c",&selected);
switch(selected)
{
case '1':
printf("$8.75/hr,Enter the work hours:");
scanf("%f",&hours);
cal_salary(8.75,hours);
break;
case '2':
printf("$9.33/hr,Enter the work hours:");
scanf("%f",&hours);
cal_salary(9.33,hours);
break;
case '3':
printf("$10.00/hr,Enter the work hours:");
scanf("%f",&hours);
cal_salary(10.00,hours);
break;
case '4':
printf("$11.20/hr,Enter the work hours:");
scanf("%f",&hours);
cal_salary(11.20,hours);
break;
case '5':
break;
default:
printf("Error,please retry!\n");
break;
}
}while(selected != '5');
printf("Done\n");
return 0;
}
void show_menu(void)
{
char s1[] = "1) $8.75/hr";
char s2[] = "2) $9.33/hr";
char s3[] = "3) $10.00/hr";
char s4[] = "4) $11.20/hr";
char s5[] = "5) quit";
printf("***************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action\n");
printf("%-40s%-40s\n",s1,s2);
printf("%-40s%-40s\n",s3,s4);
printf("%-40s\n",s5);
printf("***************************************************************\n");
}
void cal_salary(float base_salary,float hours)
{
float salary,tax;
if(hours<=40)
{
salary = hours*base_salary;
if(salary > 300)
{
tax = 300*BASE_TAX + (salary-300)*EXTRA_TAX;
}
else
{
tax = salary*BASE_TAX;
}
}
else
{
salary = 40*base_salary + (hours-40)*EXTRA_HOUR*base_salary;
if(salary<450)
{
tax = 300*BASE_TAX + (salary-300)*EXTRA_TAX;
}
else
{
tax = 300*BASE_TAX + 150*EXTRA_TAX + (salary-450)*EXCEED_TAX;
}
}
printf("salary before tax is:%.2f tax is %.2f taxed_salary is:%.2f\n",salary, tax, salary-tax);
printf("continue...\n");
}
运行结果:
Enter the number corresponding to the desired pay rate or action
2
$9.33/hr,Enter the work hours:100
salary before tax is:1212.90 tax is 265.73 taxed_salary is:947.18
continue...
Enter the number corresponding to the desired pay rate or action
Error,please retry!
Enter the number corresponding to the desired pay rate or action
Error,please retry!
Enter the number corresponding to the desired pay rate or action
46行后面加一句:
getchar();
否则的话换行符会被当作字符读取。