fork(94) download
  1. #include <stdio.h>
  2. #include <stdlib.h> /* for malloc */
  3. #include <ctype.h>
  4.  
  5. struct employee
  6. {
  7. int id_number;
  8. float wage;
  9.  
  10. /* TODO - Add other members */
  11.  
  12. struct employee *next;
  13. };
  14.  
  15.  
  16.  
  17. /* TODO - Add Function Prototypes as needed */
  18.  
  19. /* TODO - Add Functions here as needed */
  20.  
  21. /* Optional - Add Challenge Functions here as needed */
  22.  
  23. /*-----------------------------------------------------------------------------*/
  24. /* */
  25. /* FUNCTION: print_list */
  26. /* */
  27. /* DESCRIPTION: This function will print the contents of a linked */
  28. /* list. It will traverse the list from beginning to the */
  29. /* end, printing the contents at each node. */
  30. /* */
  31. /* PARAMETERS: emp1 - pointer to a linked list */
  32. /* */
  33. /* OUTPUTS: None */
  34. /* */
  35. /* CALLS: None */
  36. /* */
  37. /*-----------------------------------------------------------------------------*/
  38. void print_list(struct employee *emp1)
  39. {
  40. struct employee *tmp; /* tmp pointer value to current node */
  41. int i = 0; /* counts the nodes printed */
  42.  
  43. /* Start a beginning of list and print out each value */
  44. /* loop until tmp points to null (remember null is 0 or false) */
  45. for(tmp = emp1; tmp ; tmp = tmp->next)
  46. {
  47. i++;
  48.  
  49. /* TODO - print other members as well */
  50. printf("\nEmployee ID: %6d, Wage: %8.2f\n",tmp->id_number,
  51. tmp->wage);
  52. }
  53.  
  54. printf("\n\nTotal Number of Employees = %d\n", i);
  55.  
  56. }
  57.  
  58. /*----------------------------------------------------------------------------*/
  59. /* */
  60. /* FUNCTION: main */
  61. /* */
  62. /* DESCRIPTION: This function will prompt the user for an employee */
  63. /* id and wage until the user indicates they are finished. */
  64. /* At that point, a list of id and wages will be */
  65. /* generated. */
  66. /* */
  67. /* PARAMETERS: None */
  68. /* */
  69. /* OUTPUTS: None */
  70. /* */
  71. /* CALLS: print_list */
  72. /* */
  73. /*----------------------------------------------------------------------------*/
  74. int main ()
  75. {
  76.  
  77. char answer[80]; /* to see if the user wants to add more employees */
  78. int more_data = 1; /* flag to check if another employee is to be processed */
  79. char value; /* gets the first character of answer */
  80.  
  81. struct employee *current_ptr, /* pointer to current node */
  82. *head_ptr; /* always points to first node */
  83.  
  84. /* Set up storage for first node */
  85. head_ptr = (struct employee *) malloc (sizeof(struct employee));
  86. current_ptr = head_ptr;
  87.  
  88. while (more_data)
  89. {
  90.  
  91.  
  92. /* TODO - Prompt for Employee Name and Hours as well here */
  93.  
  94. /* Read in Employee ID and Hourly Wage */
  95. printf("\nEnter employee ID: ");
  96. scanf("%i", & current_ptr -> id_number);
  97.  
  98. printf("\nEnter employee hourly wage: ");
  99. scanf("%f", & current_ptr -> wage);
  100.  
  101. /* TODO - Call Function(s) to calculate Overtime and Gross */
  102.  
  103. printf("Would you like to add another employee? (y/n): ");
  104. scanf("%s", answer);
  105.  
  106. /* Ask user if they want to add another employee */
  107. if ((value = toupper(answer[0])) != 'Y')
  108. {
  109. current_ptr->next = (struct employee *) NULL;
  110. more_data = 0;
  111. }
  112. else
  113. {
  114. /* set the next pointer of the current node to point to the new node */
  115. current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
  116. /* move the current node pointer to the new node */
  117. current_ptr = current_ptr->next;
  118. }
  119. } /* while */
  120.  
  121. /* Optional - Call Challenge Functions to determine totals, min, max, and averages */
  122.  
  123. /* print out listing of all employee id's and wages that were entered */
  124. print_list(head_ptr);
  125.  
  126. printf("\n\nEnd of program\n");
  127. return (0);
  128. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Enter employee ID: 
Enter employee hourly wage: Would you like to add another employee? (y/n): 
Employee ID:      0, Wage:     0.00


Total Number of Employees = 1


End of program