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