hi the requirement is i have to use two stacks for input and one for output
apply some binary operation on two stacks and store the result in 3rd stack
here is my code
the problem is that when i execute this code it runs but it shows the result of last array element but not others and say "stack is empty".Code:# include <iostream> # include <conio.h> # include <stack> using namespace std; class intstack { private: int * stackarray; int stacksize; int top; public: intstack (int); void push (); void pop (); bool isfull (void); bool isempty (void); intstack add (intstack & o); };//end class intstack::intstack (int size) { stackarray=new int[size]; stacksize=size; top=-1; }//end definition void intstack::push () { if(isfull()) { cout<<"stack is full"<<endl; }//end if else { top++; for (int i=0;i<stacksize-1;i++) { cin>>stackarray[top]; } } }//end push void intstack::pop () { int num; if(isempty()) { cout<<"stack is empty"<<endl; } else { num=stackarray[top]; top--; cout<<num; } }//end bool intstack::isfull (void) { bool status; if (top==stacksize-1) { status=true; } else { status=false; } return status; } bool intstack::isempty (void) { bool status; if (top==-1) { status=true; } else { status=false; } return status; } intstack intstack::add (intstack & o) { intstack temp(stacksize); temp.top=top; for (int i=0; i<stacksize-1; i++) { temp.stackarray[i]=stackarray[i]+o.stackarray[i]; } return (temp); } int main () { int var=0; intstack stack1(5),stack2(5),stack3(5); cout<<"enter numbers in first stack"<<endl; stack1.push (); cout<<"enter number in second stack"<<endl; stack2.push (); stack3=stack2.add (stack1); stack3.pop (); stack3.pop (); stack3.pop (); stack3.pop (); getch (); }//
for example i have entered in first array
1
2
3
4
numbers in 2nd array
5
6
7
8
then it shows
12
stack is empty
stack is empty
stack is empty
can anyone correct my code please



LinkBack URL
About LinkBacks


