write aprogram in c++ to read the tokens in one at a time ,if it is
integer,push it on stack, if it is binary operator, pop two elements
from the stack, apply the operator to the two elements and push the result back on the stack??

please help me to solve this Question ??? i tried to solve a question but i failed with compiler c++

Recommended Answers

All 9 Replies

Please show us the work you've done so far, and explain where you're stuck.

do your homework... first try if stuck den ask

sorry my tired is simpleton :-
and this my tired:===

#include<iostream.h>
const int size=5;
void push(int[],int&,int&);
void pop(int[],int&,int&);
void main()
{int stack[size],item,i,top=0;
 for(i=0;i<size;i++)
   {cout<<"enter item"<<endl;
    cin>>item;
    push(stack,top,item);
   }
 for(i=0;i<size;i++)
    {pop(stack,top,item);
     if(item==int)
      push(stack,top,item);
    else // binary operator 
     for(i=0;i<2;i++)
      pop(stack,top,item);
    for(i=0;i<size;i++)   // result
     {pop(stack,top,item);
      push(stack,top,item);
     }
}
void push(int stack[size],int&top,int item)
{if(top<size)
   {top++;
    stack[top]=item;
    }
  else
 cout<<"stack is FULL"<<endl;
}
void pop(int stack[size],int&top,int&item)
{if(top>0)
   {item=stack[top];
     top--;
   }
else
cout<<"stack is Empty"<<endl;
}

Don't push size number of tokens onto the stack all at once, push input onto the stack only if the information entered is an int.

Swap lines 26 and 27 since top starts at zero, which is a valid index, and will be necessary if you want allow up to size number of elements on the stack.

This:
if(item==int)
is a no no. == requires two objects/instances, not a type. Your best choice is to accept input of each token as a string and then convert the string to an int if that is appropriate.

thanks but i dont any idea about this question

Use a pencil and paper first. Write out each step; like this:

1) obtain input from user/source as a string representing a token (could be either operator or operand)one string/token at a time
3) determine if input is operator (mathematical symbol like +, -, /, *, etc) or operand (integer, can it be a negative integer in your program?)
3) if input is operand then convert it into desired numeric variable type and push that numeric value onto stack
4) if input is operator, then determine number of operands to pop from stack (usually 2) and pop the required number from the stack storing them in appropriate variables.
5) perform the desired operation on the operands and push the result back onto the stack.
6) repeat the above until no further input desired.
7) Pop the stack (should be just one value left on it) and that's the final answer.

Now flesh out each step to be able to do what each step says you should do. Depending on your knowledge base some steps have more than one solution. You might even want to change the steps I've listed, it's your program after all.

very thanks but could you tell me the code programming to this questoin i need it

Sorry, no. This site is dedicated to helping, not doing.

First of all "i m sorry" but i have to say that i can see lots of error in your above made program logical as well as syntax errors. In above program you just try to create stack and applied push and pop operation. so please make effort once again and this time you should be to the point....make your own algo or flowchart and do this. as for my concern you should do this assignment in classes because it will be easy to understand and will clear your lots of concepts.

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.