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

The total employees processed was: 5


 *** End of Program ***