fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  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 are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30. #define NUM_EMPL 5
  31. #define OVERTIME_HOURS 40
  32. #define OVERTIME_RATE 1.5
  33.  
  34. // Structure for employee information
  35. struct employee {
  36. char name[20];
  37. char state[3];
  38. int clockNum;
  39. float wage;
  40. };
  41.  
  42. // Structure for totals
  43. struct totals {
  44. float hours;
  45. float regular;
  46. float overtime;
  47. float grossPay;
  48. float stateTax;
  49. float fedTax;
  50. float netPay;
  51. };
  52.  
  53. // Structure for min and max values
  54. struct min_max {
  55. float minWage;
  56. float maxWage;
  57. float minHours;
  58. float maxHours;
  59. float minOT;
  60. float maxOT;
  61. float minGross;
  62. float maxGross;
  63. float minState;
  64. float maxState;
  65. float minFed;
  66. float maxFed;
  67. float minNet;
  68. float maxNet;
  69. };
  70.  
  71. // Function prototypes
  72. void getHours(struct employee *emp_ptr, struct totals *emp_totals_ptr);
  73. void calcPay(struct employee *emp_ptr, struct totals *emp_totals_ptr);
  74. void printPay(struct employee *emp_ptr, struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr);
  75. void calculateTotals(struct totals *emp_totals_ptr);
  76. void calculateAverages(struct totals *emp_totals_ptr);
  77. void findMinMax(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr);
  78.  
  79. int main() {
  80. // Set up an array of structures to store the employee information
  81. // Initialize the name, tax state, clock number, and wage rate
  82. struct employee employeeData[NUM_EMPL] = {
  83. { "Connie Cobol", "MA", 98401, 10.60},
  84. { "Mary Apl", "NH", 526488, 9.75 },
  85. { "Frank Fortran", "VT", 765349, 10.50 },
  86. { "Jeff Ada", "NY", 34645, 12.25 },
  87. { "Anton Pascal","CA",127615, 8.35 }
  88. };
  89.  
  90. // Pointer to the array of employee structures
  91. struct employee *emp_ptr = employeeData;
  92.  
  93. // Set up structure to store totals and initialize all to zero
  94. struct totals employeeTotals = {0,0,0,0,0,0,0};
  95.  
  96. // Pointer to the employeeTotals structure
  97. struct totals *emp_totals_ptr = &employeeTotals;
  98.  
  99. // Set up structure to store min and max values and initialize all to zero
  100. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  101.  
  102. // Pointer to the employeeMinMax structure
  103. struct min_max *emp_minMax_ptr = &employeeMinMax;
  104.  
  105. // Function calls
  106. getHours(emp_ptr, emp_totals_ptr);
  107. calcPay(emp_ptr, emp_totals_ptr);
  108. calculateTotals(emp_totals_ptr);
  109. calculateAverages(emp_totals_ptr);
  110. findMinMax(emp_totals_ptr, emp_minMax_ptr);
  111. printPay(emp_ptr, emp_totals_ptr, emp_minMax_ptr);
  112.  
  113. return 0;
  114. }
  115.  
  116. // Function to get hours worked
  117. void getHours(struct employee *emp_ptr, struct totals *emp_totals_ptr) {
  118. printf("Please enter the hours worked for each employee:\n");
  119. for (int i = 0; i < NUM_EMPL; ++i) {
  120. printf("%s %s: ", emp_ptr[i].name, emp_ptr[i].state);
  121. scanf("%f", &emp_totals_ptr[i].hours);
  122. }
  123. }
  124.  
  125. // Function to calculate pay
  126. void calcPay(struct employee *emp_ptr, struct totals *emp_totals_ptr) {
  127. for (int i = 0; i < NUM_EMPL; ++i) {
  128. if (emp_totals_ptr[i].hours > OVERTIME_HOURS) {
  129. emp_totals_ptr[i].regular = OVERTIME_HOURS * emp_ptr[i].wage;
  130. emp_totals_ptr[i].overtime = (emp_totals_ptr[i].hours - OVERTIME_HOURS) * emp_ptr[i].wage * OVERTIME_RATE;
  131. } else {
  132. emp_totals_ptr[i].regular = emp_totals_ptr[i].hours * emp_ptr[i].wage;
  133. emp_totals_ptr[i].overtime = 0;
  134. }
  135. emp_totals_ptr[i].grossPay = emp_totals_ptr[i].regular + emp_totals_ptr[i].overtime;
  136. }
  137. }
  138.  
  139. // Function to print pay
  140. void printPay(struct employee *emp_ptr, struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr) {
  141. printf("*** Pay Calculator ***\n\n");
  142. printf("---------------------------------------------------------------------------------\n");
  143. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  144. printf(" State Pay Tax Tax Pay\n");
  145. printf("---------------------------------------------------------------------------------\n");
  146.  
  147. for (int i = 0; i < NUM_EMPL; ++i) {
  148. printf("%-20s %2s %06d %5.2f %5.1f %4.1f %6.2f %6.2f %6.2f %7.2f\n",
  149. emp_ptr[i].name, emp_ptr[i].state, emp_ptr[i].clockNum, emp_ptr[i].wage,
  150. emp_totals_ptr[i].hours, emp_totals_ptr[i].overtime, emp_totals_ptr[i].grossPay,
  151. emp_totals_ptr[i].stateTax, emp_totals_ptr[i].fedTax, emp_totals_ptr[i].netPay);
  152. }
  153.  
  154. printf("---------------------------------------------------------------------------------\n");
  155. printf("Totals: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  156. emp_totals_ptr->hours, emp_totals_ptr->overtime, emp_totals_ptr->grossPay,
  157. emp_totals_ptr->stateTax, emp_totals_ptr->fedTax, emp_totals_ptr->netPay);
  158. printf("Averages: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  159. emp_totals_ptr->hours / NUM_EMPL, emp_totals_ptr->overtime / NUM_EMPL,
  160. emp_totals_ptr->grossPay / NUM_EMPL, emp_totals_ptr->stateTax / NUM_EMPL,
  161. emp_totals_ptr->fedTax / NUM_EMPL, emp_totals_ptr->netPay / NUM_EMPL);
  162. printf("Minimum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  163. emp_minMax_ptr->minWage, emp_minMax_ptr->minHours, emp_minMax_ptr->minOT,
  164. emp_minMax_ptr->minGross, emp_minMax_ptr->minState, emp_minMax_ptr->minFed,
  165. emp_minMax_ptr->minNet);
  166. printf("Maximum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  167. emp_minMax_ptr->maxWage, emp_minMax_ptr->maxHours, emp_minMax_ptr->maxOT,
  168. emp_minMax_ptr->maxGross, emp_minMax_ptr->maxState, emp_minMax_ptr->maxFed,
  169. emp_minMax_ptr->maxNet);
  170. }
  171.  
  172.  
  173. // Function to calculate totals
  174. void calculateTotals(struct totals *emp_totals_ptr) {
  175. for (int i = 0; i < NUM_EMPL; ++i) {
  176. emp_totals_ptr->hours += emp_totals_ptr[i].hours;
  177. emp_totals_ptr->regular += emp_totals_ptr[i].regular;
  178. emp_totals_ptr->overtime += emp_totals_ptr[i].overtime;
  179. emp_totals_ptr->grossPay += emp_totals_ptr[i].grossPay;
  180. }
  181. }
  182.  
  183. // Function to calculate averages
  184. void calculateAverages(struct totals *emp_totals_ptr) {
  185. emp_totals_ptr->hours /= NUM_EMPL;
  186. emp_totals_ptr->regular /= NUM_EMPL;
  187. emp_totals_ptr->overtime /= NUM_EMPL;
  188. emp_totals_ptr->grossPay /= NUM_EMPL;
  189. }
  190.  
  191. // Function to find min and max values
  192. void findMinMax(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr) {
  193. emp_minMax_ptr->minWage = emp_minMax_ptr->maxWage = emp_totals_ptr[0].grossPay;
  194. emp_minMax_ptr->minHours = emp_minMax_ptr->maxHours = emp_totals_ptr[0].hours;
  195. emp_minMax_ptr->minOT = emp_minMax_ptr->maxOT = emp_totals_ptr[0].overtime;
  196. emp_minMax_ptr->minGross = emp_minMax_ptr->maxGross = emp_totals_ptr[0].grossPay;
  197. emp_minMax_ptr->minState = emp_minMax_ptr->maxState = emp_totals_ptr[0].stateTax;
  198. emp_minMax_ptr->minFed = emp_minMax_ptr->maxFed = emp_totals_ptr[0].fedTax;
  199. emp_minMax_ptr->minNet = emp_minMax_ptr->maxNet = emp_totals_ptr[0].netPay;
  200.  
  201. for (int i = 1; i < NUM_EMPL; ++i) {
  202. if (emp_totals_ptr[i].grossPay < emp_minMax_ptr->minGross)
  203. emp_minMax_ptr->minGross = emp_totals_ptr[i].grossPay;
  204. if (emp_totals_ptr[i].grossPay > emp_minMax_ptr->maxGross)
  205. emp_minMax_ptr->maxGross = emp_totals_ptr[i].grossPay;
  206.  
  207. if (emp_totals_ptr[i].hours < emp_minMax_ptr->minHours)
  208. emp_minMax_ptr->minHours = emp_totals_ptr[i].hours;
  209. if (emp_totals_ptr[i].hours > emp_minMax_ptr->maxHours)
  210. emp_minMax_ptr->maxHours = emp_totals_ptr[i].hours;
  211.  
  212. if (emp_totals_ptr[i].overtime < emp_minMax_ptr->minOT)
  213. emp_minMax_ptr->minOT = emp_totals_ptr[i].overtime;
  214. if (emp_totals_ptr[i].overtime > emp_minMax_ptr->maxOT)
  215. emp_minMax_ptr->maxOT = emp_totals_ptr[i].overtime;
  216.  
  217. if (emp_totals_ptr[i].stateTax < emp_minMax_ptr->minState)
  218. emp_minMax_ptr->minState = emp_totals_ptr[i].stateTax;
  219. if (emp_totals_ptr[i].stateTax > emp_minMax_ptr->maxState)
  220. emp_minMax_ptr->maxState = emp_totals_ptr[i].stateTax;
  221.  
  222. if (emp_totals_ptr[i].fedTax < emp_minMax_ptr->minFed)
  223. emp_minMax_ptr->minFed = emp_totals_ptr[i].fedTax;
  224. if (emp_totals_ptr[i].fedTax > emp_minMax_ptr->maxFed)
  225. emp_minMax_ptr->maxFed = emp_totals_ptr[i].fedTax;
  226.  
  227. if (emp_totals_ptr[i].netPay < emp_minMax_ptr->minNet)
  228. emp_minMax_ptr->minNet = emp_totals_ptr[i].netPay;
  229. if (emp_totals_ptr[i].netPay > emp_minMax_ptr->maxNet)
  230. emp_minMax_ptr->maxNet = emp_totals_ptr[i].netPay;
  231. }
  232. }
  233.  
