fork download
  1.  
  2. //********************************************************
  3. //
  4. // Assignment 10 - Linked Lists, Typedef, and Macros
  5. //
  6. // Name: John Semenuk
  7. //
  8. // Class: C Programming, <replace with Semester and Year>
  9. //
  10. // Date: April 17, 2026
  11. //
  12. // Description: Program which determines overtime and
  13. // gross pay for a set of employees with outputs sent
  14. // to standard output (the screen).
  15. //
  16. // This assignment also adds the employee name, their tax state,
  17. // and calculates the state tax, federal tax, and net pay. It
  18. // also calculates totals, averages, minimum, and maximum values.
  19. //
  20. // Array and Structure references have all been replaced with
  21. // pointer references to speed up the processing of this code.
  22. // A linked list has been created and deployed to dynamically
  23. // allocate and process employees as needed.
  24. //
  25. // It will also take advantage of the C Preprocessor features,
  26. // in particular with using macros, and will replace all
  27. // struct type references in the code with a typedef alias
  28. // reference.
  29. //
  30. // Call by Reference design (using pointers)
  31. //
  32. //********************************************************
  33.  
  34. // necessary header files
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <ctype.h> // for char functions
  38. #include <stdlib.h> // for malloc
  39.  
  40. // define constants
  41. #define STD_HOURS 40.0
  42. #define OT_RATE 1.5
  43. #define MA_TAX_RATE 0.05
  44. #define NH_TAX_RATE 0.0
  45. #define VT_TAX_RATE 0.06
  46. #define CA_TAX_RATE 0.07
  47. #define DEFAULT_STATE_TAX_RATE 0.08
  48. #define NAME_SIZE 20
  49. #define TAX_STATE_SIZE 3
  50. #define FED_TAX_RATE 0.25
  51. #define FIRST_NAME_SIZE 10
  52. #define LAST_NAME_SIZE 10
  53.  
  54. // define macros
  55. #define CALC_OT_HOURS(h) ((h > STD_HOURS) ? (h - STD_HOURS) : 0)
  56. #define CALC_STATE_TAX(p,r) ((p)*(r))
  57. #define CALC_FED_TAX(p) ((p)*FED_TAX_RATE)
  58. #define CALC_NET_PAY(p,s,f) ((p)-((s)+(f)))
  59.  
  60. #define CALC_NORMAL_PAY(w,h,ot) ((w)*((h)-(ot)))
  61. #define CALC_OT_PAY(w,ot) ((ot)*(OT_RATE*(w)))
  62.  
  63. #define CALC_MIN(v,m) ((v)<(m)?(v):(m))
  64. #define CALC_MAX(v,m) ((v)>(m)?(v):(m))
  65.  
  66. // Define a global structure type to store an employee name
  67. // ... note how one could easily extend this to other parts
  68. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  69. struct name {
  70. char firstName[FIRST_NAME_SIZE];
  71. char lastName[LAST_NAME_SIZE];
  72. };
  73.  
  74. // Define a global structure type to pass employee data between functions
  75. // Note that the structure type is global, but you don't want a variable
  76. // of that type to be global. Best to declare a variable of that type
  77. // in a function like main or another function and pass as needed.
  78.  
  79. // Note the "next" member has been added as a pointer to structure employee.
  80. // This allows us to point to another data item of this same type,
  81. // allowing us to set up and traverse through all the linked
  82. // list nodes, with each node containing the employee information below.
  83.  
  84. // Also note the use of typedef to create an alias for struct employee
  85.  
  86. typedef struct employee {
  87. struct name empName;
  88. char taxState[TAX_STATE_SIZE];
  89. long int clockNumber;
  90. float wageRate;
  91. float hours;
  92. float overtimeHrs;
  93. float grossPay;
  94. float stateTax;
  95. float fedTax;
  96. float netPay;
  97. struct employee *next;
  98. } EMPLOYEE;
  99.  
  100. // This structure type defines the totals of all floating point items
  101. // so they can be totaled and used also to calculate averages
  102.  
  103. // Also note the use of typedef to create an alias for struct totals
  104.  
  105.  
  106. typedef struct totals {
  107. float total_wageRate;
  108. float total_hours;
  109. float total_overtimeHrs;
  110. float total_grossPay;
  111. float total_stateTax;
  112. float total_fedTax;
  113. float total_netPay;
  114. } TOTALS;
  115.  
  116. // This structure type defines the min and max values of all floating
  117. // point items so they can be display in our final report
  118.  
  119. // Also note the use of typedef to create an alias for struct min_max
  120.  
  121. typedef struct min_max {
  122. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  123. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  124. } MIN_MAX;
  125.  
  126. // Define prototypes here for each function except main
  127. //
  128. // Note the use of the typedef alias values throughout
  129. // the rest of this program, starting with the fucntions
  130. // prototypes
  131. //
  132. // EMPLOYEE instead of struct employee
  133. // TOTALS instead of struct totals
  134. // MIN_MAX instead of struct min_max
  135.  
  136. // prototypes
  137. EMPLOYEE *getEmpData(void);
  138. int isEmployeeSize(EMPLOYEE *head_ptr);
  139.  
  140. void calcOvertimeHrs(EMPLOYEE *head_ptr);
  141. void calcGrossPay(EMPLOYEE *head_ptr);
  142. void calcStateTax(EMPLOYEE *head_ptr);
  143. void calcFedTax(EMPLOYEE *head_ptr);
  144. void calcNetPay(EMPLOYEE *head_ptr);
  145.  
  146. void calcEmployeeTotals(EMPLOYEE *head_ptr, TOTALS *t);
  147. void calcEmployeeMinMax(EMPLOYEE *head_ptr, MIN_MAX *m);
  148.  
  149. void printHeader(void);
  150. void printEmp(EMPLOYEE *head_ptr);
  151. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size);
  152.  
  153. // MAIN
  154. int main()
  155. {
  156. // ******************************************************************
  157. // Set up head pointer in the main function to point to the
  158. // start of the dynamically allocated linked list nodes that will be
  159. // created and stored in the Heap area.
  160. // ******************************************************************
  161.  
  162. EMPLOYEE *head_ptr; // always points to first linked list node
  163. int size; // number of employees processed
  164.  
  165. // set up structure to store totals and initialize all to zero
  166.  
  167. TOTALS totals = {0,0,0,0,0,0,0};
  168. MIN_MAX mm = {0};
  169.  
  170. head_ptr = getEmpData();
  171. size = isEmployeeSize(head_ptr);
  172.  
  173. if (size <= 0)
  174. {
  175. printf("\n**** No employees ****\n");
  176. return 0;
  177. }
  178.  
  179. calcOvertimeHrs(head_ptr);
  180. calcGrossPay(head_ptr);
  181. calcStateTax(head_ptr);
  182. calcFedTax(head_ptr);
  183. calcNetPay(head_ptr);
  184.  
  185. calcEmployeeTotals(head_ptr, &totals);
  186. calcEmployeeMinMax(head_ptr, &mm);
  187.  
  188. printHeader();
  189. printEmp(head_ptr);
  190. printEmpStatistics(&totals, &mm, size);
  191.  
  192. printf("\n\n *** End of Program *** \n");
  193. return 0;
  194. }
  195.  
  196. // ================= INPUT =================
  197. EMPLOYEE *getEmpData(void)
  198. {
  199. EMPLOYEE *head, *cur;
  200. char ans[10];
  201. int more = 1;
  202.  
  203. head = malloc(sizeof(EMPLOYEE));
  204. cur = head;
  205.  
  206. while (more)
  207. {
  208. printf("\nFirst name: ");
  209. scanf("%s", cur->empName.firstName);
  210.  
  211. printf("Last name: ");
  212. scanf("%s", cur->empName.lastName);
  213.  
  214. printf("State: ");
  215. scanf("%s", cur->taxState);
  216.  
  217. printf("Clock #: ");
  218. scanf("%li", &cur->clockNumber);
  219.  
  220. printf("Wage: ");
  221. scanf("%f", &cur->wageRate);
  222.  
  223. printf("Hours: ");
  224. scanf("%f", &cur->hours);
  225.  
  226. printf("More? (y/n): ");
  227. scanf("%s", ans);
  228.  
  229. if (toupper(ans[0]) != 'Y')
  230. {
  231. cur->next = NULL;
  232. more = 0;
  233. }
  234. else
  235. {
  236. cur->next = malloc(sizeof(EMPLOYEE));
  237. cur = cur->next;
  238. }
  239. }
  240.  
  241. return head;
  242. }
  243.  
  244. // ================= SIZE =================
  245. int isEmployeeSize(EMPLOYEE *h)
  246. {
  247. int c = 0;
  248. for (; h; h = h->next) c++;
  249. return c;
  250. }
  251.  
  252. // ================= HEADER =================
  253. void printHeader(void)
  254. {
  255. printf("\n\n*** Pay Calculator ***\n\n");
  256. printf("---------------------------------------------------------------------------------\n");
  257. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  258. printf(" State Pay Tax Tax Pay\n");
  259. printf("---------------------------------------------------------------------------------\n");
  260. }
  261.  
  262. // ================= PRINT EMP =================
  263. void printEmp(EMPLOYEE *h)
  264. {
  265. char name[30];
  266.  
  267. for (; h; h = h->next)
  268. {
  269. strcpy(name, h->empName.firstName);
  270. strcat(name, " ");
  271. strcat(name, h->empName.lastName);
  272.  
  273. printf("\n%-20.20s %-2s %06li %5.2f %5.1f %4.1f %7.2f %6.2f %7.2f %7.2f",
  274. name,
  275. h->taxState,
  276. h->clockNumber,
  277. h->wageRate,
  278. h->hours,
  279. h->overtimeHrs,
  280. h->grossPay,
  281. h->stateTax,
  282. h->fedTax,
  283. h->netPay);
  284. }
  285. }
  286.  
  287. // ================= CALCULATIONS =================
  288. void calcOvertimeHrs(EMPLOYEE *h)
  289. {
  290. for (; h; h = h->next)
  291. h->overtimeHrs = CALC_OT_HOURS(h->hours);
  292. }
  293.  
  294. void calcGrossPay(EMPLOYEE *h)
  295. {
  296. for (; h; h = h->next)
  297. h->grossPay = CALC_NORMAL_PAY(h->wageRate,h->hours,h->overtimeHrs)
  298. + CALC_OT_PAY(h->wageRate,h->overtimeHrs);
  299. }
  300.  
  301. void calcStateTax(EMPLOYEE *h)
  302. {
  303. for (; h; h = h->next)
  304. {
  305. if (!strcmp(h->taxState,"MA"))
  306. h->stateTax = CALC_STATE_TAX(h->grossPay,MA_TAX_RATE);
  307. else if (!strcmp(h->taxState,"NH"))
  308. h->stateTax = CALC_STATE_TAX(h->grossPay,NH_TAX_RATE);
  309. else if (!strcmp(h->taxState,"VT"))
  310. h->stateTax = CALC_STATE_TAX(h->grossPay,VT_TAX_RATE);
  311. else if (!strcmp(h->taxState,"CA"))
  312. h->stateTax = CALC_STATE_TAX(h->grossPay,CA_TAX_RATE);
  313. else
  314. h->stateTax = CALC_STATE_TAX(h->grossPay,DEFAULT_STATE_TAX_RATE);
  315. }
  316. }
  317.  
  318. void calcFedTax(EMPLOYEE *h)
  319. {
  320. for (; h; h = h->next)
  321. h->fedTax = CALC_FED_TAX(h->grossPay);
  322. }
  323.  
  324. void calcNetPay(EMPLOYEE *h)
  325. {
  326. for (; h; h = h->next)
  327. h->netPay = CALC_NET_PAY(h->grossPay,h->stateTax,h->fedTax);
  328. }
  329.  
  330. // ================= TOTALS =================
  331. void calcEmployeeTotals(EMPLOYEE *h, TOTALS *t)
  332. {
  333. for (; h; h = h->next)
  334. {
  335. t->total_wageRate += h->wageRate;
  336. t->total_hours += h->hours;
  337. t->total_overtimeHrs += h->overtimeHrs;
  338. t->total_grossPay += h->grossPay;
  339. t->total_stateTax += h->stateTax;
  340. t->total_fedTax += h->fedTax;
  341. t->total_netPay += h->netPay;
  342. }
  343. }
  344.  
  345. // ================= MIN MAX =================
  346. void calcEmployeeMinMax(EMPLOYEE *h, MIN_MAX *m)
  347. {
  348. EMPLOYEE *c = h;
  349.  
  350. m->min_wageRate = m->max_wageRate = c->wageRate;
  351. m->min_hours = m->max_hours = c->hours;
  352. m->min_overtimeHrs = m->max_overtimeHrs = c->overtimeHrs;
  353. m->min_grossPay = m->max_grossPay = c->grossPay;
  354. m->min_stateTax = m->max_stateTax = c->stateTax;
  355. m->min_fedTax = m->max_fedTax = c->fedTax;
  356. m->min_netPay = m->max_netPay = c->netPay;
  357.  
  358. for (c = c->next; c; c = c->next)
  359. {
  360. m->min_wageRate = CALC_MIN(c->wageRate,m->min_wageRate);
  361. m->max_wageRate = CALC_MAX(c->wageRate,m->max_wageRate);
  362.  
  363. m->min_hours = CALC_MIN(c->hours,m->min_hours);
  364. m->max_hours = CALC_MAX(c->hours,m->max_hours);
  365.  
  366. m->min_overtimeHrs = CALC_MIN(c->overtimeHrs,m->min_overtimeHrs);
  367. m->max_overtimeHrs = CALC_MAX(c->overtimeHrs,m->max_overtimeHrs);
  368.  
  369. m->min_grossPay = CALC_MIN(c->grossPay,m->min_grossPay);
  370. m->max_grossPay = CALC_MAX(c->grossPay,m->max_grossPay);
  371.  
  372. m->min_stateTax = CALC_MIN(c->stateTax,m->min_stateTax);
  373. m->max_stateTax = CALC_MAX(c->stateTax,m->max_stateTax);
  374.  
  375. m->min_fedTax = CALC_MIN(c->fedTax,m->min_fedTax);
  376. m->max_fedTax = CALC_MAX(c->fedTax,m->max_fedTax);
  377.  
  378. m->min_netPay = CALC_MIN(c->netPay,m->min_netPay);
  379. m->max_netPay = CALC_MAX(c->netPay,m->max_netPay);
  380. }
  381. }
  382.  
  383. // ================= STATS =================
  384. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size)
  385. {
  386. printf("\n---------------------------------------------------------------------------------");
  387.  
  388. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  389. t->total_wageRate,
  390. t->total_hours,
  391. t->total_overtimeHrs,
  392. t->total_grossPay,
  393. t->total_stateTax,
  394. t->total_fedTax,
  395. t->total_netPay);
  396.  
  397. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  398. t->total_wageRate/size,
  399. t->total_hours/size,
  400. t->total_overtimeHrs/size,
  401. t->total_grossPay/size,
  402. t->total_stateTax/size,
  403. t->total_fedTax/size,
  404. t->total_netPay/size);
  405.  
  406. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  407. m->min_wageRate,
  408. m->min_hours,
  409. m->min_overtimeHrs,
  410. m->min_grossPay,
  411. m->min_stateTax,
  412. m->min_fedTax,
  413. m->min_netPay);
  414.  
  415. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  416. m->max_wageRate,
  417. m->max_hours,
  418. m->max_overtimeHrs,
  419. m->max_grossPay,
  420. m->max_stateTax,
  421. m->max_fedTax,
  422. m->max_netPay);
  423.  
  424. printf("\n\nThe total employees processed was: %d\n", size);
  425. }
Success #stdin #stdout 0s 5324KB
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: More? (y/n): 
First name: Last name: State: Clock #: Wage: Hours: More? (y/n): 
First name: Last name: State: Clock #: Wage: Hours: More? (y/n): 
First name: Last name: State: Clock #: Wage: Hours: More? (y/n): 
First name: Last name: State: Clock #: Wage: Hours: More? (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 ***