Hi, i need to program this algorthim using 2 stack in java, for example to evaluate such a given valid infix expression : (5*3)+(4/(6-2)).
i need help, i don't know much about stacks..

Create two empty stacks, one for operands, and one for operators. 
Read infix expression as a string ; 
while(there are more tokens in the string){
if(current token is an operand)
push token in the operand stack;
else if(current token is an operator){
push token in the operator stack;
     else if (current token is a left parentheses)
ignore
   else if (current token is a right parentheses)
        {
            operand1 = pop form operand stack;
            operand2 = pop from operand stack;
operator = pop from operator stack
            result = operand2 operator operand1;
            push result in the operand stack;

        }

while(there are more operators in the operator stack){
        {
            operand1 = pop form operand stack;
            operand2 = pop from operand stack;
operator = pop from operator stack
            result = operand2 operator operand1;
            push result in the operand stack;

        }
pop result from the operand stack and print it;

Recommended Answers

All 6 Replies

What exactly do you need help with? "I need help" tells us nothing.

i need help in stacks

That's not much clearer. What about stacks? Have you read the WikiPedia articles on stacks, and arithetic expression parsing? I

mmm i know they're a data structure and yes i did read about them
but don't know how to porgram them

Sounds like you're in a state of "starting panic" - it looks so scary that you're aftraid to start.
You have a complete pseudo-code to follow, so how far have you got with that?
How about step 1: Create two empty stacks, one for operands, and one for operators ?
(java.util contains a Stack class that may help)
Or step 2: Read infix expression as a string - surely you can do that.

Does your professor require you to write your own Stack class? If not, then the java.util.Stack class would solve your issues right there.

OTOH, stacks are easy enough to implement yourself; any linear data structure which allows you to add and remove an element from the same end (that is, in Last In, First Out order) can be treated as a stack, so long as you only change it in that order.

The easiest way to design a stack class is to use an array of the type the stack is supposed to hold - String, in this case, as it needs to be able to hold both operators and numbers of one or more digits - and have an int holding the index to the top of the stack. You the would write a method push() that would copy its argument to the top of the stack, and increment the stack index by one. The pop() method would decrement the index by one, and return the value that had been at the top of the stack. This is, in fact, how the built-in hardware stack used by the CPU itself works (though for various reasons, the stacks on most processors grow top down in memory rather than bottom up).

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.