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