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

*** Pay Calculator ***

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

The total employees processed was: 5


 *** End of Program ***