fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Matt Smith
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 4/18/25
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references have all been replaced with
  20. // pointer references to speed up the processing of this code.
  21. // A linked list has been created and deployed to dynamically
  22. // allocate and process employees as needed.
  23. //
  24. // It will also take advantage of the C Preprocessor features,
  25. // in particular with using macros, and will replace all
  26. // struct type references in the code with a typedef alias
  27. // reference.
  28. //
  29. // Call by Reference design (using pointers)
  30. //
  31. //********************************************************
  32.  
  33. // necessary header files
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h> // for char functions
  37. #include <stdlib.h> // for malloc
  38.  
  39. // define constants
  40. #define STD_HOURS 40.0
  41. #define OT_RATE 1.5
  42. #define MA_TAX_RATE 0.05
  43. #define NH_TAX_RATE 0.0
  44. #define VT_TAX_RATE 0.06
  45. #define CA_TAX_RATE 0.07
  46. #define DEFAULT_STATE_TAX_RATE 0.08
  47. #define NAME_SIZE 20
  48. #define TAX_STATE_SIZE 3
  49. #define FED_TAX_RATE 0.25
  50. #define FIRST_NAME_SIZE 10
  51. #define LAST_NAME_SIZE 10
  52.  
  53. // define macros
  54. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  55. #define CALC_STATE_TAX(thePay,theStateTaxRate) (thePay * theStateTaxRate)
  56. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  57.  
  58. // 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. } MIN_MAX;
  156.  
  157. // Define prototypes here for each function except main
  158. //
  159. // Note the use of the typedef alias values throughout
  160. // the rest of this program, starting with the fucntions
  161. // prototypes
  162. //
  163. // EMPLOYEE instead of struct employee
  164. // TOTALS instead of struct totals
  165. // MIN_MAX instead of struct min_max
  166.  
  167. EMPLOYEE * getEmpData (void);
  168. int isEmployeeSize (EMPLOYEE * head_ptr);
  169. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  170. void calcGrossPay (EMPLOYEE * head_ptr);
  171. void printHeader (void);
  172. void printEmp (EMPLOYEE * head_ptr);
  173. void calcStateTax (EMPLOYEE * head_ptr);
  174. void calcFedTax (EMPLOYEE * head_ptr);
  175. void calcNetPay (EMPLOYEE * head_ptr);
  176. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  177. TOTALS * emp_totals_ptr);
  178.  
  179. // TODO - Update these two prototypes with the MIN_MAX typedef alias
  180. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  181. MIN_MAX * emp_minMax_ptr);
  182.  
  183. void printEmpStatistics (TOTALS * emp_totals_ptr,
  184. MIN_MAX * emp_minMax_ptr,
  185. int size);
  186.  
  187. int main ()
  188. {
  189.  
  190. // ******************************************************************
  191. // Set up head pointer in the main function to point to the
  192. // start of the dynamically allocated linked list nodes that will be
  193. // created and stored in the Heap area.
  194. // ******************************************************************
  195. EMPLOYEE * head_ptr; // always points to first linked list node
  196.  
  197. int theSize; // number of employees processed
  198.  
  199. // set up structure to store totals and initialize all to zero
  200. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  201.  
  202. // pointer to the employeeTotals structure
  203. TOTALS * emp_totals_ptr = &employeeTotals;
  204.  
  205. // Updated to use MIN_MAX typedef alias
  206. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  207. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  208.  
  209. // ********************************************************************
  210. // Read the employee input and dynamically allocate and set up our
  211. // linked list in the Heap area. The address of the first linked
  212. // list item representing our first employee will be returned and
  213. // its value is set in our head_ptr. We can then use the head_ptr
  214. // throughout the rest of this program anytime we want to get to get
  215. // to the beginning of our linked list.
  216. // ********************************************************************
  217.  
  218. head_ptr = getEmpData ();
  219.  
  220. // ********************************************************************
  221. // With the head_ptr now pointing to the first linked list node, we
  222. // can pass it to any function who needs to get to the starting point
  223. // of the linked list in the Heap. From there, functions can traverse
  224. // through the linked list to access and/or update each employee.
  225. //
  226. // Important: Don't update the head_ptr ... otherwise, you could lose
  227. // the address in the heap of the first linked list node.
  228. //
  229. // ********************************************************************
  230.  
  231. // determine how many employees are in our linked list
  232.  
  233. theSize = isEmployeeSize (head_ptr);
  234.  
  235. // Skip all the function calls to process the data if there
  236. // was no employee information to read in the input
  237. if (theSize <= 0)
  238. {
  239. // print a user friendly message and skip the rest of the processing
  240. printf("\n\n**** There was no employee input to process ***\n");
  241. }
  242.  
  243. else // there are employees to be processed
  244. {
  245.  
  246. // *********************************************************
  247. // Perform calculations and print out information as needed
  248. // *********************************************************
  249.  
  250. // Calculate the overtime hours
  251. calcOvertimeHrs (head_ptr);
  252.  
  253. // Calculate the weekly gross pay
  254. calcGrossPay (head_ptr);
  255.  
  256. // Calculate the state tax
  257. calcStateTax (head_ptr);
  258.  
  259. // Calculate the federal tax
  260. calcFedTax (head_ptr);
  261.  
  262. // Calculate the net pay after taxes
  263. calcNetPay (head_ptr);
  264.  
  265. // *********************************************************
  266. // Keep a running sum of the employee totals
  267. //
  268. // Note the & to specify the address of the employeeTotals
  269. // structure. Needed since pointers work with addresses.
  270. // Unlike array names, C does not see structure names
  271. // as address, hence the need for using the &employeeTotals
  272. // which the complier sees as "address of" employeeTotals
  273. // *********************************************************
  274. calcEmployeeTotals (head_ptr,
  275. &employeeTotals);
  276.  
  277. // *****************************************************************
  278. // Keep a running update of the employee minimum and maximum values
  279. //
  280. // Note we are passing the address of the MinMax structure
  281. // *****************************************************************
  282. calcEmployeeMinMax (head_ptr,
  283. &employeeMinMax);
  284.  
  285. // Print the column headers
  286. printHeader();
  287.  
  288. // print out final information on each employee
  289. printEmp (head_ptr);
  290.  
  291. // **************************************************
  292. // print the totals and averages for all float items
  293. //
  294. // Note that we are passing the addresses of the
  295. // the two structures
  296. // **************************************************
  297. printEmpStatistics (&employeeTotals,
  298. &employeeMinMax,
  299. theSize);
  300. }
  301.  
  302. // indicate that the program completed all processing
  303. printf ("\n\n *** End of Program *** \n");
  304.  
  305. return (0); // success
  306.  
  307. } // main
  308.  
  309. //**************************************************************
  310. // Function: getEmpData
  311. //
  312. // Purpose: Obtains input from user: employee name (first an last),
  313. // tax state, clock number, hourly wage, and hours worked
  314. // in a given week.
  315. //
  316. // Information in stored in a dynamically created linked
  317. // list for all employees.
  318. //
  319. // Parameters: void
  320. //
  321. // Returns:
  322. //
  323. // head_ptr - a pointer to the beginning of the dynamically
  324. // created linked list that contains the initial
  325. // input for each employee.
  326. //
  327. //**************************************************************
  328.  
  329. EMPLOYEE * getEmpData (void)
  330. {
  331.  
  332. char answer[80]; // user prompt response
  333. int more_data = 1; // a flag to indicate if another employee
  334. // needs to be processed
  335. char value; // the first char of the user prompt response
  336.  
  337. EMPLOYEE *current_ptr, // pointer to current node
  338. *head_ptr; // always points to first node
  339.  
  340. // Set up storage for first node
  341. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  342. current_ptr = head_ptr;
  343.  
  344. // process while there is still input
  345. while (more_data)
  346. {
  347.  
  348. // read in employee first and last name
  349. printf ("\nEnter employee first name: ");
  350. scanf ("%s", current_ptr->empName.firstName);
  351. printf ("\nEnter employee last name: ");
  352. scanf ("%s", current_ptr->empName.lastName);
  353.  
  354. // read in employee tax state
  355. printf ("\nEnter employee two character tax state: ");
  356. scanf ("%s", current_ptr->taxState);
  357.  
  358. // read in employee clock number
  359. printf("\nEnter employee clock number: ");
  360. scanf("%li", & current_ptr -> clockNumber);
  361.  
  362. // read in employee wage rate
  363. printf("\nEnter employee hourly wage: ");
  364. scanf("%f", & current_ptr -> wageRate);
  365.  
  366. // read in employee hours worked
  367. printf("\nEnter hours worked this week: ");
  368. scanf("%f", & current_ptr -> hours);
  369.  
  370. // ask user if they would like to add another employee
  371. printf("\nWould you like to add another employee? (y/n): ");
  372. scanf("%s", answer);
  373.  
  374. // check first character for a 'Y' for yes
  375. // Ask user if they want to add another employee
  376. if ((value = toupper(answer[0])) != 'Y')
  377. {
  378. // no more employees to process
  379. current_ptr->next = (EMPLOYEE *) NULL;
  380. more_data = 0;
  381. }
  382. else // Yes, another employee
  383. {
  384. // set the next pointer of the current node to point to the new node
  385. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  386. // move the current node pointer to the new node
  387. current_ptr = current_ptr->next;
  388. }
  389.  
  390. } // while
  391.  
  392. return(head_ptr);
  393.  
  394. } // getEmpData
  395.  
  396. //*************************************************************
  397. // Function: isEmployeeSize
  398. //
  399. // Purpose: Traverses the linked list and keeps a running count
  400. // on how many employees are currently in our list.
  401. //
  402. // Parameters:
  403. //
  404. // head_ptr - pointer to the initial node in our linked list
  405. //
  406. // Returns:
  407. //
  408. // theSize - the number of employees in our linked list
  409. //
  410. //**************************************************************
  411.  
  412. int isEmployeeSize (EMPLOYEE * head_ptr)
  413. {
  414.  
  415. EMPLOYEE * current_ptr; // pointer to current node
  416. int theSize; // number of link list nodes
  417. // (i.e., employees)
  418.  
  419. theSize = 0; // initialize
  420.  
  421. // assume there is no data if the first node does
  422. // not have an employee name
  423. if (head_ptr->empName.firstName[0] != '\0')
  424. {
  425.  
  426. // traverse through the linked list, keep a running count of nodes
  427. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  428. {
  429.  
  430. ++theSize; // employee node found, increment
  431.  
  432. } // for
  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. } // for
  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. struct 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. {
  567. // print the averages for all the floating point items
  568. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  569. emp_totals_ptr->total_wageRate/theSize,
  570. emp_totals_ptr->total_hours/theSize,
  571. emp_totals_ptr->total_overtimeHrs/theSize,
  572. emp_totals_ptr->total_grossPay/theSize,
  573. emp_totals_ptr->total_stateTax/theSize,
  574. emp_totals_ptr->total_fedTax/theSize,
  575. emp_totals_ptr->total_netPay/theSize);
  576.  
  577. } // if
  578.  
  579. // print the min and max values for each item
  580.  
  581. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  582. emp_minMax_ptr->min_wageRate,
  583. emp_minMax_ptr->min_hours,
  584. emp_minMax_ptr->min_overtimeHrs,
  585. emp_minMax_ptr->min_grossPay,
  586. emp_minMax_ptr->min_stateTax,
  587. emp_minMax_ptr->min_fedTax,
  588. emp_minMax_ptr->min_netPay);
  589.  
  590. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  591. emp_minMax_ptr->max_wageRate,
  592. emp_minMax_ptr->max_hours,
  593. emp_minMax_ptr->max_overtimeHrs,
  594. emp_minMax_ptr->max_grossPay,
  595. emp_minMax_ptr->max_stateTax,
  596. emp_minMax_ptr->max_fedTax,
  597. emp_minMax_ptr->max_netPay);
  598.  
  599. // print out the total employees process
  600. printf ("\n\nThe total employees processed was: %i\n", theSize);
  601.  
  602. } // printEmpStatistics
  603.  
  604. //*************************************************************
  605. // Function: calcOvertimeHrs
  606. //
  607. // Purpose: Calculates the overtime hours worked by an employee
  608. // in a given week for each employee.
  609. //
  610. // Parameters:
  611. //
  612. // head_ptr - pointer to the beginning of our linked list
  613. //
  614. // Returns: void (the overtime hours gets updated by reference)
  615. //
  616. //**************************************************************
  617.  
  618. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  619. {
  620.  
  621. EMPLOYEE * current_ptr; // pointer to current node
  622.  
  623. // traverse through the linked list to calculate overtime hours
  624. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  625. {
  626. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  627.  
  628. } // for
  629.  
  630.  
  631. } // calcOvertimeHrs
  632.  
  633. //*************************************************************
  634. // Function: calcGrossPay
  635. //
  636. // Purpose: Calculates the gross pay based on the the normal pay
  637. // and any overtime pay for a given week for each
  638. // employee.
  639. //
  640. // Parameters:
  641. //
  642. // head_ptr - pointer to the beginning of our linked list
  643. //
  644. // Returns: void (the gross pay gets updated by reference)
  645. //
  646. //**************************************************************
  647.  
  648. void calcGrossPay (EMPLOYEE * head_ptr)
  649. {
  650.  
  651. float theNormalPay; // normal pay without any overtime hours
  652. float theOvertimePay; // overtime pay
  653.  
  654. EMPLOYEE * current_ptr; // pointer to current node
  655.  
  656. // traverse through the linked list to calculate gross pay
  657. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  658. {
  659. // calculate normal pay and any overtime pay
  660. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  661. current_ptr->hours,
  662. current_ptr->overtimeHrs);
  663. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  664. current_ptr->overtimeHrs);
  665.  
  666. // calculate gross pay for employee as normalPay + any overtime pay
  667. current_ptr->grossPay = theNormalPay + theOvertimePay;
  668.  
  669. }
  670.  
  671. } // calcGrossPay
  672.  
  673. //*************************************************************
  674. // Function: calcStateTax
  675. //
  676. // Purpose: Calculates the State Tax owed based on gross pay
  677. // for each employee. State tax rate is based on the
  678. // the designated tax state based on where the
  679. // employee is actually performing the work. Each
  680. // state decides their tax rate.
  681. //
  682. // Parameters:
  683. //
  684. // head_ptr - pointer to the beginning of our linked list
  685. //
  686. // Returns: void (the state tax gets updated by reference)
  687. //
  688. //**************************************************************
  689.  
  690. void calcStateTax (EMPLOYEE * head_ptr)
  691. {
  692.  
  693. EMPLOYEE * current_ptr; // pointer to current node
  694.  
  695. // traverse through the linked list to calculate the state tax
  696. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  697. {
  698. // Make sure tax state is all uppercase
  699. if (islower(current_ptr->taxState[0]))
  700. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  701. if (islower(current_ptr->taxState[1]))
  702. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  703.  
  704. // calculate state tax based on where employee resides
  705. if (strcmp(current_ptr->taxState, "MA") == 0)
  706. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  707. MA_TAX_RATE);
  708. else if (strcmp(current_ptr->taxState, "VT") == 0)
  709. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  710. VT_TAX_RATE);
  711. else if (strcmp(current_ptr->taxState, "NH") == 0)
  712. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  713. NH_TAX_RATE);
  714. else if (strcmp(current_ptr->taxState, "CA") == 0)
  715. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  716. CA_TAX_RATE);
  717. else
  718. // any other state is the default rate
  719. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  720. DEFAULT_STATE_TAX_RATE);
  721.  
  722. } // for
  723.  
  724. } // calcStateTax
  725.  
  726. //*************************************************************
  727. // Function: calcFedTax
  728. //
  729. // Purpose: Calculates the Federal Tax owed based on the gross
  730. // pay for each employee
  731. //
  732. // Parameters:
  733. //
  734. // head_ptr - pointer to the beginning of our linked list
  735. //
  736. // Returns: void (the federal tax gets updated by reference)
  737. //
  738. //**************************************************************
  739.  
  740. void calcFedTax (EMPLOYEE * head_ptr)
  741. {
  742.  
  743. EMPLOYEE * current_ptr; // pointer to current node
  744.  
  745. // traverse through the linked list to calculate the federal tax
  746. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  747. {
  748.  
  749. // TODO - Replace the below statement after the "=" with
  750. // a call to the CALC_FED_TAX macro you created
  751.  
  752. // Fed Tax is the same for all regardless of state
  753. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  754.  
  755. } // for
  756.  
  757. } // calcFedTax
  758.  
  759. //*************************************************************
  760. // Function: calcNetPay
  761. //
  762. // Purpose: Calculates the net pay as the gross pay minus any
  763. // state and federal taxes owed for each employee.
  764. // Essentially, their "take home" pay.
  765. //
  766. // Parameters:
  767. //
  768. // head_ptr - pointer to the beginning of our linked list
  769. //
  770. // Returns: void (the net pay gets updated by reference)
  771. //
  772. //**************************************************************
  773.  
  774. void calcNetPay (EMPLOYEE * head_ptr)
  775. {
  776.  
  777. EMPLOYEE * current_ptr; // pointer to current node
  778.  
  779. // traverse through the linked list to calculate the net pay
  780. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  781. {
  782. // calculate the net pay
  783. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  784. current_ptr->stateTax,
  785. current_ptr->fedTax);
  786. } // for
  787.  
  788. } // calcNetPay
  789.  
  790. //*************************************************************
  791. // Function: calcEmployeeTotals
  792. //
  793. // Purpose: Performs a running total (sum) of each employee
  794. // floating point member item stored in our linked list
  795. //
  796. // Parameters:
  797. //
  798. // head_ptr - pointer to the beginning of our linked list
  799. // emp_totals_ptr - pointer to a structure containing the
  800. // running totals of each floating point
  801. // member for all employees in our linked
  802. // list
  803. //
  804. // Returns:
  805. //
  806. // void (the employeeTotals structure gets updated by reference)
  807. //
  808. //**************************************************************
  809.  
  810. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  811. TOTALS * emp_totals_ptr)
  812. {
  813.  
  814. EMPLOYEE * current_ptr; // pointer to current node
  815.  
  816. // traverse through the linked list to calculate a running
  817. // sum of each employee floating point member item
  818. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  819. {
  820. // add current employee data to our running totals
  821. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  822. emp_totals_ptr->total_hours += current_ptr->hours;
  823. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  824. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  825. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  826. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  827. emp_totals_ptr->total_netPay += current_ptr->netPay;
  828.  
  829. // Note: We don't need to increment emp_totals_ptr
  830.  
  831. } // for
  832.  
  833. // no need to return anything since we used pointers and have
  834. // been referencing the linked list stored in the Heap area.
  835. // Since we used a pointer as well to the totals structure,
  836. // all values in it have been updated.
  837.  
  838. } // calcEmployeeTotals
  839.  
  840. //*************************************************************
  841. // Function: calcEmployeeMinMax
  842. //
  843. // Purpose: Accepts various floating point values from an
  844. // employee and adds to a running update of min
  845. // and max values
  846. //
  847. // Parameters:
  848. //
  849. // head_ptr - pointer to the beginning of our linked list
  850. // emp_minMax_ptr - pointer to the min/max structure
  851. //
  852. // Returns:
  853. //
  854. // void (employeeMinMax structure updated by reference)
  855. //
  856. //**************************************************************
  857.  
  858. // TODO - Update the emp_minMax_ptr parameter below to use the
  859. // the MIN_MAX typedef alias
  860.  
  861. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  862. struct min_max * emp_minMax_ptr)
  863. {
  864.  
  865. EMPLOYEE * current_ptr; // pointer to current node
  866.  
  867. // *************************************************
  868. // At this point, head_ptr is pointing to the first
  869. // employee .. the first node of our linked list
  870. //
  871. // As this is the first employee, set each min
  872. // min and max value using our emp_minMax_ptr
  873. // to the associated member fields below. They
  874. // will become the initial baseline that we
  875. // can check and update if needed against the
  876. // remaining employees in our linked list.
  877. // *************************************************
  878.  
  879.  
  880. // set to first employee, our initial linked list node
  881. current_ptr = head_ptr;
  882.  
  883. // set the min to the first employee members
  884. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  885. emp_minMax_ptr->min_hours = current_ptr->hours;
  886. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  887. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  888. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  889. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  890. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  891.  
  892. // set the max to the first employee members
  893. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  894. emp_minMax_ptr->max_hours = current_ptr->hours;
  895. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  896. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  897. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  898. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  899. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  900.  
  901. // ******************************************************
  902. // move to the next employee
  903. //
  904. // if this the only employee in our linked list
  905. // current_ptr will be NULL and will drop out the
  906. // the for loop below, otherwise, the second employee
  907. // and rest of the employees (if any) will be processed
  908. // ******************************************************
  909. current_ptr = current_ptr->next;
  910.  
  911. // traverse the linked list
  912. // compare the rest of the employees to each other for min and max
  913. for (; current_ptr; current_ptr = current_ptr->next)
  914. {
  915.  
  916. // check if current Wage Rate is the new min and/or max
  917. emp_minMax_ptr->min_wageRate =
  918. CALC_MIN(current_ptr->wageRate,emp_minMax_ptr->min_wageRate);
  919. emp_minMax_ptr->max_wageRate =
  920. CALC_MAX(current_ptr->wageRate,emp_minMax_ptr->max_wageRate);
  921.  
  922. // check if current Hours is the new min and/or max
  923. emp_minMax_ptr->min_hours =
  924. CALC_MIN(current_ptr->hours,emp_minMax_ptr->min_hours);
  925. emp_minMax_ptr->max_hours =
  926. CALC_MAX(current_ptr->hours,emp_minMax_ptr->max_hours);
  927.  
  928. // check if current Overtime Hours is the new min and/or max
  929. emp_minMax_ptr->min_overtimeHrs =
  930. CALC_MIN(current_ptr->overtimeHrs,emp_minMax_ptr->min_overtimeHrs);
  931. emp_minMax_ptr->max_overtimeHrs =
  932. CALC_MAX(current_ptr->overtimeHrs,emp_minMax_ptr->max_overtimeHrs);
  933.  
  934. // check if current Gross Pay is the new min and/or max
  935. emp_minMax_ptr->min_grossPay =
  936. CALC_MIN(current_ptr->grossPay,emp_minMax_ptr->min_grossPay);
  937. emp_minMax_ptr->max_grossPay =
  938. CALC_MAX(current_ptr->grossPay,emp_minMax_ptr->max_grossPay);
  939.  
  940. // check if current State Tax is the new min and/or max
  941. emp_minMax_ptr->min_stateTax =
  942. CALC_MIN(current_ptr->stateTax,emp_minMax_ptr->min_stateTax);
  943. emp_minMax_ptr->max_stateTax =
  944. CALC_MAX(current_ptr->stateTax,emp_minMax_ptr->max_stateTax);
  945.  
  946. // check if current Federal Tax is the new min and/or max
  947. emp_minMax_ptr->min_fedTax =
  948. CALC_MIN(current_ptr->fedTax,emp_minMax_ptr->min_fedTax);
  949. emp_minMax_ptr->max_fedTax =
  950. CALC_MAX(current_ptr->fedTax,emp_minMax_ptr->max_fedTax);
  951.  
  952. // check if current Net Pay is the new min and/or max
  953. emp_minMax_ptr->min_netPay =
  954. CALC_MIN(current_ptr->netPay,emp_minMax_ptr->min_netPay);
  955. emp_minMax_ptr->max_netPay =
  956. CALC_MAX(current_ptr->netPay,emp_minMax_ptr->max_netPay);
  957.  
  958. } // for
  959.  
  960. // no need to return anything since we used pointers and have
  961. // been referencing all the nodes in our linked list where
  962. // they reside in memory (the Heap area)
  963.  
  964. } // calcEmployeeMinMax
Success #stdin #stdout 0.01s 5260KB
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
NConnie
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
NConnie
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
NConnie
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
NConnie
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 ***