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