Advertisement
UMIT_GATH

DS - HW#1

Dec 17th, 2023
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.07 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. class student {
  11.  
  12. public:
  13.     int ID;
  14.     string first_name;
  15.     string last_name;
  16.     int age;
  17.     student(int ID , string first_name , string last_name , int age);
  18.     student(){};
  19.  
  20. };
  21.  
  22. student::student(int ID , string first_name , string last_name , int age)
  23. {
  24.     this->ID = ID;
  25.     this->first_name = first_name;
  26.     this->last_name = last_name;
  27.     this->age = age;
  28. }
  29.  
  30.  
  31. bool isFull()
  32. {
  33.     if(current_size < n) return false;
  34.     else return true;
  35. }
  36.  
  37. bool isEmpty()
  38. {
  39.     if(current_size > 0) return false;
  40.     else return true;
  41. }
  42.  
  43.  
  44. void Increase_Array_Size(student* &arr)
  45. {
  46.           student* temp_array = new student[n+5];
  47.  
  48.         for(int i=0 ; i<n ; i++)
  49.             { // copy old elements
  50.                 temp_array[i].ID = arr[i].ID;
  51.                 temp_array[i].first_name = arr[i].first_name;
  52.                 temp_array[i].last_name = arr[i].last_name;
  53.                 temp_array[i].age = arr[i].age;
  54.  
  55.             }
  56.  
  57.             delete [] arr;
  58.  
  59.             arr = temp_array;
  60.             n+=5;
  61. }
  62.  
  63.  
  64.  
  65. void Decrease_Array_Size(student* &arr)
  66. {
  67.           student* temp_array = new student[n-1];
  68.  
  69.         for(int i=0 ; i<n-1 ; i++)
  70.             { // copy old elements
  71.                 temp_array[i].ID = arr[i].ID;
  72.                 temp_array[i].first_name = arr[i].first_name;
  73.                 temp_array[i].last_name = arr[i].last_name;
  74.                 temp_array[i].age = arr[i].age;
  75.  
  76.             }
  77.  
  78.             delete [] arr;
  79.  
  80.             arr = temp_array;
  81.             n-=1;
  82. }
  83.  
  84.  
  85. int Find_Student(student* &arr,int ID)
  86. {
  87.         for(int i=0; i<current_size ; i++)
  88.             if(arr[i].ID == ID) return i;
  89.  
  90. return -1;
  91. }
  92.  
  93.  
  94. void Add_Student(student* &arr , student newStudent)
  95. {
  96.     if(isFull())
  97.     {
  98.         Increase_Array_Size(arr);
  99.     }
  100.  
  101.     arr[current_size] = newStudent;
  102.     current_size++;
  103.  
  104. }
  105.  
  106. bool Delete_Student(student* &arr , int studentIndex)
  107. {
  108.     if(isEmpty())
  109.     {
  110.         return false;
  111.     }
  112.  
  113.  
  114.     for(int i=studentIndex; i<current_size-1 ; i++) // left shift to ignore Last element and delete it
  115.     {
  116.  
  117.         arr[i] = arr[i+1];
  118.  
  119.     }
  120.  
  121.     current_size--;
  122.  
  123.     Decrease_Array_Size(arr);
  124.  
  125.     return true;
  126.  
  127. }
  128.  
  129. bool Edit_Student(student* &arr , student &anStudent,int ID , string new_first_name , string new_last_name , int age)
  130. {
  131.     if(Find_Student(arr,ID) != -1) return false; // new id is already exist
  132.  
  133.  
  134.  anStudent.ID = ID;
  135.  anStudent.first_name = new_first_name;
  136.  anStudent.last_name = new_last_name;
  137.  anStudent.age = age;
  138.  
  139.  return true;
  140. }
  141.  
  142. void Print_Students(student *arr)
  143. {
  144.  
  145.     if(current_size < 1)
  146.     {
  147.         cout<<"\nList Is Empty !!!\n\n";
  148.         return;
  149.     }
  150.  
  151.     for(int i=0; i<current_size ; i++)
  152.     {
  153.         cout<<"Student ID: "<<arr[i].ID<<"\nStudent First Name: "
  154.         <<arr[i].first_name<<"\nStudent Last Name: "
  155.         <<arr[i].last_name<<"\nStudent Age: "
  156.         <<arr[i].age<<"\n\n";
  157.  
  158.     }
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. int main()
  167. {
  168.  
  169.     student *students_list = new student[n];
  170.  
  171.  
  172.     int choice = -1;
  173.  
  174.     student tempStudent;
  175.  
  176.     do{
  177.  
  178.             cout<<"\n\n######## Students Manager V1 ########\n\n"
  179.             <<"1-Add new student\n"
  180.             <<"2-Edit exist student\n"
  181.             <<"3-Delete exist student\n"
  182.             <<"4-Find exist student\n"
  183.             <<"5-Print All Students\n"
  184.             <<"6-Exit\n\n"
  185.             <<"Enter Your Choice ==> ";
  186.  
  187.             cin>>choice;
  188.  
  189.             switch(choice)
  190.             {
  191.             case (1):{
  192.  
  193.  
  194.                 cout<<"Enter Student ID: ";
  195.                 cin>>tempStudent.ID;
  196.  
  197.                 while(Find_Student(students_list,tempStudent.ID) != -1)
  198.                 {
  199.                     cout<<"This ID is already in list \nPlease Enter Valid ID: ";
  200.                     cin>>tempStudent.ID;
  201.                 }
  202.  
  203.                 cout<<"Enter Student First Name: ";
  204.                 cin>>tempStudent.first_name;
  205.  
  206.                 cout<<"Enter Student Last Name: ";
  207.                 cin>>tempStudent.last_name;
  208.  
  209.                 cout<<"Enter Student Age: ";
  210.                 cin>>tempStudent.age;
  211.  
  212.                 Add_Student(students_list,tempStudent);
  213.  
  214.                 cout<<"\nStudent Added Succefully !\n\n";
  215.  
  216.             break;}
  217.  
  218.             case (2):{
  219.  
  220.  
  221.                 cout<<"Enter Student ID: ";
  222.                 cin>>tempStudent.ID;
  223.  
  224.                 int search_result = Find_Student(students_list,tempStudent.ID);
  225.  
  226.                 if(search_result == -1)
  227.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  228.                 else
  229.                     {
  230.                         cout<<"Enter New Student Details:\n"
  231.                           <<"ID ["<<students_list[search_result].ID <<"] ==> ";
  232.                           cin>>students_list[search_result].ID;
  233.  
  234.                         cout<<"First Name ["<<students_list[search_result].first_name <<"] ==> ";
  235.                           cin>>students_list[search_result].first_name;
  236.  
  237.                         cout<<"Last Name ["<<students_list[search_result].last_name <<"] ==> ";
  238.                           cin>>students_list[search_result].last_name;
  239.  
  240.                         cout<<"Age ["<<students_list[search_result].age <<"] ==> ";
  241.                           cin>>students_list[search_result].age;
  242.                     }
  243.  
  244.             break;}
  245.  
  246.  
  247.             case (3):{
  248.  
  249.                 cout<<"Enter Student ID: ";
  250.                 cin>>tempStudent.ID;
  251.  
  252.                 int search_result = Find_Student(students_list,tempStudent.ID);
  253.  
  254.                 if(search_result == -1)
  255.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  256.                 else
  257.                     {
  258.                         Delete_Student(students_list,search_result);
  259.                         cout<<"Student at location ["<<search_result<<"] Have Been Deleted !!\n\n";
  260.                     }
  261.  
  262.  
  263.             break;}
  264.  
  265.             case (4):{
  266.  
  267.                 cout<<"Enter Student ID: ";
  268.                 cin>>tempStudent.ID;
  269.  
  270.                 int search_result = Find_Student(students_list,tempStudent.ID);
  271.  
  272.                 if(search_result == -1)
  273.                     {cout<<"This Student Dosn't Exist !!\n\n";}
  274.                 else
  275.                     {
  276.                         cout<<"Student Details:\n"
  277.                           <<"ID => "<<students_list[search_result].ID <<endl
  278.                           <<"First Name => "<<students_list[search_result].first_name <<endl
  279.                           <<"Last Name => "<<students_list[search_result].last_name <<endl
  280.                           <<"Age => "<<students_list[search_result].age <<"\n\n";
  281.                     }
  282.  
  283.             break;}
  284.  
  285.             case (5):{
  286.                 Print_Students(students_list);
  287.             break;}
  288.  
  289.             case (6):{
  290.                 cout<<"Good Bye !!!\n";
  291.             break;}
  292.  
  293.             default:
  294.                 cout<<"This option not exist !!!\n";
  295.             }
  296.  
  297.  
  298.     } while(choice != 6);
  299.  
  300.  
  301.     delete []students_list;
  302.     return 0;
  303. }
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement