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