Code:
/*
Write a program to process weekly employee time cards for all employees of an organization.
Each employee will have three data items: an identification number, the hourly wage rate,
and the number of hours worked during the week. Each employee is to be paid time and
one half for all hours worked over 40. A tax amount of 23.625 percent of gross salary will be
deducted. The program output should show the employee's identification number and net pay.
Display the total payroll and the average amount paid at the end of the run.
Make the program modular by creating functions to:
- Get the employee data
- Calculate gross pay
- Calculate net pay
- Display results
You may choose to get the input data from the keyboard or from a text file
of your own creation. You must determine what type of loop control you will use.
For example, you may choose to use Sentinel value or an EOF controlled loop.
*/
#include <stdio.h>
#include <stdlib.h>
/* declare functions */
/* Get the employee data */
void get_employee_data(FILE* fp,int* id, float* rate, float* hours);
/* Calculate gross pay */
float calc_gross_pay(float rate, float hours);
/* Calculate net pay */
float calc_net_pay(float gross_pay);
/* Display results */
void display(FILE* fp,int id, float net_pay);
int main()
{
int id; /* employee identification */
float rate; /* hourly wage */
float hours; /* hours worked */
float gross_pay; /* gross pay */
float net_pay; /* net pay */
float total_payroll=0; /* total payroll */
float average_payroll=0; /* total payroll */
int nums=0; /* number employees */
FILE *fpin; /* read file pointer */
FILE *fpout; /* write file pointer */
fpin = fopen("employees.in","r"); /* open file for reading */
/* check if file open */
if(fpin == NULL)
{
printf("file 'employees.in' cannot be opened for reading\n");
exit(1); /* close all file streams and terminate */
}
fpout = fopen("employees.out","w"); /* open file for writing */
/* check if file open */
if(fpout == NULL)
{
printf("file 'employees.out' cannot be opened for writing\n");
exit(1); /* close all file streams and terminate */
}
/* read employees in loop */
/* Get the employee data */
get_employee_data(fpin,&id, &rate, &hours);
while(!feof(fpin))
{
nums++; /* count employees */
/* Calculate gross pay */
gross_pay = calc_gross_pay(rate, hours);
/* Calculate net pay */
net_pay = calc_net_pay(gross_pay);
/* sum totals */
total_payroll = total_payroll + net_pay;
/* Display results */
display(fpout,id, net_pay);
/* get next employee data */
get_employee_data(fpin,&id, &rate, &hours);
}
/* close read file */
fclose(fpin);
/* caculate average payents */
average_payroll = total_payroll / nums;
/* print total */
fprintf(fpout,"\nTotal Payroll: %.2f Average Payroll: %.2f\n",total_payroll, average_payroll);
printf("\nTotal Payroll: %.2f Average Payroll: %.2f\n",total_payroll, average_payroll);
/* close write file */
fclose(fpout);
return 0;
}
/* Get the employee data */
void get_employee_data(FILE* fp,int* id, float* rate, float* hours)
{
/* get employee id */
fscanf(fp,"%d",id);
/* get employee id */
fscanf(fp,"%f",rate);
/* get employee id */
fscanf(fp,"%f",hours);
}
/* Calculate gross pay */
float calc_gross_pay(float rate, float hours)
{
float gross_pay = 0;
if(hours <= 40)
gross_pay = rate * hours;
else
gross_pay = rate * 40 + (hours - 40) * rate * 1.5f;
return gross_pay;
}
/* Calculate net pay */
float calc_net_pay(float gross_pay)
{
float tax = .23625f * gross_pay;
float net_pay = gross_pay - tax;
return net_pay;
}
/* Display results */
void display(FILE* fp, int id, float net_pay)
{
fprintf(fp,"%d %.2f\n",id,net_pay);
printf("%d %.2f\n",id,net_pay);
}