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

The total employees processed was: 5


 *** End of Program ***