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.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  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 NAME_SIZE 20
  40. #define TAX_STATE_SIZE 3
  41. #define FED_TAX_RATE 0.25
  42. #define FIRST_NAME_SIZE 10
  43. #define LAST_NAME_SIZE 10
  44.  
  45. // Define a structure type to store an employee name
  46. struct name {
  47. char firstName[FIRST_NAME_SIZE];
  48. char lastName[LAST_NAME_SIZE];
  49. };
  50.  
  51. // Define a structure type to pass employee data between functions
  52. struct employee {
  53. struct name empName;
  54. char taxState[TAX_STATE_SIZE];
  55. long int clockNumber;
  56. float wageRate;
  57. float hours;
  58. float overtimeHrs;
  59. float grossPay;
  60. float stateTax;
  61. float fedTax;
  62. float netPay;
  63. };
  64.  
  65. // This structure type defines the totals of all floating point items
  66. struct totals {
  67. float total_wageRate;
  68. float total_hours;
  69. float total_overtimeHrs;
  70. float total_grossPay;
  71. float total_stateTax;
  72. float total_fedTax;
  73. float total_netPay;
  74. };
  75.  
  76. // This structure type defines the min and max values of all floating point items
  77. struct min_max {
  78. float min_wageRate;
  79. float min_hours;
  80. float min_overtimeHrs;
  81. float min_grossPay;
  82. float min_stateTax;
  83. float min_fedTax;
  84. float min_netPay;
  85. float max_wageRate;
  86. float max_hours;
  87. float max_overtimeHrs;
  88. float max_grossPay;
  89. float max_stateTax;
  90. float max_fedTax;
  91. float max_netPay;
  92. };
  93.  
  94. // Function Prototypes
  95. void getHours(struct employee *emp_ptr, int theSize);
  96. void printEmp(struct employee *emp_ptr, int theSize);
  97. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  98. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize);
  99. void printHeader(void);
  100. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  101. void calcGrossPay(struct employee *emp_ptr, int theSize);
  102. void calcStateTax(struct employee *emp_ptr, int theSize);
  103. void calcFedTax(struct employee *emp_ptr, int theSize);
  104. void calcNetPay(struct employee *emp_ptr, int theSize);
  105. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);
  106.  
  107. int main() {
  108. struct employee employeeData[SIZE] = {
  109. {{"Connie", "Cobol"}, "MA", 98401, 10.60, 51.0},
  110. {{"Mary", "Apl"}, "NH", 526488, 9.75, 42.5},
  111. {{"Frank", "Fortran"}, "VT", 765349, 10.50, 37.0},
  112. {{"Jeff", "Ada"}, "NY", 34645, 12.25, 45.0},
  113. {{"Anton", "Pascal"}, "CA", 127615, 8.35, 40.0}
  114. };
  115.  
  116. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  117. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  118.  
  119. struct employee *emp_ptr = employeeData;
  120. struct totals *emp_totals_ptr = &employeeTotals;
  121. struct min_max *emp_minMax_ptr = &employeeMinMax;
  122.  
  123. // Call functions to read and calculate employee data
  124. getHours(emp_ptr, SIZE);
  125. calcOvertimeHrs(emp_ptr, SIZE);
  126. calcGrossPay(emp_ptr, SIZE);
  127. calcStateTax(emp_ptr, SIZE);
  128. calcFedTax(emp_ptr, SIZE);
  129. calcNetPay(emp_ptr, SIZE);
  130. calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
  131. calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
  132.  
  133. // Print headers and employee details
  134. printHeader();
  135. printEmp(emp_ptr, SIZE);
  136. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
  137.  
  138. return 0;
  139. }
  140.  
  141. // Function Definitions
  142.  
  143. void getHours(struct employee *emp_ptr, int theSize) {
  144. for (int i = 0; i < theSize; ++i) {
  145. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  146. scanf("%f", &emp_ptr->hours);
  147. ++emp_ptr;
  148. }
  149. }
  150.  
  151. void printHeader(void) {
  152. printf("\n\n*** Pay Calculator ***\n");
  153. printf("---------------------------------------------------------------------------------\n");
  154. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  155. printf(" State Pay Tax Tax Pay\n");
  156. printf("---------------------------------------------------------------------------------\n");
  157. }
  158.  
  159. void printEmp(struct employee *emp_ptr, int theSize) {
  160. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  161. for (int i = 0; i < theSize; ++i) {
  162. snprintf(name, sizeof(name), "%s %s", emp_ptr->empName.firstName, emp_ptr->empName.lastName);
  163. printf("%-20.20s %-2.2s %06li %5.2f %5.1f %5.1f %8.2f %7.2f %7.2f %8.2f\n",
  164. name, emp_ptr->taxState, emp_ptr->clockNumber,
  165. emp_ptr->wageRate, emp_ptr->hours,
  166. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  167. emp_ptr->stateTax, emp_ptr->fedTax,
  168. emp_ptr->netPay);
  169. ++emp_ptr;
  170. }
  171. printf("---------------------------------------------------------------------------------\n");
  172. }
  173.  
  174. void calcOvertimeHrs(struct employee *emp_ptr, int theSize) {
  175. for (int i = 0; i < theSize; ++i) {
  176. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? emp_ptr->hours - STD_HOURS : 0;
  177. ++emp_ptr;
  178. }
  179. }
  180.  
  181. void calcGrossPay(struct employee *emp_ptr, int theSize) {
  182. for (int i = 0; i < theSize; ++i) {
  183. float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
  184. float overtimePay = emp_ptr->overtimeHrs * OT_RATE * emp_ptr->wageRate;
  185. emp_ptr->grossPay = normalPay + overtimePay;
  186. ++emp_ptr;
  187. }
  188. }
  189.  
  190. void calcStateTax(struct employee *emp_ptr, int theSize) {
  191. for (int i = 0; i < theSize; ++i) {
  192. if (strcmp(emp_ptr->taxState, "MA") == 0)
  193. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  194. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  195. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  196. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  197. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  198. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  199. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  200. else
  201. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  202. ++emp_ptr;
  203. }
  204. }
  205.  
  206. void calcFedTax(struct employee *emp_ptr, int theSize) {
  207. for (int i = 0; i < theSize; ++i) {
  208. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  209. ++emp_ptr;
  210. }
  211. }
  212.  
  213. void calcNetPay(struct employee *emp_ptr, int theSize) {
  214. for (int i = 0; i < theSize; ++i) {
  215. emp_ptr->netPay = emp_ptr->grossPay - (emp_ptr->stateTax + emp_ptr->fedTax);
  216. ++emp_ptr;
  217. }
  218. }
  219.  
  220. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
  221. for (int i = 0; i < theSize; ++i) {
  222. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  223. emp_totals_ptr->total_hours += emp_ptr->hours;
  224. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  225. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  226. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  227. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  228. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  229. ++emp_ptr;
  230. }
  231. }
  232.  
  233. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize) {
  234. emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  235. emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = emp_ptr->hours;
  236. emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  237. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  238. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  239. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  240. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  241.  
  242. for (int i = 1; i < theSize; ++i) {
  243. ++emp_ptr;
  244. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate) emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  245. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate) emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  246. if (emp_ptr->hours < emp_minMax_ptr->min_hours) emp_minMax_ptr->min_hours = emp_ptr->hours;
  247. if (emp_ptr->hours > emp_minMax_ptr->max_hours) emp_minMax_ptr->max_hours = emp_ptr->hours;
  248. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs) emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  249. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs) emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  250. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay) emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  251. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay) emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  252. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax) emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  253. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax) emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  254. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax) emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  255. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax) emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  256. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay) emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  257. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay) emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  258. }
  259. }
  260.  
  261. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize) {
  262. printf("Totals: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  263. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  264. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  265. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  266. emp_totals_ptr->total_netPay);
  267.  
  268. if (theSize > 0) {
  269. printf("Averages: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  270. emp_totals_ptr->total_wageRate / theSize,
  271. emp_totals_ptr->total_hours / theSize,
  272. emp_totals_ptr->total_overtimeHrs / theSize,
  273. emp_totals_ptr->total_grossPay / theSize,
  274. emp_totals_ptr->total_stateTax / theSize,
  275. emp_totals_ptr->total_fedTax / theSize,
  276. emp_totals_ptr->total_netPay / theSize);
  277. }
  278.  
  279. printf("Minimum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  280. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  281. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  282. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  283. emp_minMax_ptr->min_netPay);
  284.  
  285. printf("Maximum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  286. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  287. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  288. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  289. emp_minMax_ptr->max_netPay);
  290. }
  291.  
Success #stdin #stdout 0s 5284KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
Enter hours worked by emp # 098401: 
Enter hours worked by emp # 526488: 
Enter hours worked by emp # 765349: 
Enter hours worked by emp # 034645: 
Enter hours worked by emp # 127615: 

*** 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