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