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