fork download
  1. #include <stdio.h>
  2.  
  3. //---------------- Constants ----------------//
  4. #define SIZE 5 // number of employees
  5. #define STD_HOURS 40.0 // standard work week
  6. #define OT_RATE 1.5 // overtime pay rate
  7.  
  8. //---------------- Structure Definition ----------------//
  9. struct employee {
  10. long int clockNumber;
  11. float wageRate;
  12. float hours;
  13. float overtimeHrs;
  14. float grossPay;
  15. };
  16.  
  17. //---------------- Function Prototypes ----------------//
  18. float getHours(long int clockNumber);
  19. float calcOvertime(float hours);
  20. float calcGrossPay(float wageRate, float hours, float overtimeHrs);
  21. void printHeader(void);
  22. void printEmp(struct employee empList[], int size);
  23.  
  24. //**************************************************************
  25. // ===== Function: main =====
  26. // Purpose: Main driver function to process all employees.
  27. // Parameters: None
  28. // Returns: 0 on successful execution
  29. //**************************************************************
  30. int main(void)
  31. {
  32. struct employee empList[SIZE] = {
  33. {98401, 10.60, 0, 0, 0},
  34. {526488, 9.75, 0, 0, 0},
  35. {765349, 10.50, 0, 0, 0},
  36. {34645, 12.25, 0, 0, 0},
  37. {127615, 8.35, 0, 0, 0}
  38. };
  39.  
  40. printf("\n*** Pay Calculator ***\n\n");
  41.  
  42. // TODO: Process each employee (input, calculations)
  43. for (int i = 0; i < SIZE; ++i)
  44. {
  45. empList[i].hours = getHours(empList[i].clockNumber);
  46. empList[i].overtimeHrs = calcOvertime(empList[i].hours);
  47. empList[i].grossPay = calcGrossPay(empList[i].wageRate, empList[i].hours, empList[i].overtimeHrs);
  48. }
  49.  
  50. // TODO: Display results in table format
  51. printHeader();
  52. printEmp(empList, SIZE);
  53.  
  54. return 0;
  55. }
  56.  
  57. //**************************************************************
  58. // ===== Function: getHours =====
  59. // Purpose: Prompts user for hours worked by each employee
  60. // Parameters: clockNumber - (long int) employee ID
  61. // Returns: (float) hours worked for the week
  62. //**************************************************************
  63. float getHours(long int clockNumber)
  64. {
  65. float hours;
  66. // TODO: Read input for each employee
  67. printf("Enter hours worked by emp #%06li: ", clockNumber);
  68. scanf("%f", &hours);
  69. return hours;
  70. }
  71.  
  72. //**************************************************************
  73. // ===== Function: calcOvertime =====
  74. // Purpose: Calculates overtime hours if total hours exceed STD_HOURS
  75. // Parameters: hours - (float) total hours worked
  76. // Returns: (float) overtime hours worked, or 0 if none
  77. //**************************************************************
  78. float calcOvertime(float hours)
  79. {
  80. float overtime;
  81.  
  82. if (hours > STD_HOURS)
  83. overtime = hours - STD_HOURS;
  84. else
  85. overtime = 0.0;
  86.  
  87. return overtime;
  88. }
  89.  
  90. //**************************************************************
  91. // ===== Function: calcGrossPay =====
  92. // Purpose: Calculates gross pay, including normal and overtime pay
  93. // Parameters:
  94. // wageRate - (float) hourly wage
  95. // hours - (float) total hours worked
  96. // overtimeHrs - (float) overtime hours worked
  97. // Returns: (float) total gross pay for the week
  98. //**************************************************************
  99. float calcGrossPay(float wageRate, float hours, float overtimeHrs)
  100. {
  101. // TODO: Apply overtime rate properly
  102. if (hours > STD_HOURS)
  103. return (STD_HOURS * wageRate) + (overtimeHrs * wageRate * OT_RATE);
  104. else
  105. return hours * wageRate;
  106. }
  107.  
  108. //**************************************************************
  109. // ===== Function: printHeader =====
  110. // Purpose: Prints the header for the output table
  111. // Parameters: None
  112. // Returns: void
  113. //**************************************************************
  114. void printHeader(void)
  115. {
  116. // TODO: Format table neatly
  117. printf("\n----------------------------------------------------------\n");
  118. printf(" Clock# Wage Hours OT Gross\n");
  119. printf("----------------------------------------------------------\n");
  120. }
  121.  
  122. //**************************************************************
  123. // ===== Function: printEmp =====
  124. // Purpose: Displays all employee data in table format
  125. // Parameters:
  126. // empList - (struct employee[]) employee data
  127. // size - (int) number of employees
  128. // Returns: void
  129. //**************************************************************
  130. void printEmp(struct employee empList[], int size)
  131. {
  132. // TODO: Print all employee information clearly
  133. for (int i = 0; i < size; ++i)
  134. {
  135. printf("%07li %7.2f %7.1f %7.1f %10.2f\n",
  136. empList[i].clockNumber,
  137. empList[i].wageRate,
  138. empList[i].hours,
  139. empList[i].overtimeHrs,
  140. empList[i].grossPay);
  141. }
  142. }
Success #stdin #stdout 0s 5320KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
*** Pay Calculator ***

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: 
----------------------------------------------------------
  Clock#   Wage   Hours    OT     Gross
----------------------------------------------------------
0098401   10.60    51.0    11.0     598.90
0526488    9.75    42.5     2.5     426.56
0765349   10.50    37.0     0.0     388.50
0034645   12.25    45.0     5.0     581.88
0127615    8.35     0.0     0.0       0.00