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