fork download
  1. // necessary header files
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. // define constants
  7. #define SIZE 5
  8. #define STD_HOURS 40.0
  9. #define OT_RATE 1.5
  10. #define MA_TAX_RATE 0.05
  11. #define NH_TAX_RATE 0.0
  12. #define VT_TAX_RATE 0.06
  13. #define CA_TAX_RATE 0.07
  14. #define DEFAULT_TAX_RATE 0.08
  15. #define NAME_SIZE 20
  16. #define TAX_STATE_SIZE 3
  17. #define FED_TAX_RATE 0.25
  18. #define FIRST_NAME_SIZE 10
  19. #define LAST_NAME_SIZE 10
  20.  
  21. // Define structures
  22. struct name {
  23. char firstName[FIRST_NAME_SIZE];
  24. char lastName[LAST_NAME_SIZE];
  25. };
  26.  
  27. struct employee {
  28. struct name empName;
  29. char taxState[TAX_STATE_SIZE];
  30. long int clockNumber;
  31. float wageRate;
  32. float hours;
  33. float overtimeHrs;
  34. float grossPay;
  35. float stateTax;
  36. float fedTax;
  37. float netPay;
  38. };
  39.  
  40. struct totals {
  41. float total_wageRate;
  42. float total_hours;
  43. float total_overtimeHrs;
  44. float total_grossPay;
  45. float total_stateTax;
  46. float total_fedTax;
  47. float total_netPay;
  48. };
  49.  
  50. struct min_max {
  51. float min_wageRate;
  52. float min_hours;
  53. float min_overtimeHrs;
  54. float min_grossPay;
  55. float min_stateTax;
  56. float min_fedTax;
  57. float min_netPay;
  58. float max_wageRate;
  59. float max_hours;
  60. float max_overtimeHrs;
  61. float max_grossPay;
  62. float max_stateTax;
  63. float max_fedTax;
  64. float max_netPay;
  65. };
  66.  
  67. // Function prototypes
  68. void getHours(struct employee[], int);
  69. void calcOvertimeHrs(struct employee[], int);
  70. void calcGrossPay(struct employee[], int);
  71. void calcStateTax(struct employee[], int);
  72. void calcFedTax(struct employee[], int);
  73. void calcNetPay(struct employee[], int);
  74. struct totals calcEmployeeTotals(struct employee[], struct totals, int);
  75. struct min_max calcEmployeeMinMax(struct employee[], struct min_max, int);
  76. void printHeader(void);
  77. void printEmp(struct employee[], int);
  78. void printEmpStatistics(struct totals, struct min_max, int);
  79.  
  80. int main() {
  81. // Employee data
  82. struct employee employeeData[SIZE] = {
  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. struct totals employeeTotals = {0,0,0,0,0,0,0};
  91. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  92.  
  93. getHours(employeeData, SIZE);
  94. calcOvertimeHrs(employeeData, SIZE);
  95. calcGrossPay(employeeData, SIZE);
  96. calcStateTax(employeeData, SIZE);
  97. calcFedTax(employeeData, SIZE);
  98. calcNetPay(employeeData, SIZE);
  99. employeeTotals = calcEmployeeTotals(employeeData, employeeTotals, SIZE);
  100. employeeMinMax = calcEmployeeMinMax(employeeData, employeeMinMax, SIZE);
  101.  
  102. printHeader();
  103. printEmp(employeeData, SIZE);
  104. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  105.  
  106. return 0;
  107. }
  108.  
  109. //**************************************************************
  110. void getHours(struct employee employeeData[], int theSize) {
  111. for (int i = 0; i < theSize; i++) {
  112. printf("\nEnter hours worked by emp # %06li: ", employeeData[i].clockNumber);
  113. scanf("%f", &employeeData[i].hours);
  114. }
  115. }
  116.  
  117. //**************************************************************
  118. void printHeader(void) {
  119. printf("\n\n*** Pay Calculator ***\n");
  120. printf("\n--------------------------------------------------------------");
  121. printf("-------------------");
  122. printf("\nName Tax Clock# Wage Hours OT Gross ");
  123. printf(" State Fed Net");
  124. printf("\n State Pay ");
  125. printf(" Tax Tax Pay");
  126. printf("\n--------------------------------------------------------------");
  127. printf("-------------------");
  128. }
  129.  
  130. //**************************************************************
  131. void printEmp(struct employee employeeData[], int theSize) {
  132. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  133.  
  134. for (int i = 0; i < theSize; i++) {
  135. strcpy(name, employeeData[i].empName.firstName);
  136. strcat(name, " ");
  137. strcat(name, employeeData[i].empName.lastName);
  138.  
  139. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  140. name, employeeData[i].taxState, employeeData[i].clockNumber,
  141. employeeData[i].wageRate, employeeData[i].hours,
  142. employeeData[i].overtimeHrs, employeeData[i].grossPay,
  143. employeeData[i].stateTax, employeeData[i].fedTax,
  144. employeeData[i].netPay);
  145. }
  146. }
  147.  
  148. //**************************************************************
  149. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize) {
  150. printf("\n--------------------------------------------------------------");
  151. printf("-------------------");
  152.  
  153. // Totals
  154. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  155. employeeTotals.total_wageRate,
  156. employeeTotals.total_hours,
  157. employeeTotals.total_overtimeHrs,
  158. employeeTotals.total_grossPay,
  159. employeeTotals.total_stateTax,
  160. employeeTotals.total_fedTax,
  161. employeeTotals.total_netPay);
  162.  
  163. if (theSize > 0) {
  164. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  165. employeeTotals.total_wageRate / theSize,
  166. employeeTotals.total_hours / theSize,
  167. employeeTotals.total_overtimeHrs / theSize,
  168. employeeTotals.total_grossPay / theSize,
  169. employeeTotals.total_stateTax / theSize,
  170. employeeTotals.total_fedTax / theSize,
  171. employeeTotals.total_netPay / theSize);
  172. }
  173.  
  174. // Min values
  175. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  176. employeeMinMax.min_wageRate,
  177. employeeMinMax.min_hours,
  178. employeeMinMax.min_overtimeHrs,
  179. employeeMinMax.min_grossPay,
  180. employeeMinMax.min_stateTax,
  181. employeeMinMax.min_fedTax,
  182. employeeMinMax.min_netPay);
  183.  
  184. // Max values
  185. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  186. employeeMinMax.max_wageRate,
  187. employeeMinMax.max_hours,
  188. employeeMinMax.max_overtimeHrs,
  189. employeeMinMax.max_grossPay,
  190. employeeMinMax.max_stateTax,
  191. employeeMinMax.max_fedTax,
  192. employeeMinMax.max_netPay);
  193. }
  194.  
  195. //**************************************************************
  196. void calcOvertimeHrs(struct employee employeeData[], int theSize) {
  197. for (int i = 0; i < theSize; i++) {
  198. if (employeeData[i].hours > STD_HOURS)
  199. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  200. else
  201. employeeData[i].overtimeHrs = 0;
  202. }
  203. }
  204.  
  205. //**************************************************************
  206. void calcGrossPay(struct employee employeeData[], int theSize) {
  207. for (int i = 0; i < theSize; i++) {
  208. float normalPay = employeeData[i].wageRate * (employeeData[i].hours - employeeData[i].overtimeHrs);
  209. float overtimePay = employeeData[i].overtimeHrs * OT_RATE * employeeData[i].wageRate;
  210. employeeData[i].grossPay = normalPay + overtimePay;
  211. }
  212. }
  213.  
  214. //**************************************************************
  215. void calcStateTax(struct employee employeeData[], int theSize) {
  216. for (int i = 0; i < theSize; i++) {
  217. if (islower(employeeData[i].taxState[0])) employeeData[i].taxState[0] = toupper(employeeData[i].taxState[0]);
  218. if (islower(employeeData[i].taxState[1])) employeeData[i].taxState[1] = toupper(employeeData[i].taxState[1]);
  219.  
  220. if (strcmp(employeeData[i].taxState, "MA") == 0)
  221. employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
  222. else if (strcmp(employeeData[i].taxState, "NH") == 0)
  223. employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
  224. else if (strcmp(employeeData[i].taxState, "VT") == 0)
  225. employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
  226. else if (strcmp(employeeData[i].taxState, "CA") == 0)
  227. employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
  228. else
  229. employeeData[i].stateTax = employeeData[i].grossPay * DEFAULT_TAX_RATE;
  230. }
  231. }
  232.  
  233. //**************************************************************
  234. void calcFedTax(struct employee employeeData[], int theSize) {
  235. for (int i = 0; i < theSize; i++)
  236. employeeData[i].fedTax = employeeData[i].grossPay * FED_TAX_RATE;
  237. }
  238.  
  239. //**************************************************************
  240. void calcNetPay(struct employee employeeData[], int theSize) {
  241. for (int i = 0; i < theSize; i++)
  242. employeeData[i].netPay = employeeData[i].grossPay - (employeeData[i].stateTax + employeeData[i].fedTax);
  243. }
  244.  
  245. //**************************************************************
  246. struct totals calcEmployeeTotals(struct employee employeeData[], struct totals employeeTotals, int theSize) {
  247. for (int i = 0; i < theSize; i++) {
  248. employeeTotals.total_wageRate += employeeData[i].wageRate;
  249. employeeTotals.total_hours += employeeData[i].hours;
  250. employeeTotals.total_overtimeHrs += employeeData[i].overtimeHrs;
  251. employeeTotals.total_grossPay += employeeData[i].grossPay;
  252. employeeTotals.total_stateTax += employeeData[i].stateTax;
  253. employeeTotals.total_fedTax += employeeData[i].fedTax;
  254. employeeTotals.total_netPay += employeeData[i].netPay;
  255. }
  256. return employeeTotals;
  257. }
  258.  
  259. //**************************************************************
  260. struct min_max calcEmployeeMinMax(struct employee employeeData[], struct min_max employeeMinMax, int theSize) {
  261. // Initialize with first employee
  262. employeeMinMax.min_wageRate = employeeMinMax.max_wageRate = employeeData[0].wageRate;
  263. employeeMinMax.min_hours = employeeMinMax.max_hours = employeeData[0].hours;
  264. employeeMinMax.min_overtimeHrs = employeeMinMax.max_overtimeHrs = employeeData[0].overtimeHrs;
  265. employeeMinMax.min_grossPay = employeeMinMax.max_grossPay = employeeData[0].grossPay;
  266. employeeMinMax.min_stateTax = employeeMinMax.max_stateTax = employeeData[0].stateTax;
  267. employeeMinMax.min_fedTax = employeeMinMax.max_fedTax = employeeData[0].fedTax;
  268. employeeMinMax.min_netPay = employeeMinMax.max_netPay = employeeData[0].netPay;
  269.  
  270. for (int i = 1; i < theSize; i++) {
  271. if (employeeData[i].wageRate < employeeMinMax.min_wageRate) employeeMinMax.min_wageRate = employeeData[i].wageRate;
  272. if (employeeData[i].wageRate > employeeMinMax.max_wageRate) employeeMinMax.max_wageRate = employeeData[i].wageRate;
  273.  
  274. if (employeeData[i].hours < employeeMinMax.min_hours) employeeMinMax.min_hours = employeeData[i].hours;
  275. if (employeeData[i].hours > employeeMinMax.max_hours) employeeMinMax.max_hours = employeeData[i].hours;
  276.  
  277. if (employeeData[i].overtimeHrs < employeeMinMax.min_overtimeHrs) employeeMinMax.min_overtimeHrs = employeeData[i].overtimeHrs;
  278. if (employeeData[i].overtimeHrs > employeeMinMax.max_overtimeHrs) employeeMinMax.max_overtimeHrs = employeeData[i].overtimeHrs;
  279.  
  280. if (employeeData[i].grossPay < employeeMinMax.min_grossPay) employeeMinMax.min_grossPay = employeeData[i].grossPay;
  281. if (employeeData[i].grossPay > employeeMinMax.max_grossPay) employeeMinMax.max_grossPay = employeeData[i].grossPay;
  282.  
  283. if (employeeData[i].stateTax < employeeMinMax.min_stateTax) employeeMinMax.min_stateTax = employeeData[i].stateTax;
  284. if (employeeData[i].stateTax > employeeMinMax.max_stateTax) employeeMinMax.max_stateTax = employeeData[i].stateTax;
  285.  
  286. if (employeeData[i].fedTax < employeeMinMax.min_fedTax) employeeMinMax.min_fedTax = employeeData[i].fedTax;
  287. if (employeeData[i].fedTax > employeeMinMax.max_fedTax) employeeMinMax.max_fedTax = employeeData[i].fedTax;
  288.  
  289. if (employeeData[i].netPay < employeeMinMax.min_netPay) employeeMinMax.min_netPay = employeeData[i].netPay;
  290. if (employeeData[i].netPay > employeeMinMax.max_netPay) employeeMinMax.max_netPay = employeeData[i].netPay;
  291. }
  292.  
  293. return employeeMinMax;
  294. }
Success #stdin #stdout 0s 5320KB
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