fork download
  1. #include <stdio.h>
  2.  
  3. // constants
  4. #define SIZE 5
  5. #define OVERTIME_RATE 1.5f
  6. #define STD_WORK_WEEK 40.0f
  7.  
  8. // function prototypes
  9. float getHours(long int clockNumber);
  10. void printHeader(void);
  11. void printEmp(long int clockNumber, float wageRate, float hours,
  12. float overtimeHrs, float grossPay);
  13. float calcOvertimeHrs(float hours);
  14. float calcGrossPay(float hours, float wageRate, float overtimeHrs);
  15.  
  16. int main()
  17. {
  18. /* Variable Declarations */
  19. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615};
  20. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35};
  21. float hours[SIZE];
  22. float overtimeHrs[SIZE];
  23. float grossPay[SIZE];
  24. int i;
  25.  
  26. // process each employee
  27. for (i = 0; i < SIZE; ++i)
  28. {
  29. // Read in hours for employee
  30. hours[i] = getHours(clockNumber[i]);
  31.  
  32. // Calculate overtime hours
  33. overtimeHrs[i] = calcOvertimeHrs(hours[i]);
  34.  
  35. // Calculate gross pay
  36. grossPay[i] = calcGrossPay(hours[i], wageRate[i], overtimeHrs[i]);
  37. }
  38.  
  39. // print the header info
  40. printHeader();
  41.  
  42. // print out each employee
  43. for (i = 0; i < SIZE; ++i)
  44. {
  45. printEmp(clockNumber[i], wageRate[i], hours[i],
  46. overtimeHrs[i], grossPay[i]);
  47. }
  48.  
  49. return 0;
  50. }
  51.  
  52. //**************************************************************
  53. // Function: getHours
  54. // Purpose: Prompts user for hours worked by a specific employee
  55. //**************************************************************
  56. float getHours(long int clockNumber)
  57. {
  58. float hoursWorked;
  59. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  60. scanf("%f", &hoursWorked);
  61. return hoursWorked;
  62. }
  63.  
  64. //**************************************************************
  65. // Function: calcOvertimeHrs
  66. // Purpose: Calculates overtime hours worked in a week
  67. //**************************************************************
  68. float calcOvertimeHrs(float hours)
  69. {
  70. if (hours > STD_WORK_WEEK)
  71. return hours - STD_WORK_WEEK;
  72. else
  73. return 0.0f;
  74. }
  75.  
  76. //**************************************************************
  77. // Function: calcGrossPay
  78. // Purpose: Calculates total gross pay including overtime
  79. //**************************************************************
  80. float calcGrossPay(float hours, float wageRate, float overtimeHrs)
  81. {
  82. float grossPay;
  83.  
  84. if (hours > STD_WORK_WEEK)
  85. {
  86. grossPay = (STD_WORK_WEEK * wageRate) +
  87. (overtimeHrs * wageRate * OVERTIME_RATE);
  88. }
  89. else
  90. {
  91. grossPay = hours * wageRate;
  92. }
  93.  
  94. return grossPay;
  95. }
  96.  
  97. //**************************************************************
  98. // Function: printHeader
  99. // Purpose: Prints the initial table header information
  100. //**************************************************************
  101. void printHeader(void)
  102. {
  103. printf("\n\n*** Pay Calculator ***\n");
  104. printf("\nClock# Wage Hours OT Gross\n");
  105. printf("-----------------------------------------\n");
  106. }
  107.  
  108. //**************************************************************
  109. // Function: printEmp
  110. // Purpose: Prints a single employee’s payroll information
  111. //**************************************************************
  112. void printEmp(long int clockNumber, float wageRate, float hours,
  113. float overtimeHrs, float grossPay)
  114. {
  115. printf("%06li %5.2f %5.1f %5.1f %8.2f\n",
  116. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  117. }
  118.  
Success #stdin #stdout 0s 5296KB
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