I am trying to calculate algebraic formulas with complete formula braces through a stack. So for example I would have this formula: ((6 ∗ (4+2))+(5−1)). The closing braces being indicators that the stack should stop "reading" and calculate the term withn the set of braces. My stack should "read" till the first closing brace and then calculate the forumula within (4+2), then it would notice the second closing brace and calculate that. In my case it would be 6* 6 ( as 4+2) had been calculated earlier. Next 5-1 would need to be read into the stack and calculated and lastsly 4 would be added to 36.
The stack is implemented like the following:
public class Stack<T> {
private List<T> elemente = new ArrayList<T>();
public void push(T element) {
elemente.add(element);
}
public T pop() {
if (elemente.size() == 0) {
throw new IllegalArgumentException("Stack ist leer.");
}
T element = elemente.get(elemente.size() - 1);
elemente.remove(elemente.size() - 1);
return element;
}
public T top() {
if (elemente.size() == 0) {
throw new IllegalArgumentException("Stack ist leer.");
}
return elemente.get(elemente.size() - 1);
}
public T showElems(int i) {
return this.elemente.get(i);
}
public int showSize() {
return this.elemente.size();
}
public boolean isEmpty() {
return elemente.size() == 0;
}
public void clear() {
elemente.clear();
}}
I have come up with the following:
public class Parser {
static Stack<String> stack = new Stack<String>();
static String ausdruck = "( ( 2 * ( 6 * ( 4 + 2 ) ) ) + ( 6 * ( 4 + 2 ) ) * ( 8 / 2 ) ) ";
static Scanner s = new Scanner(ausdruck);
public static void main(String[] args) {
Parser.routine();
Parser.showErg();
}
public static void berechne() {
while (!stack.isEmpty() && stack.showSize() > 3) {
Integer operator1 = Integer.parseInt(stack.pop());
String operation = stack.pop();
Integer operator2 = Integer.parseInt(stack.pop());
stack.pop();
System.out.println("");
System.out.println(operator1);
System.out.println(operation);
System.out.println(operator2);
System.out.println("");
if (operation.equals("+")) {
stack.push(Integer.toString(operator1 + operator2));
}
if (operation.equals("-")) {
stack.push(Integer.toString(operator2 - operator1));
}
if (operation.equals("*")) {
stack.push(Integer.toString(operator1 * operator2));
}
if (operation.equals("/")) {
stack.push(Integer.toString(operator2 / operator1));
}
}
}
public static void printStack() {
for (int i = 0; i < stack.showSize(); i++) {
System.out.println(stack.showElems(i) + " --- StackPos ---> " + i);
}
System.out.println("");
}
public static void routine() {
stack.push("ende");
while (s.hasNext()) {
String temp = s.next();
switch (temp) {
case ")":
Parser.printStack();
Parser.berechne();
break;
default:
stack.push(temp);
break;
}
}
s.close();
}
public static void showErg() {
String erg = stack.pop();
System.out.println("Ergebnis : " + erg);
}
}
My problem is that if I add another depth of braces my parser does not work properly. If I were to try the following instead of the forluma above there is a Number format exception. Example that does not work: (((6 ∗ (4+2))+(5−1))*3).
Thank you!