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 14,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. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37.  
  38. // Constants
  39. #define MA_TAX_RATE 0.06
  40. #define VT_TAX_RATE 0.04
  41. #define NH_TAX_RATE 0.00
  42. #define CA_TAX_RATE 0.08
  43. #define DEFAULT_STATE_TAX_RATE 0.05
  44. #define FED_TAX_RATE 0.10
  45.  
  46. #define FIRST_NAME_SIZE 20
  47. #define LAST_NAME_SIZE 30
  48.  
  49. // Macros
  50. #define CALC_OT_HOURS(hours) ((hours) > 40 ? (hours) - 40 : 0)
  51. #define CALC_NORMAL_PAY(rate, hours, overtime) ((hours) - (overtime)) * (rate)
  52. #define CALC_OT_PAY(rate, overtime) ((overtime) * (rate) * 1.5)
  53. #define CALC_STATE_TAX(grossPay, rate) ((grossPay) * (rate))
  54. #define CALC_FED_TAX(grossPay) ((grossPay) * FED_TAX_RATE)
  55. #define CALC_NET_PAY(grossPay, stateTax, fedTax) ((grossPay) - (stateTax) - (fedTax))
  56. #define CALC_MIN(a, b) ((a) < (b) ? (a) : (b))
  57. #define CALC_MAX(a, b) ((a) > (b) ? (a) : (b))
  58.  
  59. // Struct Definitions
  60. typedef struct {
  61. char firstName[FIRST_NAME_SIZE];
  62. char lastName[LAST_NAME_SIZE];
  63. } Name;
  64.  
  65. typedef struct employee {
  66. Name empName;
  67. char taxState[3]; // State code (2 characters)
  68. long clockNumber;
  69. float wageRate;
  70. float hours;
  71. float overtimeHrs;
  72. float grossPay;
  73. float stateTax;
  74. float fedTax;
  75. float netPay;
  76. struct employee *next;
  77. } EMPLOYEE;
  78.  
  79. typedef struct {
  80. float total_wageRate;
  81. float total_hours;
  82. float total_overtimeHrs;
  83. float total_grossPay;
  84. float total_stateTax;
  85. float total_fedTax;
  86. float total_netPay;
  87. } TOTALS;
  88.  
  89. typedef struct min_max {
  90. float min_wageRate;
  91. float max_wageRate;
  92. float min_hours;
  93. float max_hours;
  94. float min_overtimeHrs;
  95. float max_overtimeHrs;
  96. float min_grossPay;
  97. float max_grossPay;
  98. float min_stateTax;
  99. float max_stateTax;
  100. float min_fedTax;
  101. float max_fedTax;
  102. float min_netPay;
  103. float max_netPay;
  104. } MIN_MAX;
  105.  
  106. // Function Prototypes
  107. void initializeEmployee(EMPLOYEE *emp);
  108. void printEmployee(EMPLOYEE *emp);
  109. void calculateEmployeePay(EMPLOYEE *emp);
  110. void calculateTotals(EMPLOYEE *emp, TOTALS *totals);
  111. void printTotals(TOTALS *totals);
  112. void calculateMinMax(EMPLOYEE *emp, MIN_MAX *minMax);
  113. void printMinMax(MIN_MAX *minMax);
  114. float getTaxRate(char *state);
  115.  
  116. // Main function
  117. int main() {
  118. EMPLOYEE *head = NULL;
  119. EMPLOYEE *current = NULL;
  120. TOTALS totals = {0};
  121. MIN_MAX minMax = {0};
  122.  
  123. char choice;
  124.  
  125. do {
  126. EMPLOYEE *newEmployee = (EMPLOYEE *)malloc(sizeof(EMPLOYEE));
  127. if (!newEmployee) {
  128. printf("Memory allocation failed.\n");
  129. return 1;
  130. }
  131.  
  132. initializeEmployee(newEmployee);
  133.  
  134. calculateEmployeePay(newEmployee);
  135. calculateTotals(newEmployee, &totals);
  136. calculateMinMax(newEmployee, &minMax);
  137.  
  138. // Add employee to linked list
  139. if (head == NULL) {
  140. head = newEmployee;
  141. } else {
  142. current->next = newEmployee;
  143. }
  144. current = newEmployee;
  145.  
  146. printf("Do you want to enter another employee? (Y/N): ");
  147. choice = toupper(getchar());
  148. getchar(); // To consume the newline character after input
  149.  
  150. } while (choice == 'Y');
  151.  
  152. // Print all employees
  153. current = head;
  154. while (current != NULL) {
  155. printEmployee(current);
  156. current = current->next;
  157. }
  158.  
  159. // Print totals and min/max
  160. printTotals(&totals);
  161. printMinMax(&minMax);
  162.  
  163. return 0;
  164. }
  165.  
  166. // Function Definitions
  167.  
  168. void initializeEmployee(EMPLOYEE *emp) {
  169. printf("Enter employee first name: ");
  170. fgets(emp->empName.firstName, FIRST_NAME_SIZE, stdin);
  171. emp->empName.firstName[strcspn(emp->empName.firstName, "\n")] = '\0'; // Remove newline
  172.  
  173. printf("Enter employee last name: ");
  174. fgets(emp->empName.lastName, LAST_NAME_SIZE, stdin);
  175. emp->empName.lastName[strcspn(emp->empName.lastName, "\n")] = '\0'; // Remove newline
  176.  
  177. printf("Enter employee clock number: ");
  178. scanf("%ld", &emp->clockNumber);
  179.  
  180. printf("Enter employee wage rate: ");
  181. scanf("%f", &emp->wageRate);
  182.  
  183. printf("Enter employee hours worked: ");
  184. scanf("%f", &emp->hours);
  185.  
  186. emp->overtimeHrs = CALC_OT_HOURS(emp->hours);
  187. }
  188.  
  189. void printEmployee(EMPLOYEE *emp) {
  190. printf("\nEmployee Name: %s %s\n", emp->empName.firstName, emp->empName.lastName);
  191. printf("Clock Number: %ld\n", emp->clockNumber);
  192. printf("Wage Rate: %.2f\n", emp->wageRate);
  193. printf("Hours Worked: %.2f\n", emp->hours);
  194. printf("Overtime Hours: %.2f\n", emp->overtimeHrs);
  195. printf("Gross Pay: %.2f\n", emp->grossPay);
  196. printf("State Tax: %.2f\n", emp->stateTax);
  197. printf("Federal Tax: %.2f\n", emp->fedTax);
  198. printf("Net Pay: %.2f\n\n", emp->netPay);
  199. }
  200.  
  201. void calculateEmployeePay(EMPLOYEE *emp) {
  202. float overtime = emp->overtimeHrs;
  203. emp->grossPay = CALC_NORMAL_PAY(emp->wageRate, emp->hours, overtime) + CALC_OT_PAY(emp->wageRate, overtime);
  204. emp->stateTax = CALC_STATE_TAX(emp->grossPay, getTaxRate(emp->taxState));
  205. emp->fedTax = CALC_FED_TAX(emp->grossPay);
  206. emp->netPay = CALC_NET_PAY(emp->grossPay, emp->stateTax, emp->fedTax);
  207. }
  208.  
  209. void calculateTotals(EMPLOYEE *emp, TOTALS *totals) {
  210. totals->total_wageRate += emp->wageRate;
  211. totals->total_hours += emp->hours;
  212. totals->total_overtimeHrs += emp->overtimeHrs;
  213. totals->total_grossPay += emp->grossPay;
  214. totals->total_stateTax += emp->stateTax;
  215. totals->total_fedTax += emp->fedTax;
  216. totals->total_netPay += emp->netPay;
  217. }
  218.  
  219. void printTotals(TOTALS *totals) {
  220. printf("\nTotal Wage Rate: %.2f\n", totals->total_wageRate);
  221. printf("Total Hours Worked: %.2f\n", totals->total_hours);
  222. printf("Total Overtime Hours: %.2f\n", totals->total_overtimeHrs);
  223. printf("Total Gross Pay: %.2f\n", totals->total_grossPay);
  224. printf("Total State Tax: %.2f\n", totals->total_stateTax);
  225. printf("Total Federal Tax: %.2f\n", totals->total_fedTax);
  226. printf("Total Net Pay: %.2f\n\n", totals->total_netPay);
  227. }
  228.  
  229. void calculateMinMax(EMPLOYEE *emp, MIN_MAX *minMax) {
  230. minMax->min_wageRate = (minMax->min_wageRate == 0) ? emp->wageRate : CALC_MIN(minMax->min_wageRate, emp->wageRate);
  231. minMax->max_wageRate = (minMax->max_wageRate == 0) ? emp->wageRate : CALC_MAX(minMax->max_wageRate, emp->wageRate);
  232. minMax->min_hours = (minMax->min_hours == 0) ? emp->hours : CALC_MIN(minMax->min_hours, emp->hours);
  233. minMax->max_hours = (minMax->max_hours == 0) ? emp->hours : CALC_MAX(minMax->max_hours, emp->hours);
  234. minMax->min_overtimeHrs = (minMax->min_overtimeHrs == 0) ? emp->overtimeHrs : CALC_MIN(minMax->min_overtimeHrs, emp->overtimeHrs);
  235. minMax->max_overtimeHrs = (minMax->max_overtimeHrs == 0) ? emp->overtimeHrs : CALC_MAX(minMax->max_overtimeHrs, emp->overtimeHrs);
  236. minMax->min_grossPay = (minMax->min_grossPay == 0) ? emp->grossPay : CALC_MIN(minMax->min_grossPay, emp->grossPay);
  237. minMax->max_grossPay = (minMax->max_grossPay == 0) ? emp->grossPay : CALC_MAX(minMax->max_grossPay, emp->grossPay);
  238. minMax->min_stateTax = (minMax->min_stateTax == 0) ? emp->stateTax : CALC_MIN(minMax->min_stateTax, emp->stateTax);
  239. minMax->max_stateTax = (minMax->max_stateTax == 0) ? emp->stateTax : CALC_MAX(minMax->max_stateTax, emp->stateTax);
  240. minMax->min_fedTax = (minMax->min_fedTax == 0) ? emp->fedTax : CALC_MIN(minMax->min_fedTax, emp->fedTax);
  241. minMax->max_fedTax = (minMax->max_fedTax == 0) ? emp->fedTax : CALC_MAX(minMax->max_fedTax, emp->fedTax);
  242. minMax->min_netPay = (minMax->min_netPay == 0) ? emp->netPay : CALC_MIN(minMax->min_netPay, emp->netPay);
  243. minMax->max_netPay = (minMax->max_netPay == 0) ? emp->netPay : CALC_MAX(minMax->max_netPay, emp->netPay);
  244. }
  245.  
  246. void printMinMax(MIN_MAX *minMax) {
  247. printf("\nMin Wage Rate: %.2f | Max Wage Rate: %.2f\n", minMax->min_wageRate, minMax->max_wageRate);
  248. printf("Min Hours: %.2f | Max Hours: %.2f\n", minMax->min_hours, minMax->max_hours);
  249. printf("Min Overtime Hours: %.2f | Max Overtime Hours: %.2f\n", minMax->min_overtimeHrs, minMax->max_overtimeHrs);
  250. printf("Min Gross Pay: %.2f | Max Gross Pay: %.2f\n", minMax->min_grossPay, minMax->max_grossPay);
  251. printf("Min State Tax: %.2f | Max State Tax: %.2f\n", minMax->min_stateTax, minMax->max_stateTax);
  252. printf("Min Federal Tax: %.2f | Max Federal Tax: %.2f\n", minMax->min_fedTax, minMax->max_fedTax);
  253. printf("Min Net Pay: %.2f | Max Net Pay: %.2f\n", minMax->min_netPay, minMax->max_netPay);
  254. }
  255.  
  256. float getTaxRate(char *state) {
  257. if (strcmp(state, "MA") == 0) {
  258. return MA_TAX_RATE;
  259. } else if (strcmp(state, "VT") == 0) {
  260. return VT_TAX_RATE;
  261. } else if (strcmp(state, "NH") == 0) {
  262. return NH_TAX_RATE;
  263. } else if (strcmp(state, "CA") == 0) {
  264. return CA_TAX_RATE;
  265. } else {
  266. return DEFAULT_STATE_TAX_RATE;
  267. }
  268. }
Success #stdin #stdout 0.01s 5288KB
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 clock number: Enter employee wage rate: Enter employee hours worked: Do you want to enter another employee? (Y/N): 
Employee Name: Connie Cobol
Clock Number: 0
Wage Rate: 0.00
Hours Worked: 0.00
Overtime Hours: 0.00
Gross Pay: 0.00
State Tax: 0.00
Federal Tax: 0.00
Net Pay: 0.00


Total Wage Rate: 0.00
Total Hours Worked: 0.00
Total Overtime Hours: 0.00
Total Gross Pay: 0.00
Total State Tax: 0.00
Total Federal Tax: 0.00
Total Net Pay: 0.00


Min Wage Rate: 0.00 | Max Wage Rate: 0.00
Min Hours: 0.00 | Max Hours: 0.00
Min Overtime Hours: 0.00 | Max Overtime Hours: 0.00
Min Gross Pay: 0.00 | Max Gross Pay: 0.00
Min State Tax: 0.00 | Max State Tax: 0.00
Min Federal Tax: 0.00 | Max Federal Tax: 0.00
Min Net Pay: 0.00 | Max Net Pay: 0.00