fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: <Kyle Merrihew>
  6. //
  7. // Class: C Programming, <Spring 2025>
  8. //
  9. // Date: <April 17,2025>
  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. // necessary header files
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include <stdlib.h>
  38. // define constants
  39. #define STD_HOURS 40.0
  40. #define OT_RATE 1.5
  41. #define MA_TAX_RATE 0.05
  42. #define NH_TAX_RATE 0.0
  43. #define VT_TAX_RATE 0.06
  44. #define CA_TAX_RATE 0.07
  45. #define NY_TAX_RATE 0.08
  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. // define macros
  53. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  54. #define CALC_STATE_TAX(thePay,theStateTaxRate) (thePay * theStateTaxRate)
  55. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  56. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  57. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) (theWageRate * (theHours - theOvertimeHrs))
  58. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  59. #define CALC_MIN(theValue, currentMin) ((theValue < currentMin) ? theValue : currentMin)
  60. #define CALC_MAX(theValue, currentMax) ((theValue > currentMax) ? theValue : currentMax)
  61.  
  62. struct name {
  63. char firstName[FIRST_NAME_SIZE];
  64. char lastName [LAST_NAME_SIZE];
  65. };
  66.  
  67. typedef struct employee {
  68. struct name empName;
  69. char taxState[TAX_STATE_SIZE];
  70. long int clockNumber;
  71. float wageRate;
  72. float hours;
  73. float overtimeHrs;
  74. float grossPay;
  75. float stateTax;
  76. float fedTax;
  77. float netPay;
  78. struct employee * next;
  79. } EMPLOYEE;
  80.  
  81. typedef struct totals {
  82. float total_wageRate;
  83. float total_hours;
  84. float total_overtimeHrs;
  85. float total_grossPay;
  86. float total_stateTax;
  87. float total_fedTax;
  88. float total_netPay;
  89. } TOTALS;
  90.  
  91. typedef struct min_max {
  92. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  93. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  94. } MIN_MAX;
  95.  
  96. EMPLOYEE * createEmployee(char first[], char last[], char state[], long int clock, float wage, float hours);
  97. void printHeader(void);
  98. void computePayroll(EMPLOYEE * emp, TOTALS * totals, MIN_MAX * minmax);
  99. void printEmployeeData(EMPLOYEE * emp);
  100. void printTotals(TOTALS totals, int count);
  101. void printMinMax(MIN_MAX minmax);
  102.  
  103. int main(void) {
  104. EMPLOYEE * head = NULL, * current = NULL;
  105. TOTALS totals = {0};
  106. MIN_MAX minmax = {999999,999999,999999,999999,999999,999999,999999,0,0,0,0,0,0,0};
  107.  
  108. head = createEmployee("Connie","Cobol","MA",98401,10.60,51.0);
  109. head->next = createEmployee("Mary","Apl","NH",526488,9.75,42.5);
  110. head->next->next = createEmployee("Frank","Fortran","VT",765349,10.50,37.0);
  111. head->next->next->next = createEmployee("Jeff","Ada","NY",34645,12.25,45);
  112. head->next->next->next->next = createEmployee("Anton","Pascal","CA",127615,8.35,40.0);
  113.  
  114. printf("\n *** Pay Calculator ***\n\n");
  115. printHeader();
  116. current = head;
  117. int count = 0;
  118. while (current != NULL) {
  119. computePayroll(current, &totals, &minmax);
  120. printEmployeeData(current);
  121. current = current->next;
  122. count++;
  123. }
  124. printf("---------------------------------------------------------------------------------\n");
  125. printf("Totals: %5.2f %6.1f %5.1f %7.2f %7.2f %7.2f %8.2f\n",
  126. totals.total_wageRate, totals.total_hours, totals.total_overtimeHrs,
  127. totals.total_grossPay, totals.total_stateTax, totals.total_fedTax, totals.total_netPay);
  128. printTotals(totals, count);
  129. printMinMax(minmax);
  130. printf("\n\nThe total employees processed was: %d\n\n", count);
  131. printf(" *** End of Program ***\n");
  132. return 0;
  133. }
  134.  
  135. EMPLOYEE * createEmployee(char first[], char last[], char state[], long int clock, float wage, float hours) {
  136. EMPLOYEE * emp = (EMPLOYEE *) malloc(sizeof(EMPLOYEE));
  137. strcpy(emp->empName.firstName, first);
  138. strcpy(emp->empName.lastName, last);
  139. strcpy(emp->taxState, state);
  140. emp->clockNumber = clock;
  141. emp->wageRate = wage;
  142. emp->hours = hours;
  143. emp->next = NULL;
  144. return emp;
  145. }
  146.  
  147. void computePayroll(EMPLOYEE * emp, TOTALS * totals, MIN_MAX * minmax) {
  148. emp->overtimeHrs = CALC_OT_HOURS(emp->hours);
  149. float regPay = CALC_NORMAL_PAY(emp->wageRate, emp->hours, emp->overtimeHrs);
  150. float otPay = CALC_OT_PAY(emp->wageRate, emp->overtimeHrs);
  151. emp->grossPay = regPay + otPay;
  152.  
  153. float rate;
  154. if (strcmp(emp->taxState, "MA") == 0) rate = MA_TAX_RATE;
  155. else if (strcmp(emp->taxState, "NH") == 0) rate = NH_TAX_RATE;
  156. else if (strcmp(emp->taxState, "VT") == 0) rate = VT_TAX_RATE;
  157. else if (strcmp(emp->taxState, "CA") == 0) rate = CA_TAX_RATE;
  158. else if (strcmp(emp->taxState, "NY") == 0) rate = NY_TAX_RATE;
  159. else rate = DEFAULT_STATE_TAX_RATE;
  160.  
  161. emp->stateTax = CALC_STATE_TAX(emp->grossPay, rate);
  162. emp->fedTax = CALC_FED_TAX(emp->grossPay);
  163. emp->netPay = CALC_NET_PAY(emp->grossPay, emp->stateTax, emp->fedTax);
  164.  
  165. totals->total_wageRate += emp->wageRate;
  166. totals->total_hours += emp->hours;
  167. totals->total_overtimeHrs += emp->overtimeHrs;
  168. totals->total_grossPay += emp->grossPay;
  169. totals->total_stateTax += emp->stateTax;
  170. totals->total_fedTax += emp->fedTax;
  171. totals->total_netPay += emp->netPay;
  172.  
  173. minmax->min_wageRate = CALC_MIN(emp->wageRate, minmax->min_wageRate);
  174. minmax->min_hours = CALC_MIN(emp->hours, minmax->min_hours);
  175. minmax->min_overtimeHrs = CALC_MIN(emp->overtimeHrs, minmax->min_overtimeHrs);
  176. minmax->min_grossPay = CALC_MIN(emp->grossPay, minmax->min_grossPay);
  177. minmax->min_stateTax = CALC_MIN(emp->stateTax, minmax->min_stateTax);
  178. minmax->min_fedTax = CALC_MIN(emp->fedTax, minmax->min_fedTax);
  179. minmax->min_netPay = CALC_MIN(emp->netPay, minmax->min_netPay);
  180.  
  181. minmax->max_wageRate = CALC_MAX(emp->wageRate, minmax->max_wageRate);
  182. minmax->max_hours = CALC_MAX(emp->hours, minmax->max_hours);
  183. minmax->max_overtimeHrs = CALC_MAX(emp->overtimeHrs, minmax->max_overtimeHrs);
  184. minmax->max_grossPay = CALC_MAX(emp->grossPay, minmax->max_grossPay);
  185. minmax->max_stateTax = CALC_MAX(emp->stateTax, minmax->max_stateTax);
  186. minmax->max_fedTax = CALC_MAX(emp->fedTax, minmax->max_fedTax);
  187. minmax->max_netPay = CALC_MAX(emp->netPay, minmax->max_netPay);
  188. }
  189.  
  190. void printHeader(void) {
  191. printf("---------------------------------------------------------------------------------\n");
  192. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  193. printf(" State Pay Tax Tax Pay\n");
  194. printf("---------------------------------------------------------------------------------\n");
  195. }
  196.  
  197. void printEmployeeData(EMPLOYEE * emp) {
  198. printf("%-10s %-10s %-2s %06li %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %8.2f\n",
  199. emp->empName.firstName, emp->empName.lastName, emp->taxState,
  200. emp->clockNumber, emp->wageRate, emp->hours, emp->overtimeHrs,
  201. emp->grossPay, emp->stateTax, emp->fedTax, emp->netPay);
  202. }
  203.  
  204. void printTotals(TOTALS totals, int count) {
  205. printf("Averages: %5.2f %6.1f %5.1f %7.2f %7.2f %7.2f %8.2f\n",
  206. totals.total_wageRate / count, totals.total_hours / count,
  207. totals.total_overtimeHrs / count, totals.total_grossPay / count,
  208. totals.total_stateTax / count, totals.total_fedTax / count,
  209. totals.total_netPay / count);
  210. }
  211.  
  212. void printMinMax(MIN_MAX minmax) {
  213. printf("Minimum: %5.2f %6.1f %5.1f %7.2f %7.2f %7.2f %8.2f\n",
  214. minmax.min_wageRate, minmax.min_hours, minmax.min_overtimeHrs,
  215. minmax.min_grossPay, minmax.min_stateTax, minmax.min_fedTax, minmax.min_netPay);
  216. printf("Maximum: %5.2f %6.1f %5.1f %7.2f %7.2f %7.2f %8.2f\n",
  217. minmax.max_wageRate, minmax.max_hours, minmax.max_overtimeHrs,
  218. minmax.max_grossPay, minmax.max_stateTax, minmax.max_fedTax, minmax.max_netPay);
  219. }
  220.  
Success #stdin #stdout 0.01s 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
 *** 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 ***