fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Morgan Card-Gimpelman
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 4/20/25
  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. #define CALC_FED_TAX(thePay) ((thePay) * FED_TAX_RATE)
  57. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  58. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  59. (theWageRate * (theHours - theOvertimeHrs))
  60. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  61. #define CALC_MIN(theValue, currentMin) ((theValue) < (currentMin) ? (theValue) : (currentMin))
  62. #define CALC_MAX(theValue, currentMax) ((theValue) > (currentMax) ? (theValue) : (currentMax))
  63.  
  64.  
  65. // Define a global structure type to store an employee name
  66. struct name
  67. {
  68. char firstName[FIRST_NAME_SIZE];
  69. char lastName [LAST_NAME_SIZE];
  70. };
  71.  
  72. // Define a global structure type to pass employee data between functions
  73. typedef struct employee
  74. {
  75. struct name empName;
  76. char taxState [TAX_STATE_SIZE];
  77. long int clockNumber;
  78. float wageRate;
  79. float hours;
  80. float overtimeHrs;
  81. float grossPay;
  82. float stateTax;
  83. float fedTax;
  84. float netPay;
  85. struct employee * next;
  86. } EMPLOYEE;
  87.  
  88. // This structure type defines the totals of all floating point items
  89. // so they can be totaled and used also to calculate averages
  90. typedef struct totals
  91. {
  92. float total_wageRate;
  93. float total_hours;
  94. float total_overtimeHrs;
  95. float total_grossPay;
  96. float total_stateTax;
  97. float total_fedTax;
  98. float total_netPay;
  99. } TOTALS;
  100.  
  101. typedef struct min_max
  102. {
  103. float min_wageRate;
  104. float min_hours;
  105. float min_overtimeHrs;
  106. float min_grossPay;
  107. float min_stateTax;
  108. float min_fedTax;
  109. float min_netPay;
  110. float max_wageRate;
  111. float max_hours;
  112. float max_overtimeHrs;
  113. float max_grossPay;
  114. float max_stateTax;
  115. float max_fedTax;
  116. float max_netPay;
  117. } MIN_MAX;
  118.  
  119. EMPLOYEE * getEmpData (void);
  120. int isEmployeeSize (EMPLOYEE * head_ptr);
  121. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  122. void calcGrossPay (EMPLOYEE * head_ptr);
  123. void printHeader (void);
  124. void printEmp (EMPLOYEE * head_ptr);
  125. void calcStateTax (EMPLOYEE * head_ptr);
  126. void calcFedTax (EMPLOYEE * head_ptr);
  127. void calcNetPay (EMPLOYEE * head_ptr);
  128. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  129. TOTALS * emp_totals_ptr);
  130. void calcEmployeeMinMax (EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr);
  131.  
  132. void printEmpStatistics (TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr,
  133. int size);
  134.  
  135. int main ()
  136. {
  137.  
  138. // ******************************************************************
  139. // Set up head pointer in the main function to point to the
  140. // start of the dynamically allocated linked list nodes that will be
  141. // created and stored in the Heap area.
  142. // ******************************************************************
  143. EMPLOYEE * head_ptr; // always points to first linked list node
  144.  
  145. int theSize; // number of employees processed
  146.  
  147. // set up structure to store totals and initialize all to zero
  148. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  149.  
  150. // pointer to the employeeTotals structure
  151. TOTALS * emp_totals_ptr = &employeeTotals;
  152.  
  153. // set up structure to store min and max values and initialize all to zero
  154. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  155.  
  156. // pointer to the employeeMinMax structure
  157. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  158.  
  159. // ********************************************************************
  160. // Read the employee input and dynamically allocate and set up our
  161. // linked list in the Heap area. The address of the first linked
  162. // list item representing our first employee will be returned and
  163. // its value is set in our head_ptr. We can then use the head_ptr
  164. // throughout the rest of this program anytime we want to get to get
  165. // to the beginning of our linked list.
  166. // ********************************************************************
  167.  
  168. head_ptr = getEmpData ();
  169.  
  170. // ********************************************************************
  171. // With the head_ptr now pointing to the first linked list node, we
  172. // can pass it to any function who needs to get to the starting point
  173. // of the linked list in the Heap. From there, functions can traverse
  174. // through the linked list to access and/or update each employee.
  175. //
  176. // Important: Don't update the head_ptr ... otherwise, you could lose
  177. // the address in the heap of the first linked list node.
  178. //
  179. // ********************************************************************
  180.  
  181. // determine how many employees are in our linked list
  182.  
  183. theSize = isEmployeeSize (head_ptr);
  184.  
  185. // Skip all the function calls to process the data if there
  186. // was no employee information to read in the input
  187. if (theSize <= 0)
  188. {
  189. // print a user friendly message and skip the rest of the processing
  190. printf("\n\n**** There was no employee input to process ***\n");
  191. }
  192.  
  193. else // there are employees to be processed
  194. {
  195.  
  196. // *********************************************************
  197. // Perform calculations and print out information as needed
  198. // *********************************************************
  199.  
  200. // Calculate the overtime hours
  201. calcOvertimeHrs (head_ptr);
  202.  
  203. // Calculate the weekly gross pay
  204. calcGrossPay (head_ptr);
  205.  
  206. // Calculate the state tax
  207. calcStateTax (head_ptr);
  208.  
  209. // Calculate the federal tax
  210. calcFedTax (head_ptr);
  211.  
  212. // Calculate the net pay after taxes
  213. calcNetPay (head_ptr);
  214.  
  215. // *********************************************************
  216. // Keep a running sum of the employee totals
  217. //
  218. // Note the & to specify the address of the employeeTotals
  219. // structure. Needed since pointers work with addresses.
  220. // Unlike array names, C does not see structure names
  221. // as address, hence the need for using the &employeeTotals
  222. // which the complier sees as "address of" employeeTotals
  223. // *********************************************************
  224. calcEmployeeTotals (head_ptr,
  225. &employeeTotals);
  226.  
  227. // *****************************************************************
  228. // Keep a running update of the employee minimum and maximum values
  229. //
  230. // Note we are passing the address of the MinMax structure
  231. // *****************************************************************
  232. calcEmployeeMinMax (head_ptr,
  233. &employeeMinMax);
  234.  
  235. // Print the column headers
  236. printHeader();
  237.  
  238. // print out final information on each employee
  239. printEmp (head_ptr);
  240.  
  241. // **************************************************
  242. // print the totals and averages for all float items
  243. //
  244. // Note that we are passing the addresses of the
  245. // the two structures
  246. // **************************************************
  247. printEmpStatistics (&employeeTotals,
  248. &employeeMinMax,
  249. theSize);
  250. }
  251.  
  252. // indicate that the program completed all processing
  253. printf ("\n\n *** End of Program *** \n");
  254.  
  255. return (0); // success
  256.  
  257. } // main
  258.  
  259. //**************************************************************
  260. // Function: getEmpData
  261. //
  262. // Purpose: Obtains input from user: employee name (first an last),
  263. // tax state, clock number, hourly wage, and hours worked
  264. // in a given week.
  265. //
  266. // Information in stored in a dynamically created linked
  267. // list for all employees.
  268. //
  269. // Parameters: void
  270. //
  271. // Returns:
  272. //
  273. // head_ptr - a pointer to the beginning of the dynamically
  274. // created linked list that contains the initial
  275. // input for each employee.
  276. //
  277. //**************************************************************
  278.  
  279. EMPLOYEE * getEmpData (void)
  280. {
  281.  
  282. char answer[80]; // user prompt response
  283. int more_data = 1; // a flag to indicate if another employee
  284. // needs to be processed
  285. char value; // the first char of the user prompt response
  286.  
  287. EMPLOYEE *current_ptr, // pointer to current node
  288. *head_ptr; // always points to first node
  289.  
  290. // Set up storage for first node
  291. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  292. current_ptr = head_ptr;
  293.  
  294. // process while there is still input
  295. while (more_data)
  296. {
  297.  
  298. // read in employee first and last name
  299. printf ("\nEnter employee first name: ");
  300. scanf ("%s", current_ptr->empName.firstName);
  301. printf ("\nEnter employee last name: ");
  302. scanf ("%s", current_ptr->empName.lastName);
  303.  
  304. // read in employee tax state
  305. printf ("\nEnter employee two character tax state: ");
  306. scanf ("%s", current_ptr->taxState);
  307.  
  308. // read in employee clock number
  309. printf("\nEnter employee clock number: ");
  310. scanf("%li", & current_ptr -> clockNumber);
  311.  
  312. // read in employee wage rate
  313. printf("\nEnter employee hourly wage: ");
  314. scanf("%f", & current_ptr -> wageRate);
  315.  
  316. // read in employee hours worked
  317. printf("\nEnter hours worked this week: ");
  318. scanf("%f", & current_ptr -> hours);
  319.  
  320. // ask user if they would like to add another employee
  321. printf("\nWould you like to add another employee? (y/n): ");
  322. scanf("%s", answer);
  323.  
  324. // check first character for a 'Y' for yes
  325. // Ask user if they want to add another employee
  326. if ((value = toupper(answer[0])) != 'Y')
  327. {
  328. // no more employees to process
  329. current_ptr->next = (EMPLOYEE *) NULL;
  330. more_data = 0;
  331. }
  332. else // Yes, another employee
  333. {
  334. // set the next pointer of the current node to point to the new node
  335. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  336. // move the current node pointer to the new node
  337. current_ptr = current_ptr->next;
  338. }
  339.  
  340. } // while
  341.  
  342. return(head_ptr);
  343.  
  344. } // getEmpData
  345.  
  346. //*************************************************************
  347. // Function: isEmployeeSize
  348. //
  349. // Purpose: Traverses the linked list and keeps a running count
  350. // on how many employees are currently in our list.
  351. //
  352. // Parameters:
  353. //
  354. // head_ptr - pointer to the initial node in our linked list
  355. //
  356. // Returns:
  357. //
  358. // theSize - the number of employees in our linked list
  359. //
  360. //**************************************************************
  361.  
  362. int isEmployeeSize (EMPLOYEE * head_ptr)
  363. {
  364.  
  365. EMPLOYEE * current_ptr; // pointer to current node
  366. int theSize; // number of link list nodes
  367. // (i.e., employees)
  368.  
  369. theSize = 0; // initialize
  370.  
  371. // assume there is no data if the first node does
  372. // not have an employee name
  373. if (head_ptr->empName.firstName[0] != '\0')
  374. {
  375.  
  376. // traverse through the linked list, keep a running count of nodes
  377. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  378. {
  379.  
  380. ++theSize; // employee node found, increment
  381.  
  382. } // for
  383. }
  384.  
  385. return (theSize); // number of nodes (i.e., employees)
  386.  
  387.  
  388. } // isEmployeeSize
  389.  
  390. //**************************************************************
  391. // Function: printHeader
  392. //
  393. // Purpose: Prints the initial table header information.
  394. //
  395. // Parameters: none
  396. //
  397. // Returns: void
  398. //
  399. //**************************************************************
  400.  
  401. void printHeader (void)
  402. {
  403.  
  404. printf ("\n\n*** Pay Calculator ***\n");
  405.  
  406. // print the table header
  407. printf("\n--------------------------------------------------------------");
  408. printf("-------------------");
  409. printf("\nName Tax Clock# Wage Hours OT Gross ");
  410. printf(" State Fed Net");
  411. printf("\n State Pay ");
  412. printf(" Tax Tax Pay");
  413.  
  414. printf("\n--------------------------------------------------------------");
  415. printf("-------------------");
  416.  
  417. } // printHeader
  418.  
  419. //*************************************************************
  420. // Function: printEmp
  421. //
  422. // Purpose: Prints out all the information for each employee
  423. // in a nice and orderly table format.
  424. //
  425. // Parameters:
  426. //
  427. // head_ptr - pointer to the beginning of our linked list
  428. //
  429. // Returns: void
  430. //
  431. //**************************************************************
  432.  
  433. void printEmp (EMPLOYEE * head_ptr)
  434. {
  435.  
  436. // Used to format the employee name
  437. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  438.  
  439. EMPLOYEE * current_ptr; // pointer to current node
  440.  
  441. // traverse through the linked list to process each employee
  442. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  443. {
  444.  
  445. strcpy (name, current_ptr->empName.firstName);
  446. strcat (name, " "); // add a space between first and last names
  447. strcat (name, current_ptr->empName.lastName);
  448.  
  449. // Print out current employee in the current linked list node
  450. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  451. name, current_ptr->taxState, current_ptr->clockNumber,
  452. current_ptr->wageRate, current_ptr->hours,
  453. current_ptr->overtimeHrs, current_ptr->grossPay,
  454. current_ptr->stateTax, current_ptr->fedTax,
  455. current_ptr->netPay);
  456.  
  457. } // for
  458.  
  459. } // printEmp
  460.  
  461. //*************************************************************
  462. // Function: printEmpStatistics
  463. //
  464. // Purpose: Prints out the summary totals and averages of all
  465. // floating point value items for all employees
  466. // that have been processed. It also prints
  467. // out the min and max values.
  468. //
  469. // Parameters:
  470. //
  471. // emp_totals_ptr - pointer to a structure containing a running total
  472. // of all employee floating point items
  473. //
  474. // emp_minMax_ptr - pointer to a structure containing
  475. // the minimum and maximum values of all
  476. // employee floating point items
  477. //
  478. // tjeSize - the total number of employees processed, used
  479. // to check for zero or negative divide condition.
  480. //
  481. // Returns: void
  482. //
  483. //**************************************************************
  484.  
  485. void printEmpStatistics (TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr,
  486. int theSize)
  487. {
  488.  
  489. // print a separator line
  490. printf("\n--------------------------------------------------------------");
  491. printf("-------------------");
  492.  
  493. // print the totals for all the floating point items
  494. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  495. emp_totals_ptr->total_wageRate,
  496. emp_totals_ptr->total_hours,
  497. emp_totals_ptr->total_overtimeHrs,
  498. emp_totals_ptr->total_grossPay,
  499. emp_totals_ptr->total_stateTax,
  500. emp_totals_ptr->total_fedTax,
  501. emp_totals_ptr->total_netPay);
  502.  
  503. // make sure you don't divide by zero or a negative number
  504. if (theSize > 0)
  505. {
  506. // print the averages for all the floating point items
  507. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  508. emp_totals_ptr->total_wageRate/theSize,
  509. emp_totals_ptr->total_hours/theSize,
  510. emp_totals_ptr->total_overtimeHrs/theSize,
  511. emp_totals_ptr->total_grossPay/theSize,
  512. emp_totals_ptr->total_stateTax/theSize,
  513. emp_totals_ptr->total_fedTax/theSize,
  514. emp_totals_ptr->total_netPay/theSize);
  515.  
  516. } // if
  517.  
  518. // print the min and max values for each item
  519.  
  520. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  521. emp_minMax_ptr->min_wageRate,
  522. emp_minMax_ptr->min_hours,
  523. emp_minMax_ptr->min_overtimeHrs,
  524. emp_minMax_ptr->min_grossPay,
  525. emp_minMax_ptr->min_stateTax,
  526. emp_minMax_ptr->min_fedTax,
  527. emp_minMax_ptr->min_netPay);
  528.  
  529. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  530. emp_minMax_ptr->max_wageRate,
  531. emp_minMax_ptr->max_hours,
  532. emp_minMax_ptr->max_overtimeHrs,
  533. emp_minMax_ptr->max_grossPay,
  534. emp_minMax_ptr->max_stateTax,
  535. emp_minMax_ptr->max_fedTax,
  536. emp_minMax_ptr->max_netPay);
  537.  
  538. // print out the total employees process
  539. printf ("\n\nThe total employees processed was: %i\n", theSize);
  540.  
  541. } // printEmpStatistics
  542.  
  543. //*************************************************************
  544. // Function: calcOvertimeHrs
  545. //
  546. // Purpose: Calculates the overtime hours worked by an employee
  547. // in a given week for each employee.
  548. //
  549. // Parameters:
  550. //
  551. // head_ptr - pointer to the beginning of our linked list
  552. //
  553. // Returns: void (the overtime hours gets updated by reference)
  554. //
  555. //**************************************************************
  556.  
  557. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  558. {
  559.  
  560. EMPLOYEE * current_ptr; // pointer to current node
  561.  
  562. // traverse through the linked list to calculate overtime hours
  563. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  564. {
  565. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  566.  
  567. } // for
  568.  
  569.  
  570. } // calcOvertimeHrs
  571.  
  572. //*************************************************************
  573. // Function: calcGrossPay
  574. //
  575. // Purpose: Calculates the gross pay based on the the normal pay
  576. // and any overtime pay for a given week for each
  577. // employee.
  578. //
  579. // Parameters:
  580. //
  581. // head_ptr - pointer to the beginning of our linked list
  582. //
  583. // Returns: void (the gross pay gets updated by reference)
  584. //
  585. //**************************************************************
  586.  
  587. void calcGrossPay (EMPLOYEE * head_ptr)
  588. {
  589.  
  590. float theNormalPay; // normal pay without any overtime hours
  591. float theOvertimePay; // overtime pay
  592.  
  593. EMPLOYEE * current_ptr; // pointer to current node
  594.  
  595. // traverse through the linked list to calculate gross pay
  596. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  597. {
  598. // calculate normal pay and any overtime pay
  599. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  600. current_ptr->hours,
  601. current_ptr->overtimeHrs);
  602. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  603. current_ptr->overtimeHrs);
  604.  
  605. // calculate gross pay for employee as normalPay + any overtime pay
  606. current_ptr->grossPay = theNormalPay + theOvertimePay;
  607.  
  608. }
  609.  
  610. } // calcGrossPay
  611.  
  612. //*************************************************************
  613. // Function: calcStateTax
  614. //
  615. // Purpose: Calculates the State Tax owed based on gross pay
  616. // for each employee. State tax rate is based on the
  617. // the designated tax state based on where the
  618. // employee is actually performing the work. Each
  619. // state decides their tax rate.
  620. //
  621. // Parameters:
  622. //
  623. // head_ptr - pointer to the beginning of our linked list
  624. //
  625. // Returns: void (the state tax gets updated by reference)
  626. //
  627. //**************************************************************
  628.  
  629. void calcStateTax (EMPLOYEE * head_ptr)
  630. {
  631.  
  632. EMPLOYEE * current_ptr; // pointer to current node
  633.  
  634. // traverse through the linked list to calculate the state tax
  635. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  636. {
  637. // Make sure tax state is all uppercase
  638. if (islower(current_ptr->taxState[0]))
  639. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  640. if (islower(current_ptr->taxState[1]))
  641. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  642.  
  643. // calculate state tax based on where employee resides
  644. if (strcmp(current_ptr->taxState, "MA") == 0)
  645. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  646. MA_TAX_RATE);
  647. else if (strcmp(current_ptr->taxState, "VT") == 0)
  648. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  649. VT_TAX_RATE);
  650. else if (strcmp(current_ptr->taxState, "NH") == 0)
  651. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  652. NH_TAX_RATE);
  653. else if (strcmp(current_ptr->taxState, "CA") == 0)
  654. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  655. CA_TAX_RATE);
  656. else
  657. // any other state is the default rate
  658. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  659. DEFAULT_STATE_TAX_RATE);
  660.  
  661. } // for
  662.  
  663. } // calcStateTax
  664.  
  665. //*************************************************************
  666. // Function: calcFedTax
  667. //
  668. // Purpose: Calculates the Federal Tax owed based on the gross
  669. // pay for each employee
  670. //
  671. // Parameters:
  672. //
  673. // head_ptr - pointer to the beginning of our linked list
  674. //
  675. // Returns: void (the federal tax gets updated by reference)
  676. //
  677. //**************************************************************
  678.  
  679. void calcFedTax (EMPLOYEE * head_ptr)
  680. {
  681.  
  682. EMPLOYEE * current_ptr; // pointer to current node
  683.  
  684. // traverse through the linked list to calculate the federal tax
  685. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  686. {
  687.  
  688. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  689.  
  690. } // for
  691.  
  692. } // calcFedTax
  693.  
  694. //*************************************************************
  695. // Function: calcNetPay
  696. //
  697. // Purpose: Calculates the net pay as the gross pay minus any
  698. // state and federal taxes owed for each employee.
  699. // Essentially, their "take home" pay.
  700. //
  701. // Parameters:
  702. //
  703. // head_ptr - pointer to the beginning of our linked list
  704. //
  705. // Returns: void (the net pay gets updated by reference)
  706. //
  707. //**************************************************************
  708.  
  709. void calcNetPay (EMPLOYEE * head_ptr)
  710. {
  711.  
  712. EMPLOYEE * current_ptr; // pointer to current node
  713.  
  714. // traverse through the linked list to calculate the net pay
  715. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  716. {
  717. // calculate the net pay
  718. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  719. current_ptr->stateTax,
  720. current_ptr->fedTax);
  721. } // for
  722.  
  723. } // calcNetPay
  724.  
  725. //*************************************************************
  726. // Function: calcEmployeeTotals
  727. //
  728. // Purpose: Performs a running total (sum) of each employee
  729. // floating point member item stored in our linked list
  730. //
  731. // Parameters:
  732. //
  733. // head_ptr - pointer to the beginning of our linked list
  734. // emp_totals_ptr - pointer to a structure containing the
  735. // running totals of each floating point
  736. // member for all employees in our linked
  737. // list
  738. //
  739. // Returns:
  740. //
  741. // void (the employeeTotals structure gets updated by reference)
  742. //
  743. //**************************************************************
  744.  
  745. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  746. TOTALS * emp_totals_ptr)
  747. {
  748.  
  749. EMPLOYEE * current_ptr; // pointer to current node
  750.  
  751. // traverse through the linked list to calculate a running
  752. // sum of each employee floating point member item
  753. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  754. {
  755. // add current employee data to our running totals
  756. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  757. emp_totals_ptr->total_hours += current_ptr->hours;
  758. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  759. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  760. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  761. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  762. emp_totals_ptr->total_netPay += current_ptr->netPay;
  763.  
  764. // Note: We don't need to increment emp_totals_ptr
  765.  
  766. } // for
  767.  
  768. } // calcEmployeeTotals
  769.  
  770. //*************************************************************
  771. // Function: calcEmployeeMinMax
  772. //
  773. // Purpose: Accepts various floating point values from an
  774. // employee and adds to a running update of min
  775. // and max values
  776. //
  777. // Parameters:
  778. //
  779. // head_ptr - pointer to the beginning of our linked list
  780. // emp_minMax_ptr - pointer to the min/max structure
  781. //
  782. // Returns:
  783. //
  784. // void (employeeMinMax structure updated by reference)
  785. //
  786. //**************************************************************
  787.  
  788. void calcEmployeeMinMax (EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr)
  789. {
  790.  
  791. EMPLOYEE * current_ptr; // pointer to current node
  792.  
  793. // *************************************************
  794. // At this point, head_ptr is pointing to the first
  795. // employee .. the first node of our linked list
  796. //
  797. // As this is the first employee, set each min
  798. // min and max value using our emp_minMax_ptr
  799. // to the associated member fields below. They
  800. // will become the initial baseline that we
  801. // can check and update if needed against the
  802. // remaining employees in our linked list.
  803. // *************************************************
  804.  
  805.  
  806. // set to first employee, our initial linked list node
  807. current_ptr = head_ptr;
  808.  
  809. // set the min to the first employee members
  810. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  811. emp_minMax_ptr->min_hours = current_ptr->hours;
  812. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  813. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  814. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  815. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  816. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  817.  
  818. // set the max to the first employee members
  819. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  820. emp_minMax_ptr->max_hours = current_ptr->hours;
  821. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  822. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  823. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  824. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  825. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  826.  
  827. // ******************************************************
  828. // move to the next employee
  829. //
  830. // if this the only employee in our linked list
  831. // current_ptr will be NULL and will drop out the
  832. // the for loop below, otherwise, the second employee
  833. // and rest of the employees (if any) will be processed
  834. // ******************************************************
  835. current_ptr = current_ptr->next;
  836.  
  837. // traverse the linked list
  838. // compare the rest of the employees to each other for min and max
  839. for (; current_ptr; current_ptr = current_ptr->next)
  840. {
  841.  
  842. // check if current Wage Rate is the new min and/or max
  843. emp_minMax_ptr->min_wageRate =
  844. CALC_MIN(current_ptr->wageRate,emp_minMax_ptr->min_wageRate);
  845. emp_minMax_ptr->max_wageRate =
  846. CALC_MAX(current_ptr->wageRate,emp_minMax_ptr->max_wageRate);
  847.  
  848. // check if current Hours is the new min and/or max
  849. emp_minMax_ptr->min_hours =
  850. CALC_MIN(current_ptr->hours,emp_minMax_ptr->min_hours);
  851. emp_minMax_ptr->max_hours =
  852. CALC_MAX(current_ptr->hours,emp_minMax_ptr->max_hours);
  853.  
  854. // check if current Overtime Hours is the new min and/or max
  855. emp_minMax_ptr->min_overtimeHrs =
  856. CALC_MIN(current_ptr->overtimeHrs,emp_minMax_ptr->min_overtimeHrs);
  857. emp_minMax_ptr->max_overtimeHrs =
  858. CALC_MAX(current_ptr->overtimeHrs,emp_minMax_ptr->max_overtimeHrs);
  859.  
  860. // check if current Gross Pay is the new min and/or max
  861. emp_minMax_ptr->min_grossPay =
  862. CALC_MIN(current_ptr->grossPay,emp_minMax_ptr->min_grossPay);
  863. emp_minMax_ptr->max_grossPay =
  864. CALC_MAX(current_ptr->grossPay,emp_minMax_ptr->max_grossPay);
  865.  
  866. // check if current State Tax is the new min and/or max
  867. emp_minMax_ptr->min_stateTax =
  868. CALC_MIN(current_ptr->stateTax,emp_minMax_ptr->min_stateTax);
  869. emp_minMax_ptr->max_stateTax =
  870. CALC_MAX(current_ptr->stateTax,emp_minMax_ptr->max_stateTax);
  871.  
  872. // check if current Federal Tax is the new min and/or max
  873. emp_minMax_ptr->min_fedTax =
  874. CALC_MIN(current_ptr->fedTax,emp_minMax_ptr->min_fedTax);
  875. emp_minMax_ptr->max_fedTax =
  876. CALC_MAX(current_ptr->fedTax,emp_minMax_ptr->max_fedTax);
  877.  
  878. // check if current Net Pay is the new min and/or max
  879. emp_minMax_ptr->min_netPay =
  880. CALC_MIN(current_ptr->netPay,emp_minMax_ptr->min_netPay);
  881. emp_minMax_ptr->max_netPay =
  882. CALC_MAX(current_ptr->netPay,emp_minMax_ptr->max_netPay);
  883.  
  884. } // for
  885.  
  886. } // calcEmployeeMinMax
Success #stdin #stdout 0.01s 5280KB
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 ***