fork 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. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <float.h> // For FLT_MAX
  29.  
  30. // Define constants
  31. #define SIZE 5
  32. #define STD_HOURS 40.0
  33. #define OT_RATE 1.5
  34. #define MA_TAX_RATE 0.05
  35. #define NH_TAX_RATE 0.0
  36. #define VT_TAX_RATE 0.06
  37. #define CA_TAX_RATE 0.07
  38. #define DEFAULT_TAX_RATE 0.08
  39. #define FIRST_NAME_SIZE 10
  40. #define LAST_NAME_SIZE 10
  41. #define TAX_STATE_SIZE 3
  42. #define FED_TAX_RATE 0.25
  43.  
  44. // Define a structure type to store an employee name
  45. struct name {
  46. char firstName[FIRST_NAME_SIZE];
  47. char lastName [LAST_NAME_SIZE];
  48. };
  49.  
  50. // Define a structure type to store employee data
  51. struct employee {
  52. struct name empName;
  53. char taxState [TAX_STATE_SIZE];
  54. long int clockNumber;
  55. float wageRate;
  56. float hours;
  57. float overtimeHrs;
  58. float grossPay;
  59. float stateTax;
  60. float fedTax;
  61. float netPay;
  62. };
  63.  
  64. // This structure type defines the totals of all floating point items
  65. struct totals {
  66. float total_wageRate;
  67. float total_hours;
  68. float total_overtimeHrs;
  69. float total_grossPay;
  70. float total_stateTax;
  71. float total_fedTax;
  72. float total_netPay;
  73. };
  74.  
  75. // This structure type defines the min and max values for employee data
  76. struct min_max {
  77. float min_wageRate;
  78. float min_hours;
  79. float min_overtimeHrs;
  80. float min_grossPay;
  81. float min_stateTax;
  82. float min_fedTax;
  83. float min_netPay;
  84. float max_wageRate;
  85. float max_hours;
  86. float max_overtimeHrs;
  87. float max_grossPay;
  88. float max_stateTax;
  89. float max_fedTax;
  90. float max_netPay;
  91. };
  92.  
  93. // Function prototypes
  94. void getHours(struct employee *emp_ptr, int theSize);
  95. void printEmp(struct employee *emp_ptr, int theSize);
  96. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  97. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize);
  98. void printHeader(void);
  99. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  100. void calcGrossPay(struct employee *emp_ptr, int theSize);
  101. void calcStateTax(struct employee *emp_ptr, int theSize);
  102. void calcFedTax(struct employee *emp_ptr, int theSize);
  103. void calcNetPay(struct employee *emp_ptr, int theSize);
  104. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_MinMax_ptr, int theSize);
  105.  
  106. int main() {
  107. // Set up a local variable to store the employee information
  108. struct employee employeeData[SIZE] = {
  109. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  110. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  111. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  112. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  113. { {"Anton", "Pascal"},"CA",127615, 8.35 }
  114. };
  115.  
  116. // Declare a pointer to the array of employee structures
  117. struct employee *emp_ptr = employeeData;
  118.  
  119. // Set up structure to store totals and initialize all to zero
  120. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  121. struct totals *emp_totals_ptr = &employeeTotals;
  122.  
  123. // Set up structure to store min and max values and initialize all to zero or appropriate extreme values
  124. struct min_max employeeMinMax = {
  125. FLT_MAX, // min_wageRate
  126. FLT_MAX, // min_hours
  127. FLT_MAX, // min_overtimeHrs
  128. FLT_MAX, // min_grossPay
  129. FLT_MAX, // min_stateTax
  130. FLT_MAX, // min_fedTax
  131. FLT_MAX, // min_netPay
  132. 0.0, // max_wageRate
  133. 0.0, // max_hours
  134. 0.0, // max_overtimeHrs
  135. 0.0, // max_grossPay
  136. 0.0, // max_stateTax
  137. 0.0, // max_fedTax
  138. 0.0 // max_netPay
  139. };
  140. struct min_max *emp_minMax_ptr = &employeeMinMax;
  141.  
  142. // Call functions as needed to read and calculate information
  143. getHours(emp_ptr, SIZE);
  144. calcOvertimeHrs(emp_ptr, SIZE);
  145. calcGrossPay(emp_ptr, SIZE);
  146. calcStateTax(emp_ptr, SIZE);
  147. calcFedTax(emp_ptr, SIZE);
  148. calcNetPay(emp_ptr, SIZE);
  149.  
  150. calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
  151. calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
  152.  
  153. // Print the column headers
  154. printHeader();
  155.  
  156. // Print out final information on each employee
  157. printEmp(emp_ptr, SIZE);
  158.  
  159. // Print the totals and averages for all float items
  160. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
  161.  
  162. return 0;
  163. }
  164.  
  165. // Function Definitions
  166.  
  167. // Function to get hours worked from user input
  168. void getHours(struct employee *emp_ptr, int theSize) {
  169. for (int i = 0; i < theSize; i++) {
  170. printf("Enter hours worked for %s %s (Employee #%ld): ", emp_ptr[i].empName.firstName, emp_ptr[i].empName.lastName, emp_ptr[i].clockNumber);
  171. if (scanf("%f", &emp_ptr[i].hours) != 1) {
  172. printf("Invalid input. Please enter a valid number for hours.\n");
  173. i--; // Decrement the counter to retry the input for this employee
  174. }
  175. }
  176. }
  177.  
  178. // Function to calculate overtime hours
  179. void calcOvertimeHrs(struct employee *emp_ptr, int theSize) {
  180. for (int i = 0; i < theSize; i++) {
  181. if (emp_ptr[i].hours > STD_HOURS) {
  182. emp_ptr[i].overtimeHrs = emp_ptr[i].hours - STD_HOURS;
  183. } else {
  184. emp_ptr[i].overtimeHrs = 0.0;
  185. }
  186. }
  187. }
  188.  
  189. // Function to calculate gross pay
  190. void calcGrossPay(struct employee *emp_ptr, int theSize) {
  191. for (int i = 0; i < theSize; i++) {
  192. if (emp_ptr[i].hours <= STD_HOURS) {
  193. emp_ptr[i].grossPay = emp_ptr[i].wageRate * emp_ptr[i].hours;
  194. } else {
  195. emp_ptr[i].grossPay = (STD_HOURS * emp_ptr[i].wageRate) + (emp_ptr[i].overtimeHrs * emp_ptr[i].wageRate * OT_RATE);
  196. }
  197. }
  198. }
  199.  
  200. // Function to calculate state tax
  201. void calcStateTax(struct employee *emp_ptr, int theSize) {
  202. for (int i = 0; i < theSize; i++) {
  203. if (strcmp(emp_ptr[i].taxState, "MA") == 0) {
  204. emp_ptr[i].stateTax = emp_ptr[i].grossPay * MA_TAX_RATE;
  205. } else if (strcmp(emp_ptr[i].taxState, "NH") == 0) {
  206. emp_ptr[i].stateTax = emp_ptr[i].grossPay * NH_TAX_RATE;
  207. } else if (strcmp(emp_ptr[i].taxState, "VT") == 0) {
  208. emp_ptr[i].stateTax = emp_ptr[i].grossPay * VT_TAX_RATE;
  209. } else if (strcmp(emp_ptr[i].taxState, "CA") == 0) {
  210. emp_ptr[i].stateTax = emp_ptr[i].grossPay * CA_TAX_RATE;
  211. } else {
  212. emp_ptr[i].stateTax = emp_ptr[i].grossPay * DEFAULT_TAX_RATE;
  213. }
  214. }
  215. }
  216.  
  217. // Function to calculate federal tax
  218. void calcFedTax(struct employee *emp_ptr, int theSize) {
  219. for (int i = 0; i < theSize; i++) {
  220. emp_ptr[i].fedTax = emp_ptr[i].grossPay * FED_TAX_RATE;
  221. }
  222. }
  223.  
  224. // Function to calculate net pay
  225. void calcNetPay(struct employee *emp_ptr, int theSize) {
  226. for (int i = 0; i < theSize; i++) {
  227. emp_ptr[i].netPay = emp_ptr[i].grossPay - emp_ptr[i].stateTax - emp_ptr[i].fedTax;
  228. }
  229. }
  230.  
  231. // Function to calculate totals
  232. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
  233. for (int i = 0; i < theSize; i++) {
  234. emp_totals_ptr->total_wageRate += emp_ptr[i].wageRate;
  235. emp_totals_ptr->total_hours += emp_ptr[i].hours;
  236. emp_totals_ptr->total_overtimeHrs += emp_ptr[i].overtimeHrs;
  237. emp_totals_ptr->total_grossPay += emp_ptr[i].grossPay;
  238. emp_totals_ptr->total_stateTax += emp_ptr[i].stateTax;
  239. emp_totals_ptr->total_fedTax += emp_ptr[i].fedTax;
  240. emp_totals_ptr->total_netPay += emp_ptr[i].netPay;
  241. }
  242. }
  243.  
  244. // Function to calculate min and max values for each item
  245. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize) {
  246. for (int i = 0; i < theSize; i++) {
  247. if (emp_ptr[i].wageRate < emp_MinMax_ptr->min_wageRate) emp_MinMax_ptr->min_wageRate = emp_ptr[i].wageRate;
  248. if (emp_ptr[i].wageRate > emp_MinMax_ptr->max_wageRate) emp_MinMax_ptr->max_wageRate = emp_ptr[i].wageRate;
  249.  
  250. if (emp_ptr[i].hours < emp_MinMax_ptr->min_hours) emp_MinMax_ptr->min_hours = emp_ptr[i].hours;
  251. if (emp_ptr[i].hours > emp_MinMax_ptr->max_hours) emp_MinMax_ptr->max_hours = emp_ptr[i].hours;
  252.  
  253. if (emp_ptr[i].overtimeHrs < emp_MinMax_ptr->min_overtimeHrs) emp_MinMax_ptr->min_overtimeHrs = emp_ptr[i].overtimeHrs;
  254. if (emp_ptr[i].overtimeHrs > emp_MinMax_ptr->max_overtimeHrs) emp_MinMax_ptr->max_overtimeHrs = emp_ptr[i].overtimeHrs;
  255.  
  256. if (emp_ptr[i].grossPay < emp_MinMax_ptr->min_grossPay) emp_MinMax_ptr->min_grossPay = emp_ptr[i].grossPay;
  257. if (emp_ptr[i].grossPay > emp_MinMax_ptr->max_grossPay) emp_MinMax_ptr->max_grossPay = emp_ptr[i].grossPay;
  258.  
  259. if (emp_ptr[i].stateTax < emp_MinMax_ptr->min_stateTax) emp_MinMax_ptr->min_stateTax = emp_ptr[i].stateTax;
  260. if (emp_ptr[i].stateTax > emp_MinMax_ptr->max_stateTax) emp_MinMax_ptr->max_stateTax = emp_ptr[i].stateTax;
  261.  
  262. if (emp_ptr[i].fedTax < emp_MinMax_ptr->min_fedTax) emp_MinMax_ptr->min_fedTax = emp_ptr[i].fedTax;
  263. if (emp_ptr[i].fedTax > emp_MinMax_ptr->max_fedTax) emp_MinMax_ptr->max_fedTax = emp_ptr[i].fedTax;
  264.  
  265. if (emp_ptr[i].netPay < emp_MinMax_ptr->min_netPay) emp_MinMax_ptr->min_netPay = emp_ptr[i].netPay;
  266. if (emp_ptr[i].netPay > emp_MinMax_ptr->max_netPay) emp_MinMax_ptr->max_netPay = emp_ptr[i].netPay;
  267. }
  268. }
  269.  
  270. // Function to print the header for output
  271. void printHeader(void) {
  272. printf("\n%-20s %-4s %-7s %-5s %-5s %-3s %-6s %-6s %-6s %-6s\n", "Name", "Tax", "Clock#", "Wage", "Hours", "OT", "Gross", "State", "Fed", "Net");
  273. printf("----------------------------------------------------------------------------------------\n");
  274. }
  275.  
  276. // Function to print employee data
  277. void printEmp(struct employee *emp_ptr, int theSize) {
  278. for (int i = 0; i < theSize; i++) {
  279. printf("%-20s %-4s %-7ld %-5.2f %-5.1f %-3.1f %-6.2f %-6.2f %-6.2f %-6.2f\n",
  280. emp_ptr[i].empName.firstName, emp_ptr[i].empName.lastName, emp_ptr[i].clockNumber,
  281. emp_ptr[i].wageRate, emp_ptr[i].hours, emp_ptr[i].overtimeHrs, emp_ptr[i].grossPay,
  282. emp_ptr[i].stateTax, emp_ptr[i].fedTax, emp_ptr[i].netPay);
  283. }
  284. }
  285.  
  286. // Function to print total, averages, min and max values
  287. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_MinMax_ptr, int theSize) {
  288. printf("\nTotals:\n");
  289. printf("Wage: %.2f Hours: %.1f OT: %.1f Gross: %.2f State: %.2f Fed: %.2f Net: %.2f\n",
  290. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours, emp_totals_ptr->total_overtimeHrs,
  291. emp_totals_ptr->total_grossPay, emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  292. emp_totals_ptr->total_netPay);
  293.  
  294. printf("\nAverages:\n");
  295. printf("Wage: %.2f Hours: %.1f OT: %.1f Gross: %.2f State: %.2f Fed: %.2f Net: %.2f\n",
  296. emp_totals_ptr->total_wageRate / theSize,
  297. emp_totals_ptr->total_hours / theSize,
  298. emp_totals_ptr->total_overtimeHrs / theSize,
  299. emp_totals_ptr->total_grossPay / theSize,
  300. emp_totals_ptr->total_stateTax / theSize,
  301. emp_totals_ptr->total_fedTax / theSize,
  302. emp_totals_ptr->total_netPay / theSize);
  303.  
  304. printf("\nMinimums:\n");
  305. printf("Wage: %.2f Hours: %.1f OT: %.1f Gross: %.2f State: %.2f Fed: %.2f Net: %.2f\n",
  306. emp_MinMax_ptr->min_wageRate, emp_MinMax_ptr->min_hours, emp_MinMax_ptr->min_overtimeHrs,
  307. emp_MinMax_ptr->min_grossPay, emp_MinMax_ptr->min_stateTax, emp_MinMax_ptr->min_fedTax,
  308. emp_MinMax_ptr->min_netPay);
  309.  
  310. printf("\nMaximums:\n");
  311. printf("Wage: %.2f Hours: %.1f OT: %.1f Gross: %.2f State: %.2f Fed: %.2f Net: %.2f\n",
  312. emp_MinMax_ptr->max_wageRate, emp_MinMax_ptr->max_hours, emp_MinMax_ptr->max_overtimeHrs,
  313. emp_MinMax_ptr->max_grossPay, emp_MinMax_ptr->max_stateTax, emp_MinMax_ptr->max_fedTax,
  314. emp_MinMax_ptr->max_netPay);
  315. }
