Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_NAME_SZ 256
typedef struct{
char *name;
char *title;
char ........num;
double salary;
int withhold;
} *employeeT;
typedef struct{
employeeT employ;
int numberOfEmp;
} *payrollT;
payrollT getPayRoll();
void weeklyPay(payrollT pRoll);
double calTax(double gross, int deduction);
double calNet(double gross, int deduction);
char *retNew(char *str1, char *str2, int space);
void memfree(payrollT pRoll);
int main(){
payrollT test = getPayRoll();
weeklyPay(test);
memfree(test);
return 0;
}
payrollT getPayRoll(){
printf("How many employees: ");
fflush(stdout);
int userNum = 0, index = 0;
scanf("%d", &userNum);
payrollT pRoll;
pRoll = (payrollT) malloc(sizeof *((payrollT) NULL)); //creates single malloced space for 2 var
pRoll->numberOfEmp = userNum;
pRoll->employ = (employeeT) malloc(userNum * sizeof*((employeeT) NULL));
index = 0;
while(index < userNum){
char temp[MAX_NAME_SZ];
char temp2[MAX_NAME_SZ];
printf("Employee #%d:\n", (index+1));
printf("Name: ");
fflush(stdout);
scanf("%s %s", temp, temp2);
char *here = retNew(temp, temp2, 1);
pRoll->employ[index].name = strdup(here);
free(here);
printf("Title: ");
fflush(stdout);
scanf("%s", temp);
pRoll->employ[index].title = strdup(temp);
printf("Social Security Number: ");
fflush(stdout);
scanf("%s", temp);
pRoll->employ[index].ssnum = strdup(temp);
printf("Enter Salary: ");
fflush(stdout);
scanf("%lf", &pRoll->employ[index].salary);
printf("Withholding Exemptions: ");
scanf("%d", &pRoll->employ[index].withhold);
index++;
}
return pRoll;
}
double calNet(double gross, int deduction){
double x = gross - (deduction * 1);
double cal = x * .25;
return (gross - cal);
}
double calTax(double gross, int deduction){
double x = gross - (deduction * 1);
double cal = x * .25;
return cal;
}
void weeklyPay(payrollT pRoll){
int track = pRoll->numberOfEmp, index;
printf("%s\t\t\t\t\t%s\t\t%s\t\t%s\n","Name","Gross","Tax","Net");
printf("-----------------------------------------------------------------------------\n");
for(index = 0; index < track; index++){
if(strlen(pRoll->employ[index].name) > 7){
printf("%s\t\t\t\t%.2lf\t-\t%.2lf\t\t%.2lf\n", (pRoll->employ[index].name), (pRoll->employ[index].salary), (calTax(pRoll->employ[index].salary, pRoll->employ[index].withhold)), (calNet(pRoll->employ[index].salary, pRoll->employ[index].withhold)));
fflush(stdout);
} else {
printf("%s\t\t\t\t\t%.2lf\t-\t%.2lf\t\t%.2lf\n", (pRoll->employ[index].name), (pRoll->employ[index].salary), (calTax(pRoll->employ[index].salary, pRoll->employ[index].withhold)), (calNet(pRoll->employ[index].salary, pRoll->employ[index].withhold)));
fflush(stdout);
}
}
}
char *retNew(char *str1, char *str2, int space){
char *strNew = malloc(strlen(str1) + strlen(str2) + 1 * sizeof(char));
strNew = strcpy(strNew, str1);
if(space == 0){
strNew = strcat(strNew, str2);
return strNew;
} else {
char *sp = " ";
strNew = strcat(strNew, sp);
strNew = strcat(strNew, str2);
return strNew;
}
}
void memfree(payrollT pRoll){
int track = pRoll->numberOfEmp, index;
for(index = 0; index < track; index++){
free(pRoll->employ[index].name);
free(pRoll->employ[index].title);
free(pRoll->employ[index].ssnum);
}
free(pRoll->employ);
free(pRoll);
}