Infix to postfix and evaluate: having problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aitia
    New Member
    • Jul 2008
    • 1

    Infix to postfix and evaluate: having problems

    this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help?

    [CODE=java]import java.io.*;
    import java.util.*;

    public class Postfix
    {
    private static Stack operators = new Stack();
    private static Stack operands = new Stack();

    public static void main(String argv[]) throws IOException
    {
    String infix;

    //create an input stream object
    BufferedReader keyboard = new BufferedReader (new
    InputStreamRead er(System.in));

    //get input from user
    System.out.prin t("\nEnter the algebraic expression in infix: ");
    infix = keyboard.readLi ne();

    //output as postfix
    System.out.prin tln("The expression in postfix is:" + toPostfix(infix ));

    //get answer
    System.out.prin tln("The answer to the equation is: " + evaluate
    (toPostfix(infi x)) + "\n");
    }

    private static String toPostfix(Strin g infix)
    //converts an infix expression to postfix
    {
    StringTokenizer s = new StringTokenizer (infix);
    //divides the input into tokens
    String symbol, postfix = "";
    while (s.hasMoreToken s())
    //while there is input to be read
    {
    symbol = s.nextToken();
    //if it's a number, add it to the string
    if (Character.isDi git(symbol.char At(0)))
    postfix = postfix + " " + (Integer.parseI nt
    (symbol));
    else if (symbol.equals( "("))
    //push (
    {
    Character operator = new Character('(');
    operators.push( operator);
    }
    else if (symbol.equals( ")"))
    //push everything back to (
    {
    while (((Character)op erators.peek()) .charValue() != '(')
    {
    postfix = postfix + " " + operators.pop() ;
    }
    operators.pop() ;
    }
    else
    //print operators occurring before it that have greater precedence
    {
    while (!operators.emp ty() && !(operators.pee k()).equals("(" ) && prec(symbol.cha rAt(0)) <= prec(((Characte r)operators.pee k()).charValue( )))
    postfix = postfix + " " + operators.pop() ;
    Character operator = new Character(symbo l.charAt(0));
    operators.push( operator);
    }
    }
    while (!operators.emp ty())
    postfix = postfix + " " + operators.pop() ;
    return postfix;
    }

    private static int evaluate(String postfix)
    {
    StringTokenizer s = new StringTokenizer (postfix);
    //divides the input into tokens
    int value;
    String symbol;
    while (s.hasMoreToken s())
    {
    symbol = s.nextToken();
    if (Character.isDi git(symbol.char At(0)))
    //if it's a number, push it
    {
    Integer operand = new Integer(Integer .parseInt(symbo l));
    operands.push(o perand);
    }
    else //if it's an operator, operate on the previous two operands
    {
    int op2 = ((Integer)opera nds.pop()).intV alue();
    int op1 = ((Integer)opera nds.pop()).intV alue();
    int result = 0;
    switch(symbol.c harAt(0))
    {
    case '*': {result = op1 * op2; break;}
    case '+': {result = op1 + op2; break;}
    case '-': {result = op1 - op2; break;}
    case '/': {result = op1 / op2; break;}
    case '%': {result = op1 % op2; break;}
    }
    Integer operand = new Integer(result) ;
    operands.push(o perand);
    }
    }
    value = ((Integer)opera nds.pop()).intV alue();
    return value;
    }

    private static int prec(char x)
    {
    if (x == '+' || x == '-')
    return 1;
    if (x == '*' || x == '/' || x == '%')
    return 2;
    return 0;
    }
    }[/CODE]
  • samido
    New Member
    • Oct 2007
    • 52

    #2
    what is the matter with this code ...? what it was suppose to do and not happenign yet, what do you pass as inputs and do you expect as out put...? you didn't ask any questionon this thread and I wonder what is it you want us to do.

    Sam Rabophala

    Comment

    Working...