UMIT_GATH

DS - HW#2

Dec 22nd, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.47 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int n=5;
  7. int current_size = 0;
  8.  
  9.  
  10.  
  11.  
  12. class student {
  13.  
  14. public:
  15.     int ID;
  16.     string first_name;
  17.     string last_name;
  18.     int age;
  19.     student(int ID , string first_name , string last_name , int age);
  20.     student(){};
  21.     static student clone(student target);
  22.  
  23. };
  24.  
  25. student::student(int ID , string first_name , string last_name , int age)
  26. {
  27.     this->ID = ID;
  28.     this->first_name = first_name;
  29.     this->last_name = last_name;
  30.     this->age = age;
  31. }
  32.  
  33.  
  34. student student::clone(student target)
  35.     {
  36.         student newStudent;
  37.         newStudent.age = target.age;
  38.         newStudent.first_name = target.first_name;
  39.         newStudent.last_name = target.last_name;
  40.         newStudent.ID = target.ID;
  41.         return newStudent;
  42.     }
  43.  
  44. bool isFull()
  45. {
  46.     if(current_size < n) return false;
  47.     else return true;
  48. }
  49.  
  50. bool isEmpty()
  51. {
  52.     if(current_size > 0) return false;
  53.     else return true;
  54. }
  55.  
  56.  
  57. void Increase_Array_Size(student* &arr)
  58. {
  59.           student* temp_array = new student[n+5];
  60.  
  61.         for(int i=0 ; i<n ; i++)
  62.             { // copy old elements
  63.                 temp_array[i].ID = arr[i].ID;
  64.                 temp_array[i].first_name = arr[i].first_name;
  65.                 temp_array[i].last_name = arr[i].last_name;
  66.                 temp_array[i].age = arr[i].age;
  67.  
  68.             }
  69.  
  70.             delete [] arr;
  71.  
  72.             arr = temp_array;
  73.             n+=5;
  74. }
  75.  
  76.  
  77.  
  78. void Decrease_Array_Size(student* &arr)
  79. {
  80.           student* temp_array = new student[n-1];
  81.  
  82.         for(int i=0 ; i<n-1 ; i++)
  83.             { // copy old elements
  84.                 temp_array[i].ID = arr[i].ID;
  85.                 temp_array[i].first_name = arr[i].first_name;
  86.                 temp_array[i].last_name = arr[i].last_name;
  87.                 temp_array[i].age = arr[i].age;
  88.  
  89.             }
  90.  
  91.             delete [] arr;
  92.  
  93.             arr = temp_array;
  94.             n-=1;
  95. }
  96.  
  97.  
  98. int Find_Student(student* &arr,int ID)
  99. {
  100.         for(int i=0; i<current_size ; i++)
  101.             if(arr[i].ID == ID) return i;
  102.  
  103. return -1;
  104. }
  105.  
  106.  
  107. void Add_Student(student* &arr , student newStudent)
  108. {
  109.     if(isFull())
  110.     {
  111.         Increase_Array_Size(arr);
  112.     }
  113.  
  114.     arr[current_size] = newStudent;
  115.     current_size++;
  116.  
  117. }
  118.  
  119. bool Delete_Student(student* &arr , int studentIndex)
  120. {
  121.     if(isEmpty())
  122.     {
  123.         return false;
  124.     }
  125.  
  126.  
  127.     for(int i=studentIndex; i<current_size-1 ; i++) // left shift to ignore Last element and delete it
  128.     {
  129.  
  130.         arr[i] = arr[i+1];
  131.  
  132.     }
  133.  
  134.     current_size--;
  135.  
  136.     Decrease_Array_Size(arr);
  137.  
  138.     return true;
  139.  
  140. }
  141.  
  142. bool Edit_Student(student* &arr , student &anStudent,int ID , string new_first_name , string new_last_name , int age)
  143. {
  144.     if(Find_Student(arr,ID) != -1) return false; // new id is already exist
  145.  
  146.  
  147.  anStudent.ID = ID;
  148.  anStudent.first_name = new_first_name;
  149.  anStudent.last_name = new_last_name;
  150.  anStudent.age = age;
  151.  
  152.  return true;
  153. }
  154.  
  155. void Print_Students(student *arr)
  156. {
  157.     for(int i=0; i<current_size ; i++)
  158.     {
  159.         cout<<"Student ID: "<<arr[i].ID<<"\nStudent First Name: "
  160.         <<arr[i].first_name<<"\nStudent Last Name: "
  161.         <<arr[i].last_name<<"\nStudent Age: "
  162.         <<arr[i].age<<"\n\n";
  163.  
  164.     }
  165. }
  166.  
  167.  
  168.  
  169. struct Action
  170. {
  171.    char operation;
  172.    student Data;
  173.  
  174. };
  175.  
  176.  
  177.  
  178.  
  179. class myStack
  180. {
  181. public:
  182.     int top;
  183.     Action *stack_array;
  184.     int stack_size;
  185.  
  186.     myStack(int stack_size)
  187.     {
  188.        this->top = -1;
  189.        this->stack_array = new Action[stack_size];
  190.        this->stack_size = stack_size;
  191.  
  192.     }
  193.  
  194.     ~myStack()
  195.     {
  196.         delete [] stack_array;
  197.     }
  198.  
  199.  
  200.     void push(Action item)
  201.     {
  202.         if((top+1) >= this->stack_size)
  203.         { cout<<"Stack Overflow\n";
  204.             return;}
  205.  
  206.         stack_array[++this->top] = item;
  207.  
  208.     }
  209.  
  210.     Action pop()
  211.     {
  212.         return (stack_array[this->top--]);
  213.  
  214.     }
  215.  
  216.     Action peek()
  217.     {
  218.         return (stack_array[this->top]);
  219.     }
  220.  
  221.     bool isEmpty()
  222.     {
  223.         return (this->top < 0);
  224.     }
  225.  
  226.     void print() /// this function isn't subject to stack LIFO condition , we made it to check the log
  227.     {
  228.  
  229.         if(this->top > -1)
  230.         {for(int i=0 ; i <= this->top ; i++)
  231.         {
  232.             cout<<this->stack_array[i].operation<<'\t'<<this->stack_array[i].Data.ID<<'\t'<<this->stack_array[i].Data.first_name<<'\n';
  233.         }
  234.  
  235.         cout<<'\n';}
  236.         else
  237.         {
  238.             cout<<"This Stack Is Empty \n\n";
  239.         }
  240.     }
  241.  
  242. };
  243.  
  244.  
  245.  
  246.     void Execute_Redo(student* &students_list , myStack &Undo , myStack &Redo)
  247. {
  248.     Action temp_action;
  249.  
  250.   if(!Redo.isEmpty())
  251.   {
  252.       temp_action = Redo.pop();
  253.  
  254.       if(temp_action.operation == 'A')
  255.         {
  256.             Undo.push(temp_action);
  257.  
  258.             Add_Student(students_list,temp_action.Data);
  259.  
  260.         }
  261.         else if(temp_action.operation == 'D')
  262.         {
  263.  
  264.                  Undo.push(temp_action);
  265.  
  266.                int search_result = Find_Student(students_list,temp_action.Data.ID);
  267.                Delete_Student(students_list,search_result);
  268.  
  269.         }
  270.         else if(temp_action.operation == 'E')
  271.         {
  272.  
  273.  
  274.             student temp_student;
  275.  
  276.             int search_result = Find_Student(students_list,temp_action.Data.ID);
  277.  
  278.             temp_student.ID = students_list[search_result].ID;
  279.             temp_student.first_name = students_list[search_result].first_name;
  280.             temp_student.last_name = students_list[search_result].last_name;
  281.             temp_student.age = students_list[search_result].age;
  282.  
  283.             students_list[search_result].first_name = temp_action.Data.first_name;
  284.             students_list[search_result].last_name = temp_action.Data.last_name;
  285.             students_list[search_result].age = temp_action.Data.age;
  286.  
  287.             temp_action.Data = temp_student;
  288.  
  289.             Undo.push(temp_action);
  290.  
  291.         }
  292.  
  293.  
  294.  
  295.   }else
  296.   {
  297.       cout<<"Redo is empty !!!\n\n";
  298.   }
  299. }
  300.  
  301.  
  302.     void Execute_Undo(student* &students_list , myStack &Undo , myStack &Redo)
  303. {
  304.     Action temp_action;
  305.  
  306.   if(!Undo.isEmpty())
  307.   {
  308.       temp_action = Undo.pop();
  309.  
  310.       if(temp_action.operation == 'A')
  311.         {
  312.             Redo.push(temp_action);
  313.  
  314.                int search_result = Find_Student(students_list,temp_action.Data.ID);
  315.                Delete_Student(students_list,search_result);
  316.  
  317.         }
  318.         else if(temp_action.operation == 'D')
  319.         {
  320.  
  321.                  Redo.push(temp_action);
  322.  
  323.                  Add_Student(students_list,temp_action.Data);
  324.  
  325.         }
  326.         else if(temp_action.operation == 'E')
  327.         {
  328.  
  329.  
  330.             student temp_student;
  331.  
  332.             int search_result = Find_Student(students_list,temp_action.Data.ID);
  333.  
  334.             temp_student.ID = students_list[search_result].ID;
  335.             temp_student.first_name = students_list[search_result].first_name;
  336.             temp_student.last_name = students_list[search_result].last_name;
  337.             temp_student.age = students_list[search_result].age;
  338.  
  339.             students_list[search_result].first_name = temp_action.Data.first_name;
  340.             students_list[search_result].last_name = temp_action.Data.last_name;
  341.             students_list[search_result].age = temp_action.Data.age;
  342.  
  343.             temp_action.Data = temp_student;
  344.  
  345.             Redo.push(temp_action);
  346.  
  347.         }
  348.  
  349.  
  350.  
  351.   }else
  352.   {
  353.       cout<<"Undo is empty !!!\n\n";
  354.   }
  355. }
  356.  
  357.  
  358. int main()
  359. {
  360.  
  361.     myStack Undo(30);
  362.     myStack Redo(30);
  363.  
  364.     Action temp_action;
  365.  
  366.     student *students_list = new student[n];
  367.  
  368.  
  369.     int choice = -1;
  370.  
  371.     student tempStudent;
  372.  
  373.     do{
  374.  
  375.             cout<<"\n\n######## Students Manager V1 ########\n\n"
  376.             <<"1-Add new student\n"
  377.             <<"2-Edit exist student\n"
  378.             <<"3-Delete exist student\n"
  379.             <<"4-Find exist student\n"
  380.             <<"5-Print All Students\n"
  381.             <<"6-Undo Last Action\n"
  382.             <<"7-Redo Last Action\n"
  383.             <<"8-Print Undo Log (LIFO)\n"
  384.             <<"9-Print Redo Log (LIFO)\n"
  385.             <<"10-Exit\n\n"
  386.             <<"Enter Your Choice ==> ";
  387.  
  388.             cin>>choice;
  389.  
  390.             switch(choice)
  391.             {
  392.             case (1):{
  393.  
  394.  
  395.                 cout<<"Enter Student ID: ";
  396.                 cin>>tempStudent.ID;
  397.  
  398.                 while(Find_Student(students_list,tempStudent.ID) != -1)
  399.                 {
  400.                     cout<<"This ID is already in list \nPlease Enter Valid ID: ";
  401.                     cin>>tempStudent.ID;
  402.                 }
  403.  
  404.                 cout<<"Enter Student First Name: ";
  405.                 cin>>tempStudent.first_name;
  406.  
  407.                 cout<<"Enter Student Last Name: ";
  408.                 cin>>tempStudent.last_name;
  409.  
  410.                 cout<<"Enter Student Age: ";
  411.                 cin>>tempStudent.age;
  412.  
  413.                 Add_Student(students_list,tempStudent);
  414.  
  415.                 cout<<"\nStudent Added Succefully !\n\n";
  416.  
  417.                 temp_action.operation = 'A';
  418.                 temp_action.Data = student::clone(tempStudent);
  419.                 Undo.push(temp_action);
  420.  
  421.             break;}
  422.  
  423.             case (2):{
  424.  
  425.  
  426.                 cout<<"Enter Student ID: ";
  427.                 cin>>tempStudent.ID;
  428.  
  429.                 int search_result = Find_Student(students_list,tempStudent.ID);
  430.  
  431.                 if(search_result == -1)
  432.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  433.                 else
  434.                     {
  435.  
  436.                         temp_action.operation = 'E'; // add undo before edit
  437.                         temp_action.Data = student::clone(students_list[search_result]);
  438.                         Undo.push(temp_action);
  439.  
  440.                         cout<<"Enter New Student Details:\n";
  441.  
  442.                         cout<<"First Name ["<<students_list[search_result].first_name <<"] ==> ";
  443.                           cin>>students_list[search_result].first_name;
  444.  
  445.                         cout<<"Last Name ["<<students_list[search_result].last_name <<"] ==> ";
  446.                           cin>>students_list[search_result].last_name;
  447.  
  448.                         cout<<"Age ["<<students_list[search_result].age <<"] ==> ";
  449.                           cin>>students_list[search_result].age;
  450.  
  451.  
  452.                     }
  453.  
  454.             break;}
  455.  
  456.  
  457.             case (3):{
  458.  
  459.                 cout<<"Enter Student ID: ";
  460.                 cin>>tempStudent.ID;
  461.  
  462.                 int search_result = Find_Student(students_list,tempStudent.ID);
  463.  
  464.                 if(search_result == -1)
  465.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  466.                 else
  467.                     {
  468.  
  469.  
  470.                         temp_action.operation = 'D'; /// must be added before delete
  471.                         temp_action.Data = student::clone(students_list[search_result]);
  472.                         Undo.push(temp_action);
  473.  
  474.  
  475.                         Delete_Student(students_list,search_result);
  476.                         cout<<"Student at location ["<<search_result<<"] Have Been Deleted !!\n\n";
  477.                     }
  478.  
  479.  
  480.             break;}
  481.  
  482.             case (4):{
  483.  
  484.                 cout<<"Enter Student ID: ";
  485.                 cin>>tempStudent.ID;
  486.  
  487.                 int search_result = Find_Student(students_list,tempStudent.ID);
  488.  
  489.                 if(search_result == -1)
  490.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  491.                 else
  492.                     {
  493.                         cout<<"Student Details:\n"
  494.                           <<"ID => "<<students_list[search_result].ID <<endl
  495.                           <<"First Name => "<<students_list[search_result].first_name <<endl
  496.                           <<"Last Name => "<<students_list[search_result].last_name <<endl
  497.                           <<"Age => "<<students_list[search_result].age <<"\n\n";
  498.                     }
  499.  
  500.             break;}
  501.  
  502.             case (5):{
  503.                 Print_Students(students_list);
  504.             break;}
  505.  
  506.             case (6):{
  507.                 Execute_Undo(students_list,Undo,Redo);
  508.             break;}
  509.  
  510.             case (7):{
  511.                 Execute_Redo(students_list,Undo,Redo);
  512.             break;}
  513.  
  514.             case (8):{
  515.                 Undo.print();
  516.             break;}
  517.  
  518.             case (9):{
  519.                 Redo.print();
  520.             break;}
  521.  
  522.             case (10):{
  523.                 cout<<"Good Bye !!!\n";
  524.             break;}
  525.  
  526.             default:
  527.                 cout<<"This option not exist !!!\n";
  528.             }
  529.  
  530.  
  531.     } while(choice != 10);
  532.  
  533.  
  534.     delete []students_list;
  535.     return 0;
  536. }
  537.  
Add Comment
Please, Sign In to add comment