Success #stdin #stdout 0s 5284KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
Enter hours worked for Connie Cobol (Employee #98401): Enter hours worked for Mary Apl (Employee #526488): Enter hours worked for Frank Fortran (Employee #765349): Enter hours worked for Jeff Ada (Employee #34645): Enter hours worked for Anton Pascal (Employee #127615): 
Name                 Tax  Clock#  Wage  Hours OT  Gross  State  Fed    Net   
----------------------------------------------------------------------------------------
Connie               Cobol 98401   10.60 51.0  11.0 598.90 29.95  149.73 419.23
Mary                 Apl  526488  9.75  42.5  2.5 426.56 0.00   106.64 319.92
Frank                Fortran 765349  10.50 37.0  0.0 388.50 23.31  97.12  268.07
Jeff                 Ada  34645   12.25 45.0  5.0 581.88 46.55  145.47 389.86
Anton                Pascal 127615  8.35  40.0  0.0 334.00 23.38  83.50  227.12

Totals:
Wage: 51.45  Hours: 215.5  OT: 18.5  Gross: 2329.84  State: 123.18  Fed: 582.46  Net: 1624.19

Averages:
Wage: 10.29  Hours: 43.1  OT: 3.7  Gross: 465.97  State: 24.64  Fed: 116.49  Net: 324.84

Minimums:
Wage: 8.35  Hours: 37.0  OT: 0.0  Gross: 334.00  State: 0.00  Fed: 83.50  Net: 227.12

Maximums:
Wage: 12.25  Hours: 51.0  OT: 11.0  Gross: 598.90  State: 46.55  Fed: 149.73  Net: 419.23