0

I need some idea how to correct this program. Though I think I understand the concept, my implementation is wrong. My program only prints the orignal string and does nothing with it.

public class ArrayStack<E> implements Stack<E> {
protected int capacity;
protected static final int CAPACITY = 1000;
protected E S[];
protected int top = -1;

public ArrayStack()
{
this(CAPACITY);
}

public ArrayStack(int cap)
{
capacity = cap;
S = (E[]) new Object[capacity];
}

public int size() { return (top + 1); }

public boolean isEmpty() { return (top < 0); }

public E top() {
if (isEmpty())
   throw new EmptyStackException("Stack is Empty");
return S[top];
}

public void push(E e)
{
if (size() == capacity)
   throw new FullStackException("Stack is full");
S[++top] = e;
}

public E pop()
{
E o;
if (isEmpty())
   throw new EmptyStackException("Stack is Empty");
o = S[top];
S[top--] = null;
return o;
}


public String toString()
{
String s = "";
for (int i = 0; i <= size()-1; i++)
{
s += " " + S[i];
}
return s;
}

public String inFix(String s)
{

   String output = "";
   ArrayStack<String> stackVar = new ArrayStack<String>();
   ArrayStack<String> stackOp = new ArrayStack<String>();
   for (int i = 0; i <= s.length()-1; i++)
    {

	char c = s.charAt(i);
	if (c != '*' || c != '/' || c != '+' || c != '-'|| c != '$')
	{

	   String s1 = Character.toString(c);
	   stackVar.push(s1);
	} 
	else
	{
	   String s2 = Character.toString(c);
	   stackOp.push(s2);
	   doOp(stackVar, stackOp);
	}
    }
while (!stackVar.isEmpty())
output += stackVar.pop();
return output;

}

public void doOp(ArrayStack<String> a, ArrayStack<String> b)
{

		String exp = "";
		String e1 = a.pop();
		String o = b.pop();
		String e2 = a.pop();
		exp += "(" + e1 + o + e2 + ")";
		a.push(exp);
}


public static void main(String[] args)
{
ArrayStack a = new ArrayStack();
String st = a.inFix("ABX+-");
System.out.println(st);
}

}

Question:
Every arithmetic expression given in the postfix form corresponds to
a unique fully parenthesized infix expression.For example, the
postfix expression “ABC + AC – / * D +” corresponds to the fully
parenthesized expression ((A * ((B + C) / ( A – C))) + D).
Design an algorithm and write a Java&program implementing your
algorithm which uses stack(s) to convert a given postfix expression
into the corresponding fully parenthesized infix form. Assume that
the variables are single alphabets and the postfix expression contains
binary operators only.
Test your program on the following expressions in which a
$ represents an exponentiation.
(a) AB + C –
(b) ABC + –
(c) AB – C + DEF – + $
(d) ABCDE– + $ *EF * –
For this question, you will submit Java class(es) implementing your
algorithm, and well as script sessions clearly showing the program being compiled and run, then generating valid output for parts a – d.

2
Contributors
1
Reply
5
Views
6 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.