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

The total employees processed was: 5


 *** End of Program ***