/*----------------------------------------------------------------------*/ /* */ /* HOMEWORK: 8 - Chapter 11 */ /* */ /* Name: Mike Chappell */ /* */ /* Class: C Programming, Cybercourse, Fall 2006 */ /* */ /* Date: 11/29/06 */ /* */ /* Description: Program which determines gross Pay, calculates */ /* overtime and outputs a formated answer for */ /* multiple data sets using arrays, functions, structures, */ /* strings and pointers. */ /* */ /*----------------------------------------------------------------------*/ #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) { ++i; printf("Employee ID: %6d Wage: %8.2f Hours %8.2f\n", tmp->id_number, tmp->wage, tmp->hours); } printf("\n\nTotal Number of Employees = %i\n", i); } /*----------------------------------------------------------------------*/ /* */ /* 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(current_ptr); printf("\n\nEnd of program\n"); return 0; }