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