fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: <your name>
  6. // Class: C Programming, <Semester Year>
  7. // Date: <date>
  8. //
  9. // Description: Linked list payroll system using pointers,
  10. // typedefs, and macros.
  11. //
  12. //********************************************************
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18.  
  19. // =========================
  20. // CONSTANTS
  21. // =========================
  22. #define STD_HOURS 40.0
  23. #define OT_RATE 1.5
  24.  
  25. #define MA_TAX_RATE 0.05
  26. #define NH_TAX_RATE 0.0
  27. #define VT_TAX_RATE 0.06
  28. #define CA_TAX_RATE 0.07
  29. #define DEFAULT_STATE_TAX_RATE 0.08
  30.  
  31. #define FED_TAX_RATE 0.25
  32.  
  33. #define FIRST_NAME_SIZE 10
  34. #define LAST_NAME_SIZE 10
  35. #define TAX_STATE_SIZE 3
  36.  
  37. // =========================
  38. // MACROS
  39. // =========================
  40. #define CALC_OT_HOURS(h) ((h > STD_HOURS) ? (h - STD_HOURS) : 0)
  41.  
  42. #define CALC_STATE_TAX(pay, rate) ((pay) * (rate))
  43.  
  44. #define CALC_FED_TAX(pay) ((pay) * FED_TAX_RATE)
  45.  
  46. #define CALC_NET_PAY(pay, stateTax, fedTax) ((pay) - ((stateTax) + (fedTax)))
  47.  
  48. #define CALC_NORMAL_PAY(rate, hours, ot) ((rate) * ((hours) - (ot)))
  49.  
  50. #define CALC_OT_PAY(rate, ot) ((ot) * (OT_RATE * (rate)))
  51.  
  52. #define CALC_MIN(val, currentMin) ((val) < (currentMin) ? (val) : (currentMin))
  53.  
  54. #define CALC_MAX(val, currentMax) ((val) > (currentMax) ? (val) : (currentMax))
  55.  
  56. // =========================
  57. // STRUCTURES
  58. // =========================
  59. struct name {
  60. char firstName[FIRST_NAME_SIZE];
  61. char lastName[LAST_NAME_SIZE];
  62. };
  63.  
  64. typedef struct employee {
  65. struct name empName;
  66. char taxState[TAX_STATE_SIZE];
  67. long clockNumber;
  68. float wageRate;
  69. float hours;
  70. float overtimeHrs;
  71. float grossPay;
  72. float stateTax;
  73. float fedTax;
  74. float netPay;
  75. struct employee *next;
  76. } EMPLOYEE;
  77.  
  78. typedef struct totals {
  79. float total_wageRate;
  80. float total_hours;
  81. float total_overtimeHrs;
  82. float total_grossPay;
  83. float total_stateTax;
  84. float total_fedTax;
  85. float total_netPay;
  86. } TOTALS;
  87.  
  88. typedef struct min_max {
  89. float min_wageRate, min_hours, min_overtimeHrs;
  90. float min_grossPay, min_stateTax, min_fedTax, min_netPay;
  91.  
  92. float max_wageRate, max_hours, max_overtimeHrs;
  93. float max_grossPay, max_stateTax, max_fedTax, max_netPay;
  94. } MIN_MAX;
  95.  
  96. // =========================
  97. // PROTOTYPES
  98. // =========================
  99. EMPLOYEE *getEmpData(void);
  100. int isEmployeeSize(EMPLOYEE *head_ptr);
  101.  
  102. void calcOvertimeHrs(EMPLOYEE *head_ptr);
  103. void calcGrossPay(EMPLOYEE *head_ptr);
  104. void calcStateTax(EMPLOYEE *head_ptr);
  105. void calcFedTax(EMPLOYEE *head_ptr);
  106. void calcNetPay(EMPLOYEE *head_ptr);
  107.  
  108. void calcEmployeeTotals(EMPLOYEE *head_ptr, TOTALS *totals);
  109. void calcEmployeeMinMax(EMPLOYEE *head_ptr, MIN_MAX *minmax);
  110.  
  111. void printHeader(void);
  112. void printEmp(EMPLOYEE *head_ptr);
  113. void printEmpStatistics(TOTALS *totals, MIN_MAX *minmax, int size);
  114.  
  115. // =========================
  116. // MAIN
  117. // =========================
  118. int main() {
  119.  
  120. EMPLOYEE *head_ptr;
  121. int size;
  122.  
  123. TOTALS totals = {0};
  124. TOTALS *totals_ptr = &totals;
  125.  
  126. MIN_MAX minmax = {0};
  127. MIN_MAX *minmax_ptr = &minmax;
  128.  
  129. head_ptr = getEmpData();
  130.  
  131. size = isEmployeeSize(head_ptr);
  132.  
  133. if (size <= 0) {
  134. printf("\n**** No employee data ****\n");
  135. return 0;
  136. }
  137.  
  138. calcOvertimeHrs(head_ptr);
  139. calcGrossPay(head_ptr);
  140. calcStateTax(head_ptr);
  141. calcFedTax(head_ptr);
  142. calcNetPay(head_ptr);
  143.  
  144. calcEmployeeTotals(head_ptr, totals_ptr);
  145. calcEmployeeMinMax(head_ptr, minmax_ptr);
  146.  
  147. printHeader();
  148. printEmp(head_ptr);
  149. printEmpStatistics(totals_ptr, minmax_ptr, size);
  150.  
  151. printf("\n\n*** End of Program ***\n");
  152.  
  153. return 0;
  154. }
  155.  
  156. // =========================
  157. // INPUT FUNCTION
  158. // =========================
  159. EMPLOYEE *getEmpData(void) {
  160.  
  161. EMPLOYEE *head, *current;
  162. char answer[10];
  163. int more = 1;
  164.  
  165. head = malloc(sizeof(EMPLOYEE));
  166. current = head;
  167.  
  168. while (more) {
  169.  
  170. printf("\nFirst name: ");
  171. scanf("%s", current->empName.firstName);
  172.  
  173. printf("Last name: ");
  174. scanf("%s", current->empName.lastName);
  175.  
  176. printf("State: ");
  177. scanf("%s", current->taxState);
  178.  
  179. printf("Clock #: ");
  180. scanf("%li", &current->clockNumber);
  181.  
  182. printf("Wage: ");
  183. scanf("%f", &current->wageRate);
  184.  
  185. printf("Hours: ");
  186. scanf("%f", &current->hours);
  187.  
  188. printf("Add another (y/n)? ");
  189. scanf("%s", answer);
  190.  
  191. if (toupper(answer[0]) != 'Y') {
  192. current->next = NULL;
  193. more = 0;
  194. } else {
  195. current->next = malloc(sizeof(EMPLOYEE));
  196. current = current->next;
  197. }
  198. }
  199.  
  200. return head;
  201. }
  202.  
  203. // =========================
  204. // SIZE
  205. // =========================
  206. int isEmployeeSize(EMPLOYEE *head_ptr) {
  207.  
  208. int count = 0;
  209. EMPLOYEE *p = head_ptr;
  210.  
  211. while (p) {
  212. count++;
  213. p = p->next;
  214. }
  215.  
  216. return count;
  217. }
  218.  
  219. // =========================
  220. // CALCULATIONS
  221. // =========================
  222. void calcOvertimeHrs(EMPLOYEE *p) {
  223. while (p) {
  224. p->overtimeHrs = CALC_OT_HOURS(p->hours);
  225. p = p->next;
  226. }
  227. }
  228.  
  229. void calcGrossPay(EMPLOYEE *p) {
  230. float normal, ot;
  231.  
  232. while (p) {
  233. normal = CALC_NORMAL_PAY(p->wageRate, p->hours, p->overtimeHrs);
  234. ot = CALC_OT_PAY(p->wageRate, p->overtimeHrs);
  235. p->grossPay = normal + ot;
  236. p = p->next;
  237. }
  238. }
  239.  
  240. void calcStateTax(EMPLOYEE *p) {
  241. while (p) {
  242.  
  243. if (strcmp(p->taxState, "MA") == 0)
  244. p->stateTax = CALC_STATE_TAX(p->grossPay, MA_TAX_RATE);
  245. else if (strcmp(p->taxState, "VT") == 0)
  246. p->stateTax = CALC_STATE_TAX(p->grossPay, VT_TAX_RATE);
  247. else if (strcmp(p->taxState, "NH") == 0)
  248. p->stateTax = 0;
  249. else if (strcmp(p->taxState, "CA") == 0)
  250. p->stateTax = CALC_STATE_TAX(p->grossPay, CA_TAX_RATE);
  251. else
  252. p->stateTax = CALC_STATE_TAX(p->grossPay, DEFAULT_STATE_TAX_RATE);
  253.  
  254. p = p->next;
  255. }
  256. }
  257.  
  258. void calcFedTax(EMPLOYEE *p) {
  259. while (p) {
  260. p->fedTax = CALC_FED_TAX(p->grossPay);
  261. p = p->next;
  262. }
  263. }
  264.  
  265. void calcNetPay(EMPLOYEE *p) {
  266. while (p) {
  267. p->netPay = CALC_NET_PAY(p->grossPay, p->stateTax, p->fedTax);
  268. p = p->next;
  269. }
  270. }
  271.  
  272. // =========================
  273. // TOTALS
  274. // =========================
  275. void calcEmployeeTotals(EMPLOYEE *p, TOTALS *t) {
  276.  
  277. while (p) {
  278. t->total_wageRate += p->wageRate;
  279. t->total_hours += p->hours;
  280. t->total_overtimeHrs += p->overtimeHrs;
  281. t->total_grossPay += p->grossPay;
  282. t->total_stateTax += p->stateTax;
  283. t->total_fedTax += p->fedTax;
  284. t->total_netPay += p->netPay;
  285. p = p->next;
  286. }
  287. }
  288.  
  289. // =========================
  290. // MIN / MAX
  291. // =========================
  292. void calcEmployeeMinMax(EMPLOYEE *p, MIN_MAX *m) {
  293.  
  294. *m = (MIN_MAX){
  295. p->wageRate, p->hours, p->overtimeHrs,
  296. p->grossPay, p->stateTax, p->fedTax, p->netPay,
  297. p->wageRate, p->hours, p->overtimeHrs,
  298. p->grossPay, p->stateTax, p->fedTax, p->netPay
  299. };
  300.  
  301. p = p->next;
  302.  
  303. while (p) {
  304.  
  305. m->min_wageRate = CALC_MIN(p->wageRate, m->min_wageRate);
  306. m->max_wageRate = CALC_MAX(p->wageRate, m->max_wageRate);
  307.  
  308. m->min_hours = CALC_MIN(p->hours, m->min_hours);
  309. m->max_hours = CALC_MAX(p->hours, m->max_hours);
  310.  
  311. m->min_overtimeHrs = CALC_MIN(p->overtimeHrs, m->min_overtimeHrs);
  312. m->max_overtimeHrs = CALC_MAX(p->overtimeHrs, m->max_overtimeHrs);
  313.  
  314. m->min_grossPay = CALC_MIN(p->grossPay, m->min_grossPay);
  315. m->max_grossPay = CALC_MAX(p->grossPay, m->max_grossPay);
  316.  
  317. m->min_stateTax = CALC_MIN(p->stateTax, m->min_stateTax);
  318. m->max_stateTax = CALC_MAX(p->stateTax, m->max_stateTax);
  319.  
  320. m->min_fedTax = CALC_MIN(p->fedTax, m->min_fedTax);
  321. m->max_fedTax = CALC_MAX(p->fedTax, m->max_fedTax);
  322.  
  323. m->min_netPay = CALC_MIN(p->netPay, m->min_netPay);
  324. m->max_netPay = CALC_MAX(p->netPay, m->max_netPay);
  325.  
  326. p = p->next;
  327. }
  328. }
  329.  
  330. // =========================
  331. // OUTPUT
  332. // =========================
  333. void printHeader(void) {
  334. printf("\nPAYROLL REPORT\n");
  335. printf("------------------------------------------------------------\n");
  336. }
  337.  
  338. void printEmp(EMPLOYEE *p) {
  339.  
  340. char name[25];
  341.  
  342. while (p) {
  343. sprintf(name, "%s %s", p->empName.firstName, p->empName.lastName);
  344.  
  345. printf("\n%-15s %-2s %6ld %6.2f %5.1f %5.1f %8.2f %7.2f %7.2f %8.2f",
  346. name, p->taxState, p->clockNumber,
  347. p->wageRate, p->hours, p->overtimeHrs,
  348. p->grossPay, p->stateTax, p->fedTax, p->netPay);
  349.  
  350. p = p->next;
  351. }
  352. }
  353.  
  354. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size) {
  355.  
  356. printf("\n\nTOTALS & STATS\n");
  357. printf("Employees: %d\n", size);
  358. }
  359.  
Success #stdin #stdout 0s 5316KB
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
First name: Last name: State: Clock #: Wage: Hours: Add another (y/n)? 
First name: Last name: State: Clock #: Wage: Hours: Add another (y/n)? 
First name: Last name: State: Clock #: Wage: Hours: Add another (y/n)? 
First name: Last name: State: Clock #: Wage: Hours: Add another (y/n)? 
First name: Last name: State: Clock #: Wage: Hours: Add another (y/n)? 
PAYROLL REPORT
------------------------------------------------------------

Connie Cobol    MA  98401  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  34645  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 & STATS
Employees: 5


*** End of Program ***