fork download
  1. #include <stdio.h>
  2.  
  3. // constants
  4. #define SIZE 5
  5. #define STD_HOURS 40.0
  6. #define OT_RATE 1.5
  7.  
  8. // employee structure type
  9. struct employee
  10. {
  11. long int clockNumber;
  12. float wageRate;
  13. float hours;
  14. float overtimeHrs;
  15. float grossPay;
  16. };
  17.  
  18. // functions
  19. float getHours(long int clockNumber);
  20. float calcOT(float hours);
  21. float calcGross(float wageRate, float hours, float overtimeHrs);
  22. void printHeader(void);
  23. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  24.  
  25. int main(void)
  26. {
  27. // employee information
  28. struct employee employeeData[SIZE] = {
  29. { 98401, 10.60, 0.0, 0.0, 0.0 },
  30. { 526488, 9.75, 0.0, 0.0, 0.0 },
  31. { 765349, 10.50, 0.0, 0.0, 0.0 },
  32. { 34645, 12.25, 0.0, 0.0, 0.0 },
  33. { 127615, 8.35, 0.0, 0.0, 0.0 }
  34. };
  35.  
  36. int i;
  37.  
  38. // hours worked, calculate overtime, and calculate gross pay for each employee
  39. for (i = 0; i < SIZE; ++i)
  40. {
  41. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  42. employeeData[i].overtimeHrs = calcOT(employeeData[i].hours);
  43. employeeData[i].grossPay = calcGross(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  44. }
  45.  
  46. // header
  47. printHeader();
  48.  
  49. // each employee's data formated
  50. for (i = 0; i < SIZE; ++i)
  51. {
  52. printEmp(employeeData[i].clockNumber,
  53. employeeData[i].wageRate,
  54. employeeData[i].hours,
  55. employeeData[i].overtimeHrs,
  56. employeeData[i].grossPay);
  57. }
  58.  
  59. return 0;
  60. }
  61.  
  62. // function: getHours
  63. float getHours(long int clockNumber)
  64. {
  65. float hoursWorked;
  66.  
  67. printf("Enter number of hours worked for employee %06li: ", clockNumber);
  68. scanf("%f", &hoursWorked);
  69.  
  70. return hoursWorked;
  71. }
  72.  
  73. // function: calcOT
  74. float calcOT(float hours)
  75. {
  76. float overtime;
  77.  
  78. if (hours > STD_HOURS)
  79. {
  80. overtime = hours - STD_HOURS;
  81. }
  82. else
  83. {
  84. overtime = 0.0;
  85. }
  86.  
  87. return overtime;
  88. }
  89.  
  90. // function: calcGross
  91. float calcGross(float wageRate, float hours, float overtimeHrs)
  92. {
  93. float normalPay;
  94. float overtimePay;
  95. float totalGross;
  96.  
  97. if (hours > STD_HOURS)
  98. {
  99. normalPay = STD_HOURS * wageRate;
  100. overtimePay = overtimeHrs * wageRate * OT_RATE;
  101. }
  102. else
  103. {
  104. normalPay = hours * wageRate;
  105. overtimePay = 0.0;
  106. }
  107.  
  108. totalGross = normalPay + overtimePay;
  109.  
  110. return totalGross;
  111. }
  112.  
  113. // function: printHeader
  114. void printHeader(void)
  115. {
  116. printf("\n");
  117. printf("Clock# Wage Hours OT Gross\n");
  118. printf("------------------------------------\n");
  119. }
  120.  
  121. // function: printEmp data
  122. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay)
  123. {
  124. printf("%06ld %6.2f %5.1f %5.1f %8.2f\n",
  125. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  126. }
  127.  
Success #stdin #stdout 0s 5316KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter number of hours worked for employee 098401: Enter number of hours worked for employee 526488: Enter number of hours worked for employee 765349: Enter number of hours worked for employee 034645: Enter number of hours worked for employee 127615: 
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