Hi
i write a calculator ,This calculator get an arithmetic expression written in infix notation , then change it to postfix notation , then finds the value of postfix notation .
i have a problem , this program only work for one digit numbers :(
please help me to slove this problem

Very bad style of programming and it's difficult to read your code.
Try to use stringtokenizer to parse the string, try to avoid unnecessary variable creation within a loop, try to see what variable are being created and destroyed.
After trying above, paste your code here, I'll help to solve your problem.

I question the wisdom of this function specification:

String toPostfix(String Q)

Suppose Q = "12+34". What should the return value be?

"1234+" ?

Does that represent "1+234"? "12+34"? "123+4"?

All three give different answers.

Hi
i know my good friend , i want to slove this problem but i dont know how !!! please help me

Don't have the function return a String. It needs to split apart the equation into numbers and operators. Numbers are not stored as characters or strings. They should be stored as numbers (integers or doubles). Operators are stored as characters. Looks like you are trying to set up a stack, which is fine. I don't know how complicated these math operations are going to get. If you have a single binary operator, you don't need any stack. If you need to deal with stuff like this:

(a+b)/(c-a++)*(d-(-c)+e)

things get more complicated and you need a stack. Regardless, you can't parse a mathematical operation and have it not be clear when the first number stops and the next number starts, so be careful about returning a String from the function. Here's a really simple program. It'll need to be heavily modified if you are using stacks. Right now it simply hard-codes the equation as "12+34". You'll need to parse that math String to get the real numbers and the real operator.

public class Main
{
  public static void main(String[] args)
  {
      String infix = "12+34";
      MathOperation mop = new MathOperation (infix);
      int answer = mop.Evaluate();
      System.out.println (answer);
  }
}


class MathOperation
{
    int operand1;
    int operand2;
    char operator;

    public MathOperation (String eqInfix)
    // eqInfix is Math Operation in "a+b" (infix) form.  Parse string for operands and operator.
    {
        // use String.split function or some other method to help you do this.
        operand1 = 12;
        operand2 = 34;
        operator = '+';
    }


    public int Evaluate ()
    // assumes operator = '+'
    {
        return operand1 + operand2;
    }
}

Hi
i know my good friend , i want to slove this problem but i dont know how !!! please help me

1) You can use space to seperate tokens
2) You can use string tokenizer in order to reduce you code and pain

It's not possible to solve your problem with few line changes, you've to change the design.

This article has been dead for over six months. Start a new discussion instead.