fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. // Constants
  7. #define STD_HOURS 40.0
  8. #define OT_RATE 1.5
  9. #define MA_TAX_RATE 0.05
  10. #define NH_TAX_RATE 0.0
  11. #define VT_TAX_RATE 0.06
  12. #define CA_TAX_RATE 0.07
  13. #define DEFAULT_STATE_TAX_RATE 0.08
  14. #define TAX_STATE_SIZE 3
  15. #define FED_TAX_RATE 0.25
  16. #define FIRST_NAME_SIZE 10
  17. #define LAST_NAME_SIZE 10
  18.  
  19. // Macros
  20. #define CALC_OT_HOURS(h) ((h) > STD_HOURS ? (h) - STD_HOURS : 0)
  21. #define CALC_STATE_TAX(p,r) ((p) * (r))
  22. #define CALC_FED_TAX(p) ((p) * FED_TAX_RATE)
  23. #define CALC_NET_PAY(p,st,ft) ((p) - ((st) + (ft)))
  24. #define CALC_NORMAL_PAY(w,h,oh) ((w) * ((h) - (oh)))
  25. #define CALC_OT_PAY(w,oh) ((oh) * (OT_RATE * (w)))
  26. #define CALC_MIN(v,c) ((v) < (c) ? (v) : (c))
  27. #define CALC_MAX(v,c) ((v) > (c) ? (v) : (c))
  28.  
  29. // Structure declarations
  30. typedef struct {
  31. char firstName[FIRST_NAME_SIZE];
  32. char lastName[LAST_NAME_SIZE];
  33. } Name;
  34.  
  35. typedef struct employee {
  36. Name empName;
  37. char taxState[TAX_STATE_SIZE];
  38. long clockNumber;
  39. float wageRate;
  40. float hours;
  41. float overtimeHrs;
  42. float grossPay;
  43. float stateTax;
  44. float fedTax;
  45. float netPay;
  46. struct employee *next;
  47. } Employee;
  48.  
  49. typedef struct {
  50. float total_wageRate;
  51. float total_hours;
  52. float total_overtimeHrs;
  53. float total_grossPay;
  54. float total_stateTax;
  55. float total_fedTax;
  56. float total_netPay;
  57. } Totals;
  58.  
  59. typedef struct {
  60. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay;
  61. float min_stateTax, min_fedTax, min_netPay;
  62. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay;
  63. float max_stateTax, max_fedTax, max_netPay;
  64. } MinMax;
  65.  
  66. // Function prototypes
  67. Employee *getEmployees();
  68. int countEmployees(Employee *head);
  69. void calculateOvertime(Employee *head);
  70. void calculateGrossPay(Employee *head);
  71. void calculateStateTax(Employee *head);
  72. void calculateFederalTax(Employee *head);
  73. void calculateNetPay(Employee *head);
  74. void calculateTotals(Employee *head, Totals *totals);
  75. void calculateMinMax(Employee *head, MinMax *mm);
  76. void printHeader();
  77. void printEmployee(Employee *head);
  78. void printStatistics(Totals *totals, MinMax *mm, int count);
  79. void freeEmployees(Employee *head);
  80.  
  81. int main() {
  82. Employee *head = NULL;
  83. Totals totals = {0};
  84. MinMax minmax = {0};
  85. int employeeCount = 0;
  86.  
  87. head = getEmployees();
  88. employeeCount = countEmployees(head);
  89.  
  90. if (employeeCount <= 0) {
  91. printf("\nNo employees to process\n");
  92. return 0;
  93. }
  94.  
  95. calculateOvertime(head);
  96. calculateGrossPay(head);
  97. calculateStateTax(head);
  98. calculateFederalTax(head);
  99. calculateNetPay(head);
  100. calculateTotals(head, &totals);
  101. calculateMinMax(head, &minmax);
  102.  
  103. printHeader();
  104. printEmployee(head);
  105. printStatistics(&totals, &minmax, employeeCount);
  106.  
  107. freeEmployees(head);
  108. printf("\nProgram completed successfully\n");
  109. return 0;
  110. }
  111.  
  112.  
  113. //Input Functions with Error Checking for ideone compatibility
  114.  
  115. Employee *getEmployees() {
  116. Employee *head = NULL, *current = NULL, *prev = NULL;
  117. char choice[4];
  118. int first = 1;
  119.  
  120. while(1) {
  121. Employee *newEmp = (Employee *)malloc(sizeof(Employee));
  122. if(!newEmp) {
  123. printf("Memory allocation failed!\n");
  124. exit(1);
  125. }
  126.  
  127. // Input validation for all fields
  128. printf("\nEnter first name: ");
  129. if(scanf("%9s", newEmp->empName.firstName) != 1) {
  130. printf("Invalid input!\n");
  131. free(newEmp);
  132. exit(1);
  133. }
  134.  
  135. printf("Enter last name: ");
  136. if(scanf("%9s", newEmp->empName.lastName) != 1) {
  137. printf("Invalid input!\n");
  138. free(newEmp);
  139. exit(1);
  140. }
  141.  
  142. printf("Enter tax state (2 chars): ");
  143. if(scanf("%2s", newEmp->taxState) != 1) {
  144. printf("Invalid input!\n");
  145. free(newEmp);
  146. exit(1);
  147. }
  148.  
  149. printf("Enter clock number: ");
  150. if(scanf("%ld", &newEmp->clockNumber) != 1) {
  151. printf("Invalid input!\n");
  152. free(newEmp);
  153. exit(1);
  154. }
  155.  
  156. printf("Enter hourly wage: ");
  157. if(scanf("%f", &newEmp->wageRate) != 1) {
  158. printf("Invalid input!\n");
  159. free(newEmp);
  160. exit(1);
  161. }
  162.  
  163. printf("Enter hours worked: ");
  164. if(scanf("%f", &newEmp->hours) != 1) {
  165. printf("Invalid input!\n");
  166. free(newEmp);
  167. exit(1);
  168. }
  169.  
  170. // Initialize pointers
  171. newEmp->next = NULL;
  172. if(first) {
  173. head = current = newEmp;
  174. first = 0;
  175. } else {
  176. prev->next = newEmp;
  177. current = newEmp;
  178. }
  179. prev = current;
  180.  
  181. // Add another employee?
  182. printf("\nAdd another employee? (yes/no): ");
  183. if(scanf("%3s", choice) != 1) {
  184. printf("Invalid input!\n");
  185. freeEmployees(head);
  186. exit(1);
  187. }
  188. if(toupper(choice[0]) != 'Y') break;
  189. }
  190. return head;
  191. }
  192.  
  193. void calculateOvertime(Employee *head) {
  194. while(head) {
  195. head->overtimeHrs = CALC_OT_HOURS(head->hours);
  196. head = head->next;
  197. }
  198. }
  199.  
  200. void calculateGrossPay(Employee *head) {
  201. while(head) {
  202. float normal = CALC_NORMAL_PAY(head->wageRate, head->hours, head->overtimeHrs);
  203. float overtime = CALC_OT_PAY(head->wageRate, head->overtimeHrs);
  204. head->grossPay = normal + overtime;
  205. head = head->next;
  206. }
  207. }
  208.  
  209. void calculateStateTax(Employee *head) {
  210. while(head) {
  211. // Convert state code to uppercase
  212. for(int i = 0; i < 2; i++) {
  213. head->taxState[i] = toupper(head->taxState[i]);
  214. }
  215.  
  216. float rate;
  217. if(strcmp(head->taxState, "MA") == 0) rate = MA_TAX_RATE;
  218. else if(strcmp(head->taxState, "VT") == 0) rate = VT_TAX_RATE;
  219. else if(strcmp(head->taxState, "NH") == 0) rate = NH_TAX_RATE;
  220. else if(strcmp(head->taxState, "CA") == 0) rate = CA_TAX_RATE;
  221. else rate = DEFAULT_STATE_TAX_RATE;
  222.  
  223. head->stateTax = CALC_STATE_TAX(head->grossPay, rate);
  224. head = head->next;
  225. }
  226. }
  227.  
  228. void calculateFederalTax(Employee *head) {
  229. while(head) {
  230. head->fedTax = CALC_FED_TAX(head->grossPay);
  231. head = head->next;
  232. }
  233. }
  234.  
  235. void calculateNetPay(Employee *head) {
  236. while(head) {
  237. head->netPay = CALC_NET_PAY(head->grossPay, head->stateTax, head->fedTax);
  238. head = head->next;
  239. }
  240. }
  241.  
  242. //Calculations
  243.  
  244. void calculateTotals(Employee *head, Totals *t) {
  245. while(head) {
  246. t->total_wageRate += head->wageRate;
  247. t->total_hours += head->hours;
  248. t->total_overtimeHrs += head->overtimeHrs;
  249. t->total_grossPay += head->grossPay;
  250. t->total_stateTax += head->stateTax;
  251. t->total_fedTax += head->fedTax;
  252. t->total_netPay += head->netPay;
  253. head = head->next;
  254. }
  255. }
  256.  
  257. void calculateMinMax(Employee *head, MinMax *mm) {
  258. if(!head) return;
  259.  
  260. // Initialize with first employee
  261. mm->min_wageRate = mm->max_wageRate = head->wageRate;
  262. mm->min_hours = mm->max_hours = head->hours;
  263. mm->min_overtimeHrs = mm->max_overtimeHrs = head->overtimeHrs;
  264. mm->min_grossPay = mm->max_grossPay = head->grossPay;
  265. mm->min_stateTax = mm->max_stateTax = head->stateTax;
  266. mm->min_fedTax = mm->max_fedTax = head->fedTax;
  267. mm->min_netPay = mm->max_netPay = head->netPay;
  268.  
  269. // Process remaining employees
  270. while((head = head->next)) {
  271. mm->min_wageRate = CALC_MIN(head->wageRate, mm->min_wageRate);
  272. mm->max_wageRate = CALC_MAX(head->wageRate, mm->max_wageRate);
  273.  
  274. mm->min_hours = CALC_MIN(head->hours, mm->min_hours);
  275. mm->max_hours = CALC_MAX(head->hours, mm->max_hours);
  276.  
  277. mm->min_overtimeHrs = CALC_MIN(head->overtimeHrs, mm->min_overtimeHrs);
  278. mm->max_overtimeHrs = CALC_MAX(head->overtimeHrs, mm->max_overtimeHrs);
  279.  
  280. mm->min_grossPay = CALC_MIN(head->grossPay, mm->min_grossPay);
  281. mm->max_grossPay = CALC_MAX(head->grossPay, mm->max_grossPay);
  282.  
  283. mm->min_stateTax = CALC_MIN(head->stateTax, mm->min_stateTax);
  284. mm->max_stateTax = CALC_MAX(head->stateTax, mm->max_stateTax);
  285.  
  286. mm->min_fedTax = CALC_MIN(head->fedTax, mm->min_fedTax);
  287. mm->max_fedTax = CALC_MAX(head->fedTax, mm->max_fedTax);
  288.  
  289. mm->min_netPay = CALC_MIN(head->netPay, mm->min_netPay);
  290. mm->max_netPay = CALC_MAX(head->netPay, mm->max_netPay);
  291. }
  292. }
  293.  
  294.  
  295. // Output Functions
  296.  
  297. void printHeader() {
  298. printf("\n\n*** Pay Calculator ***\n");
  299. printf("--------------------------------------------------------------\n");
  300. printf("%-20s %-6s %-10s %-6s %-5s %-8s %-8s %-8s\n",
  301. "Name", "State", "Clock#", "Wage", "Hours", "Gross", "State Tax",
  302. "Fed Tax", "Net Pay");
  303. printf("--------------------------------------------------------------\n");
  304. }
  305.  
  306. void printEmployee(Employee *head) {
  307. while(head) {
  308. char fullName[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
  309. snprintf(fullName, sizeof(fullName), "%s %s",
  310. head->empName.firstName, head->empName.lastName);
  311.  
  312. printf("%-20s %-6s %-10ld %-6.2f %-5.1f %-8.2f %-8.2f %-8.2f %-8.2f\n",
  313. fullName,
  314. head->taxState,
  315. head->clockNumber,
  316. head->wageRate,
  317. head->hours,
  318. head->grossPay,
  319. head->stateTax,
  320. head->fedTax,
  321. head->netPay);
  322.  
  323. head = head->next;
  324. }
  325. }
  326.  
  327. void printStatistics(Totals *t, MinMax *mm, int count) {
  328. printf("\n--- Payroll Statistics ---\n");
  329. printf("Totals:\n");
  330. printf(" Wages: $%.2f Hours: %.1f OT: %.1f\n",
  331. t->total_wageRate, t->total_hours, t->total_overtimeHrs);
  332. printf(" Gross: $%.2f State Tax: $%.2f Federal Tax: $%.2f Net: $%.2f\n",
  333. t->total_grossPay, t->total_stateTax, t->total_fedTax, t->total_netPay);
  334.  
  335. printf("\nAverages (%d employees):\n", count);
  336. printf(" Wage: $%.2f/hr Hours: %.1f OT: %.1f\n",
  337. t->total_wageRate/count, t->total_hours/count, t->total_overtimeHrs/count);
  338. printf(" Gross: $%.2f Net: $%.2f\n",
  339. t->total_grossPay/count, t->total_netPay/count);
  340.  
  341. printf("\nMinimums:\n");
  342. printf(" Wage: $%.2f Hours: %.1f Gross: $%.2f Net: $%.2f\n",
  343. mm->min_wageRate, mm->min_hours, mm->min_grossPay, mm->min_netPay);
  344.  
  345. printf("\nMaximums:\n");
  346. printf(" Wage: $%.2f Hours: %.1f Gross: $%.2f Net: $%.2f\n",
  347. mm->max_wageRate, mm->max_hours, mm->max_grossPay, mm->max_netPay);
  348. }
  349.  
  350. /********************************************************
  351. * Utility Functions
  352. ********************************************************/
  353. int countEmployees(Employee *head) {
  354. int count = 0;
  355. while(head) {
  356. count++;
  357. head = head->next;
  358. }
  359. return count;
  360. }
  361.  
  362. void freeEmployees(Employee *head) {
  363. while(head) {
  364. Employee *temp = head;
  365. head = head->next;
  366. free(temp);
  367. }
  368. }
Success #stdin #stdout 0.01s 5296KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Y
Mary
Apl
NH
526488
9.75
42.5
Y
Frank
Fortran
VT
765349
10.50
37.0
Y
Jeff
Ada
NY
34645
12.25
45
Y
Anton
Pascal
CA
127615
8.35
40.0
N
stdout
Enter first name: Enter last name: Enter tax state (2 chars): Enter clock number: Enter hourly wage: Enter hours worked: 
Add another employee? (yes/no): 
Enter first name: Enter last name: Enter tax state (2 chars): Enter clock number: Enter hourly wage: Enter hours worked: 
Add another employee? (yes/no): 
Enter first name: Enter last name: Enter tax state (2 chars): Enter clock number: Enter hourly wage: Enter hours worked: 
Add another employee? (yes/no): 
Enter first name: Enter last name: Enter tax state (2 chars): Enter clock number: Enter hourly wage: Enter hours worked: 
Add another employee? (yes/no): 
Enter first name: Enter last name: Enter tax state (2 chars): Enter clock number: Enter hourly wage: Enter hours worked: 
Add another employee? (yes/no): 

*** Pay Calculator ***
--------------------------------------------------------------
Name                 State  Clock#     Wage   Hours Gross    State Tax Fed Tax 
--------------------------------------------------------------
Connie Cobol         MA     98401      10.60  51.0  598.90   29.95    149.73   419.23  
Mary Apl             NH     526488     9.75   42.5  426.56   0.00     106.64   319.92  
Frank Fortran        VT     765349     10.50  37.0  388.50   23.31    97.12    268.07  
Jeff Ada             NY     34645      12.25  45.0  581.88   46.55    145.47   389.86  
Anton Pascal         CA     127615     8.35   40.0  334.00   23.38    83.50    227.12  

--- Payroll Statistics ---
Totals:
 Wages: $51.45  Hours: 215.5  OT: 18.5
 Gross: $2329.84  State Tax: $123.18  Federal Tax: $582.46  Net: $1624.19

Averages (5 employees):
 Wage: $10.29/hr  Hours: 43.1  OT: 3.7
 Gross: $465.97  Net: $324.84

Minimums:
 Wage: $8.35  Hours: 37.0  Gross: $334.00  Net: $227.12

Maximums:
 Wage: $12.25  Hours: 51.0  Gross: $598.90  Net: $419.23

Program completed successfully