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