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