Results 1 to 1 of 1
Thread: Infix to Postfix using array
- 10-11-2010, 06:57 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Infix to Postfix using array
Hello all!
I have an assignment to convert an infix expression in to postfix and I have to use an array rather than an actual stack, which I believe is the typical to do this process. I keep running into a problem when parentheses are in the expression and the array seems to replace operators with parentheses in the array and I cannot figure out why.
Specifically, when it comes to the second '(' sign, it replaces the '*' sign with it in the array.
The Expression I'm reading is, "(A*(B+C)".
This is my first post so let me know if I'm missing anything or if anyone needs anymore information. Thank you for any help!
Java Code:// Translates arithmetic expressions (infix) into postfix expressions. Checks for // parenthetical correctness. Implements a user-defined stack. import java.util.*; import java.io.*; public class Main { // Initialize private static String output = ""; private static String input = ""; private static int top_ptr = -1; static char[] emu_stack = new char[9]; // Emulate a stack using a char // array public Main(String in) { input = in; } public static void main(String[] args) throws IOException { // change to asking for // input file name // Initialize Main the_translation = new Main(input); File file = new File("C:\\Program Files\\first_input.txt"); BufferedReader reader = null; reader = new BufferedReader(new FileReader(file)); input = reader.readLine(); output = the_translation.translation(); //readInput(); //print user input first!! goes here System.out.println(output); } public void readInput() throws IOException // later make sure to // change to asking for // input file name // Initialize { File file = new File("C:\\Program Files\\first_input.txt"); BufferedReader reader = null; reader = new BufferedReader(new FileReader(file)); input = reader.readLine(); translation(); } public String translation() throws IOException { String test = "(A*(B+C))"; char ch = 0; for (int i = 0; i < test.length(); i++) { ch = test.charAt(i); if(ch == ';') { System.out.println("All Done!"); break; } System.out.println("for ch: " + ch); System.out.println("contents of stack: " + Arrays.toString(emu_stack)); switch (ch) { default: output = output + ch; break; case '+': case '-': if (top_ptr == -1) { emu_stack[++top_ptr] = ch; break; } else { got_operator(ch, 1, top_ptr); break; } case '*': case '/': if (top_ptr == -1) { emu_stack[++top_ptr] = ch; System.out.println("check"); break; } else { got_operator(ch, 2, top_ptr); break; } case '^': if (top_ptr == -1) { emu_stack[++top_ptr] = ch; break; } else { got_operator(ch, 3, top_ptr); break; } case '(': emu_stack[++top_ptr] = ch; break; case ')': got_parenentheses(ch, top_ptr); break; } } while( top_ptr != -1) { if(emu_stack[top_ptr] == '(') { System.out.println("( was hit"); top_ptr--; } else output = output + emu_stack[top_ptr--]; } return output; }//end translation() public void got_parenentheses(char ch, int top_ptr) { top_ptr++; while (top_ptr != -1) { System.out.println("contents of stack end: " + Arrays.toString(emu_stack)); char chx = emu_stack[top_ptr--]; if (chx == '('){ System.out.println("paren why"); break; } else output = output + chx; System.out.println("output = " +output); } } public static void got_operator(char top_operator , int precedence, int top_ptr) { while (top_ptr != -1) { char top_op_popped = emu_stack[top_ptr--]; { if (top_op_popped == '(') { emu_stack[++top_ptr] = top_op_popped; break; } else { int precedence2 = 0; if (top_op_popped == '+' || top_op_popped == '-') { precedence2 = 1; } else if(top_op_popped == '*' || top_op_popped == '/') precedence2 = 2; else if (top_op_popped == '^') precedence2 = 3; if (precedence2 < precedence) { top_op_popped = emu_stack[++top_ptr]; break; } else { output = output + top_op_popped; } } }//end else }//end while emu_stack[++top_ptr] = top_operator; } }
Similar Threads
-
Infix to Prefix
By Sasarai in forum Advanced JavaReplies: 4Last Post: 12-08-2010, 04:57 PM -
Postfix this!!
By hasysf in forum New To JavaReplies: 4Last Post: 09-07-2009, 07:44 PM -
Infix to Post fix
By Tenn in forum New To JavaReplies: 0Last Post: 05-03-2009, 10:21 PM -
any can help me? About MDAS and PEMDAS rules and Infix-Prefix, Infix-Postfix rules
By darlineth in forum New To JavaReplies: 1Last Post: 07-05-2008, 04:08 PM -
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 08:36 PM
Bookmarks