fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define NUM_EMPL 5
  5. #define STD_HOURS 40.0
  6. #define OT_RATE 1.5
  7. #define FED_TAX_RATE 0.25
  8.  
  9. struct name {
  10. char firstName[10];
  11. char lastName[10];
  12. };
  13.  
  14. struct employee {
  15. struct name empName;
  16. char taxState[3];
  17. long int clockNumber;
  18. float wageRate, hours, overtimeHrs;
  19. float grossPay, stateTax, fedTax, netPay;
  20. };
  21.  
  22. struct totals {
  23. float wage, hours, overtime, gross, stateTax, fedTax, net;
  24. };
  25.  
  26. struct min_max {
  27. float min[7], max[7];
  28. };
  29.  
  30. /* Simplified Function Prototypes */
  31. void getHours(struct employee *emp_ptr, int size);
  32. void calculatePay(struct employee *emp_ptr, int size);
  33. void printEmp(struct employee *emp_ptr, int size);
  34. void processStatistics(struct employee *emp_ptr, struct totals *t_ptr, struct min_max *mm_ptr, int size);
  35.  
  36. int main(void)
  37. {
  38. struct employee employeeData[NUM_EMPL] = {
  39. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  40. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  41. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  42. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  43. { {"Anton", "Pascal"}, "CA", 127615, 8.35 }
  44. };
  45.  
  46. struct totals employeeTotals = {0};
  47. struct min_max employeeMinMax = {0};
  48.  
  49. struct employee *emp_ptr = employeeData;
  50. struct totals *emp_totals_ptr = &employeeTotals;
  51. struct min_max *emp_minMax_ptr = &employeeMinMax;
  52.  
  53. getHours(emp_ptr, NUM_EMPL);
  54. calculatePay(emp_ptr, NUM_EMPL);
  55.  
  56. printf("\n*** Pay Calculator ***\n");
  57. printEmp(emp_ptr, NUM_EMPL);
  58. processStatistics(emp_ptr, emp_totals_ptr, emp_minMax_ptr, NUM_EMPL);
  59.  
  60. return 0;
  61. }
  62.  
  63. void getHours(struct employee *emp_ptr, int size)
  64. {
  65. for (int i = 0; i < size; i++, emp_ptr++) {
  66. printf("Enter hours for employee %06li: ", emp_ptr->clockNumber);
  67. if (scanf("%f", &emp_ptr->hours) != 1) {
  68. printf("Error reading input.\n");
  69. }
  70. }
  71. }
  72.  
  73. void calculatePay(struct employee *emp_ptr, int size)
  74. {
  75. for (int i = 0; i < size; i++, emp_ptr++) {
  76. float rate = 0.08;
  77.  
  78. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? (emp_ptr->hours - STD_HOURS) : 0;
  79. emp_ptr->grossPay = ((emp_ptr->hours - emp_ptr->overtimeHrs) * emp_ptr->wageRate) +
  80. (emp_ptr->overtimeHrs * emp_ptr->wageRate * OT_RATE);
  81.  
  82. if (strcmp(emp_ptr->taxState, "MA") == 0) rate = 0.05;
  83. else if (strcmp(emp_ptr->taxState, "NH") == 0) rate = 0.00;
  84. else if (strcmp(emp_ptr->taxState, "VT") == 0) rate = 0.06;
  85. else if (strcmp(emp_ptr->taxState, "CA") == 0) rate = 0.07;
  86.  
  87. emp_ptr->stateTax = emp_ptr->grossPay * rate;
  88. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  89. emp_ptr->netPay = emp_ptr->grossPay - emp_ptr->stateTax - emp_ptr->fedTax;
  90. }
  91. }
  92.  
  93. void printEmp(struct employee *emp_ptr, int size)
  94. {
  95. printf("---------------------------------------------------------------------------------\n");
  96. printf("%-20s %-5s %8s %7s %6s %6s %8s %8s %8s %8s\n",
  97. "Name", "State", "Clock#", "Wage", "Hours", "OT", "Gross", "StateTax", "FedTax", "NetPay");
  98. printf("---------------------------------------------------------------------------------\n");
  99.  
  100. for (int i = 0; i < size; i++, emp_ptr++) {
  101. char fullName[21];
  102. snprintf(fullName, sizeof(fullName), "%s %s", emp_ptr->empName.firstName, emp_ptr->empName.lastName);
  103.  
  104. printf("%-20s %-5s %08li %7.2f %6.1f %6.1f %8.2f %8.2f %8.2f %8.2f\n",
  105. fullName, emp_ptr->taxState, emp_ptr->clockNumber,
  106. emp_ptr->wageRate, emp_ptr->hours, emp_ptr->overtimeHrs,
  107. emp_ptr->grossPay, emp_ptr->stateTax, emp_ptr->fedTax, emp_ptr->netPay);
  108. }
  109. }
  110.  
  111. /* Combines calculation and printing logic cleanly using simple index looping */
  112. void processStatistics(struct employee *emp_ptr, struct totals *t_ptr, struct min_max *mm_ptr, int size)
  113. {
  114. // Initialize min and max values with the first employee's values
  115. float structural_vals[7] = {
  116. emp_ptr->wageRate, emp_ptr->hours, emp_ptr->overtimeHrs,
  117. emp_ptr->grossPay, emp_ptr->stateTax, emp_ptr->fedTax, emp_ptr->netPay
  118. };
  119. for (int j = 0; j < 7; j++) {
  120. mm_ptr->min[j] = structural_vals[j];
  121. mm_ptr->max[j] = structural_vals[j];
  122. }
  123.  
  124. // Accumulate total metrics and track min/max scales across all records
  125. for (int i = 0; i < size; i++) {
  126. struct employee *c = emp_ptr + i;
  127. float row[7] = { c->wageRate, c->hours, c->overtimeHrs, c->grossPay, c->stateTax, c->fedTax, c->netPay };
  128.  
  129. t_ptr->wage += c->wageRate; t_ptr->hours += c->hours;
  130. t_ptr->overtime += c->overtimeHrs; t_ptr->gross += c->grossPay;
  131. t_ptr->stateTax += c->stateTax; t_ptr->fedTax += c->fedTax;
  132. t_ptr->net += c->netPay;
  133.  
  134. for (int j = 0; j < 7; j++) {
  135. if (row[j] < mm_ptr->min[j]) mm_ptr->min[j] = row[j];
  136. if (row[j] > mm_ptr->max[j]) mm_ptr->max[j] = row[j];
  137. }
  138. }
  139.  
  140. // Print summary blocks cleanly using our streamlined struct arrays
  141. printf("---------------------------------------------------------------------------------\n");
  142. printf("%-20s %-5s %8s %7.2f %6.1f %6.1f %8.2f %8.2f %8.2f %8.2f\n", "Totals:", "", "",
  143. t_ptr->wage, t_ptr->hours, t_ptr->overtime, t_ptr->gross, t_ptr->stateTax, t_ptr->fedTax, t_ptr->net);
  144.  
  145. printf("%-20s %-5s %8s %7.2f %6.1f %6.1f %8.2f %8.2f %8.2f %8.2f\n", "Averages:", "", "",
  146. t_ptr->wage / size, t_ptr->hours / size, t_ptr->overtime / size, t_ptr->gross / size, t_ptr->stateTax / size, t_ptr->fedTax / size, t_ptr->net / size);
  147.  
  148. printf("%-20s %-5s %8s %7.2f %6.1f %6.1f %8.2f %8.2f %8.2f %8.2f\n", "Minimum:", "", "",
  149. mm_ptr->min[0], mm_ptr->min[1], mm_ptr->min[2], mm_ptr->min[3], mm_ptr->min[4], mm_ptr->min[5], mm_ptr->min[6]);
  150.  
  151. printf("%-20s %-5s %8s %7.2f %6.1f %6.1f %8.2f %8.2f %8.2f %8.2f\n", "Maximum:", "", "",
  152. mm_ptr->max[0], mm_ptr->max[1], mm_ptr->max[2], mm_ptr->max[3], mm_ptr->max[4], mm_ptr->max[5], mm_ptr->max[6]);
  153. }
Success #stdin #stdout 0s 5284KB
stdin
51.0   
42.5
37.0
45.0
40.0
stdout
Enter hours for employee 098401: Enter hours for employee 526488: Enter hours for employee 765349: Enter hours for employee 034645: Enter hours for employee 127615: 
*** Pay Calculator ***
---------------------------------------------------------------------------------
Name                 State   Clock#    Wage  Hours     OT    Gross StateTax   FedTax   NetPay
---------------------------------------------------------------------------------
Connie Cobol         MA    00098401   10.60   51.0   11.0   598.90    29.95   149.73   419.23
Mary Apl             NH    00526488    9.75   42.5    2.5   426.56     0.00   106.64   319.92
Frank Fortran        VT    00765349   10.50   37.0    0.0   388.50    23.31    97.12   268.07
Jeff Ada             NY    00034645   12.25   45.0    5.0   581.88    46.55   145.47   389.86
Anton Pascal         CA    00127615    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