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

سؤال عن المصفوفات

بدأه shofata.heart في 6 مارس 2012 · 3 رد · 666 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

اخباركم إيش عاملين

عندي طلب صغير

كتبت كود لاراي اتعامل معاها علي انها ستاك
طول الاراي كان ثابت ودحين ابغاه يكون مدخل من اليوزر كيف ؟؟

هذا هو الكود

header file

وهنا عرف الكلاس إلي فيه الدوال إلي راح اعملها في الستاك

class StackType{public:StackType();void MakeEmpty();bool IsEmpty () const ; // ·„  ÷⁄Ì «·«fiÊ«”bool IsFull () const ; // ·„  ÷⁄Ì «·«fiÊ«”void Push( int item);void Pop (int & item);void DisplayStack();private:int top;int items[10];};وهذا ملف ثاني كتبت فيه الآكواد حقت الدوال#include <iostream>#include"StackType.h"using namespace std;StackType::StackType(){top=-1;}void StackType::MakeEmpty(){top=-1;}bool StackType::IsEmpty() const{return (top==-1);}bool StackType::IsFull() const{return (top==9);}void StackType::Push(int newItem){top++;items[top]=newItem;}void StackType::Pop(int& item){item=items[top];top--;}void StackType::DisplayStack(){cout<<"top-->";for(int i=top;i>=0;i--)cout<<"\t|\t" <<items <<"\t|" <<endl;cout<<"\t|-----------------|"<<endl;}وهذا هو المين#include <iostream>#include"StackType.h"//#include"StackType.cpp"using namespace std;int main(){StackType r;int size;cout<<"please enter number of elements you want to be less than 10" <<"\n";cin>>size;int temp=0;for(int i=1; i<=size;i++){cout<<"enter the element number "<<i<<" to push into the stack \n";cin>>temp;r.Push(temp);cout<<endl;}cout<<"display the elaments of the stack: ";r.DisplayStack();int key;cout<<endl<<" pop the elements from the stack : "<<endl;while(!r.IsEmpty()){r.Pop(key);cout<<" "<<key<<"is removed from stack"<<endl;}cout<<endl<<" push 7 and then 6 into the stack \n";r.Push(7);r.Push(6);cout<<endl<<"display the elements of the stack\n";r.DisplayStack();cout<<endl<<" Make the stack empty \n"<<endl;r.MakeEmpty();cout<<endl<<"check whether the stack is empty\n"<<endl;if(r.IsEmpty())cout<<"the stack is empty\n";elsecout<<"the stack isn't empty\n";return 0;}

دحين ابغا اخلي المصفوفه

int items طولها مدخل من المستخدم حاول اني اكتب التالي

int x;
int items[x] ; cin >> x ;

لكن للاسف مو راضي ياريت لو تساعدوني

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 6 نوفمبر 2013 في 10:21 — السبب: وضع وسم الكود

#2

السلام عليكم

قم بإستخدام المؤشرات لأن المصفوفات لا تقبل هذا فى لغة السي وعلى ما أظن أن الأمر كذلك فى لغة السي++ .

لاتحسبن العلم ينفع وحده مالم يتوج ربه بخلاق

لا إله إلا أنت سبحانك إني كنت من الظالمين

#3

السلام عيكم

إستخدم الحجز الديناميكي

int *items = new int(15)
#4

أخى الملف بعد التعديل:

تم اضافة دالة واحدة وهى :

    void InitSatck(int Size);

وتغير المتغير items الى مؤشر: *items

ملف StackType.h

#ifndef STACKTYPE_H
#define	STACKTYPE_H

class StackType {
public:
    StackType();
    void InitSatck(int Size);
    void MakeEmpty();
    bool IsEmpty() const; // ·„  ÷⁄Ì «·«fiÊ«”
    bool IsFull() const; // ·„  ÷⁄Ì «·«fiÊ«”
    void Push(int item);
    void Pop(int & item);
    void DisplayStack();
private:
    int top;
    int *items;
};

#endif	/* STACKTYPE_H */

ملف: StackType.cpp

#include <iostream>
#include <iomanip>
#include"StackType.h"
using namespace std;

void StackType::InitSatck(int Size)
{
    items = new int[Size];
}

StackType::StackType()
{
    top = -1;
}

void StackType::MakeEmpty()
{
    top = -1;
}

bool StackType::IsEmpty() const
{
    return (top == -1);
}

bool StackType::IsFull() const
{
    return (top == 9);
}

void StackType::Push(int newItem)
{
    top++;
    items[top] = newItem;
}

void StackType::Pop(int& item)
{
    item = items[top];
    top--;
}

void StackType::DisplayStack()
{
    cout << "top-->" << endl;
    for (int i = top; i >= 0; i--)
        cout << setw(20) << items << endl;
    cout << "|-----------------|" << endl;
}

واخيرا ملف : main.cpp

/* 
 * File:   main.cpp
 * Author: Mohammed Abu Zayed
 *
 * Created on March 11, 2012, 8:24 AM
 */
#include <iostream>
#include"StackType.h"
using namespace std;

int main()
{
    StackType rNewOne;
    int size;
    cout << "please enter number of elements" << endl
            << "you want to be less than 10: ";
    cin >> size;
    int temp = 0;
    for (int i = 1; i <= size; i++)
    {
        cout << "enter the element number " << i << " to push into the stack: ";
        cin >> temp;
        rNewOne.Push(temp);
    }
    cout << "display the elaments of the stack: ";

    rNewOne.DisplayStack();

    int key;
    cout << endl << " pop the elements from the stack : " << endl;
    while (!rNewOne.IsEmpty())
    {
        rNewOne.Pop(key);
        cout << " " << key << "is removed from stack" << endl;
    }
    cout << endl << " push 7 and then 6 into the stack" << endl;
    rNewOne.Push(7);
    rNewOne.Push(6);
    cout << endl << "display the elements of the stack" << endl;
    rNewOne.DisplayStack();
    cout << endl << " Make the stack empty" << endl;
    rNewOne.MakeEmpty();
    cout << endl << "check whether the stack is empty" << endl;
    if (rNewOne.IsEmpty())
        cout << "the stack is empty" << endl;
    else
        cout << "the stack isn't empty" << endl;
    return 0;
}

طبعا الكود سيعمل لكن هناك مشاكل

-عند حذف عنصر من المصفوفة ﻻبد من اعادة تحريك كل العناصر من بعد العنصر المحذوف الى نهاية المصفوفة بمقدار واحد لتحل محل العنصر المحذوف

-مشكلة هادم الفئة

اخيرا انصحك ام ان تعدل الكود لتحل المشاكل السابقة أو تستخدم نوع بيانات من نوع vector او استخدام القوائم الموصولة أم ان تستخدم المتراصة كما هى

والله ولى التوفيق

تم تعديل هذه المشاركة بواسطة محمد ابوزايد في 11 مارس 2012 في 10:37

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

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