Member Avatar for HASHMI007
#include <iostream.h>
#include <stdlib.h>
template < class ItemType >
class stack {
  private :
  int top;
  int maxStk;
  ItemType  *items;
  public :
  stack();
  stack(int max);
  int isEmpty();
  int isFull();
  void push(ItemType newItm);
  int Pop(ItemType &Itm);

};
template <class ItemType>
stack <ItemType>::stack(){
top=-1;
maxStk=500;
items=new ItemType[500];
}
template <class ItemType>
stack <ItemType>::stack(int max){
maxStk =max;
top=-1;
items=new ItemType[max];
}

template <class ItemType>
int stack <ItemType>::isEmpty(){
 return (top==-1);
}
template <class ItemType>
int stack <ItemType>::isFull(){
 return (top ==maxStk-1);
}
template <class ItemType>
void stack <ItemType>::push(ItemType newItm){

	  if (isFull()){
	  cout<<"Stack is OverFlow";
	  exit(1);
	  }
	  top++;
	  items[top]=newItm;

}
template < class ItemType>
int stack <ItemType>::Pop(ItemType &item){
		if(isEmpty()){
		cout<<"Underflow";
		exit(1);
		}
		return (item=items[top]);
		top--;
}
/*void calculate(){
} */
int main(){
//calculate();
int opndstk;
int op1,op2,value;
int f;
stack <int> s;
char st[3];
for (int ie=0;ie<3;ie++){
cin>> st[ie];
st[ie]=ie;
cout<<"?\n";
}
 for(int i=0;i<3;i++){
		if (st[i]=='+'){
			op1 = s.Pop(f);
			op2 = s.Pop(f);
			value= op1+op2;


		}
		else   {
			 s.push(st[i]);
		}
		}
		cout<<value<<endl;
		//s.push(value);

}

provide Wrong answer 2 3 + =5
but it give wrong Answer why ??????

Member Avatar for HASHMI007
char st[3];
for (int ie=0;ie<3;ie++){
cin>> st[ie];
st[ie]=ie;
cout<<"?\n";
}
 for(int i=0;i<3;i++){
		if (st[i]=='+'){
			op1 = s.Pop(f);
			op2 = s.Pop(f);
			value= op1+op2;
 
 
		}
		else   {
			 s.push(st[i]);
		}
		}
		cout<<value<<endl;
		//s.push(value);
 
}

problem in main not provide true result

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.