Question

In: Computer Science

// CSE240 Fall 2020 HW 7 & 8 // Write your name here // Write the...

// CSE240 Fall 2020 HW 7 & 8
// Write your name here
// Write the compiler used: Visual studio or gcc

// READ BEFORE YOU START:
// You are given a partially completed program that creates a linked list of employee information.
// The global linked list 'list' is a list of employees with each node being struct 'employeeList'.
// 'employeeList' consists of struct 'employee' which has: employee name, room number, and a linked list of 'supervisors'.
// The linked list of supervisors has each node containing simply the name of the supervisor.
// HW7 ignores the 'supervisors' linked list since there is no operation/manipulation to be done on 'supervisors' list in HW7.
// HW8 has operations/manipulations to be done with 'supervisors' list like add a supervisor, display last added supervisor.

// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error.
// You are not allowed to modify main ().
// You can use string library functions.

// ***** WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE. *****
// ***** GIVE MEANINGFUL NAMES TO VARIABLES. *****
// ***** Before implementing any function, see how it is called in executeAction() *****


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma warning(disable: 4996) // for Visual Studio

#define MAX_NAME 30

// global linked list 'list' contains the list of employees
struct employeeList {
   struct employee* employee;
   struct employeeList* next;
} *list = NULL;               // currently empty list

// structure "employee" contains the employee's name, room number and linked list of supervisors
struct employee {
   char name[MAX_NAME];
   unsigned int roomNumber;
   struct supervisor* supervisors;       // linked list 'supervisors' contains names of supervisors
};

// structure 'supervisor' contains supervisor's name
struct supervisor {
   char name[MAX_NAME];
   struct supervisor* next;
};

// forward declaration of functions (already implmented)
void flushStdIn();
void executeAction(char);

// functions that need implementation:
// HW 7
void addEmployee(char* employeeNameInput, unsigned int roomNumInput); // 20 points
void displayEmployeeList(struct employeeList* tempList);   // 15 points
struct employee* searchEmployee(char* employeeNameInput);   // 15 points
//HW 8
void addSupervisor(char* employeeNameInput, char* supervisorNameInput);   // 15 points
void displayEmployeeSupervisorList(struct employeeList* tempList);   // 15 points
void removeEmployee(char* employeeNameInput);           // 20 points

int main()
{
   char selection = 'a';       // initialized to a dummy value
   do
   {
       printf("\nCSE240 HW 7,8\n");
       printf("Please enter your selection:\n");
       printf("HW7:\n");
       printf("\t a: add a new employee to the list\n");
       printf("\t d: display employee list (no supervisors)\n");
       printf("\t b: search for an employee on the list\n");
       printf("\t q: quit\n");
       printf("HW8:\n");
       printf("\t c: add a supervisor of a employee\n");
       printf("\t l: display employees who report to a specific supervisor\n");
       printf("\t r: remove an employee\n");
       printf("\t q: quit\n");

       selection = getchar();
       flushStdIn();
       executeAction(selection);
   } while (selection != 'q');

   return 0;
}

// flush out leftover '\n' characters
void flushStdIn()
{
   char c;
   do c = getchar();
   while (c != '\n' && c != EOF);
}

