Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String convertToPostfix() {
- MyStack stack = new MyStackLinked();
- char c;
- StringBuffer postfix = new StringBuffer(infix.length());
- char symbol;
- for(int i=0;i<infix.length();++i) {
- symbol = infix.charAt(i);
- if (Character.isDigit(symbol)) {
- postfix.append(symbol).append(" ");
- }
- else if (symbol=='(') {
- stack.push(symbol);
- }
- else if (symbol==')')
- {
- while ((char)stack.peek() != '(') {
- postfix.append((char)stack.pop()).append(" ");
- }
- stack.pop();
- }
- else{
- while (!stack.isEmpty() && !((char)stack.peek()=='(') && isLowerPrecedence(symbol) <= isLowerPrecedence((char)stack.peek()))
- postfix.append((char)stack.pop()).append(" ");
- stack.push(symbol);
- }
- }
- while (!stack.isEmpty())
- postfix.append((char)stack.pop()).append(" ");
- return postfix.toString();
- }// end convertToPostfix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement