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