// Ask for details from user for the given selection and perform that action
// Read the function case by case
void executeAction(char c)
{
   char employeeNameInput[MAX_NAME], supervisorNameInput[MAX_NAME];
   unsigned int roomNumInput;
   struct employee* searchResult = NULL;

   switch (c)
   {
   case 'a':   // add employee
               // input employee details from user
       printf("\nPlease enter employee's name: ");
       fgets(employeeNameInput, sizeof(employeeNameInput), stdin);
       employeeNameInput[strlen(employeeNameInput) - 1] = '\0';   // discard the trailing '\n' char
       printf("Please enter room number: ");
       scanf("%d", &roomNumInput);
       flushStdIn();

       if (searchEmployee(employeeNameInput) == NULL)   // un-comment this line after implementing searchEmployee()                  
       //if (1)                                   // comment out this line after implementing searchEmployee()
       {
           addEmployee(employeeNameInput, roomNumInput);
           printf("\nEmployee successfully added to the list!\n");
       }
       else
           printf("\nThat employee is already on the list!\n");
       break;

   case 'd':       // display the list
       displayEmployeeList(list);
       break;

   case 'b':       // search for an employee on the list
       printf("\nPlease enter employee's name: ");
       fgets(employeeNameInput, sizeof(employeeNameInput), stdin);
       employeeNameInput[strlen(employeeNameInput) - 1] = '\0';   // discard the trailing '\n' char

       if (searchEmployee(employeeNameInput) == NULL)   // un-comment this line after implementing searchEmployee()                  
       //if (0)                                   // comment out this line after implementing searchEmployee()
           printf("\nEmployee name does not exist or the list is empty! \n\n");
       else
       {
           printf("\nEmployee name exists on the list! \n\n");
       }
       break;

   case 'r':       // remove employee
       printf("\nPlease enter employee's name: ");
       fgets(employeeNameInput, sizeof(employeeNameInput), stdin);
       employeeNameInput[strlen(employeeNameInput) - 1] = '\0';   // discard the trailing '\n' char

       if (searchEmployee(employeeNameInput) == NULL)   // un-comment this line after implementing searchEmployee()                  
       //if (0)                                   // comment out this line after implementing searchEmployee()
           printf("\nEmployee name does not exist or the list is empty! \n\n");
       else
       {
           removeEmployee(employeeNameInput);
           printf("\nEmployee successfully removed from the list! \n\n");
       }
       break;

   case 'c':       // add supervisor
       printf("\nPlease enter employee's name: ");
       fgets(employeeNameInput, sizeof(employeeNameInput), stdin);
       employeeNameInput[strlen(employeeNameInput) - 1] = '\0';   // discard the trailing '\n' char

       if (searchEmployee(employeeNameInput) == NULL)   // un-comment this line after implementing searchEmployee()                  
       //if (0)                                       // comment out this line after implementing searchEmployee()
           printf("\nEmployee name does not exist or the list is empty! \n\n");
       else
       {
           printf("\nPlease enter supervisor's name: ");
           fgets(supervisorNameInput, sizeof(supervisorNameInput), stdin);
           supervisorNameInput[strlen(supervisorNameInput) - 1] = '\0';   // discard the trailing '\n' char

           addSupervisor(employeeNameInput, supervisorNameInput);
           printf("\nSupervisor added! \n\n");
       }
       break;

   case 'l':       // list supervisor's employees
       displayEmployeeSupervisorList(list);
       break;

   case 'q':       // quit
       break;

   default: printf("%c is invalid input!\n", c);
   }
}

// HW7 Q1: addEmployee (20 points)
// This function is used to insert a new employee in the linked list.
// You must insert the new employee to the head of linked list 'list'.
// You need NOT check if the employee already exists in the list because that is taken care by searchEmployee() called in executeAction(). Look at how this function is used in executeAction().
// Don't bother to check how to implement searchEmployee() while implementing this function. Simply assume that employee does not exist in the list while implementing this function.
// NOTE: The function needs to add the employee to the head of the list.
// NOTE: This function does not add supervisors to the employee info. There is another function addSupervisor() in HW8 for that.
// Hint: In this question, no supervisors means NULL supervisors.

void addEmployee(char* employeeNameInput, unsigned int roomNumInput)
{
   // YOUR CODE HERE
}

// HW7 Q2: displayEmployeeList (15 points)
// This function displays the employee details (struct elements) of each employee.
// Parse through the linked list 'list' and print the employee details ( name and room number) one after the other. See expected output screenshots in homework question file.
// You should not display supervisor names (because they are not added in HW7).
// You MUST use recursion in the function to get full points. Notice that 'list' is passed to the function argument. Use recursion to keep calling this function till end of list.

void displayEmployeeList(struct employeeList* tempList)
{
   // YOUR CODE HERE

}

// HW7 Q3: searchEmployee (15 points)
// This function searches the 'list' to check if the given employee exists in the list. Search by employee name.
// If it exists then return that 'employee' node of the list. Notice the return type of this function.
// If the employee does not exist in the list, then return NULL.
// NOTE: After implementing this fucntion, go to executeAction() to comment and un-comment the lines mentioned there which use searchEmployee()
// in case 'a', case 'r', case 'l' (total 3 places)
struct employee* searchEmployee(char* employeeNameInput)
{

   struct employeeList* tempList = list;           // work on a copy of 'list'
  
   // YOUR CODE HERE

   return NULL;
}

Help me complete the code thank you.

Solutions

Expert Solution

// I am attaching the code, didn't have time to compile. I'm sure you can follow along and make changes if required

// Do put the effort in understanding the comments.

void addEmployee(char *employeeNameInput, unsigned int roomNumInput)

{

    // YOUR CODE HERE

    // create new employee

    employee* new_employee = malloc(sizeof(employee)); // allot new memory

    strcpy(new_employee->name, employeeNameInput); // copy

    new_employee->roomNumber = roomNumInput; // copy

    

    // add to the 'list'

    employeeList* new_node = malloc(sizeof(employeeList)); // allot new memory for node in the list

    new_node->employee = new_employee; // copy pointer

    if(list != NULL) // if list is not empty

        new_node->next = list->next; // adding before the head

    else

        new_node->next = NULL; // first node in the list (so next node is null)

    list = new_node; // create the new head

}

