hiiiiiiiii
can any1 teach me how to make a function to compare between 2 stacks??
like it should have the same stackTop and the values also
pleasee becouse my midterm is next week and i need to know it :rolleyes:
hiiiiiiiii
can any1 teach me how to make a function to compare between 2 stacks??
like it should have the same stackTop and the values also
pleasee becouse my midterm is next week and i need to know it :rolleyes:
السلام عليكم ورحمة الله وبركاته
اخي الغالي munatii عمل function راح يكون كالآتي
اولا تعمل loop ثم تعممل pop وتقارن اذا كانوا غير متساويين ترجع false واذا طلع من الـ loop وكانوا كلهم متساويين نرجع كل stack ونخليها زي ماكانت
وبعدين نرجع true طبعا الـ function يكون من النوع bool
وشف تمشي معك ولا لا واي مساعدة اقدرافيدك فيها إن شاء الله أنا حاضر ..
و السلام عليكم ورحمة الله وبركاته :D
اخوك \AL7ANOTY
تم تعديل هذه المشاركة بواسطة AL7ANOTY في 15 نوفمبر 2006 في 02:01
اقتباسلا تقوم بعمل POP للمقارنة .. فقط انسى تماماً أنهم Stacks و عاملهم على أنهم Arrays عادية و قم بقارنة كل element ثم قارن الtop في كل منهما
هى بس المشكلة إن فى تعريف ال stacks مفيش دالة بتعاملها كا array, زى مثلا ال std::stack فى السى++ مش موجودة موجودة فيها الإمكانية دى.
بس ممكن إنت تكتب الفنكشن اللى بتعاملها على إنها array مفيش مشاكل بس جوة الفنكشن برضه حتحتاج تعمل pop, أو الحل التانى إنك تكتب الكلاس بتاع ال stack الخاص بيك و ت implement الإمكانية دى.
bashmohandes كتب:لا تقوم بعمل POP للمقارنة .. فقط انسى تماماً أنهم Stacks و عاملهم على أنهم Arrays عادية و قم بقارنة كل element ثم قارن الtop في كل منهما
هذه طريقة . لكن إذا لم يمكنك معاملة ال stack ك array فتستطيع إستعمال طريقة أخرى هي ال recursive
وهذه هي الخوارزمية وحولها إلى كود بنفسك بسبب إن وضع الكود الجاهز لا أحبه وغير مسموح :-
1 ) يعني في البداية تكتب دالة تستقبل stacks 2
2) تصلح pop لكل واحد وتخزن القيم في عنصرين
3) تستدعي نفس الدالة مع إرسال نفس ال stacks
4) عند الوصول إلى الحالة الأساسية نقرن بين العنصرين ونرجع true أو false
5) وعند الرجوع إلى الدالة نرجع العنصرين اللي صلحنا لهما pop إلى كل من ال stack ونعيد القيمة المعيدة سواء true او false
وبكذا راح تقارن بينهم بدون تعديل
طبعا إذا حاولت
تحويله إلى كود فسوف تواجه مشاكل حاول أن تحلها وإذا لم تقدر راسلني والسلام عليكم
تم تعديل هذه المشاركة بواسطة b.m.s في 18 نوفمبر 2006 في 23:39
بسم الله , سوف احاول أن اكتب الكود مع الشرح حتى تتضح الصوره لك, واجو اذا لم تفهم اي شئ ان تخبرني
طبعا هذا الكود غير مجرب على الكمبايلر. كتبته هنا,
bool compare(Stack s1,Stack s2)
{
if(s1.getsize()!=s2.getsize()) return false;//if ths size of the two stacks are not equal, so stacks as well
else
{
Stack t1=s1;//assuming that the assignment operator is overloaded
Stack t2=s2;
Stack_entry x,y;
for(int i=0;i<s1.getsize();i++)
{
t1.top(x);t1.pop();//top take a copy from the top of the stack and pop remove it
t2.top(y);t2.pop();
if(x!=y)break;
}
return (i==s1.getsize());
}
}I hope you got the idea, i did not try this code in the compiler cuz i have to write the stack class
first, but the main idea of this code os clear, so if there is any error try to fix it by yourself
you may have different data members than mine , adjust it
تم تعديل هذه المشاركة بواسطة Super_emad55 في 19 نوفمبر 2006 في 00:37
ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust
hii
i solved it but i got 1error
that it returns always 1 , if its same or even if its not
whyy ??
can any1 check it?
#include <iostream.h>
template <class T>
class stackType
{
private:
int maxStackSize;
int stackTop;
T *list;
void copyStack(const stackType<T>& otherStack);
public:
const stackType& operator=(const stackType&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const T& newItem);
T top();
void pop();
bool isCom(stackType<T>, stackType<T>);
bool operator==(const stackType& otherStack);
stackType(int stackSize = 100);
stackType(const stackType<T>& otherStack);
~stackType();
};
//------------------------------------------
template <class T>
void stackType<T>::initializeStack()
{
stackTop = 0;
}
template <class T>
void stackType<T>::destroyStack()
{
stackTop = 0;
}
template <class T>
bool stackType<T>::isEmptyStack()
{
return(stackTop == 0);
}
template <class T>
bool stackType<T>::isFullStack()
{
return(stackTop == maxStackSize);
}
template <class T>
void stackType<T>::push(const T& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cerr<<"Cannot add to a full stack. "<<endl;
}
template <class T>
T stackType<T>::top()
{
return list[stackTop-1];
}
template <class T>
void stackType<T>::pop()
{
if(!isEmptyStack())
stackTop--;
else
cerr<<"Cannor remove from an empty stack. "<<endl;
}
template <class T>
void stackType<T>::copyStack(const stackType<T>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new T[maxStackSize];
for(int j=0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template <class T>
stackType<T>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"The size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new T[maxStackSize];
}
template <class T>
stackType<T>::~stackType()
{
delete [] list;
}
template <class T>
stackType<T>::stackType(const stackType<T>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template <class T>
const stackType<T>& stackType<T>::operator=(const stackType<T>& otherStack)
{
if(this != &otherStack)
copyStack(otherStack);
return *this;
}
//--------------
template <class T>
bool stackType<T>::isCom(stackType stack1, stackType stack2)
{
return(stack1 == stack2);
}
//------------
template <class T>
bool stackType<T>::operator==(const stackType<T>& otherStack)
{
if (maxStackSize == otherStack.maxStackSize)
return true;
{
if( stackTop == otherStack.stackTop )
return true;
list = new T[maxStackSize];
for(int j=0; j < stackTop; j++)
{
if (list[j] == otherStack.list[j])
return true;
}
return true;
}
return false;
}
//------------------------------------------------------------------------
int main()
{
int num1, num2;
stackType<int> stack1(100);
stackType<int> stack2(100);
cout<<"Enter the numbers in stack 1. Press 999 to end"<<endl;
cin>>num1;
while(num1 != 999)
{
stack1.push(num1);
cin>>num1;
}
cout<<"Enter the numbers in stack 2. Press 999 to end"<<endl;
cin>>num2;
while(num2 != 999)
{
stack2.push(num2);
cin>>num2;
}
cout<<"Stack 1 = ";
while(!stack1.isEmptyStack())
{
cout<<stack1.top()<<" ";
stack1.pop();
}
cout<<endl;
cout<<"Stack 2 = ";
while(!stack2.isEmptyStack())
{
cout<<stack2.top()<<" ";
stack2.pop();
}
cout<<endl;
cout<<"after comparing the stacks: the result is "<<stack1.isCom(stack1,stack2)<<endl;
return 0;
}السلام عليكم,
بصراحه بصراحه, اخر رد اثار غضبي :angry: كيف يمكن لطالب داتا ستركتشر ان يكتب هكذا كود.... عجيــــــــــــــــــــب
اما انك نايم و انت بتكتب او انك نايم <_<
كتبت بعض الcomments على السطور المثيره للغضب,و لكن لم استطع تكميل تصحيح الكود
#include <iostream.h>
template <class T>
class stackType
{
private:
int maxStackSize;
int stackTop;
T *list;
void copyStack(const stackType<T>& otherStack);
public:
const stackType& operator=(const stackType&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const T& newItem);
T top();
void pop();
bool isCom(stackType<T>, stackType<T>);
bool operator==(const stackType& otherStack);
stackType(int stackSize = 100);
stackType(const stackType<T>& otherStack);
~stackType();
};
//------------------------------------------
template <class T>
void stackType<T>::initializeStack()
{
stackTop = 0;
}
template <class T>
void stackType<T>::destroyStack()
{
stackTop = 0;
}
template <class T>
bool stackType<T>::isEmptyStack()
{
return(stackTop == 0);
}
template <class T>
bool stackType<T>::isFullStack()
{
return(stackTop == maxStackSize);
}
template <class T>
void stackType<T>::push(const T& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cerr<<"Cannot add to a full stack. "<<endl;
}
template <class T>
T stackType<T>::top()
{
return list[stackTop-1];
}
template <class T>
void stackType<T>::pop()
{
if(!isEmptyStack())
stackTop--;
else
cerr<<"Cannor remove from an empty stack. "<<endl;
}
template <class T>
void stackType<T>::copyStack(const stackType<T>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new T[maxStackSize];
for(int j=0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template <class T>
stackType<T>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"The size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new T[maxStackSize];
}
template <class T>
stackType<T>::~stackType()
{
delete [] list;
}
template <class T>
stackType<T>::stackType(const stackType<T>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template <class T>
const stackType<T>& stackType<T>::operator=(const stackType<T>& otherStack)
{
if(this != &otherStack)
copyStack(otherStack);
return *this;
}
//--------------
template <class T>
bool stackType<T>::isCom(stackType stack1, stackType stack2)
{
return(stack1 == stack2);
}
//------------
template <class T>
bool stackType<T>::operator==(const stackType<T>& otherStack)
{
if (maxStackSize == otherStack.maxStackSize)
return true; //maxsize for both if equal this does not means that these two sacks are equal
if( stackTop == otherStack.stackTop )
return true; //stackTop for both if equal this does not means that these two sacks are equal
list = new T[maxStackSize];
for(int j=0; j < stackTop; j++)
{
if (list[j] == otherStack.list[j])// if the first two elements are equal this will return true
return true;
}
return true;
return false;
}
//------------------------------------------------------------------------
int main()
{
int num1, num2;
stackType<int> stack1(100);
stackType<int> stack2(100);
cout<<"Enter the numbers in stack 1. Press 999 to end"<<endl;
cin>>num1;
while(num1 != 999)
{
stack1.push(num1);
cin>>num1;
}
cout<<"Enter the numbers in stack 2. Press 999 to end"<<endl;
cin>>num2;
while(num2 != 999)
{
stack2.push(num2);
cin>>num2;
}
cout<<"Stack 1 = ";
while(!stack1.isEmptyStack())
{
cout<<stack1.top()<<" ";
stack1.pop();
}
cout<<endl;
cout<<"Stack 2 = ";
while(!stack2.isEmptyStack())
{
cout<<stack2.top()<<" ";
stack2.pop();
}
cout<<endl;
cout<<"after comparing the stacks: the result is "<<stack1.isCom(stack1,stack2)<<endl;// how come you are comparing the two stacks after poping all the elements
return 0;}
ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust
template <class T>
bool stackType<T>::operator==(const stackType<T>& otherStack)
{
if (maxStackSize != otherStack.maxStackSize)
{
return false;
}
if( stackTop != otherStack.stackTop )
{
return false;
}
for (int i=stackTop; i>0;i--)
{
if(list[i-1]!= otherStack.list[i-1])
return false;
}
return true;
}i fixed it but still the same problem
it always returns 1
هذا الموضوع مغلق.