fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h> // for char functions
  4. #include <stdlib.h> // for malloc
  5.  
  6. // define 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_TAX_RATE 0.08
  14. #define NAME_SIZE 20
  15. #define TAX_STATE_SIZE 3
  16. #define FED_TAX_RATE 0.25
  17. #define FIRST_NAME_SIZE 10
  18. #define LAST_NAME_SIZE 10
  19.  
  20. // Define a global structure type to store an employee name
  21. struct name {
  22. char firstName[FIRST_NAME_SIZE];
  23. char lastName [LAST_NAME_SIZE];
  24. };
  25.  
  26. // Define a global structure type to pass employee data between functions
  27. struct employee {
  28. struct name empName;
  29. char taxState[TAX_STATE_SIZE];
  30. long int clockNumber;
  31. float wageRate;
  32. float hours;
  33. float overtimeHrs;
  34. float grossPay;
  35. float stateTax;
  36. float fedTax;
  37. float netPay;
  38. struct employee * next;
  39. };
  40.  
  41. // this structure type defines the totals of all floating point items
  42. struct totals {
  43. float total_wageRate;
  44. float total_hours;
  45. float total_overtimeHrs;
  46. float total_grossPay;
  47. float total_stateTax;
  48. float total_fedTax;
  49. float total_netPay;
  50. };
  51.  
  52. // this structure type defines the min and max values of all floating point items
  53. struct min_max {
  54. float min_wageRate;
  55. float min_hours;
  56. float min_overtimeHrs;
  57. float min_grossPay;
  58. float min_stateTax;
  59. float min_fedTax;
  60. float min_netPay;
  61. float max_wageRate;
  62. float max_hours;
  63. float max_overtimeHrs;
  64. float max_grossPay;
  65. float max_stateTax;
  66. float max_fedTax;
  67. float max_netPay;
  68. };
  69.  
  70. // Function prototypes
  71. struct employee * getEmpData (void);
  72. int isEmployeeSize (struct employee * head_ptr);
  73. void calcOvertimeHrs (struct employee * head_ptr);
  74. void calcGrossPay (struct employee * head_ptr);
  75. void printHeader (void);
  76. void printEmp (struct employee * head_ptr);
  77. void calcStateTax (struct employee * head_ptr);
  78. void calcFedTax (struct employee * head_ptr);
  79. void calcNetPay (struct employee * head_ptr);
  80. void calcEmployeeTotals (struct employee * head_ptr, struct totals * emp_totals_ptr);
  81. void calcEmployeeMinMax (struct employee * head_ptr, struct min_max * emp_minMax_ptr);
  82. void printEmpStatistics (struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize);
  83.  
  84. int main () {
  85. struct employee * head_ptr;
  86. int theSize;
  87. struct totals employeeTotals = {0,0,0,0,0,0,0};
  88. struct totals * emp_totals_ptr = &employeeTotals;
  89. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  90. struct min_max * emp_minMax_ptr = &employeeMinMax;
  91.  
  92. head_ptr = getEmpData();
  93.  
  94. theSize = isEmployeeSize(head_ptr);
  95.  
  96. if (theSize <= 0) {
  97. printf("\n\n**** There was no employee input to process ***\n");
  98. } else {
  99. calcOvertimeHrs(head_ptr);
  100. calcGrossPay(head_ptr);
  101. calcStateTax(head_ptr);
  102. calcFedTax(head_ptr);
  103. calcNetPay(head_ptr);
  104. calcEmployeeTotals(head_ptr, &employeeTotals);
  105. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  106. printHeader();
  107. printEmp(head_ptr);
  108. printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);
  109. }
  110.  
  111. printf("\n\n *** End of Program *** \n");
  112. return 0;
  113. }
  114.  
  115. // getEmpData() implementation remains unchanged
  116. struct employee * getEmpData (void) {
  117. char answer[80];
  118. int more_data = 1;
  119. char value;
  120. struct employee *current_ptr, *head_ptr;
  121.  
  122. head_ptr = (struct employee *) malloc(sizeof(struct employee));
  123. current_ptr = head_ptr;
  124.  
  125. while (more_data) {
  126. printf("\nEnter employee first name: ");
  127. (void)scanf("%s", current_ptr->empName.firstName);
  128. printf("\nEnter employee last name: ");
  129. (void)scanf("%s", current_ptr->empName.lastName);
  130. printf("\nEnter employee two character tax state: ");
  131. (void)scanf("%s", current_ptr->taxState);
  132. printf("\nEnter employee clock number: ");
  133. (void)scanf("%li", &current_ptr->clockNumber);
  134. printf("\nEnter employee hourly wage: ");
  135. (void)scanf("%f", &current_ptr->wageRate);
  136. printf("\nEnter hours worked this week: ");
  137. (void)scanf("%f", &current_ptr->hours);
  138. printf("\nWould you like to add another employee? (y/n): ");
  139. (void)scanf("%s", answer);
  140. if ((value = toupper(answer[0])) != 'Y') {
  141. current_ptr->next = NULL;
  142. more_data = 0;
  143. } else {
  144. current_ptr->next = (struct employee *) malloc(sizeof(struct employee));
  145. current_ptr = current_ptr->next;
  146. }
  147. }
  148. return head_ptr;
  149. }
  150.  
  151. // isEmployeeSize() implementation remains unchanged
  152. int isEmployeeSize (struct employee * head_ptr) {
  153. struct employee * current_ptr;
  154. int theSize = 0;
  155. if (head_ptr->empName.firstName[0] != '\0') {
  156. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  157. ++theSize;
  158. }
  159. }
  160. return theSize;
  161. }
  162.  
  163. // printHeader() implementation remains unchanged
  164. void printHeader (void) {
  165. printf("\n\n*** Pay Calculator ***\n");
  166. printf("\n--------------------------------------------------------------");
  167. printf("-------------------");
  168. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  169. printf(" Pay Tax Tax Pay");
  170. printf("\n State Pay ");
  171. printf(" Tax Tax Pay");
  172. printf("\n--------------------------------------------------------------");
  173. printf("-------------------");
  174. }
  175.  
  176. // printEmp() implementation remains unchanged
  177. void printEmp (struct employee * head_ptr) {
  178. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  179. struct employee * current_ptr;
  180. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  181. strcpy(name, current_ptr->empName.firstName);
  182. strcat(name, " ");
  183. strcat(name, current_ptr->empName.lastName);
  184. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  185. name, current_ptr->taxState, current_ptr->clockNumber,
  186. current_ptr->wageRate, current_ptr->hours,
  187. current_ptr->overtimeHrs, current_ptr->grossPay,
  188. current_ptr->stateTax, current_ptr->fedTax,
  189. current_ptr->netPay);
  190. }
  191. }
  192.  
  193. // calcOvertimeHrs() implementation remains unchanged
  194. void calcOvertimeHrs (struct employee * head_ptr) {
  195. struct employee * current_ptr;
  196. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  197. if (current_ptr->hours >= STD_HOURS) {
  198. current_ptr->overtimeHrs = current_ptr->hours - STD_HOURS;
  199. } else {
  200. current_ptr->overtimeHrs = 0;
  201. }
  202. }
  203. }
  204.  
  205. // calcGrossPay() implementation remains unchanged
  206. void calcGrossPay (struct employee * head_ptr) {
  207. float theNormalPay;
  208. float theOvertimePay;
  209. struct employee * current_ptr;
  210. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  211. theNormalPay = current_ptr->wageRate * (current_ptr->hours - current_ptr->overtimeHrs);
  212. theOvertimePay = current_ptr->overtimeHrs * (OT_RATE * current_ptr->wageRate);
  213. current_ptr->grossPay = theNormalPay + theOvertimePay;
  214. }
  215. }
  216.  
  217. // calcStateTax() with debug prints
  218. void calcStateTax (struct employee * head_ptr) {
  219. struct employee * current_ptr;
  220. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  221. if (islower(current_ptr->taxState[0]))
  222. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  223. if (islower(current_ptr->taxState[1]))
  224. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  225.  
  226. float stateTaxRate;
  227. if (strcmp(current_ptr->taxState, "MA") == 0)
  228. stateTaxRate = MA_TAX_RATE;
  229. else if (strcmp(current_ptr->taxState, "NH") == 0)
  230. stateTaxRate = NH_TAX_RATE;
  231. else if (strcmp(current_ptr->taxState, "VT") == 0)
  232. stateTaxRate = VT_TAX_RATE;
  233. else if (strcmp(current_ptr->taxState, "NY") == 0)
  234. stateTaxRate = 0.10;
  235. else if (strcmp(current_ptr->taxState, "CA") == 0)
  236. stateTaxRate = CA_TAX_RATE;
  237. else
  238. stateTaxRate = DEFAULT_TAX_RATE;
  239.  
  240. current_ptr->stateTax = current_ptr->grossPay * stateTaxRate;
  241.  
  242. // Debug print
  243. printf("DEBUG: %s (%s) Gross: %.2f, StateTax Rate: %.2f, Calculated StateTax: %.2f\n",
  244. current_ptr->empName.firstName, current_ptr->taxState,
  245. current_ptr->grossPay, stateTaxRate, current_ptr->stateTax);
  246. }
  247. }
  248.  
  249. // calcFedTax() with debug prints
  250. void calcFedTax (struct employee * head_ptr) {
  251. struct employee * current_ptr;
  252. for (current_ptr = head_ptr; current_ptr != NULL; current_ptr = current_ptr->next) {
  253. if (current_ptr->grossPay <= 400)
  254. current_ptr->fedTax = current_ptr->grossPay * 0.10;
  255. else
  256. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  257.  
  258. // Debug print
  259. printf("DEBUG: %s Gross: %.2f, FedTax: %.2f\n",
  260. current_ptr->empName.firstName, current_ptr->grossPay, current_ptr->fedTax);
  261. }
  262. }
  263.  
  264. // calcNetPay() implementation remains unchanged
  265. void calcNetPay (struct employee * head_ptr) {
  266. struct employee * current_ptr;
  267. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  268. float theTotalTaxes = current_ptr->stateTax + current_ptr->fedTax;
  269. current_ptr->netPay = current_ptr->grossPay - theTotalTaxes;
  270. }
  271. }
  272.  
  273. // calcEmployeeTotals() with debug prints
  274. void calcEmployeeTotals (struct employee * head_ptr, struct totals * emp_totals_ptr) {
  275. struct employee * current_ptr;
  276. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  277. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  278. emp_totals_ptr->total_hours += current_ptr->hours;
  279. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  280. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  281. // Debug print
  282. printf("DEBUG: %s totals so far: StateTax=%.2f, FedTax=%.2f\n",
  283. current_ptr->empName.firstName, current_ptr->stateTax, current_ptr->fedTax);
  284. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  285. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  286. emp_totals_ptr->total_netPay += current_ptr->netPay;
  287. }
  288. }
  289.  
  290. // calcEmployeeMinMax() implementation remains unchanged
  291. void calcEmployeeMinMax (struct employee * head_ptr, struct min_max * emp_minMax_ptr) {
  292. struct employee * current_ptr;
  293. current_ptr = head_ptr;
  294.  
  295. // Set initial min and max from first employee
  296. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  297. emp_minMax_ptr->min_hours = current_ptr->hours;
  298. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  299. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  300. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  301. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  302. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  303.  
  304. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  305. emp_minMax_ptr->max_hours = current_ptr->hours;
  306. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  307. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  308. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  309. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  310. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  311.  
  312. current_ptr = current_ptr->next;
  313.  
  314. for (; current_ptr; current_ptr = current_ptr->next) {
  315. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  316. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  317. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  318. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  319.  
  320. if (current_ptr->hours < emp_minMax_ptr->min_hours)
  321. emp_minMax_ptr->min_hours = current_ptr->hours;
  322. if (current_ptr->hours > emp_minMax_ptr->max_hours)
  323. emp_minMax_ptr->max_hours = current_ptr->hours;
  324.  
  325. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  326. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  327. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  328. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  329.  
  330. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  331. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  332. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  333. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  334.  
  335. // Debug for state tax min/max
  336. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  337. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  338. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  339. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  340.  
  341. // Debug for federal tax min/max
  342. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  343. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  344. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  345. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  346.  
  347. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  348. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  349. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  350. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  351. }
  352. }
  353.  
  354. // printEmpStatistics() implementation
  355. void printEmpStatistics (struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize) {
  356. printf("\n--------------------------------------------------------------");
  357. printf("-------------------");
  358. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  359. emp_totals_ptr->total_wageRate,
  360. emp_totals_ptr->total_hours,
  361. emp_totals_ptr->total_overtimeHrs,
  362. emp_totals_ptr->total_grossPay,
  363. emp_totals_ptr->total_stateTax,
  364. emp_totals_ptr->total_fedTax,
  365. emp_totals_ptr->total_netPay);
  366.  
  367. if (theSize > 0) {
  368. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  369. emp_totals_ptr->total_wageRate / theSize,
  370. emp_totals_ptr->total_hours / theSize,
  371. emp_totals_ptr->total_overtimeHrs / theSize,
  372. emp_totals_ptr->total_grossPay / theSize,
  373. emp_totals_ptr->total_stateTax / theSize,
  374. emp_totals_ptr->total_fedTax / theSize,
  375. emp_totals_ptr->total_netPay / theSize);
  376. }
  377.  
  378. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  379. emp_minMax_ptr->min_wageRate,
  380. emp_minMax_ptr->min_hours,
  381. emp_minMax_ptr->min_overtimeHrs,
  382. emp_minMax_ptr->min_grossPay,
  383. emp_minMax_ptr->min_stateTax,
  384. emp_minMax_ptr->min_fedTax,
  385. emp_minMax_ptr->min_netPay);
  386.  
  387. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  388. emp_minMax_ptr->max_wageRate,
  389. emp_minMax_ptr->max_hours,
  390. emp_minMax_ptr->max_overtimeHrs,
  391. emp_minMax_ptr->max_grossPay,
  392. emp_minMax_ptr->max_stateTax,
  393. emp_minMax_ptr->max_fedTax,
  394. emp_minMax_ptr->max_netPay);
  395.  
  396. printf("\n\nThe total employees processed was: %i\n", theSize);
  397. }
