Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n=5;
- int current_size = 0;
- class student {
- public:
- int ID;
- string first_name;
- string last_name;
- int age;
- student(int ID , string first_name , string last_name , int age);
- student(){};
- static student clone(student target);
- };
- student::student(int ID , string first_name , string last_name , int age)
- {
- this->ID = ID;
- this->first_name = first_name;
- this->last_name = last_name;
- this->age = age;
- }
- student student::clone(student target)
- {
- student newStudent;
- newStudent.age = target.age;
- newStudent.first_name = target.first_name;
- newStudent.last_name = target.last_name;
- newStudent.ID = target.ID;
- return newStudent;
- }
- bool isFull()
- {
- if(current_size < n) return false;
- else return true;
- }
- bool isEmpty()
- {
- if(current_size > 0) return false;
- else return true;
- }
- void Increase_Array_Size(student* &arr)
- {
- student* temp_array = new student[n+5];
- for(int i=0 ; i<n ; i++)
- { // copy old elements
- temp_array[i].ID = arr[i].ID;
- temp_array[i].first_name = arr[i].first_name;
- temp_array[i].last_name = arr[i].last_name;
- temp_array[i].age = arr[i].age;
- }
- delete [] arr;
- arr = temp_array;
- n+=5;
- }
- void Decrease_Array_Size(student* &arr)
- {
- student* temp_array = new student[n-1];
- for(int i=0 ; i<n-1 ; i++)
- { // copy old elements
- temp_array[i].ID = arr[i].ID;
- temp_array[i].first_name = arr[i].first_name;
- temp_array[i].last_name = arr[i].last_name;
- temp_array[i].age = arr[i].age;
- }
- delete [] arr;
- arr = temp_array;
- n-=1;
- }
- int Find_Student(student* &arr,int ID)
- {
- for(int i=0; i<current_size ; i++)
- if(arr[i].ID == ID) return i;
- return -1;
- }
- void Add_Student(student* &arr , student newStudent)
- {
- if(isFull())
- {
- Increase_Array_Size(arr);
- }
- arr[current_size] = newStudent;
- current_size++;
- }
- bool Delete_Student(student* &arr , int studentIndex)
- {
- if(isEmpty())
- {
- return false;
- }
- for(int i=studentIndex; i<current_size-1 ; i++) // left shift to ignore Last element and delete it
- {
- arr[i] = arr[i+1];
- }
- current_size--;
- Decrease_Array_Size(arr);
- return true;
- }
- bool Edit_Student(student* &arr , student &anStudent,int ID , string new_first_name , string new_last_name , int age)
- {
- if(Find_Student(arr,ID) != -1) return false; // new id is already exist
- anStudent.ID = ID;
- anStudent.first_name = new_first_name;
- anStudent.last_name = new_last_name;
- anStudent.age = age;
- return true;
- }
- void Print_Students(student *arr)
- {
- for(int i=0; i<current_size ; i++)
- {
- cout<<"Student ID: "<<arr[i].ID<<"\nStudent First Name: "
- <<arr[i].first_name<<"\nStudent Last Name: "
- <<arr[i].last_name<<"\nStudent Age: "
- <<arr[i].age<<"\n\n";
- }
- }
- struct Action
- {
- char operation;
- student Data;
- };
- class myStack
- {
- public:
- int top;
- Action *stack_array;
- int stack_size;
- myStack(int stack_size)
- {
- this->top = -1;
- this->stack_array = new Action[stack_size];
- this->stack_size = stack_size;
- }
- ~myStack()
- {
- delete [] stack_array;
- }
- void push(Action item)
- {
- if((top+1) >= this->stack_size)
- { cout<<"Stack Overflow\n";
- return;}
- stack_array[++this->top] = item;
- }
- Action pop()
- {
- return (stack_array[this->top--]);
- }
- Action peek()
- {
- return (stack_array[this->top]);
- }
- bool isEmpty()
- {
- return (this->top < 0);
- }
- void print() /// this function isn't subject to stack LIFO condition , we made it to check the log
- {
- if(this->top > -1)
- {for(int i=0 ; i <= this->top ; i++)
- {
- cout<<this->stack_array[i].operation<<'\t'<<this->stack_array[i].Data.ID<<'\t'<<this->stack_array[i].Data.first_name<<'\n';
- }
- cout<<'\n';}
- else
- {
- cout<<"This Stack Is Empty \n\n";
- }
- }
- };
- void Execute_Redo(student* &students_list , myStack &Undo , myStack &Redo)
- {
- Action temp_action;
- if(!Redo.isEmpty())
- {
- temp_action = Redo.pop();
- if(temp_action.operation == 'A')
- {
- Undo.push(temp_action);
- Add_Student(students_list,temp_action.Data);
- }
- else if(temp_action.operation == 'D')
- {
- Undo.push(temp_action);
- int search_result = Find_Student(students_list,temp_action.Data.ID);
- Delete_Student(students_list,search_result);
- }
- else if(temp_action.operation == 'E')
- {
- student temp_student;
- int search_result = Find_Student(students_list,temp_action.Data.ID);
- temp_student.ID = students_list[search_result].ID;
- temp_student.first_name = students_list[search_result].first_name;
- temp_student.last_name = students_list[search_result].last_name;
- temp_student.age = students_list[search_result].age;
- students_list[search_result].first_name = temp_action.Data.first_name;
- students_list[search_result].last_name = temp_action.Data.last_name;
- students_list[search_result].age = temp_action.Data.age;
- temp_action.Data = temp_student;
- Undo.push(temp_action);
- }
- }else
- {
- cout<<"Redo is empty !!!\n\n";
- }
- }
- void Execute_Undo(student* &students_list , myStack &Undo , myStack &Redo)
- {
- Action temp_action;
- if(!Undo.isEmpty())
- {
- temp_action = Undo.pop();
- if(temp_action.operation == 'A')
- {
- Redo.push(temp_action);
- int search_result = Find_Student(students_list,temp_action.Data.ID);
- Delete_Student(students_list,search_result);
- }
- else if(temp_action.operation == 'D')
- {
- Redo.push(temp_action);
- Add_Student(students_list,temp_action.Data);
- }
- else if(temp_action.operation == 'E')
- {
- student temp_student;
- int search_result = Find_Student(students_list,temp_action.Data.ID);
- temp_student.ID = students_list[search_result].ID;
- temp_student.first_name = students_list[search_result].first_name;
- temp_student.last_name = students_list[search_result].last_name;
- temp_student.age = students_list[search_result].age;
- students_list[search_result].first_name = temp_action.Data.first_name;
- students_list[search_result].last_name = temp_action.Data.last_name;
- students_list[search_result].age = temp_action.Data.age;
- temp_action.Data = temp_student;
- Redo.push(temp_action);
- }
- }else
- {
- cout<<"Undo is empty !!!\n\n";
- }
- }
- int main()
- {
- myStack Undo(30);
- myStack Redo(30);
- Action temp_action;
- student *students_list = new student[n];
- int choice = -1;
- student tempStudent;
- do{
- cout<<"\n\n######## Students Manager V1 ########\n\n"
- <<"1-Add new student\n"
- <<"2-Edit exist student\n"
- <<"3-Delete exist student\n"
- <<"4-Find exist student\n"
- <<"5-Print All Students\n"
- <<"6-Undo Last Action\n"
- <<"7-Redo Last Action\n"
- <<"8-Print Undo Log (LIFO)\n"
- <<"9-Print Redo Log (LIFO)\n"
- <<"10-Exit\n\n"
- <<"Enter Your Choice ==> ";
- cin>>choice;
- switch(choice)
- {
- case (1):{
- cout<<"Enter Student ID: ";
- cin>>tempStudent.ID;
- while(Find_Student(students_list,tempStudent.ID) != -1)
- {
- cout<<"This ID is already in list \nPlease Enter Valid ID: ";
- cin>>tempStudent.ID;
- }
- cout<<"Enter Student First Name: ";
- cin>>tempStudent.first_name;
- cout<<"Enter Student Last Name: ";
- cin>>tempStudent.last_name;
- cout<<"Enter Student Age: ";
- cin>>tempStudent.age;
- Add_Student(students_list,tempStudent);
- cout<<"\nStudent Added Succefully !\n\n";
- temp_action.operation = 'A';
- temp_action.Data = student::clone(tempStudent);
- Undo.push(temp_action);
- break;}
- case (2):{
- cout<<"Enter Student ID: ";
- cin>>tempStudent.ID;
- int search_result = Find_Student(students_list,tempStudent.ID);
- if(search_result == -1)
- {cout<<"This Student Dosn't Exist !!\n\n";}
- else
- {
- temp_action.operation = 'E'; // add undo before edit
- temp_action.Data = student::clone(students_list[search_result]);
- Undo.push(temp_action);
- cout<<"Enter New Student Details:\n";
- cout<<"First Name ["<<students_list[search_result].first_name <<"] ==> ";
- cin>>students_list[search_result].first_name;
- cout<<"Last Name ["<<students_list[search_result].last_name <<"] ==> ";
- cin>>students_list[search_result].last_name;
- cout<<"Age ["<<students_list[search_result].age <<"] ==> ";
- cin>>students_list[search_result].age;
- }
- break;}
- case (3):{
- cout<<"Enter Student ID: ";
- cin>>tempStudent.ID;
- int search_result = Find_Student(students_list,tempStudent.ID);
- if(search_result == -1)
- {cout<<"This Student Dosn't Exist !!\n\n";}
- else
- {
- temp_action.operation = 'D'; /// must be added before delete
- temp_action.Data = student::clone(students_list[search_result]);
- Undo.push(temp_action);
- Delete_Student(students_list,search_result);
- cout<<"Student at location ["<<search_result<<"] Have Been Deleted !!\n\n";
- }
- break;}
- case (4):{
- cout<<"Enter Student ID: ";
- cin>>tempStudent.ID;
- int search_result = Find_Student(students_list,tempStudent.ID);
- if(search_result == -1)
- {cout<<"This Student Dosn't Exist !!\n\n";}
- else
- {
- cout<<"Student Details:\n"
- <<"ID => "<<students_list[search_result].ID <<endl
- <<"First Name => "<<students_list[search_result].first_name <<endl
- <<"Last Name => "<<students_list[search_result].last_name <<endl
- <<"Age => "<<students_list[search_result].age <<"\n\n";
- }
- break;}
- case (5):{
- Print_Students(students_list);
- break;}
- case (6):{
- Execute_Undo(students_list,Undo,Redo);
- break;}
- case (7):{
- Execute_Redo(students_list,Undo,Redo);
- break;}
- case (8):{
- Undo.print();
- break;}
- case (9):{
- Redo.print();
- break;}
- case (10):{
- cout<<"Good Bye !!!\n";
- break;}
- default:
- cout<<"This option not exist !!!\n";
- }
- } while(choice != 10);
- delete []students_list;
- return 0;
- }
Add Comment
Please, Sign In to add comment