fork download
  1. #include <stdio.h>
  2.  
  3. // Define Constants
  4. #define SIZE 5
  5. #define STD_HOURS 40.0
  6. #define OT_RATE 1.5
  7.  
  8. // Define a global structure to store employee data
  9. struct employee
  10. {
  11. long int clockNumber;
  12. float wageRate;
  13. float hours;
  14. float overtimeHrs;
  15. float grossPay;
  16. };
  17.  
  18. // Function prototypes
  19. float getHours(long int clockNumber);
  20. void printHeader(void);
  21. void printEmp(long int clockNumber, float wageRate, float hours,
  22. float overtimeHrs, float grossPay);
  23. float calculateOvertime(float hours);
  24. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  25.  
  26. int main()
  27. {
  28. // Initialize the array of employee data
  29. struct employee employeeData[SIZE] = {
  30. {98401, 10.60},
  31. {526488, 9.75},
  32. {765349, 10.50},
  33. {34645, 12.25},
  34. {127615, 8.35}
  35. };
  36.  
  37. int i;
  38.  
  39. // Loop to process each employee's data
  40. for (i = 0; i < SIZE; ++i)
  41. {
  42. // Prompt for the number of hours worked by the employee
  43. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  44.  
  45. // Calculate overtime hours
  46. employeeData[i].overtimeHrs = calculateOvertime(employeeData[i].hours);
  47.  
  48. // Calculate gross pay
  49. employeeData[i].grossPay = calculateGrossPay(employeeData[i].wageRate,
  50. employeeData[i].hours,
  51. employeeData[i].overtimeHrs);
  52. }
  53.  
  54. // Print the table header
  55. printHeader();
  56.  
  57. // Print out each employee's data
  58. for (i = 0; i < SIZE; ++i)
  59. {
  60. printEmp(employeeData[i].clockNumber,
  61. employeeData[i].wageRate,
  62. employeeData[i].hours,
  63. employeeData[i].overtimeHrs,
  64. employeeData[i].grossPay);
  65. }
  66.  
  67. return 0; // success
  68. }
  69.  
  70. //**************************************************************
  71. // Function: getHours
  72. //
  73. // Purpose: Obtains input from the user for the number of hours
  74. // worked by an employee.
  75. //
  76. // Parameters: clockNumber - The unique employee ID
  77. //
  78. // Returns: The hours worked by the employee
  79. //**************************************************************
  80. float getHours(long int clockNumber)
  81. {
  82. float hoursWorked;
  83.  
  84. // Prompt user to enter hours worked for the employee
  85. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  86. scanf("%f", &hoursWorked);
  87.  
  88. return hoursWorked;
  89. }
  90.  
  91. //**************************************************************
  92. // Function: printHeader
  93. //
  94. // Purpose: Prints the initial table header information.
  95. //
  96. // Parameters: none
  97. //
  98. // Returns: void
  99. //**************************************************************
  100. void printHeader(void)
  101. {
  102. printf("\n\n*** Pay Calculator ***\n");
  103. printf("\nClock# Wage Hours OT Gross\n");
  104. printf("-----------------------------------------\n");
  105. }
  106.  
  107. //*************************************************************
  108. // Function: printEmp
  109. //
  110. // Purpose: Prints out an employee's information in a formatted table.
  111. //
  112. // Parameters:
  113. // clockNumber - Employee ID
  114. // wageRate - Employee's wage rate
  115. // hours - Total hours worked
  116. // overtimeHrs - Overtime hours worked
  117. // grossPay - Gross pay for the employee
  118. //
  119. // Returns: void
  120. //*************************************************************
  121. void printEmp(long int clockNumber, float wageRate, float hours,
  122. float overtimeHrs, float grossPay)
  123. {
  124. printf("%06li %5.2f %4.1f %4.1f %8.2f\n",
  125. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  126. }
  127.  
  128. //**************************************************************
  129. // Function: calculateOvertime
  130. //
  131. // Purpose: Calculates overtime hours. Anything over 40 hours is overtime.
  132. //
  133. // Parameters: hours - Total hours worked by the employee
  134. //
  135. // Returns: overtime hours
  136. //**************************************************************
  137. float calculateOvertime(float hours)
  138. {
  139. if (hours > STD_HOURS)
  140. return hours - STD_HOURS; // Overtime hours
  141. return 0.0;
  142. }
  143.  
  144. //**************************************************************
  145. // Function: calculateGrossPay
  146. //
  147. // Purpose: Calculates gross pay (normal pay + overtime pay).
  148. //
  149. // Parameters:
  150. // wageRate - Employee's hourly wage rate
  151. // hours - Total hours worked
  152. // overtimeHrs - Overtime hours worked
  153. //
  154. // Returns: gross pay (normal pay + overtime pay)
  155. //**************************************************************
  156. float calculateGrossPay(float wageRate, float hours, float overtimeHrs)
  157. {
  158. float normalPay = wageRate * (hours - overtimeHrs);
  159. float overtimePay = overtimeHrs * wageRate * OT_RATE;
  160. return normalPay + overtimePay;
  161. }
Success #stdin #stdout 0s 5288KB
stdin
51.0
42.5
37.0
45.0
0.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 ***

Clock#   Wage  Hours  OT     Gross
-----------------------------------------
098401   10.60  51.0   11.0     598.90
526488    9.75  42.5    2.5     426.56
765349   10.50  37.0    0.0     388.50
034645   12.25  45.0    5.0     581.88
127615    8.35   0.0    0.0       0.00