fork download
  1. // Assignment 10 - Employee Pay Calculator with Typedef and Macros
  2. //
  3. // Name: Felix Henriquez
  4. //
  5. // Class: C Programming, Fall 2025
  6. //
  7. // Date: November 23, 2025
  8. //
  9. // Description: This program extends the employee pay calculator by implementing
  10. // typedef aliases for structures and macros for calculations.
  11. // It processes employee data using linked lists, calculates
  12. // overtime, gross pay, state tax, federal tax, and net pay.
  13. // The program also computes totals, averages, and min/max values
  14. // for all employees using macro-based calculations.
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. // ===========================
  22. // MACRO DEFINITIONS
  23. // ===========================
  24.  
  25. // Macro to calculate overtime hours
  26. #define CALC_OT_HOURS(hours) ((hours) > 40.0 ? (hours) - 40.0 : 0.0)
  27.  
  28. // Macro to calculate regular hours
  29. #define CALC_REG_HOURS(hours, ot_hours) ((hours) - (ot_hours))
  30.  
  31. // Macro to calculate gross pay
  32. #define CALC_GROSS_PAY(reg_hours, ot_hours, wage) \
  33.   (((reg_hours) * (wage)) + ((ot_hours) * (wage) * 1.5))
  34.  
  35. // Macro to calculate state tax
  36. #define CALC_STATE_TAX(gross, state) \
  37.   (strcmp(state, "MA") == 0 ? (gross) * 0.05 : \
  38.   strcmp(state, "VT") == 0 ? (gross) * 0.06 : \
  39.   strcmp(state, "NH") == 0 ? 0.00 : \
  40.   strcmp(state, "CA") == 0 ? (gross) * 0.07 : \
  41.   strcmp(state, "NY") == 0 ? (gross) * 0.08 : 0.00)
  42.  
  43. // Macro to calculate federal tax (25% of gross pay)
  44. #define CALC_FED_TAX(gross) ((gross) * 0.25)
  45.  
  46. // Macro to calculate net pay
  47. #define CALC_NET_PAY(gross, state_tax, fed_tax) \
  48.   ((gross) - (state_tax) - (fed_tax))
  49.  
  50. // Fixed MIN and MAX macros
  51. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  52. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  53.  
  54. // ===========================
  55. // TYPE DEFINITIONS
  56. // ===========================
  57.  
  58. // Employee structure
  59. typedef struct employee {
  60. char first_name[21];
  61. char last_name[21];
  62. char tax_state[3];
  63. char clock_number[7];
  64. float wage_rate;
  65. float hours;
  66. float ot_hours;
  67. float gross_pay;
  68. float state_tax;
  69. float fed_tax;
  70. float net_pay;
  71. struct employee* next;
  72. } EMPLOYEE;
  73.  
  74. // Min/Max structure with typedef alias
  75. typedef struct min_max {
  76. float wage_rate;
  77. float hours;
  78. float ot_hours;
  79. float gross_pay;
  80. float state_tax;
  81. float fed_tax;
  82. float net_pay;
  83. } MIN_MAX;
  84.  
  85. // Employee totals structure
  86. typedef struct employee_totals {
  87. float wage_rate;
  88. float hours;
  89. float ot_hours;
  90. float gross_pay;
  91. float state_tax;
  92. float fed_tax;
  93. float net_pay;
  94. MIN_MAX min;
  95. MIN_MAX max;
  96. int count;
  97. } EMPLOYEE_TOTALS;
  98.  
  99. // ===========================
  100. // FUNCTION PROTOTYPES
  101. // ===========================
  102.  
  103. EMPLOYEE* create_employee();
  104. void calculate_employee_pay(EMPLOYEE* emp);
  105. void calculate_employee_totals(EMPLOYEE_TOTALS* totals, const EMPLOYEE* emp);
  106. void init_min_max(MIN_MAX* min_max);
  107. void print_employee_data(const EMPLOYEE* emp);
  108. void print_employee_totals(const EMPLOYEE_TOTALS* totals);
  109.  
  110. // ===========================
  111. // FUNCTION IMPLEMENTATIONS
  112. // ===========================
  113.  
  114. EMPLOYEE* create_employee() {
  115. EMPLOYEE* new_emp = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
  116. if (new_emp == NULL) {
  117. printf("Memory allocation failed!\n");
  118. exit(1);
  119. }
  120. new_emp->next = NULL;
  121. return new_emp;
  122. }
  123.  
  124. void calculate_employee_pay(EMPLOYEE* emp) {
  125. // Calculate overtime hours using macro
  126. emp->ot_hours = CALC_OT_HOURS(emp->hours);
  127.  
  128. // Calculate regular hours using macro
  129. float reg_hours = CALC_REG_HOURS(emp->hours, emp->ot_hours);
  130.  
  131. // Calculate gross pay using macro
  132. emp->gross_pay = CALC_GROSS_PAY(reg_hours, emp->ot_hours, emp->wage_rate);
  133.  
  134. // Calculate state tax using macro
  135. emp->state_tax = CALC_STATE_TAX(emp->gross_pay, emp->tax_state);
  136.  
  137. // Calculate federal tax using macro
  138. emp->fed_tax = CALC_FED_TAX(emp->gross_pay);
  139.  
  140. // Calculate net pay using macro
  141. emp->net_pay = CALC_NET_PAY(emp->gross_pay, emp->state_tax, emp->fed_tax);
  142. }
  143.  
  144. void init_min_max(MIN_MAX* min_max) {
  145. min_max->wage_rate = 999999.0;
  146. min_max->hours = 999999.0;
  147. min_max->ot_hours = 999999.0;
  148. min_max->gross_pay = 999999.0;
  149. min_max->state_tax = 999999.0;
  150. min_max->fed_tax = 999999.0;
  151. min_max->net_pay = 999999.0;
  152. }
  153.  
  154. void calculate_employee_totals(EMPLOYEE_TOTALS* totals, const EMPLOYEE* emp) {
  155. totals->wage_rate += emp->wage_rate;
  156. totals->hours += emp->hours;
  157. totals->ot_hours += emp->ot_hours;
  158. totals->gross_pay += emp->gross_pay;
  159. totals->state_tax += emp->state_tax;
  160. totals->fed_tax += emp->fed_tax;
  161. totals->net_pay += emp->net_pay;
  162. totals->count++;
  163.  
  164. // Update min calculations using MIN macro
  165. totals->min.wage_rate = MIN(totals->min.wage_rate, emp->wage_rate);
  166. totals->min.hours = MIN(totals->min.hours, emp->hours);
  167. totals->min.ot_hours = MIN(totals->min.ot_hours, emp->ot_hours);
  168. totals->min.gross_pay = MIN(totals->min.gross_pay, emp->gross_pay);
  169. totals->min.state_tax = MIN(totals->min.state_tax, emp->state_tax);
  170. totals->min.fed_tax = MIN(totals->min.fed_tax, emp->fed_tax);
  171. totals->min.net_pay = MIN(totals->min.net_pay, emp->net_pay);
  172.  
  173. // Update max calculations using MAX macro
  174. totals->max.wage_rate = MAX(totals->max.wage_rate, emp->wage_rate);
  175. totals->max.hours = MAX(totals->max.hours, emp->hours);
  176. totals->max.ot_hours = MAX(totals->max.ot_hours, emp->ot_hours);
  177. totals->max.gross_pay = MAX(totals->max.gross_pay, emp->gross_pay);
  178. totals->max.state_tax = MAX(totals->max.state_tax, emp->state_tax);
  179. totals->max.fed_tax = MAX(totals->max.fed_tax, emp->fed_tax);
  180. totals->max.net_pay = MAX(totals->max.net_pay, emp->net_pay);
  181. }
  182.  
  183. void print_employee_data(const EMPLOYEE* emp) {
  184. printf("%-19s %-2s %-6s %6.2f %5.1f %4.1f %7.2f %6.2f %6.2f %8.2f\n",
  185. emp->first_name, emp->tax_state, emp->clock_number,
  186. emp->wage_rate, emp->hours, emp->ot_hours, emp->gross_pay,
  187. emp->state_tax, emp->fed_tax, emp->net_pay);
  188. }
  189.  
  190. void print_employee_totals(const EMPLOYEE_TOTALS* totals) {
  191. printf("Totals: %24.2f %5.1f %4.1f %8.2f %6.2f %7.2f %8.2f\n",
  192. totals->wage_rate, totals->hours, totals->ot_hours, totals->gross_pay,
  193. totals->state_tax, totals->fed_tax, totals->net_pay);
  194.  
  195. printf("Averages: %22.2f %5.1f %4.1f %8.2f %6.2f %7.2f %8.2f\n",
  196. totals->wage_rate / totals->count,
  197. totals->hours / totals->count,
  198. totals->ot_hours / totals->count,
  199. totals->gross_pay / totals->count,
  200. totals->state_tax / totals->count,
  201. totals->fed_tax / totals->count,
  202. totals->net_pay / totals->count);
  203.  
  204. printf("Minimum: %23.2f %5.1f %4.1f %8.2f %6.2f %7.2f %8.2f\n",
  205. totals->min.wage_rate, totals->min.hours, totals->min.ot_hours,
  206. totals->min.gross_pay, totals->min.state_tax, totals->min.fed_tax,
  207. totals->min.net_pay);
  208.  
  209. printf("Maximum: %23.2f %5.1f %4.1f %8.2f %6.2f %7.2f %8.2f\n",
  210. totals->max.wage_rate, totals->max.hours, totals->max.ot_hours,
  211. totals->max.gross_pay, totals->max.state_tax, totals->max.fed_tax,
  212. totals->max.net_pay);
  213. }
  214.  
  215. // ===========================
  216. // MAIN FUNCTION
  217. // ===========================
  218.  
  219. int main() {
  220. EMPLOYEE* head = NULL;
  221. EMPLOYEE* tail = NULL;
  222. EMPLOYEE_TOTALS totals = {0};
  223.  
  224. // Initialize min/max values
  225. init_min_max(&totals.min);
  226. totals.max.wage_rate = -999999.0;
  227. totals.max.hours = -999999.0;
  228. totals.max.ot_hours = -999999.0;
  229. totals.max.gross_pay = -999999.0;
  230. totals.max.state_tax = -999999.0;
  231. totals.max.fed_tax = -999999.0;
  232. totals.max.net_pay = -999999.0;
  233.  
  234. // Pre-defined employee data for ideone.com (no user input required)
  235. char* first_names[] = {"Connie", "Mary", "Frank", "Jeff", "Anton"};
  236. char* last_names[] = {"Cobol", "Apl", "Fortran", "Ada", "Pascal"};
  237. char* tax_states[] = {"MA", "NH", "VT", "NY", "CA"};
  238. char* clock_numbers[] = {"98401", "526488", "765349", "34645", "127615"};
  239. float wage_rates[] = {10.60, 9.75, 10.50, 12.25, 8.35};
  240. float hours[] = {51.0, 42.5, 37.0, 45.0, 40.0};
  241. int employee_count = 5;
  242.  
  243. // Print program header
  244. printf("*** Pay Calculator ***\n\n");
  245. printf("---------------------------------------------------------------------------------\n");
  246. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  247. printf(" State Pay Tax Tax Pay\n");
  248. printf("---------------------------------------------------------------------------------\n");
  249.  
  250. // Process all employees using linked list
  251. for (int i = 0; i < employee_count; i++) {
  252. EMPLOYEE* new_emp = create_employee();
  253.  
  254. // Set employee data
  255. strcpy(new_emp->first_name, first_names[i]);
  256. strcpy(new_emp->last_name, last_names[i]);
  257. strcpy(new_emp->tax_state, tax_states[i]);
  258. strcpy(new_emp->clock_number, clock_numbers[i]);
  259. new_emp->wage_rate = wage_rates[i];
  260. new_emp->hours = hours[i];
  261.  
  262. // Calculate pay using macros
  263. calculate_employee_pay(new_emp);
  264.  
  265. // Add to linked list
  266. if (head == NULL) {
  267. head = new_emp;
  268. tail = new_emp;
  269. } else {
  270. tail->next = new_emp;
  271. tail = new_emp;
  272. }
  273.  
  274. // Calculate totals and min/max values
  275. calculate_employee_totals(&totals, new_emp);
  276.  
  277. // Print employee data
  278. print_employee_data(new_emp);
  279. }
  280.  
  281. printf("---------------------------------------------------------------------------------\n");
  282.  
  283. // Print summary statistics
  284. print_employee_totals(&totals);
  285.  
  286. printf("\nThe total employees processed was: %d\n", totals.count);
  287. printf("\n\n *** End of Program *** \n");
  288.  
  289. // Free allocated memory
  290. EMPLOYEE* current = head;
  291. while (current != NULL) {
  292. EMPLOYEE* next = current->next;
  293. free(current);
  294. current = next;
  295. }
  296.  
  297. return 0;
  298. }
Success #stdin #stdout 0s 5324KB
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              MA  98401    10.60   51.0  11.0   598.90   29.95  149.73    419.23
Mary                NH  526488    9.75   42.5   2.5   426.56    0.00  106.64    319.92
Frank               VT  765349   10.50   37.0   0.0   388.50   23.31   97.12    268.07
Jeff                NY  34645    12.25   45.0   5.0   581.88   46.55  145.47    389.86
Anton               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 ***