Advertisement
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(){};
- };
- 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;
- }
- 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)
- {
- if(current_size < 1)
- {
- cout<<"\nList Is Empty !!!\n\n";
- return;
- }
- 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";
- }
- }
- int main()
- {
- 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-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";
- 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
- {
- cout<<"Enter New Student Details:\n"
- <<"ID ["<<students_list[search_result].ID <<"] ==> ";
- cin>>students_list[search_result].ID;
- 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
- {
- 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):{
- cout<<"Good Bye !!!\n";
- break;}
- default:
- cout<<"This option not exist !!!\n";
- }
- } while(choice != 6);
- delete []students_list;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement