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> // 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.  
  57. // ✅ CALC_FED_TAX macro
  58. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  59.  
  60. // ✅ Net Pay, Regular Pay, Overtime Pay
  61. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  62. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  63. (theWageRate * (theHours - theOvertimeHrs))
  64. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  65.  
  66. // ✅ Min and Max macros
  67. #define CALC_MIN(theValue, currentMin) ((theValue < currentMin) ? theValue : currentMin)
  68. #define CALC_MAX(theValue, currentMax) ((theValue > currentMax) ? theValue : currentMax)
  69.  
  70. // Define a global structure type to store an employee name
  71. struct name
  72. {
  73. char firstName[FIRST_NAME_SIZE];
  74. char lastName [LAST_NAME_SIZE];
  75. };
  76.  
  77. // Define a global structure type to pass employee data between functions
  78. typedef struct employee
  79. {
  80. struct name empName;
  81. char taxState [TAX_STATE_SIZE];
  82. long int clockNumber;
  83. float wageRate;
  84. float hours;
  85. float overtimeHrs;
  86. float grossPay;
  87. float stateTax;
  88. float fedTax;
  89. float netPay;
  90. struct employee * next;
  91. } EMPLOYEE;
  92.  
  93. // Define totals structure
  94. typedef struct totals
  95. {
  96. float total_wageRate;
  97. float total_hours;
  98. float total_overtimeHrs;
  99. float total_grossPay;
  100. float total_stateTax;
  101. float total_fedTax;
  102. float total_netPay;
  103. } TOTALS;
  104.  
  105. // ✅ Define MIN_MAX alias for min and max tracking
  106. typedef struct min_max
  107. {
  108. float min_wageRate;
  109. float min_hours;
  110. float min_overtimeHrs;
  111. float min_grossPay;
  112. float min_stateTax;
  113. float min_fedTax;
  114. float min_netPay;
  115. float max_wageRate;
  116. float max_hours;
  117. float max_overtimeHrs;
  118. float max_grossPay;
  119. float max_stateTax;
  120. float max_fedTax;
  121. float max_netPay;
  122. } MIN_MAX;
  123. // Function Prototypes
  124. void printHeader(void);
  125. EMPLOYEE * createEmployee(char first[], char last[], char state[], long int clock, float wage, float hours);
  126. void printEmployeeData(EMPLOYEE * emp);
  127. void computePayroll(EMPLOYEE * emp, TOTALS * totals, MIN_MAX * minmax);
  128. void printTotals(TOTALS totals, int count);
  129. void printMinMax(MIN_MAX minmax);
  130.  
  131. int main(void)
  132. {
  133. EMPLOYEE * head = NULL;
  134. EMPLOYEE * current = NULL;
  135.  
  136. // Initialize totals
  137. TOTALS totals = {0};
  138. MIN_MAX minmax = {
  139. .min_wageRate = 999999.0, .min_hours = 999999.0, .min_overtimeHrs = 999999.0,
  140. .min_grossPay = 999999.0, .min_stateTax = 999999.0, .min_fedTax = 999999.0, .min_netPay = 999999.0,
  141. .max_wageRate = 0.0, .max_hours = 0.0, .max_overtimeHrs = 0.0,
  142. .max_grossPay = 0.0, .max_stateTax = 0.0, .max_fedTax = 0.0, .max_netPay = 0.0
  143. };
  144.  
  145. // Create employee linked list
  146. head = createEmployee("John", "Doe", "MA", 98401, 10.60, 51.0);
  147. head->next = createEmployee("Jane", "Smith", "NH", 526488, 9.75, 42.5);
  148. head->next->next = createEmployee("Alice", "Johnson", "VT", 765349, 10.50, 37.0);
  149. head->next->next->next = createEmployee("Bob", "Lee", "CA", 34645, 12.25, 45.0);
  150. head->next->next->next->next = createEmployee("Emma", "Davis", "TX", 127615, 8.35, 0.0);
  151.  
  152. printHeader();
  153.  
  154. current = head;
  155. int count = 0;
  156.  
  157. while (current != NULL)
  158. {
  159. computePayroll(current, &totals, &minmax);
  160. printEmployeeData(current);
  161. current = current->next;
  162. count++;
  163. }
  164.  
  165. printTotals(totals, count);
  166. printMinMax(minmax);
  167.  
  168. return 0;
  169. }
  170.  
  171. // Dummy implementations below for illustration — add full logic as needed
  172.  
  173. EMPLOYEE * createEmployee(char first[], char last[], char state[], long int clock, float wage, float hours)
  174. {
  175. EMPLOYEE * emp = (EMPLOYEE *) malloc(sizeof(EMPLOYEE));
  176.  
  177. strcpy(emp->empName.firstName, first);
  178. strcpy(emp->empName.lastName, last);
  179. strcpy(emp->taxState, state);
  180. emp->clockNumber = clock;
  181. emp->wageRate = wage;
  182. emp->hours = hours;
  183. emp->next = NULL;
  184.  
  185. return emp;
  186. }
  187.  
  188. void computePayroll(EMPLOYEE * emp, TOTALS * totals, MIN_MAX * minmax)
  189. {
  190. emp->overtimeHrs = CALC_OT_HOURS(emp->hours);
  191. float regularPay = CALC_NORMAL_PAY(emp->wageRate, emp->hours, emp->overtimeHrs);
  192. float overtimePay = CALC_OT_PAY(emp->wageRate, emp->overtimeHrs);
  193. emp->grossPay = regularPay + overtimePay;
  194.  
  195. float stateTaxRate;
  196.  
  197. if (strcmp(emp->taxState, "MA") == 0)
  198. stateTaxRate = MA_TAX_RATE;
  199. else if (strcmp(emp->taxState, "NH") == 0)
  200. stateTaxRate = NH_TAX_RATE;
  201. else if (strcmp(emp->taxState, "VT") == 0)
  202. stateTaxRate = VT_TAX_RATE;
  203. else if (strcmp(emp->taxState, "CA") == 0)
  204. stateTaxRate = CA_TAX_RATE;
  205. else
  206. stateTaxRate = DEFAULT_STATE_TAX_RATE;
  207.  
  208. emp->stateTax = CALC_STATE_TAX(emp->grossPay, stateTaxRate);
  209. emp->fedTax = CALC_FED_TAX(emp->grossPay);
  210. emp->netPay = CALC_NET_PAY(emp->grossPay, emp->stateTax, emp->fedTax);
  211.  
  212. // Update totals
  213. totals->total_wageRate += emp->wageRate;
  214. totals->total_hours += emp->hours;
  215. totals->total_overtimeHrs += emp->overtimeHrs;
  216. totals->total_grossPay += emp->grossPay;
  217. totals->total_stateTax += emp->stateTax;
  218. totals->total_fedTax += emp->fedTax;
  219. totals->total_netPay += emp->netPay;
  220.  
  221. // Update min values
  222. minmax->min_wageRate = CALC_MIN(emp->wageRate, minmax->min_wageRate);
  223. minmax->min_hours = CALC_MIN(emp->hours, minmax->min_hours);
  224. minmax->min_overtimeHrs = CALC_MIN(emp->overtimeHrs, minmax->min_overtimeHrs);
  225. minmax->min_grossPay = CALC_MIN(emp->grossPay, minmax->min_grossPay);
  226. minmax->min_stateTax = CALC_MIN(emp->stateTax, minmax->min_stateTax);
  227. minmax->min_fedTax = CALC_MIN(emp->fedTax, minmax->min_fedTax);
  228. minmax->min_netPay = CALC_MIN(emp->netPay, minmax->min_netPay);
  229.  
  230. // Update max values
  231. minmax->max_wageRate = CALC_MAX(emp->wageRate, minmax->max_wageRate);
  232. minmax->max_hours = CALC_MAX(emp->hours, minmax->max_hours);
  233. minmax->max_overtimeHrs = CALC_MAX(emp->overtimeHrs, minmax->max_overtimeHrs);
  234. minmax->max_grossPay = CALC_MAX(emp->grossPay, minmax->max_grossPay);
  235. minmax->max_stateTax = CALC_MAX(emp->stateTax, minmax->max_stateTax);
  236. minmax->max_fedTax = CALC_MAX(emp->fedTax, minmax->max_fedTax);
  237. minmax->max_netPay = CALC_MAX(emp->netPay, minmax->max_netPay);
  238. }
  239.  
  240. void printHeader(void)
  241. {
  242. printf("-----------------------------------------------------------------------\n");
  243. printf("Name Clock# Wage Hours OT Gross State Fed Net\n");
  244. printf(" Hours Pay Tax Tax Pay\n");
  245. printf("-----------------------------------------------------------------------\n");
  246. }
  247.  
  248. void printEmployeeData(EMPLOYEE * emp)
  249. {
  250. printf("%-10s %-10s %06li %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  251. emp->empName.firstName,
  252. emp->empName.lastName,
  253. emp->clockNumber,
  254. emp->wageRate,
  255. emp->hours,
  256. emp->overtimeHrs,
  257. emp->grossPay,
  258. emp->stateTax,
  259. emp->fedTax,
  260. emp->netPay);
  261. }
  262.  
  263. void printTotals(TOTALS totals, int count)
  264. {
  265. printf("\nAverages: ");
  266. printf(" %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  267. totals.total_wageRate / count,
  268. totals.total_hours / count,
  269. totals.total_overtimeHrs / count,
  270. totals.total_grossPay / count,
  271. totals.total_stateTax / count,
  272. totals.total_fedTax / count,
  273. totals.total_netPay / count);
  274. }
  275.  
  276. void printMinMax(MIN_MAX minmax)
  277. {
  278. printf("\nMinimums: ");
  279. printf(" %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  280. minmax.min_wageRate,
  281. minmax.min_hours,
  282. minmax.min_overtimeHrs,
  283. minmax.min_grossPay,
  284. minmax.min_stateTax,
  285. minmax.min_fedTax,
  286. minmax.min_netPay);
  287.  
  288. printf("Maximums: ");
  289. printf(" %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  290. minmax.max_wageRate,
  291. minmax.max_hours,
  292. minmax.max_overtimeHrs,
  293. minmax.max_grossPay,
  294. minmax.max_stateTax,
  295. minmax.max_fedTax,
  296. minmax.max_netPay);
  297. }
  298.  
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
-----------------------------------------------------------------------
Name               Clock#   Wage   Hours  OT     Gross   State  Fed    Net
                                Hours  Pay     Tax    Tax    Pay
-----------------------------------------------------------------------
John       Doe        098401  10.60   51.0   11.0   598.90   29.95  149.73   419.23
Jane       Smith      526488   9.75   42.5    2.5   426.56    0.00  106.64   319.92
Alice      Johnson    765349  10.50   37.0    0.0   388.50   23.31   97.12   268.07
Bob        Lee        034645  12.25   45.0    5.0   581.88   40.73  145.47   395.67
Emma       Davis      127615   8.35    0.0    0.0     0.00    0.00    0.00     0.00

Averages:                 10.29   35.1    3.7   399.17   18.80   99.79   280.58

Minimums:                  8.35    0.0    0.0     0.00    0.00    0.00     0.00
Maximums:                 12.25   51.0   11.0   598.90   40.73  149.73   419.23