Implement Scheduleadd based on the following rules If the Sc

Implement Schedule::add based on the following rules.

If the Schedule is empty, add the course, update the total credit hours, and return true.

If the Schedule is not empty, examine the linked list.

If a matching course is found, return false. Students can not enroll in courses with the same course number.

If no matching course is found, check the credit hours of the new course and existing total. If the this new course along with the existing courses would exceed 12 hours, return false and do not add the course.

If the total credit hours will not exceed 12, and the course number is unique, add the new course and return true.

Solution

//Following is the C++ code for given problem statement.

// In this case Schedule class created in which add() method is used for adding new element and print() //method for printing all courses enrolled by that student.

//isEmpty() method is used to check whether any course is enrolled or not.

//Each course is a instance of struct course int which its hour and no is stored;

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct course
{
   int course_no;
   int course_hour;
   course *next;
};
class Schedule
{
   struct course *root;
   int total_hour;
public:
   Schedule()
   {
       root=NULL;
       total_hour=0;
   }
   int isEmpty()
   {
       if(root==NULL)
           return 1;
       return 0;
   }
   bool add(struct course *element)
   {
       if(isEmpty())
       {
           struct course *temp=new struct course;
           temp=element;
           total_hour+=temp->course_hour;
           root=element;
           return true;
       }
       else
       {
           struct course *prev=new struct course;
           struct course *temp=root;
           while(temp!=NULL)
           {
               if(temp->course_no==element->course_no)
                   return false;
               prev=temp;
               temp=temp->next;
           }
           if((total_hour+element->course_hour)<=12)
           {
               prev->next=element;
               total_hour+=element->course_hour;
               return true;
           }
           else
               return false;

       }
   }
   void print()
   {
       struct course *temp=root;
       while(temp!=NULL)
       {
           cout<<temp->course_no<<\"\\t\"<<temp->course_hour<<\"\ \";
           temp=temp->next;
       }
       cout<<total_hour<<\"\ \";
   }
};
int main()
{
   int no,hour;
   Schedule s;
   int n;
   cin>>n;
   while(n--)
   {
       cout<<\"enter the course no and course hour\ \";
   cin>>no>>hour;
   struct course *a=new course;
   a->course_hour=hour;
   a->course_no=no;
   a->next=NULL;
   bool f=s.add(a);
   if(f==false)
       cout<<\"Course cant be enroll\ \";
   else
       cout<<\"Course can be enroll\ \";
   }
   s.print();
   return 0;
}

Output :

G580$ g++ Schedule.cpp

G580$ ./a.out
4
enter the course no and course hour
2 5
Course can be enroll
enter the course no and course hour
3 6
Course can be enroll
enter the course no and course hour
1 4
Course cant be enroll
enter the course no and course hour
4 6
Course cant be enroll
2   5
3   6
11 (total number of hourse not exceeding 12)

Implement Schedule::add based on the following rules. If the Schedule is empty, add the course, update the total credit hours, and return true. If the Schedule
Implement Schedule::add based on the following rules. If the Schedule is empty, add the course, update the total credit hours, and return true. If the Schedule
Implement Schedule::add based on the following rules. If the Schedule is empty, add the course, update the total credit hours, and return true. If the Schedule

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site