السلام عليكم
انا صديقة جديدة في المنتدى اتمنى ان تساعدوني في حل سؤال بلغة سي بلس بلس.
من المعلوم ان stack هي مصفوفة وتجرى عليها عمليات push و pop والمطلوب هو دمج العمليتين ببرنامج واحد .
انا حليت السؤال باستخدام الدوال حيث عملت دالة للpush ودالة لل pop لكن المشكلة انه عندما اضيف عنصر الى الكدس لازم يتغير قيمة ال top بعدد القيم المضافة وعندما احذف عنصر من الكدس سينقص قيمة الtop بمقدارالسحب .
فعندما اقوم باضافة عنصر تغير قيمة ال top ولكن المشكلة في السحب حيث لا ترجع قيمة ال top الى الدالة الاصلية حتى عملية ال pop تجرى عليها .
اتمنى ان يكون شرحي مفهوم واتمنى ان ارى مساعدتكم وارائكم
وهذا سؤالي الذي حليته بنفسي
#include<iostream.h>اقتباس
main()
{
int push();
int pop();
int n, top=0,max=5;
char stack[5],x='w';
cout<<"stack size is 5";
while (x!='e')
{
cout<<" choose an operation"<<endl;
cout<<"p-push"<<endl;
cout<<"o-pop"<<endl;
cin>>x;
switch (x)
{
case 'p':push();
break;
case 'o':pop();
break;
} }
return 0;
}
int push()
{
int n , max=5,top=0;
char stack[5];
cout<<"how many items you want to push? ";
cin>>n;
if (n>max)
{
cout<<"stack size is 5 , enter other number ";
cin>>n ;
}
for (int i=1;i<=n;i++)
{
cout<<"stack=";
cin>>stack;
}
top=top+n;
cout<<"you have entered "<<n<<" elements ,top="<<top<<endl;
} ;
int pop()
{
int top=0, n;
if (top==-1||top==0)
cout<<"stack is empty";
else
{
cout<<"how many numbers u want to pop ?";
cin>>n;
if (n>top)
{
cout<<"the numbers is out of range of stack top,enter other number ";
cin>>n;
}
else
if (n<top)
{
cout<<"you have poped "<<n<<" numbers";
top=top-n;
}
} };