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

**** There was no employee input to process ***


 *** End of Program ***