Success #stdin #stdout 0.01s 5284KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
Please enter the hours worked for each employee:
Connie Cobol MA: Mary Apl NH: Frank Fortran VT: Jeff Ada NY: Anton Pascal CA: *** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock#  Wage   Hours  OT   Gross   State  Fed      Net
                   State                            Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
                        000000  334.00   53.3  95.6  585.75    0.00    0.00     0.00
Mary Apl             NH 526488   9.75   42.5  585.7   40.00  585.75    0.00   585.75
Frank Fortran        VT 765349  10.50   53.3   0.0  78656833379093506030593606120636416.00    0.00  1161798963298831550296096768.00     0.00
Jeff Ada             NY 034645  12.25  585.7  91.9  581.88  160.40  1157592441567239118983790592.00     0.00
Anton Pascal         CA 127615   8.35   40.0   0.0  334.00  78656833379093506030593606120636416.00  1161798963298831550296096768.00     0.00
---------------------------------------------------------------------------------
Totals:                          53.30 95.6 585.7 0.00 0.00 0.00 0.00
Averages:                        10.66 19.1 117.1 0.00 0.00 0.00 0.00
Minimum:                          585.75 40.0 0.0 53.30 0.00 0.00 0.00
Maximum:                         585.75 585.7 585.7 585.75 78656833379093506030593606120636416.00 1161798963298831550296096768.00 585.75