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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***