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

*** Pay Calculator ***

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

The total employees processed was: 5


 *** End of Program ***