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!
// 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))";
for (int i = 0; i < test.length(); i++) {
char 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)
{
{
output = output + emu_stack[top_ptr--];
}
}
return output;
}//end translation()
public void got_parenentheses(char ch, int top_ptr) {
while (top_ptr != -1) {
char chx = emu_stack[top_ptr--];
if (chx == '(')
break;
else
output = output + chx;
}
}
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;
}
}