In: Computer Science
Write a program in C to process weekly employee timecards for
all employees of an organization (ask the user number of employees
in the start of the program). Each employee will have three data
items: an identification number, the hourly wage rate, and the
number of hours worked during a given week.A tax amount of 3.625%
of gross salary will be deducted. The program output should show
the identification number and net pay. Display the total payroll
and the average amount paid at the end of the run.
Make a simple program without using classes.
#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 = 3.625* 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);
}