First make a menu like the following one according to the requirements below the menu.
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/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 1 through 5 is entered, the program should remind the user what the proper choices are and then recycle. Use #defined constants for the various earning rates and tax rates.
Then rite a program that requests the hours worked in a week and then prints the gross pay, the taxes, and the net pay. Assume the following:
-
Basic pay rate = $10.00/hr
-
Overtime (in excess of 40 hours) = time and a half
-
Tax rate: 15% of the first $300
20% of the next $150
25% of the rest
Code 1:
/* ex7, chptr8 */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define OT 40.0
#define TIMES 1.50
#define TAX1 300.0
#define TAX2 150.0
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
void menu(void);
char get_first(void);
void calc(double payrt);
int main(void)
{
char choice;
double payrate = 0;
menu();
choice = get_first();
while(payrate == 0)
{
switch(choice)
{
case 'a': payrate = 8.75;
break;
case 'b': payrate = 9.33;
break;
case 'c': payrate = 10.00;
break;
case 'd': payrate = 11.20;
break;
case 'q': payrate = -1;
break;
default: puts("\nWrong input. Enter your choice again.");
menu();
choice = get_first();
break;
}
}
if(choice != 'q')
calc(payrate);
puts("\nBye!");
}
void menu(void)
{
int i, length;
length = strlen("Enter the number corresponding to the desired pay rate or action: ");
for(i = 0; i < length; i++)
printf("-");
puts("\nEnter the number corresponding to the desired pay rate or action: ");
puts("\na) $8.75/hr b) $9.33/hr");
puts("c) $10.00/hr d) $11.20/hr");
puts("q) quit\n");
for(i = 0; i < length; i++)
printf("*");
printf("\n\nEnter your choice: ");
}
char get_first(void)
{
char ch;
while(isspace(ch = getchar()))
continue;
return tolower(ch);
}
void calc(double payrt)
{
double workhours, grosspay, tax, netpay;
printf("\nEnter your hours of work: ");
scanf("%lf", &workhours);
if(workhours <= OT)
grosspay = payrt * workhours;
else
grosspay = payrt * OT + payrt * (workhours - 40) * TIMES;
if(grosspay <= TAX1)
tax = grosspay * RATE1;
else if(grosspay <= TAX1 + TAX2)
tax = TAX1 * RATE1 + (grosspay - TAX1) * RATE2;
else
tax = TAX1 * RATE1 + TAX2 * RATE2 + (grosspay - TAX1 - TAX2) * RATE3;
netpay = grosspay - tax;
printf("\nworkhours = %.2lf, grosspay = %.2lf, tax = %.2lf, netpay = %.2lf\n", workhours, grosspay, tax, netpay);
}
Code2:
/* Programming Exercise 8-7 */
#include <stdio.h>
#include <ctype.h>
#define BASEPAY1 8.75 /* $8.75 per hour */
#define BASEPAY2 9.33 /* $9.33 per hour */
#define BASEPAY3 10.00 /* $10.00 perhour */
#define BASEPAY4 11.20 /* $11.20 per hour */
#define BASEHRS 40 /* hours at basepay */
#define OVERTIME 1.5 /* 1.5 time */
#define AMT1 300 /* 1st rate tier */
#define AMT2 150 /* 2nd rate tier */
#define RATE1 0.15 /* rate for 1st tier */
#define RATE2 0.20 /* rate for 2nd tier */
#define RATE3 0.25 /* rate for 3rd tier */
int getfirst(void);
void menu(void);
int main(void)
{
double hours;
double gross;
double net;
double taxes;
double pay;
char response;
menu();
while((response = getfirst()) != 'q')
{
if(response == '\n') /* skip over newlines */
continue;
response = tolower(response); /* accept A as a, etc. */
switch(response)
{
case 'a': pay = BASEPAY1; break;
case 'b': pay = BASEPAY2; break;
case 'c': pay = BASEPAY3; break;
case 'd': pay = BASEPAY4; break;
default : printf("Please enter a, b, c, d, or q.\n");
menu();
continue; /* go to beginning of loop */
}
printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);
if(hours <= BASEHRS)
gross = hours * pay;
else
gross = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME;
if(gross <= AMT1)
taxes = gross * RATE1;
else if(gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
menu();
}
return 0;
}
void menu(void)
{
printf("****************************************************************\n");
printf("Enter the letter corresponding to the desired pay rate or action\n");
printf("a) $%4.2f/hr b) $%4.2f/hr\n", BASEPAY1, BASEPAY2);
printf("c) $%4.2f/hr d) $%5.2f/hr\n", BASEPAY3, BASEPAY4);
printf("q) quit\n");
printf("****************************************************************\n");
}
int getfirst(void)
{
int ch;
ch = getchar();
while(isspace(ch))
ch =getchar();
while(getchar() != '\n')
continue;
return ch;
}
以上两个代码的区别显而易见,写程序者的思维在全面性和通用性上高下立见。不同的思维造成的效果是不同的,程序的通用性和复用性是程序的生命。