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