اذهب إلى المحتوى
  • 0

خوارزمية الترتيب بلغة C++

Aya alsalam

السؤال

السلام عليكم 

هذا السؤال لاحظت وجوده ظمن الاسئلة المرسلة من قبل الاعضاء ولكن هل يمكنكم مساعدتي بالحل بطريقة جديدة افيدوني رجائاً

Write a Name_pairs class holding three members (name, age, size) where name is an array of
strings, age is an array of ints, and size is the size of name and age arrays. Write a destructor and
constructor that accepts one input argument which the size. Provide an input operation (method)
read_names that reads a series of names. Provide a read_ages operation that prompts the user
for an age for each name. Provide a print operation that prints all the (name[i], age[i]) pairs (one
per line) in the order determined by the name array. Provide a sort operation that sorts the name
array in alphabetical order and reorganizes the age array to match. Create a C++ program and let
user enter 5 names and ages and then use print method to print them. Sort the name array and
print it again.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

أهلاً بك ايه، 

هذا كود كامل للبرنامج المطلوب بلغة C++، 

#include "stdafx.h"
#include "std_lib_facilities.h"

class Name_pairs
{
private:
 vector<string> name;
 vector<double> age;
public:
 void read_names(int iterator); //قراءة مصفوفة الاسماء
 void read_ages();  //قراءة الأعمار
 void print();   //دالة للطباعة النتائج
 void sortNP();   //دالة التخزين 
};

void Name_pairs::read_names(int iterator)
{
 string names;
 for (int i = 0; i < iterator; ++i)
 {
  cout << "Name: ";
  cin >> names;
  name.push_back(names);
  cout << endl;
 }
}

void Name_pairs::read_ages()
{
 double ages;
 for (int i = 0; i < name.size(); ++i)
 {
  cout << "Age for " << name[i] << ": ";
  cin >> ages;
  age.push_back(ages);
  cout << endl;
 }
}

void Name_pairs::print()
{
 cout << endl;
 for (int i = 0; i < name.size(); ++i)
  cout << "Name: " << name[i] << "     Age: " << age[i] << endl;
}

void Name_pairs::sortNP()
{
 vector<string> name_copy = name;
 vector<double> age_copy = age;

 sort(name.begin(), name.end());  //تخزين الاسماء

 //تكرار يمر على مصفوفة الاسماء
 for (int i = 0; i < name.size(); ++i)
 {
  //تكرار يمر على كل عنصر في المصفوفة 
  for (int j = 0; j < name.size(); ++j)
  {
   //فحص عملية التخزين
   if (name[i] == name_copy[j])
   {
    //assign original age to new position to match sorted vector
    age[i] = age_copy[j];
   }
  }
 }
}

int main()
{
 cout << "How many names to read in? > ";
 int howMany;
 cin >> howMany;

 Name_pairs namePair;

 //read in names
 namePair.read_names(howMany);

 //read in ages
 namePair.read_ages();

 //print
 namePair.print();

 //sort namePair alphabetically
 namePair.sortNP();

 //print
 namePair.print();

 keep_window_open();

 return 0;
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

مرحباً ...
حل هذا السؤال موجود في هذا الرابط 

و خوارزمية الترتيب المُستخدمة هي خوارزمية الترتيب بالتحديد و هناك العديد من خوارزميات الترتيب يُمكنك الإطلاع عليها و قراءة الشرح الخاص بها من هنا: خوارزميات الترتيب على موسوعة حسوب 
و هنا نفس المثال السابق لكن بإستعمال خوارزمية الترتيب بالإدراج

#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
    
}

لتجربة البرنامج: إضغط هنا 

بالتوفيق

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...