/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
using namespace std;
// تعريف الكلاس
class Name_pairs {
// تعريف خصائص الكلاس
public:
int size;
string *names;
int *age;
// constructor
public:
Name_pairs(int size) {
cout <<"Constructor is called" <<endl;
this->size = size;
this->names = new string[size];
this->age = new int [size];
}
// destructor
public:
~Name_pairs() {
cout <<"\nDestructor is called" <<endl;
delete []names;
delete []age;
}
// تعريف الميثود المسؤولة عن قراءة الأسماء
void read_names() {
for (int i = 0; i < this->size; i++) {
cout << "Enter name " << (i+1) << ": "<<endl;
cin >> this->names[i];
}
}
// تعريف الميثود المسؤولة عن قراءة الأعمار
void read_ages() {
for (int i = 0; i < this->size; i++) {
cout << "Enter age " << (i+1) << ": "<<endl;
cin >> this->age[i];
}
}
// طباعة السجلات
void print() {
for (int i = 0; i < this->size; i++) {
cout << "record " << (i+1) << ": (" << this->names[i] << ", " << this->age[i] << ")"<<endl;
}
}
// تطبيق خوارزمية الترتيب بالإدراج
void sort() {
int i;
for(i = 1; i < this->size; i++) {
string key = this->names[i];
int tmp_age = this->age[i];
int j = i - 1;
while (j >= 0 && this->names[j][0] > key[0]) {
this->names[j + 1] = this->names[j];
this->age[j + 1] = this->age[j];
j = j - 1;
}
this->names[j + 1] = key;
this->age[j + 1] = tmp_age;
}
}
};
int main() {
// create instance from Name_paires class
Name_pairs obj(5);
obj.read_names(); // read names
obj.read_ages(); // read ages
cout << "\nRecords Before Sorting: " << endl;
obj.print(); // print records before sorting
obj.sort(); // call sort method
cout << "\nRecords After Sorting: " << endl;
obj.print(); // print records after sorting
}