fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: <Damian Uhomoibhi>
  6. //
  7. // Class: C Programming, <Fall 2024>
  8. //
  9. // Date: <01 December 2024>
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references have all been replaced with
  20. // pointer references to speed up the processing of this code.
  21. // A linked list has been created and deployed to dynamically
  22. // allocate and process employees as needed.
  23. //
  24. // It will also take advantage of the C Preprocessor features,
  25. // in particular with using macros, and will replace all
  26. // struct type references in the code with a typedef alias
  27. // reference.
  28. //
  29. // Call by Reference design (using pointers)
  30. //
  31. //********************************************************
  32.  
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h> // for char functions
  37. #include <stdlib.h> // for malloc
  38.  
  39. // define constants
  40. #define STD_HOURS 40.0
  41. #define OT_RATE 1.5
  42. #define MA_TAX_RATE 0.05
  43. #define NH_TAX_RATE 0.0
  44. #define VT_TAX_RATE 0.06
  45. #define CA_TAX_RATE 0.07
  46. #define DEFAULT_STATE_TAX_RATE 0.08
  47. #define NAME_SIZE 20
  48. #define TAX_STATE_SIZE 3
  49. #define FED_TAX_RATE 0.25
  50. #define FIRST_NAME_SIZE 10
  51. #define LAST_NAME_SIZE 10
  52.  
  53. // define macros
  54. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  55. #define CALC_STATE_TAX(thePay, theStateTaxRate) (thePay * theStateTaxRate)
  56. #define CALC_FED_TAX(thePay) ((thePay) * FED_TAX_RATE)
  57. #define CALC_NET_PAY(thePay, theStateTax, theFedTax) (thePay - (theStateTax + theFedTax))
  58. #define CALC_NORMAL_PAY(theWageRate, theHours, theOvertimeHrs) \
  59.   (theWageRate * (theHours - theOvertimeHrs))
  60. #define CALC_OT_PAY(theWageRate, theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  61. #define CALC_MIN(theValue, currentMin) (((theValue) < (currentMin)) ? (theValue) : (currentMin))
  62. #define CALC_MAX(theValue, currentMax) (((theValue) > (currentMax)) ? (theValue) : (currentMax))
  63.  
  64. // Define structures and typedef aliases
  65. typedef struct name {
  66. char firstName[FIRST_NAME_SIZE];
  67. char lastName[LAST_NAME_SIZE];
  68. } NAME;
  69.  
  70. typedef struct employee {
  71. NAME empName;
  72. char taxState[TAX_STATE_SIZE];
  73. long int clockNumber;
  74. float wageRate;
  75. float hours;
  76. float overtimeHrs;
  77. float grossPay;
  78. float stateTax;
  79. float fedTax;
  80. float netPay;
  81. struct employee* next;
  82. } EMPLOYEE;
  83.  
  84. typedef struct totals {
  85. float total_wageRate;
  86. float total_hours;
  87. float total_overtimeHrs;
  88. float total_grossPay;
  89. float total_stateTax;
  90. float total_fedTax;
  91. float total_netPay;
  92. } TOTALS;
  93.  
  94. typedef struct min_max {
  95. float min_wageRate;
  96. float min_hours;
  97. float min_overtimeHrs;
  98. float min_grossPay;
  99. float min_stateTax;
  100. float min_fedTax;
  101. float min_netPay;
  102. float max_wageRate;
  103. float max_hours;
  104. float max_overtimeHrs;
  105. float max_grossPay;
  106. float max_stateTax;
  107. float max_fedTax;
  108. float max_netPay;
  109. } MIN_MAX;
  110.  
  111. // Function prototypes
  112. EMPLOYEE* getEmpData(void);
  113. int isEmployeeSize(EMPLOYEE* head_ptr);
  114. void calcOvertimeHrs(EMPLOYEE* head_ptr);
  115. void calcGrossPay(EMPLOYEE* head_ptr);
  116. void calcStateTax(EMPLOYEE* head_ptr);
  117. void calcFedTax(EMPLOYEE* head_ptr);
  118. void calcNetPay(EMPLOYEE* head_ptr);
  119. void calcEmployeeTotals(EMPLOYEE* head_ptr, TOTALS* emp_totals_ptr);
  120. void calcEmployeeMinMax(EMPLOYEE* head_ptr, MIN_MAX* emp_minMax_ptr);
  121. void printHeader(void);
  122. void printEmp(EMPLOYEE* head_ptr);
  123. void printEmpStatistics(TOTALS* emp_totals_ptr, MIN_MAX* emp_minMax_ptr, int size);
  124.  
  125. int main() {
  126. EMPLOYEE* head_ptr;
  127. int theSize;
  128. TOTALS employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  129. TOTALS* emp_totals_ptr = &employeeTotals;
  130. MIN_MAX employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  131. MIN_MAX* emp_minMax_ptr = &employeeMinMax;
  132.  
  133. head_ptr = getEmpData();
  134. theSize = isEmployeeSize(head_ptr);
  135.  
  136. if (theSize <= 0) {
  137. printf("\n\n**** There was no employee input to process ***\n");
  138. } else {
  139. calcOvertimeHrs(head_ptr);
  140. calcGrossPay(head_ptr);
  141. calcStateTax(head_ptr);
  142. calcFedTax(head_ptr);
  143. calcNetPay(head_ptr);
  144. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  145. calcEmployeeMinMax(head_ptr, emp_minMax_ptr);
  146.  
  147. printHeader();
  148. printEmp(head_ptr);
  149. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, theSize);
  150. }
  151.  
  152. printf("\n\n *** End of Program *** \n");
  153. return 0;
  154. }
  155.  
  156. // Function definitions
  157. EMPLOYEE* getEmpData(void) {
  158. EMPLOYEE* head_ptr = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
  159. EMPLOYEE* current_ptr = head_ptr;
  160. char answer[80];
  161. int more_data = 1;
  162.  
  163. while (more_data) {
  164. printf("\nEnter employee first name: ");
  165. scanf("%s", current_ptr->empName.firstName);
  166. printf("\nEnter employee last name: ");
  167. scanf("%s", current_ptr->empName.lastName);
  168. printf("\nEnter employee two-character tax state: ");
  169. scanf("%s", current_ptr->taxState);
  170. printf("\nEnter employee clock number: ");
  171. scanf("%li", &current_ptr->clockNumber);
  172. printf("\nEnter employee hourly wage: ");
  173. scanf("%f", &current_ptr->wageRate);
  174. printf("\nEnter hours worked this week: ");
  175. scanf("%f", &current_ptr->hours);
  176.  
  177. printf("\nWould you like to add another employee? (y/n): ");
  178. scanf("%s", answer);
  179. if (toupper(answer[0]) != 'Y') {
  180. current_ptr->next = NULL;
  181. more_data = 0;
  182. } else {
  183. current_ptr->next = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
  184. current_ptr = current_ptr->next;
  185. }
  186. }
  187. return head_ptr;
  188. }
  189.  
  190. int isEmployeeSize(EMPLOYEE* head_ptr) {
  191. int count = 0;
  192. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  193. count++;
  194. }
  195. return count;
  196. }
  197.  
  198. void calcOvertimeHrs(EMPLOYEE* head_ptr) {
  199. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  200. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  201. }
  202. }
  203.  
  204. void calcGrossPay(EMPLOYEE* head_ptr) {
  205. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  206. float normalPay = CALC_NORMAL_PAY(current_ptr->wageRate, current_ptr->hours, current_ptr->overtimeHrs);
  207. float overtimePay = CALC_OT_PAY(current_ptr->wageRate, current_ptr->overtimeHrs);
  208. current_ptr->grossPay = normalPay + overtimePay;
  209. }
  210. }
  211.  
  212. void calcStateTax(EMPLOYEE* head_ptr) {
  213. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  214. if (strcmp(current_ptr->taxState, "MA") == 0)
  215. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, MA_TAX_RATE);
  216. else if (strcmp(current_ptr->taxState, "VT") == 0)
  217. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, VT_TAX_RATE);
  218. else if (strcmp(current_ptr->taxState, "NH") == 0)
  219. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, NH_TAX_RATE);
  220. else if (strcmp(current_ptr->taxState, "CA") == 0)
  221. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, CA_TAX_RATE);
  222. else
  223. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, DEFAULT_STATE_TAX_RATE);
  224. }
  225. }
  226.  
  227. void calcFedTax(EMPLOYEE* head_ptr) {
  228. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  229. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  230. }
  231. }
  232.  
  233. void calcNetPay(EMPLOYEE* head_ptr) {
  234. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  235. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay, current_ptr->stateTax, current_ptr->fedTax);
  236. }
  237. }
  238.  
  239. void calcEmployeeTotals(EMPLOYEE* head_ptr, TOTALS* emp_totals_ptr) {
  240. for (EMPLOYEE* current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  241. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  242. emp_totals_ptr->total_hours += current_ptr->hours;
  243. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  244. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  245. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  246. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  247. emp_totals_ptr->total_netPay += current_ptr->netPay;
  248. }
  249. }
  250.  
  251. void calcEmployeeMinMax(EMPLOYEE* head_ptr, MIN_MAX* emp_minMax_ptr) {
  252. EMPLOYEE* current_ptr = head_ptr;
  253. emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  254. emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = current_ptr->hours;
  255. emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  256. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  257. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  258. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  259. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;
  260.  
  261. for (current_ptr = current_ptr->next; current_ptr; current_ptr = current_ptr->next) {
  262. emp_minMax_ptr->min_wageRate = CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  263. emp_minMax_ptr->max_wageRate = CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  264. emp_minMax_ptr->min_hours = CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  265. emp_minMax_ptr->max_hours = CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  266. emp_minMax_ptr->min_overtimeHrs = CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  267. emp_minMax_ptr->max_overtimeHrs = CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  268. emp_minMax_ptr->min_grossPay = CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  269. emp_minMax_ptr->max_grossPay = CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  270. emp_minMax_ptr->min_stateTax = CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  271. emp_minMax_ptr->max_stateTax = CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  272. emp_minMax_ptr->min_fedTax = CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  273. emp_minMax_ptr->max_fedTax = CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  274. emp_minMax_ptr->min_netPay = CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  275. emp_minMax_ptr->max_netPay = CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  276. }
  277. }
  278.  
  279. void printHeader(void) {
  280. printf("\n\n*** Pay Calculator ***\n");
  281. printf("\n--------------------------------------------------------------");
  282. printf("-------------------");
  283. printf("\nName Tax Clock# Wage Hours OT Gross ");
  284. printf(" State Fed Net");
  285. printf("\n State Pay ");
  286. printf(" Tax Tax Pay");
  287. printf("\n--------------------------------------------------------------");
  288. printf("-------------------");
  289. }
  290.  
  291. void printEmp(EMPLOYEE* head_ptr) {
  292. EMPLOYEE* current_ptr;
  293. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  294. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  295. snprintf(name, sizeof(name), "%s %s", current_ptr->empName.firstName, current_ptr->empName.lastName);
  296. printf("\n%-20s %-2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  297. name, current_ptr->taxState, current_ptr->clockNumber,
  298. current_ptr->wageRate, current_ptr->hours, current_ptr->overtimeHrs,
  299. current_ptr->grossPay, current_ptr->stateTax, current_ptr->fedTax,
  300. current_ptr->netPay);
  301. }
  302. }
  303.  
  304. void printEmpStatistics(TOTALS* emp_totals_ptr, MIN_MAX* emp_minMax_ptr, int size) {
  305. printf("\n--------------------------------------------------------------");
  306. printf("-------------------");
  307. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  308. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  309. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  310. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  311. emp_totals_ptr->total_netPay);
  312. if (size > 0) {
  313. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  314. emp_totals_ptr->total_wageRate / size, emp_totals_ptr->total_hours / size,
  315. emp_totals_ptr->total_overtimeHrs / size, emp_totals_ptr->total_grossPay / size,
  316. emp_totals_ptr->total_stateTax / size, emp_totals_ptr->total_fedTax / size,
  317. emp_totals_ptr->total_netPay / size);
  318. }
  319. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  320. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  321. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  322. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  323. emp_minMax_ptr->min_netPay);
  324. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  325. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  326. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  327. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  328. emp_minMax_ptr->max_netPay);
  329. printf("\n\nThe total employees processed was: %d\n", size);
  330. }
  331.  
Success #stdin #stdout 0s 5284KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Y
Mary
Apl
NH
526488
9.75
42.5
Y
Frank
Fortran
VT
765349
10.50
37.0
Y
Jeff
Ada
NY
34645
12.25
45
Y
Anton
Pascal
CA
127615
8.35
40.0
N
stdout
Enter employee first name: 
Enter employee last name: 
Enter employee two-character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two-character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two-character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two-character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two-character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***