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