fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Rose Samedi
  6. //
  7. // Class: C Programming, Fall Semester
  8. //
  9. // Date: 12/1/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. // TODO macro - completed
  58. #define CALC_FED_TAX(thePay) ( (thePay) * FED_TAX_RATE )
  59.  
  60. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  61. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  62. (theWageRate * (theHours - theOvertimeHrs))
  63. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  64.  
  65. // TODO macros - completed
  66. #define CALC_MIN(theValue, currentMin) ( (theValue < currentMin) ? theValue : currentMin )
  67. #define CALC_MAX(theValue, currentMax) ( (theValue > currentMax) ? theValue : currentMax )
  68.  
  69. // name structure
  70. struct name
  71. {
  72. char firstName[FIRST_NAME_SIZE];
  73. char lastName [LAST_NAME_SIZE];
  74. };
  75.  
  76. // employee structure
  77. typedef struct employee
  78. {
  79. struct name empName;
  80. char taxState [TAX_STATE_SIZE];
  81. long int clockNumber;
  82. float wageRate;
  83. float hours;
  84. float overtimeHrs;
  85. float grossPay;
  86. float stateTax;
  87. float fedTax;
  88. float netPay;
  89. struct employee * next;
  90. } EMPLOYEE;
  91.  
  92. // totals structure
  93. typedef struct totals
  94. {
  95. float total_wageRate;
  96. float total_hours;
  97. float total_overtimeHrs;
  98. float total_grossPay;
  99. float total_stateTax;
  100. float total_fedTax;
  101. float total_netPay;
  102. } TOTALS;
  103.  
  104. // TODO - typedef alias MIN_MAX (completed)
  105. typedef struct min_max
  106. {
  107. float min_wageRate;
  108. float min_hours;
  109. float min_overtimeHrs;
  110. float min_grossPay;
  111. float min_stateTax;
  112. float min_fedTax;
  113. float min_netPay;
  114.  
  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.  
  123. } MIN_MAX;
  124.  
  125. // prototypes
  126. EMPLOYEE * getEmpData (void);
  127. int isEmployeeSize (EMPLOYEE * head_ptr);
  128. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  129. void calcGrossPay (EMPLOYEE * head_ptr);
  130. void printHeader (void);
  131. void printEmp (EMPLOYEE * head_ptr);
  132. void calcStateTax (EMPLOYEE * head_ptr);
  133. void calcFedTax (EMPLOYEE * head_ptr);
  134. void calcNetPay (EMPLOYEE * head_ptr);
  135. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  136. TOTALS * emp_totals_ptr);
  137.  
  138. // updated prototypes to use MIN_MAX
  139. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  140. MIN_MAX * emp_minMax_ptr);
  141.  
  142. void printEmpStatistics (TOTALS * emp_totals_ptr,
  143. MIN_MAX * emp_minMax_ptr,
  144. int size);
  145.  
  146. int main ()
  147. {
  148. EMPLOYEE * head_ptr;
  149. int theSize;
  150.  
  151. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  152. TOTALS * emp_totals_ptr = &employeeTotals;
  153.  
  154. // updated min/max declarations
  155. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  156. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  157.  
  158. head_ptr = getEmpData ();
  159. theSize = isEmployeeSize (head_ptr);
  160.  
  161. if (theSize <= 0)
  162. {
  163. printf("\n\n**** There was no employee input to process ***\n");
  164. }
  165. else
  166. {
  167. calcOvertimeHrs (head_ptr);
  168. calcGrossPay (head_ptr);
  169. calcStateTax (head_ptr);
  170. calcFedTax (head_ptr);
  171. calcNetPay (head_ptr);
  172. calcEmployeeTotals (head_ptr, &employeeTotals);
  173. calcEmployeeMinMax (head_ptr, &employeeMinMax);
  174.  
  175. printHeader();
  176. printEmp (head_ptr);
  177.  
  178. printEmpStatistics (&employeeTotals, &employeeMinMax, theSize);
  179. }
  180.  
  181. printf ("\n\n *** End of Program *** \n");
  182. return (0);
  183. }
  184.  
  185. //**************************************************************
  186. // Function: getEmpData
  187. //**************************************************************
  188. EMPLOYEE * getEmpData (void)
  189. {
  190. char answer[80];
  191. int more_data = 1;
  192. char value;
  193.  
  194. EMPLOYEE *current_ptr, *head_ptr;
  195.  
  196. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  197. current_ptr = head_ptr;
  198.  
  199. while (more_data)
  200. {
  201. printf ("\nEnter employee first name: ");
  202. scanf ("%s", current_ptr->empName.firstName);
  203. printf ("\nEnter employee last name: ");
  204. scanf ("%s", current_ptr->empName.lastName);
  205.  
  206. printf ("\nEnter employee two character tax state: ");
  207. scanf ("%s", current_ptr->taxState);
  208.  
  209. printf("\nEnter employee clock number: ");
  210. scanf("%li", & current_ptr -> clockNumber);
  211.  
  212. printf("\nEnter employee hourly wage: ");
  213. scanf("%f", & current_ptr -> wageRate);
  214.  
  215. printf("\nEnter hours worked this week: ");
  216. scanf("%f", & current_ptr -> hours);
  217.  
  218. printf("\nWould you like to add another employee? (y/n): ");
  219. scanf("%s", answer);
  220.  
  221. if ((value = toupper(answer[0])) != 'Y')
  222. {
  223. current_ptr->next = (EMPLOYEE *) NULL;
  224. more_data = 0;
  225. }
  226. else
  227. {
  228. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  229. current_ptr = current_ptr->next;
  230. }
  231. }
  232.  
  233. return(head_ptr);
  234. }
  235.  
  236. //**************************************************************
  237. int isEmployeeSize (EMPLOYEE * head_ptr)
  238. {
  239. EMPLOYEE * current_ptr;
  240. int theSize = 0;
  241.  
  242. if (head_ptr->empName.firstName[0] != '\0')
  243. {
  244. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  245. {
  246. ++theSize;
  247. }
  248. }
  249. return (theSize);
  250. }
  251.  
  252. //**************************************************************
  253. void printHeader (void)
  254. {
  255. printf ("\n\n*** Pay Calculator ***\n");
  256.  
  257. printf("\n--------------------------------------------------------------");
  258. printf("-------------------");
  259. printf("\nName Tax Clock# Wage Hours OT Gross ");
  260. printf(" State Fed Net");
  261. printf("\n State Pay ");
  262. printf(" Tax Tax Pay");
  263.  
  264. printf("\n--------------------------------------------------------------");
  265. printf("-------------------");
  266. }
  267.  
  268. //**************************************************************
  269. void printEmp (EMPLOYEE * head_ptr)
  270. {
  271. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  272. EMPLOYEE * current_ptr;
  273.  
  274. for (current_ptr = head_ptr; current_ptr;
  275. current_ptr = current_ptr->next)
  276. {
  277. strcpy (name, current_ptr->empName.firstName);
  278. strcat (name, " ");
  279. strcat (name, current_ptr->empName.lastName);
  280.  
  281. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  282. name, current_ptr->taxState, current_ptr->clockNumber,
  283. current_ptr->wageRate, current_ptr->hours,
  284. current_ptr->overtimeHrs, current_ptr->grossPay,
  285. current_ptr->stateTax, current_ptr->fedTax,
  286. current_ptr->netPay);
  287. }
  288. }
  289.  
  290. //**************************************************************
  291. void printEmpStatistics (TOTALS * emp_totals_ptr,
  292. MIN_MAX * emp_minMax_ptr,
  293. int theSize)
  294. {
  295. printf("\n--------------------------------------------------------------");
  296. printf("-------------------");
  297.  
  298. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  299. emp_totals_ptr->total_wageRate,
  300. emp_totals_ptr->total_hours,
  301. emp_totals_ptr->total_overtimeHrs,
  302. emp_totals_ptr->total_grossPay,
  303. emp_totals_ptr->total_stateTax,
  304. emp_totals_ptr->total_fedTax,
  305. emp_totals_ptr->total_netPay);
  306.  
  307. if (theSize > 0)
  308. {
  309. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  310. emp_totals_ptr->total_wageRate/theSize,
  311. emp_totals_ptr->total_hours/theSize,
  312. emp_totals_ptr->total_overtimeHrs/theSize,
  313. emp_totals_ptr->total_grossPay/theSize,
  314. emp_totals_ptr->total_stateTax/theSize,
  315. emp_totals_ptr->total_fedTax/theSize,
  316. emp_totals_ptr->total_netPay/theSize);
  317. }
  318.  
  319. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  320. emp_minMax_ptr->min_wageRate,
  321. emp_minMax_ptr->min_hours,
  322. emp_minMax_ptr->min_overtimeHrs,
  323. emp_minMax_ptr->min_grossPay,
  324. emp_minMax_ptr->min_stateTax,
  325. emp_minMax_ptr->min_fedTax,
  326. emp_minMax_ptr->min_netPay);
  327.  
  328. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  329. emp_minMax_ptr->max_wageRate,
  330. emp_minMax_ptr->max_hours,
  331. emp_minMax_ptr->max_overtimeHrs,
  332. emp_minMax_ptr->max_grossPay,
  333. emp_minMax_ptr->max_stateTax,
  334. emp_minMax_ptr->max_fedTax,
  335. emp_minMax_ptr->max_netPay);
  336.  
  337. printf ("\n\nThe total employees processed was: %i\n", theSize);
  338. }
  339.  
  340. //**************************************************************
  341. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  342. {
  343. EMPLOYEE * current_ptr;
  344.  
  345. for (current_ptr = head_ptr; current_ptr;
  346. current_ptr = current_ptr->next)
  347. {
  348. current_ptr->overtimeHrs =
  349. CALC_OT_HOURS(current_ptr->hours);
  350. }
  351. }
  352.  
  353. //**************************************************************
  354. void calcGrossPay (EMPLOYEE * head_ptr)
  355. {
  356. float theNormalPay;
  357. float theOvertimePay;
  358. EMPLOYEE * current_ptr;
  359.  
  360. for (current_ptr = head_ptr; current_ptr;
  361. current_ptr = current_ptr->next)
  362. {
  363. theNormalPay = CALC_NORMAL_PAY(
  364. current_ptr->wageRate,
  365. current_ptr->hours,
  366. current_ptr->overtimeHrs);
  367.  
  368. theOvertimePay = CALC_OT_PAY(
  369. current_ptr->wageRate,
  370. current_ptr->overtimeHrs);
  371.  
  372. current_ptr->grossPay =
  373. theNormalPay + theOvertimePay;
  374. }
  375. }
  376.  
  377. //**************************************************************
  378. void calcStateTax (EMPLOYEE * head_ptr)
  379. {
  380. EMPLOYEE * current_ptr;
  381.  
  382. for (current_ptr = head_ptr; current_ptr;
  383. current_ptr = current_ptr->next)
  384. {
  385. if (islower(current_ptr->taxState[0]))
  386. current_ptr->taxState[0] =
  387. toupper(current_ptr->taxState[0]);
  388. if (islower(current_ptr->taxState[1]))
  389. current_ptr->taxState[1] =
  390. toupper(current_ptr->taxState[1]);
  391.  
  392. if (strcmp(current_ptr->taxState, "MA") == 0)
  393. current_ptr->stateTax =
  394. CALC_STATE_TAX(current_ptr->grossPay, MA_TAX_RATE);
  395. else if (strcmp(current_ptr->taxState, "VT") == 0)
  396. current_ptr->stateTax =
  397. CALC_STATE_TAX(current_ptr->grossPay, VT_TAX_RATE);
  398. else if (strcmp(current_ptr->taxState, "NH") == 0)
  399. current_ptr->stateTax =
  400. CALC_STATE_TAX(current_ptr->grossPay, NH_TAX_RATE);
  401. else if (strcmp(current_ptr->taxState, "CA") == 0)
  402. current_ptr->stateTax =
  403. CALC_STATE_TAX(current_ptr->grossPay, CA_TAX_RATE);
  404. else
  405. current_ptr->stateTax =
  406. CALC_STATE_TAX(current_ptr->grossPay, DEFAULT_STATE_TAX_RATE);
  407. }
  408. }
  409.  
  410. //**************************************************************
  411. void calcFedTax (EMPLOYEE * head_ptr)
  412. {
  413. EMPLOYEE * current_ptr;
  414.  
  415. for (current_ptr = head_ptr; current_ptr;
  416. current_ptr = current_ptr->next)
  417. {
  418. // replaced with macro as required
  419. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  420. }
  421. }
  422.  
  423. //**************************************************************
  424. void calcNetPay (EMPLOYEE * head_ptr)
  425. {
  426. EMPLOYEE * current_ptr;
  427.  
  428. for (current_ptr = head_ptr; current_ptr;
  429. current_ptr = current_ptr->next)
  430. {
  431. current_ptr->netPay =
  432. CALC_NET_PAY(current_ptr->grossPay,
  433. current_ptr->stateTax,
  434. current_ptr->fedTax);
  435. }
  436. }
  437.  
  438. //**************************************************************
  439. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  440. TOTALS * emp_totals_ptr)
  441. {
  442. EMPLOYEE * current_ptr;
  443.  
  444. for (current_ptr = head_ptr; current_ptr;
  445. current_ptr = current_ptr->next)
  446. {
  447. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  448. emp_totals_ptr->total_hours += current_ptr->hours;
  449. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  450. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  451. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  452. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  453. emp_totals_ptr->total_netPay += current_ptr->netPay;
  454. }
  455. }
  456.  
  457. //**************************************************************
  458. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  459. MIN_MAX * emp_minMax_ptr)
  460. {
  461. EMPLOYEE * current_ptr;
  462.  
  463. current_ptr = head_ptr;
  464.  
  465. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  466. emp_minMax_ptr->min_hours = current_ptr->hours;
  467. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  468. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  469. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  470. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  471. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  472.  
  473. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  474. emp_minMax_ptr->max_hours = current_ptr->hours;
  475. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  476. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  477. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  478. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  479. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  480.  
  481. current_ptr = current_ptr->next;
  482.  
  483. for (; current_ptr; current_ptr = current_ptr->next)
  484. {
  485. emp_minMax_ptr->min_wageRate =
  486. CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  487. emp_minMax_ptr->max_wageRate =
  488. CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  489.  
  490. emp_minMax_ptr->min_hours =
  491. CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  492. emp_minMax_ptr->max_hours =
  493. CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  494.  
  495. emp_minMax_ptr->min_overtimeHrs =
  496. CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  497. emp_minMax_ptr->max_overtimeHrs =
  498. CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  499.  
  500. emp_minMax_ptr->min_grossPay =
  501. CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  502. emp_minMax_ptr->max_grossPay =
  503. CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  504.  
  505. emp_minMax_ptr->min_stateTax =
  506. CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  507. emp_minMax_ptr->max_stateTax =
  508. CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  509.  
  510. emp_minMax_ptr->min_fedTax =
  511. CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  512. emp_minMax_ptr->max_fedTax =
  513. CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  514.  
  515. emp_minMax_ptr->min_netPay =
  516. CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  517. emp_minMax_ptr->max_netPay =
  518. CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  519. }
  520. }
Success #stdin #stdout 0s 5320KB
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 ***