/*----------------------------------------------------------------------*/ /* */ /* HOMEWORK: 8 - Chapter 11 */ /* */ /* Name: Mike Chappell */ /* */ /* Class: C Programming, Cybercourse, Fall 2006 */ /* */ /* Date: 12/01/06 */ /* */ /* Description: Program which determines gross Pay, calculates */ /* overtime and outputs a formated answer for */ /* multiple data sets using arrays, functions, structures, */ /* strings, pointers, and linked lists. */ /* */ /*----------------------------------------------------------------------*/ #include #include #include /* Define Symbolic Constants*/ #define EMP_NUM 5 /* Define the number of employees*/ #define OT_RATE 1.5 /*Define Overtime Rate as 1.5 */ #define FULL_TIME 40.0 /* Define Full Time as 40 */ /* Define stuctures */ struct employee { char name[20]; /* Employee Name */ long id_number; /* Clock Number */ float wage; /* Wage Rate */ float hours; /* Hours */ float overtime; /* Overtime Hours */ float gross; /* Gross Pay */ struct employee *next; }; /*----------------------------------------------------------------------*/ /* */ /* Function: print_list */ /* */ /* Purpose: This function will print the contents of a linked */ /* list. It will traverse the list from beginning to the */ /* end, printing the contents at each node. */ /* */ /* Parameters: emp1 - pointer to a linked list */ /* */ /* Outputs: None */ /* */ /* Calls: None */ /* */ /*----------------------------------------------------------------------*/ void print_list(emp1) struct employee *emp1; { struct employee *tmp; /* tmp pointer value to current node */ int i = 0; /* counts the nodes printed */ /* Start a beginning of list and print out each value */ /* loop until tmp points to null (remember null is 0 or false) */ for (tmp = emp1; tmp; tmp = tmp->next) { printf("Employee ID: %6d Wage: %6.2f Hours: %6.2f\n", tmp->id_number, tmp->wage, tmp->hours); ++i; } printf("\n\nTotal Number of Employees = %i\n", i); } /*************************************************************************/ /* */ /* Function: calc_ot_hours */ /* */ /* Purpose: Calculates the number of overtime hours given the number */ /* of hours worked. Overtime hours are the number of hours */ /* worked after 40 hrs. */ /* */ /* Parameters: hours - Number of hours worked. */ /* */ /* Returns: The number of overtime hours. */ /* */ /*************************************************************************/ void calc_ot_hours (struct employee *emp1) { int index; /* variable used as a loop counter */ for (index = 0; index < EMP_NUM; index++) { if (emp1[index].hours > FULL_TIME) { emp1[index].overtime = emp1[index].hours - FULL_TIME; } else { emp1[index].overtime = 0; } } } /*************************************************************************/ /* */ /* Function: calc_gross_pay */ /* */ /* Purpose: Calculates gross pay including overtime given the number */ /* of hours an employee works and wage rate. */ /* */ /* Parameters: hours - Array of Number of hours worked by employees */ /* wage - Array of Hourly rates of employees. */ /* OT_Wage - Array of gross pay amounts for employees. */ /* */ /* Returns: Nothing since the gross_pay array is passed by reference */ /* */ /*************************************************************************/ void calc_gross_pay(struct employee *emp1) { int index; /*variable used as a loop counter*/ calc_ot_hours (emp1); for (index = 0; index < EMP_NUM; index++) { if (emp1[index].overtime >0) emp1[index].gross = (tmp->overtime * emp1[index].wage * OT_RATE) + (FULL_TIME * emp1[index].wage); else emp1[index].gross = emp1[index].wage * emp1[index].hours; } } /*************************************************************************/ /* */ /* Function: output */ /* */ /* Purpose: Outputs in a table format the following: */ /* - Employee Clock Number */ /* - wage Rate for Employee */ /* - Total hours worked by employee */ /* - Overtime hours */ /* - gross Pay */ /* */ /* Parameters: id_number - Array of employee clock numbers */ /* hours - Array of hours worked by employee */ /* gross - Array of gross pay for employees */ /* overtime - Array of OT hours */ /* wage - Array of employee wage rates */ /* */ /* Returns: Nothing. */ /* */ /*************************************************************************/ void output (struct employee *emp1) { int index; /*variable used as a loop counter*/ printf ("\n----------------------EMPLOYEE WAGES---------------------\n"); printf ("---------------------------------------------------------\n"); printf ("Name Clock# Wage Hours OT Gross\n"); printf ("---------------------------------------------------------\n"); for (index = 0; index < EMP_NUM; index++) { printf("%-15s %.06li %6.2f %6.1f %6.1f %9.2f\n", emp1[index].name,emp1[index].id_number,emp1[index].wage,emp1[index].hours,emp1[index].overtime,emp1[index].gross); } } /*----------------------------------------------------------------------*/ /* */ /* Function: main */ /* */ /* Purpose: This function will prompt the user for an employee */ /* id and wage until the user indicates they are finished. */ /* At that point, a list of id and wages will be */ /* generated. */ /* */ /* Parameters: None */ /* */ /* Outputs: None */ /* */ /* Calls: print_list */ /* */ /*----------------------------------------------------------------------*/ int main() { struct employee emp1 [5]={ {"Connie Cobol",98401, 10.60}, {"Mary Apl",526488, 9.75}, {"Frank Fortran",765349, 10.50}, {"Jeff Ada",34645, 12.25}, {"Anton Pascal",127615, 8.35} }; /*This ends the structure */ char answer[80]; int more_data = 1; char value; struct employee *current_ptr, /* pointer to current node */ *head_ptr; /* pointer to first node */ /* Set up storage for first node */ current_ptr = (struct employee *) malloc(sizeof(struct employee)); head_ptr = current_ptr; while (more_data) { /* prompt user to enter the employee's name, ID#, and wage */ printf("\n"); printf("Please enter the employee's name: "); scanf("\n"); gets(current_ptr->name); printf("\nPlease enter %s's id number: ", current_ptr->name); scanf("%ld", ¤t_ptr->id_number); printf("\nPlease enter %s's wage: ", current_ptr->name); scanf("%f", ¤t_ptr->wage); printf("\nPlease enter the number of hours %s worked: ", current_ptr->name); scanf("%f", ¤t_ptr->hours); printf("\n"); /* Ask user if they want to add another employee */ printf("Would you like to add another employee? (y/n): "); scanf("%s", answer); if (value = toupper(answer[0]) != 'Y') { current_ptr->next = (struct employee *) NULL; more_data = 0; } else { /* set the next pointer of the current node to point to the new node */ current_ptr->next = (struct employee *) malloc(sizeof(struct employee)); /* move the current node pointer to the new node */ current_ptr = current_ptr->next; } } /* Function call to print out listing of all employee IDs, wages, and hours */ /* that were entered */ print_list(head_ptr); /* Function Call to calculate overtime hours */ calc_ot_hours (head_ptr); /* Function Call to calculate gross pay */ calc_gross_pay (head_ptr); /* Function call to output the results */ output (head_ptr); printf("\n\nEnd of program\n"); return 0; }