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

برنامج في stack فيه مشكلة:(

مغلق
بدأه أناغيم في 13 مارس 2006 · 7 رد · 730 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

قمت بكتابة هذا البرنامج، ويتنفذ معي بس المشكلة أنه دالة pop المسؤولة عن الحذف ودالة top المسؤولة عن رأس المكدس فيهما مشكلة لم أعرفها!

مثلاً إذا أدخلت عددين إلى المكدس، وطلبت دالة pop عشان يحذف يقول لي أنه حذفها المفروض هنا أن يصبح لا يوجد في المكدس إلا عدد واحد ويكون هو top

بس لما أطلب دالة top يقول لي انه العدد اللي المفروض أنه أنحذف هو ال top، ولما أطلب دالة pop مرة ثانية يقول لي أنه حذف العدد اللي المفروض أني حذفته من قبل....... :blink:

الله يجزاكم خير ساعدوني، صدع راسي على كتابة هالبرنامج ووقفت عند المشكلة والتسليم المفروض اليوم أجلته إلى يوم الإربعاء

ساعدوني..:(

#include <iostream.h>


                                

struct intstack{
	int *stackary;
	int count;
	int stackmax;
	int top;
};


intstack *mystack;
 int i=1;
 int value;
 int *nada;

 ///////////////////////

 intstack * createstack()
{
mystack =new intstack;

cout<<"\n\hi\n";
if(mystack==NULL)
return NULL;

else
{
	cout<<"ooooo\n\n\n";
	mystack->top=-1;
mystack->count=0;
mystack->stackmax=value;
mystack->stackary=new int;
if (mystack->stackary==NULL)
delete mystack;
return NULL;
}

return mystack;


}



////////////////////

int pushstack()
{

	if (mystack->count==mystack->stackmax)

return 0;
else
{(mystack->count)++;
(mystack->top)++;
mystack->stackary[mystack->top]=value;
return 1;
}
}

///////////////////////////

int popstack ( )
{if (mystack->count==0)
return 0;
else
{*nada=mystack->stackary[mystack->top];
(mystack->count)--;
(mystack->top)--;

return 1;
}
}

//////////////////////

int stacktop()
{if (mystack->count>0)
{*nada=mystack->stackary[mystack->top];
return 1;
}
else
return 0;
}
////////////////////////

 int emptystack()
 {return (mystack->count==0);
 }

 ////////////////////

 int fullstack()
 {
  return (mystack->count==mystack->stackmax);
 }

void main()

 {

 createstack();

 cout<<"by\n";

 while (i!=0)
  { cout<<"stack manipulation:\n\n";
  cout<<"1-push\n";
  cout<<"2-pop\n";
  cout<<"3-top\n";
  cout<<"4-empty\n";
  cout<<"5-full\n";
   cout<<"6-end\n";
  cin>>i;

  if(i==1)
  {cout<<"value?\n";
  cin>>value;
  pushstack();
   cout<<"vaue"<<value<<"pushed in\n";
  
  }
  else if(i==2)
  {while(value==0)
  {cout<<"\nstack is empty\n";
  break;}
  if(value!=0)
  {
   popstack();
  cout<<"value"<<value<<"poped\n";
  }
  }
  else if(i==3)
  {   while(value==0)
  {cout<<"\nstack is empty\n";
  break;}

  if(value!=0)
  {
  stacktop();
  cout<<"value on the top"<<value<<"\n\n";
  }
  }
  else if(i==4)

emptystack();

else if(i==5)

fullstack();

else
   break;
  }
}
#2
intstack *mystack;
int i=1;
int value = 100;//  ضعي القيمة التي تريدينها 

int *nada


intstack * createstack()
{
mystack =new intstack;

cout<<"\n\hi\n";
if(mystack==NULL)
return NULL;

else
{
cout<<"ooooo\n\n\n";
mystack->top=-1;
mystack->count=0;
mystack->stackmax=value;
mystack->stackary=new int[value ];  
if (mystack->stackary==NULL)
delete [] mystack;
return NULL;
}

return mystack;


}

الخطأ هو أنك إستخدمت

mystack->stackary=new int;

بدل من

mystack->stackary=new int[value]

وأيضا

delete mystack;

بدل من

delete [] mystack;

وأيضا عند تعريف المتغير value يجب أن تعطيه قيمة

هذا ما ظهر لي على عجاله ووفقك الله

#3

الأستاذ الفاضل..b.m.s

الله يجزاك خير، لكن حاولت بطريقتك ولم تضبط معي..!

هذا غير انه لم أبدأ بدالة ال pop قبل ماادخل أي عدد يطبع لي 100

القيمة اللي اسندتها لل value

:wacko:

#4

وينكم..:(

#5

أختي غيري هذا

int *nada;

إلى

int nada;

وغيري أي وجود لــــ nada* إلى nada

عموما أنا قاعد أعدل الأخطاء في البرنامج

وليس سير البرنامج والمنطق المبني عليه ^_^

#6

هذا هو التصحيح وأتمنى تستفيدين منه

ملاحظة المتغير nada للبوب وللتوب

والمتغير value للبوش

الحل :

#include <iostream.h>


                               

struct intstack{
int *stackary;
int count;
int stackmax;
int top;
};


intstack *mystack;
int i=1;
int value;
int nada;

///////////////////////

intstack * createstack()
{
mystack =new intstack;

cout<<"\n hi \n";
if(mystack==NULL)
return NULL;

else
{
cout<<"ooooo\n\n\n";
mystack->top=-1;
mystack->count=0;
mystack->stackmax=100;
mystack->stackary=new int[mystack->stackmax];
if (mystack->stackary==NULL)
delete mystack;
return NULL;
}

return mystack;


}



////////////////////

int pushstack()
{

if (mystack->count==mystack->stackmax)

return 0;
else
{(mystack->count)++;
(mystack->top)++;
mystack->stackary[mystack->top]=value;
return 1;
}
}

///////////////////////////

int popstack ( )
{if (mystack->count==0)
return 0;
else
{nada=mystack->stackary[mystack->top];
(mystack->count)--;
(mystack->top)--;

return 1;
}
}

//////////////////////

int stacktop()
{if (mystack->count>0)
{nada=mystack->stackary[mystack->top];
return 1;
}
else
return 0;
}
////////////////////////

int emptystack()
{return (mystack->count==0);
}

////////////////////

int fullstack()
{
 return (mystack->count==mystack->stackmax);
}

void main()

{

createstack();

cout<<"by\n";

while (i!=0)
 { cout<<"stack manipulation:\n\n";
 cout<<"1-push\n";
 cout<<"2-pop\n";
 cout<<"3-top\n";
 cout<<"4-empty\n";
 cout<<"5-full\n";
  cout<<"6-end\n";
 cin>>i;

 if(i==1)
 {cout<<"value?\n";
 cin>>value;
 pushstack();
  cout<<"value"<<value<<"pushed in\n";

 }
 else 
  if(i==2)
  if(emptystack())
 	 {cout<<"\nstack is empty\n";break;}
 else
	{
	popstack();
	cout<<"value"<<value<<"poped\n";
	}
 else 
  if(i==3)
   if(emptystack())
 	 {cout<<"\nstack is empty\n";break;}
  else
  {
  stacktop();
  cout<<"value on the top"<<nada<<"\n\n";
  }
 else 
  if(i==4)
  if(emptystack())cout << "yes \n";
  else
 	 cout << "NO \n";

 else 
	if(i==5)
  if(fullstack())cout << "yes\n";
 	 else
    cout << "NO \n";
 else
    break;
 }
}

abofahad5515@hotmail.com

#7

الأستاذ.. b.m.s

حاولت ولم تضبط!..:(

لكن اشكرك على اهتمامك..

الله يوفقك..:)

_______

الأستاذ..The Cpp

شكراً جزيلاً

ضبط معي البرنامج، الله يوفقك ويجزاك خير

وشكراً مرة اخرى..:)

تم تعديل هذه المشاركة بواسطة أناغيم في 17 مارس 2006 في 13:43

#8

العفو أختي وأنا تحت أمرك في أي وقت

abofahad5515@hotmail.com

هذا الموضوع مغلق.

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