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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   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   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***