الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ممكن مساعدة

بدأه QJK في 9 فبراير 2012 · 2 رد · 372 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

عندي هذا الكلاس

class Dictionary
{
public:
	typedef string key_type;				// data type of keys
	typedef int value_type;				// data type of values
	typedef unsigned int size_type;			// data type of dictionary counts and / or sizes
	Dictionary();					// creates a dictionary (maximum size <= 100)

	value_type& operator[]( const key_type& k );		// return a reference to the value indexed by key k
	value_type& at( const key_type& k );			// return a reference to the value indexed by key k 

	void insert( const key_type& k, const value_type& v );	// insert the key-value pair in the dictionary
	void merge( const Dictionary& d );			// merge dictionary d into this disctionary

	bool find( const key_type& k ) const;			// return true if the key is in the dictionary

	bool operator== ( const Dictionary& d ) const;		// returns true if the d contains the same key-value pairs as this dictionary
	bool operator< ( const Dictionary& d ) const;		// returns true if all key-values pairs in d are in this dictionary
	bool empty() const;					// returns true if the dictionary is empty
	size_type size() const;				// returns the size of the dictionary
	size_type maxSize() const;				// returns the maximum size of the dictionary

	void clear();					// clears the dictionary returning it to the empty state
	void erase( const key_type& k );			// erase key-value pair indexed by key k

	ostream& print( ostream& = cout );			// prints the key-value items key-value pairs e.g. (k,v),(k,v),...
	void load( string fileName );				// loads the dictionary from a file named fileName
	void save( string fileName ) const;			// saves the dictionary to a file named fileName
private:
	static const size_type maxEntry = 100;			// suggested layout for private information
	key_type keys[ maxEntry];
	value_type values[ maxEntry ];
	size_type numEntry;
};

ومحتاج إلى كتابة كود لدالة ... تقوم بترجيع إذا كان الدقشنري ممتلئ

bool Dictionary::full() const;

ممكن مساعدة؟؟

:blush:

#2

الفكرة تدور حول المتغير numEntry عندما نضيف عنصر جديد للقاموس فأننا نزيدة وعند حذف عنصر فأننا ننقصة بمقدار عدد العناصر المضافة او المحذوفة

متابعة قيمة المتغيرات يحتاج منك الى تركيز اكثر وحساب قيمتها بشكل تام ﻻنة قد يحدث خطأ buffer overflow

  1. يفضل دمج كود الدالة full() بداخل الدالة insert() وذلك حتى يعمل الكود فى صمت تام وترجع الدالة insert() قيمة بولونية أم بنجاح اﻻدراج او فشلة
  2. انصحك بأستخدام متغيرات VECTOR ستغنيك عن numEntry و ستسمح لك بزيادة حجم المصفوفة بدون مشاكل ...

/* 
 * File:   main.cpp
 * Author: Mohammed Abu Zayed
 *
 * Created on February 9, 2012, 8:36 AM
 */
#include <cstdlib>
#include <iostream>
using namespace std;

class Dictionary
{
public:
    // typedef string string; // data type of keys
    // typedef int int; // data type of values
    // typedef unsigned int unsigned int; // data type of dictionary counts and / or sizes

    Dictionary()
    {
        numEntry = 0;
    }
    // creates a dictionary (maximum size <= 100)
    int& operator[](const string& k); // return a reference to the value indexed by key k
    int& at(const string& k); // return a reference to the value indexed by key k 
    void insert(const string& k, const int& v); // insert the key-value pair in the dictionary
    void merge(const Dictionary& d); // merge dictionary d into this disctionary
    bool find(const string& k) const; // return true if the key is in the dictionary
    bool operator==(const Dictionary& d) const; // returns true if the d contains the same key-value pairs as this dictionary
    bool operator<(const Dictionary& d) const; // returns true if all key-values pairs in d are in this dictionary
    bool empty() const; // returns true if the dictionary is empty
    bool full() const;
    unsigned int size() const; // returns the size of the dictionary
    unsigned int maxSize() const; // returns the maximum size of the dictionary
    void clear(); // clears the dictionary returning it to the empty state
    void erase(const string& k); // erase key-value pair indexed by key k
    ostream& print(ostream& = cout); // prints the key-value items key-value pairs e.g. (k,v),(k,v),...
    void load(string fileName); // loads the dictionary from a file named fileName
    void save(string fileName) const; // saves the dictionary to a file named fileName
private:
    static const unsigned int maxEntry = 10; // suggested layout for private information 
    unsigned int numEntry; // nums form 0 to 99 = 100 elements
    string keys[ maxEntry];
    int values[ maxEntry ];
};
// return TRUE for full FALSE not full 

bool Dictionary::full() const
{
    if (numEntry > maxEntry) return 1;
    else return 0;
}

void Dictionary::insert(const string& k, const int& v)
{
    keys[numEntry] = k;
    values[numEntry] = v;
    numEntry++;
}

/*
 * 
 */
int main(void)
{
    Dictionary dic1;
    int i = 0;
    for (i = 0; i < 20; i++)
    {
        if (!dic1.full())
        {
            dic1.insert("Mohammed", i + 1000);
            cout << "Inserted: " << i + 1 << endl;
        }        
    }
    return 0;
} // end of main

تحياتى العطرة والله الموفق ... .

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#3

شكراً لك أخي الكريم

السؤال كان بده كذا

وشكراً على النصيحة يالغالي

مواضيع مشابهة