----------------------------------------------------------------------------------------------------------------------------------------------

void displayEmployeeList(struct employeeList *tempList)

{

    // YOUR CODE HERE

    if(tempList != NULL) // till list doesn't end

    {

        printf("%s\n",*(tempList->employee->name)); // display name

        printf(("%d\n",*(tempList->employee->roomNumber))); // display room number

        displayEmployeeList(tempList->next); // resursively display next node

    }

}

------------------------------------------------------------------------------------------------------------------------------------------------

struct employee *searchEmployee(char *employeeNameInput)

{

    struct employeeList *tempList = list; // work on a copy of 'list'

    // YOUR CODE HERE

    while(tempList != NULL);

    {

        if(strcmp (tempList->employee->name, employeeNameInput) == 0 ) // if employee is found (compare the two strings)

        {

            return tempList->employee; // return employee pointer from the node

        }

        tempList=tempList->next; // go to next node if not found

    }

    return NULL;

}

--------------------------------------------------------------------------------------------------------------------------------------------


Related Solutions

8.4 Chapter 8 HW - Problem Mastery 7) LUVFINANCE, Inc. is estimating its WACC. It is...
8.4 Chapter 8 HW - Problem Mastery 7) LUVFINANCE, Inc. is estimating its WACC. It is operating at its optimal capital structure. Its outstanding bonds have a 12 percent coupon, paid semiannually, a current maturity of 17 years, and sell for $1,162.   It has 100,000 bonds outstanding. The firm can issue new 20-year maturity semiannual bonds at par but will incur flotation costs of $50 per bond (Hint: the coupon rate on the new bonds = the YTM on existing...
Write a program that reads in the name and salary of an employee. Here the salary...
Write a program that reads in the name and salary of an employee. Here the salary will denote an hourly wage, such as $9.25. Then, ask how many hours the employee worked in the past week. Be sure to accept fractional hours. Compute the pay. Any overtime work (over 40 hours per week) is paid at 150 percent of the regular wage.4 pts Your code with comments A screenshot of the execution Test Cases: Enter name: Jorge Enter wage: 9.25...
Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create...
Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination).On UNIX and Linux systems,...
Exercise 7: Name that Shape Write a program that determines the name of a shape from...
Exercise 7: Name that Shape Write a program that determines the name of a shape from its number of sides. Read the number of sides from the user and then report the appropriate name as part of a meaningful message. Your program should support shapes with anywhere from 3 up to (and including) 10 sides. If a number of sides outside of this range is entered then your program should display an appropriate error message. language use : python
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
7.Name two advantages to a floating rate exchange regime. 8.Name two disadvantages to a floating rate...
7.Name two advantages to a floating rate exchange regime. 8.Name two disadvantages to a floating rate exchange regime. 9.Name two advantages to a fixed rate exchange regime. 10.Name two disadvantages to a fixed rate exchange regime.
// This program performs a linear search on a character array // Place Your Name Here...
// This program performs a linear search on a character array // Place Your Name Here #include<iostream> using namespace std; int searchList( char[], int, char); // function prototype const int SIZE = 8; int main() { char word[SIZE] = "Harpoon"; int found; char ch; cout << "Enter a letter to search for:" << endl; cin >> ch; found = searchList(word, SIZE, ch); if (found == -1) cout << "The letter " << ch      << " was not found in...
Write the equation in general form. 8x2 + 7 = x2 − 8x + 8 Write...
Write the equation in general form. 8x2 + 7 = x2 − 8x + 8 Write the equation in general form. (y + 1)(y + 3) = 8 Solve the equation by factoring. (Enter your answers as a comma-separated list.) x2 − 8x = 48 4. Consider the quadratic equation x = x2. Rewrite the equation in general form. 0 = Factor the right side of the equation. 0 = Solve the equation. (Enter your answers as a comma-separated list.)...
[The information presented here applies to questions 7, 8 and 9.] The Smith family has borrowed...
[The information presented here applies to questions 7, 8 and 9.] The Smith family has borrowed $320,000 using a 5/1 ARM with an initial fully-indexed rate of 4.25% and paid no points at origination. The fully-indexed rate is determined by the yield on the LIBOR index plus a margin of 250 basis points. 7. What was the yield on the LIBOR index at the time when the Smith family purchased their house? 8. What is the balance remaining of their...
Write a machine language program to output your name on the output device. The name you...
Write a machine language program to output your name on the output device. The name you output must be longer than two characters. Write it in a format suitable for the loader and execute it on the Pep/9 simulator. my name is Kevin
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT