Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Structure Definitions
struct Employee{
char FName[20];
char LName[10];
int HrsWorked;
float PayRate;
int StpFlag;
float RPay;
float OPay;
float GPay;
float FITAmount;
float NPay;
};
struct Totals{
float TRPay;
float TOPay;
float TGPay;
float TFITAmount;
float TNPay;
};
//Function Prototypes
void OpenFiles(void);
void CloseFiles(void);
void PrintHeads(void);
void PrintDetails(struct Employee);
void PrintFooter(struct Totals);
struct Totals ProcessLoop(struct Totals);
struct Employee InputName(struct Employee);
struct Employee InputDetails(struct Employee);
struct Employee CalcPay(struct Employee);
struct Employee CalcGPay(struct Employee);
struct Employee CalcNPay(struct Employee);
struct Employee CalcFIT(struct Employee);
//Global Variables
FILE *PFile;
//Main Line
int main()
{
struct Totals NTot = {0,0,0,0,0};
OpenFiles();
PrintHeads();
NTot = ProcessLoop(NTot);
PrintFooter(NTot);
CloseFiles();
return 0;
}
//Open Files
void OpenFiles(void)
{
PFile = fopen("c:\datadump.txt","w");
return;
}
//Module to print report headers
void PrintHeads(void)
{
char HL1[] = " Weekly Payroll Report Page 01\n";
char HL2[] = " 2/15/05";
char HL3[] = "\n\n Employee Hours Regular Overtime Gross";
char HL4[] = "\n Name Worked Pay Pay Pay FIT Net Pay\n\n";
fprintf(PFile,HL1);
fprintf(PFile,HL2);
fprintf(PFile,HL3);
fprintf(PFile,HL4);
return;
}
//Main module for processing all data
struct Totals ProcessLoop(struct Totals NTot)
{
struct Employee NEmp = {"","",0,0,0,0,0,0,0,0};
NEmp = InputName(NEmp);
while(NEmp.StpFlag != 1)
{
NEmp = InputDetails(NEmp);
NEmp = CalcPay(NEmp);
NTot.TRPay = NTot.TRPay + NEmp.RPay;
NTot.TGPay = NTot.TGPay + NEmp.GPay;
NTot.TNPay = NTot.TNPay + NEmp.NPay;
NTot.TOPay = NTot.TOPay + NEmp.OPay;
NTot.TFITAmount = NTot.TFITAmount + NEmp.FITAmount;
PrintDetails(NEmp);
system("cls");
NEmp = InputName(NEmp);
}
return NTot;
}
//Get the Employee Names
struct Employee InputName(struct Employee NEmp)
{
char TFName[4] = "";
printf("Enter Employee Name\n");
printf("\nFirst Name: ");
scanf("%20s",NEmp.FName);
fflush(stdin);
for(int count = 0;count <= 4;count++)
TFName[count] = tolower(NEmp.FName[count]);
if(strcmp(TFName,"stop") != 0)
{
printf("Enter Last Name: ");
scanf("%10s",NEmp.LName);
fflush(stdin);
strcat(NEmp.FName," ");
strcat(NEmp.FName,NEmp.LName);
system("cls");
}
else
{
NEmp.StpFlag = 1;
}
return NEmp;
}
//Input Employee Details
struct Employee InputDetails(struct Employee NEmp)
{
printf("Enter Employee Details\n");
printf("\nEnter Hours Worked: ");
scanf("%d",&NEmp.HrsWorked);
fflush(stdin);
printf("Enter Hourly Pay: ");
scanf("%f",&NEmp.PayRate);
fflush(stdin);
return NEmp;
}
//Main Module to calculate pay and tax
struct Employee CalcPay(struct Employee NEmp)
{
if(NEmp.HrsWorked > 40)
{
NEmp.RPay = 40 * NEmp.PayRate;
NEmp.OPay = (NEmp.HrsWorked - 40) * (NEmp.PayRate * 1.5);
}
else
{
NEmp.RPay = NEmp.HrsWorked * NEmp.PayRate;
NEmp.OPay = 0;
}
NEmp = CalcGPay(NEmp);
NEmp = CalcFIT(NEmp);
NEmp = CalcNPay(NEmp);
return NEmp;
}
struct Employee CalcFIT(struct Employee NEmp)
{
const float FITRate = .15;
NEmp.FITAmount = NEmp.GPay * FITRate;
return NEmp;
}
struct Employee CalcGPay(struct Employee NEmp)
{
NEmp.GPay = NEmp.RPay + NEmp.OPay;
return NEmp;
}
struct Employee CalcNPay(struct Employee NEmp)
{
NEmp.NPay = NEmp.GPay - NEmp.FITAmount;
return NEmp;
}
void PrintFooter(struct Totals NTot)
{
char FL1[] = "\n\n Totals: ";
fprintf(PFile,FL1);
fprintf(PFile,"$ %8.2f",NTot.TRPay);
fprintf(PFile," $ %8.2f",NTot.TOPay);
fprintf(PFile," $ %8.2f",NTot.TGPay);
fprintf(PFile," $ %8.2f",NTot.TFITAmount);
fprintf(PFile," $ %8.2f",NTot.TNPay);
return;
}
void CloseFiles(void)
{
fclose(PFile);
return;
}
void PrintDetails(struct Employee NEmp)
{
fprintf(PFile," %-20s",NEmp.FName);
fprintf(PFile," %2d",NEmp.HrsWorked);
fprintf(PFile," $ %8.2f",NEmp.RPay);
fprintf(PFile," $ %8.2f",NEmp.OPay);
fprintf(PFile," $ %8.2f",NEmp.GPay);
fprintf(PFile," $ %8.2f",NEmp.FITAmount);
fprintf(PFile," $ %8.2f\n",NEmp.NPay);
return;
}