Success #stdin #stdout 0s 5328KB
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 employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): DEBUG: Connie (MA) Gross: 598.90, StateTax Rate: 0.05, Calculated StateTax: 29.95
DEBUG: Mary (NH) Gross: 426.56, StateTax Rate: 0.00, Calculated StateTax: 0.00
DEBUG: Frank (VT) Gross: 388.50, StateTax Rate: 0.06, Calculated StateTax: 23.31
DEBUG: Jeff (NY) Gross: 581.88, StateTax Rate: 0.10, Calculated StateTax: 58.19
DEBUG: Anton (CA) Gross: 334.00, StateTax Rate: 0.07, Calculated StateTax: 23.38
DEBUG: Connie Gross: 598.90, FedTax: 149.73
DEBUG: Mary Gross: 426.56, FedTax: 106.64
DEBUG: Frank Gross: 388.50, FedTax: 38.85
DEBUG: Jeff Gross: 581.88, FedTax: 145.47
DEBUG: Anton Gross: 334.00, FedTax: 33.40
DEBUG: Connie totals so far: StateTax=29.95, FedTax=149.73
DEBUG: Mary totals so far: StateTax=0.00, FedTax=106.64
DEBUG: Frank totals so far: StateTax=23.31, FedTax=38.85
DEBUG: Jeff totals so far: StateTax=58.19, FedTax=145.47
DEBUG: Anton totals so far: StateTax=23.38, FedTax=33.40


*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net                    Pay     Tax    Tax      Pay
                   State                           Pay       Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   38.85   326.34
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  58.19  145.47   378.22
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   33.40   277.22
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 134.82  474.08  1720.93
Averages:                       10.29  43.1   3.7  465.97  26.96   94.82   344.19
Minimum:                         8.35  37.0   0.0  334.00   0.00   33.40   277.22
Maximum:                        12.25  51.0  11.0  598.90  58.19  149.73   419.23

The total employees processed was: 5


 *** End of Program ***