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