Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Devaka Cooray
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
Converting Infix to Postfix Expressions
Christopher Beech
Ranch Hand
Posts: 40
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Having some problems converting infix to postfix. for example, if I enter 1+2*3, it gives me 1+12+*23*3 instead of 123*+
import java.util.*; /* Author: Chris Beech * * Purpose: The puropse is to implement a node in a linked list of Student * Records. * * Created: April 1, 2006 * Modified: April 17, 2006 */ public class InfixCalculator { public static void main(String[] args) { Stack <Character> s = new Stack <Character>(); Stack <Double> t = new Stack <Double>(); String postfixExpr = ""; String inFix = ""; String another = "y"; char ch = ' '; double result = 0; Scanner console = new Scanner (System.in); while (another.equalsIgnoreCase("y")) { System.out.println ("Please enter an infix expression: "); inFix = console.nextLine(); System.out.println(inFix); for (int index = 0; index < inFix.length(); index++) { ch = inFix.charAt(index); if (Integer.valueOf(ch) > -10 || Integer.valueOf(ch) < 10) { postfixExpr = postfixExpr + ch; if (ch == '(') { s.push(ch); } else if (ch == ')') { while (s.peek() != '(') { postfixExpr = postfixExpr + s.pop(); } s.pop(); } else { while (!s.isEmpty() && s.peek() != '(' && precedence(ch,s.peek()) != false) { postfixExpr = postfixExpr + s.pop(); } s.push(ch); } } } while (!s.isEmpty()) { postfixExpr = postfixExpr + s.pop(); } System.out.println(postfixExpr); for (int index = 0; index < postfixExpr.length(); index++) { double operand2 = 0; double operand1 = 0; ch = postfixExpr.charAt(index); if (Double.valueOf(ch) > -10 || Double.valueOf(ch) < 10) { t.push((double)ch - '0'); } else if(ch == t.pop()) { operand2 = t.pop(); operand1 = t.pop(); if (ch == '+') { result = (operand1 - '0') + (operand2 - '0'); t.push((double)result - '0'); } else if (ch == '/') { result =(operand1 - '0')/(operand2 - '0'); t.push((double)result - '0'); } else if (ch == '-') { result =(operand1 - '0')-(operand2 - '0'); t.push((double)result - '0'); } else if (ch == '*') { result = (operand1 - '0')*(operand2 - '0'); t.push((double)result - '0'); } } result = t.pop(); } System.out.println ("The result is:" + result); System.out.print ("Do another expression (y/n)?"); another = console.nextLine(); } } public static boolean precedence(char ch, char peek) { if (ch == '*' && peek == '*') { return true; } else if (ch == '*' && peek == '/') { return true; } else if (ch == '*' && peek == '+') { return false; } else if (ch == '*' && peek == '-') { return false; } else if (ch == '/' && peek == '*') { return true; } else if (ch == '/' && peek == '/') { return true; } else if (ch == '/' && peek == '+') { return false; } else if (ch == '/' && peek == '-') { return false; } else if (ch == '+' && peek == '*') { return true; } else if (ch == '+' && peek == '/') { return true; } else if (ch == '+' && peek == '+') { return true; } else if (ch == '+' && peek == '-') { return true; } else if (ch == '-' && peek == '*') { return true; } else if (ch == '-' && peek == '/') { return true; } else if (ch == '-' && peek == '+') { return true; } else { return true; } } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Selecting characters from a string
converting this to a prefix instead of a postfix calculator
Picking characters from a string
A continuation of my previous post, if I may...
Trouble evaluating postfix Expression
More...