fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: <replace with your name>
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: <replace with the current date>
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. // necessary header files
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <stdlib.h> // for exit()
  31.  
  32. // define constants
  33. #define SIZE 5
  34. #define STD_HOURS 40.0
  35. #define OT_RATE 1.5
  36. #define MA_TAX_RATE 0.05
  37. #define NH_TAX_RATE 0.0
  38. #define VT_TAX_RATE 0.06
  39. #define CA_TAX_RATE 0.07
  40. #define DEFAULT_TAX_RATE 0.08
  41. #define NAME_SIZE 20
  42. #define TAX_STATE_SIZE 3
  43. #define FED_TAX_RATE 0.25
  44. #define FIRST_NAME_SIZE 10
  45. #define LAST_NAME_SIZE 10
  46.  
  47. // Define a structure type to store an employee name
  48. struct name
  49. {
  50. char firstName[FIRST_NAME_SIZE];
  51. char lastName [LAST_NAME_SIZE];
  52. };
  53.  
  54. // Define a structure type to pass employee data between functions
  55. struct employee
  56. {
  57. struct name empName;
  58. char taxState [TAX_STATE_SIZE];
  59. long int clockNumber;
  60. float wageRate;
  61. float hours;
  62. float overtimeHrs;
  63. float grossPay;
  64. float stateTax;
  65. float fedTax;
  66. float netPay;
  67. };
  68.  
  69. // Function prototypes
  70. void inputData(struct employee *empPtr);
  71. void calculatePay(struct employee *empPtr);
  72. void calculateTaxes(struct employee *empPtr);
  73. void outputData(const struct employee *empPtr);
  74. void calculateStats(const struct employee *emps, float *totalGrossPayPtr, float *avgGrossPayPtr,
  75. float *minGrossPayPtr, float *maxGrossPayPtr);
  76. void outputStats(float totalGrossPay, float avgGrossPay, float minGrossPay, float maxGrossPay);
  77.  
  78. int main()
  79. {
  80. struct employee employees[SIZE];
  81. float totalGrossPay = 0.0, avgGrossPay = 0.0, minGrossPay = 0.0, maxGrossPay = 0.0;
  82. int i;
  83.  
  84. // Loop to input data for each employee
  85. for (i = 0; i < SIZE; i++)
  86. {
  87. printf("--- Enter data for Employee %d ---\n", i + 1);
  88. inputData(&employees[i]);
  89. calculatePay(&employees[i]);
  90. calculateTaxes(&employees[i]);
  91. }
  92.  
  93. // Output header
  94. printf("\n\n------------------------------------------------------------------------------------------------------------------\n");
  95. printf("| Name | Clock # | Wage | Hours | OT Hrs | Gross Pay | State Tax | Fed Tax | Net Pay |\n");
  96. printf("------------------------------------------------------------------------------------------------------------------\n");
  97.  
  98. // Loop to output data for each employee
  99. for (i = 0; i < SIZE; i++)
  100. {
  101. outputData(&employees[i]);
  102. }
  103. printf("------------------------------------------------------------------------------------------------------------------\n");
  104.  
  105. // Calculate and output statistics
  106. calculateStats(employees, &totalGrossPay, &avgGrossPay, &minGrossPay, &maxGrossPay);
  107. outputStats(totalGrossPay, avgGrossPay, minGrossPay, maxGrossPay);
  108.  
  109. return 0;
  110. }
  111.  
  112. // Function to input employee data using pointers
  113. void inputData(struct employee *empPtr)
  114. {
  115. printf("Enter first name: ");
  116. scanf("%s", empPtr->empName.firstName);
  117. printf("Enter last name: ");
  118. scanf("%s", empPtr->empName.lastName);
  119. printf("Enter tax state (MA, NH, VT, CA, or Other): ");
  120. scanf("%s", empPtr->taxState);
  121. printf("Enter clock number: ");
  122. scanf("%ld", &empPtr->clockNumber);
  123. printf("Enter wage rate: ");
  124. scanf("%f", &empPtr->wageRate);
  125. printf("Enter hours worked: ");
  126. scanf("%f", &empPtr->hours);
  127. }
  128.  
  129. // Function to calculate overtime hours and gross pay using pointers
  130. void calculatePay(struct employee *empPtr)
  131. {
  132. if (empPtr->hours > STD_HOURS)
  133. {
  134. empPtr->overtimeHrs = empPtr->hours - STD_HOURS;
  135. empPtr->grossPay = (STD_HOURS * empPtr->wageRate) +
  136. (empPtr->overtimeHrs * empPtr->wageRate * OT_RATE);
  137. }
  138. else
  139. {
  140. empPtr->overtimeHrs = 0.0;
  141. empPtr->grossPay = empPtr->hours * empPtr->wageRate;
  142. }
  143. }
  144.  
  145. // Function to calculate taxes using pointers and string comparison
  146. void calculateTaxes(struct employee *empPtr)
  147. {
  148. float stateTaxRate;
  149.  
  150. // Convert state input to uppercase for comparison
  151. empPtr->taxState[0] = toupper(empPtr->taxState[0]);
  152. empPtr->taxState[1] = toupper(empPtr->taxState[1]);
  153.  
  154. if (strcmp(empPtr->taxState, "MA") == 0)
  155. {
  156. stateTaxRate = MA_TAX_RATE;
  157. }
  158. else if (strcmp(empPtr->taxState, "NH") == 0)
  159. {
  160. stateTaxRate = NH_TAX_RATE;
  161. }
  162. else if (strcmp(empPtr->taxState, "VT") == 0)
  163. {
  164. stateTaxRate = VT_TAX_RATE;
  165. }
  166. else if (strcmp(empPtr->taxState, "CA") == 0)
  167. {
  168. stateTaxRate = CA_TAX_RATE;
  169. }
  170. else
  171. {
  172. stateTaxRate = DEFAULT_TAX_RATE;
  173. }
  174.  
  175. empPtr->stateTax = empPtr->grossPay * stateTaxRate;
  176. empPtr->fedTax = empPtr->grossPay * FED_TAX_RATE;
  177. empPtr->netPay = empPtr->grossPay - empPtr->stateTax - empPtr->fedTax;
  178. }
  179.  
  180. // Function to output employee data using pointers
  181. void outputData(const struct employee *empPtr)
  182. {
  183. char fullName[NAME_SIZE];
  184. sprintf(fullName, "%s %s", empPtr->empName.firstName, empPtr->empName.lastName);
  185.  
  186. printf("| %-20s | %-9ld | %-4.2f | %-5.2f | %-6.2f | %-9.2f | %-9.2f | %-9.2f | %-9.2f |\n",
  187. fullName,
  188. empPtr->clockNumber,
  189. empPtr->wageRate,
  190. empPtr->hours,
  191. empPtr->overtimeHrs,
  192. empPtr->grossPay,
  193. empPtr->stateTax,
  194. empPtr->fedTax,
  195. empPtr->netPay);
  196. }
  197.  
  198. // Function to calculate total, average, min, and max gross pay using pointers
  199. void calculateStats(const struct employee *emps, float *totalGrossPayPtr, float *avgGrossPayPtr,
  200. float *minGrossPayPtr, float *maxGrossPayPtr)
  201. {
  202. int i;
  203. *totalGrossPayPtr = 0.0;
  204. *minGrossPayPtr = emps[0].grossPay; // Initialize min/max with the first employee's data
  205. *maxGrossPayPtr = emps[0].grossPay;
  206.  
  207. for (i = 0; i < SIZE; i++)
  208. {
  209. *totalGrossPayPtr += emps[i].grossPay;
  210.  
  211. if (emps[i].grossPay < *minGrossPayPtr)
  212. {
  213. *minGrossPayPtr = emps[i].grossPay;
  214. }
  215.  
  216. if (emps[i].grossPay > *maxGrossPayPtr)
  217. {
  218. *maxGrossPayPtr = emps[i].grossPay;
  219. }
  220. }
  221.  
  222. *avgGrossPayPtr = *totalGrossPayPtr / SIZE;
  223. }
  224.  
  225. // Function to output statistics
  226. void outputStats(float totalGrossPay, float avgGrossPay, float minGrossPay, float maxGrossPay)
  227. {
  228. printf("\nPayroll Statistics:\n");
  229. printf("Total Gross Pay: $%.2f\n", totalGrossPay);
  230. printf("Average Gross Pay: $%.2f\n", avgGrossPay);
  231. printf("Minimum Gross Pay: $%.2f\n", minGrossPay);
  232. printf("Maximum Gross Pay: $%.2f\n", maxGrossPay);
  233. }
  234.  
Success #stdin #stdout 0s 5288KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
--- Enter data for Employee 1 ---
Enter first name: Enter last name: Enter tax state (MA, NH, VT, CA, or Other): Enter clock number: Enter wage rate: Enter hours worked: --- Enter data for Employee 2 ---
Enter first name: Enter last name: Enter tax state (MA, NH, VT, CA, or Other): Enter clock number: Enter wage rate: Enter hours worked: --- Enter data for Employee 3 ---
Enter first name: Enter last name: Enter tax state (MA, NH, VT, CA, or Other): Enter clock number: Enter wage rate: Enter hours worked: --- Enter data for Employee 4 ---
Enter first name: Enter last name: Enter tax state (MA, NH, VT, CA, or Other): Enter clock number: Enter wage rate: Enter hours worked: --- Enter data for Employee 5 ---
Enter first name: Enter last name: Enter tax state (MA, NH, VT, CA, or Other): Enter clock number: Enter wage rate: Enter hours worked: 

------------------------------------------------------------------------------------------------------------------
| Name                 | Clock #   | Wage | Hours | OT Hrs | Gross Pay | State Tax | Fed Tax   | Net Pay   |
------------------------------------------------------------------------------------------------------------------
| 51.0 42.5            | 45        | 0.00 | 40.00 | 0.00   | 0.00      | 0.00      | 0.00      | 0.00      |
|  ���                | 0         | 0.00 | 0.00  | 0.00   | 0.00      | 0.00      | 0.00      | 0.00      |
| �� ���          | 0         | 0.00 | 0.00  | 0.00   | 0.00      | 0.00      | 0.00      | 0.00      |
| �Ҹ�� 2          | 0         | -1699170431857645772359469633902739456.00 | 0.00  | 0.00   | -0.00     | -0.00     | -0.00     | -0.00     |
|  �                   | 140724530285190 | 0.00 | 0.00  | 0.00   | 0.00      | 0.00      | 0.00      | 0.00      |
------------------------------------------------------------------------------------------------------------------

Payroll Statistics:
Total Gross Pay: $-0.00
Average Gross Pay: $-0.00
Minimum Gross Pay: $-0.00
Maximum Gross Pay: $0.00