Question: *JAVA* Here is an extension of the current application. What if I am allowed to use (...), {...}, [...] ? Combinations such as this :
*JAVA*
Here is an extension of the current application. What if I am allowed to use (...), {...}, [...] ?
Combinations such as this : ([...]), [{...}] etc. are allowed, but you can not 'mix and match', e.g. [...)
How is this going to influence the original code? I will still be pushing all opening parentheses, but now I have to watch what it is that you are popping. So my pop function will be different - the result of the pop needs to be stored in a variable (which I will then compare to the incoming closing parentheses to see if the types match. I will need to add another error to your list of errors to handle the mismatched parentheses type.
Can somebody take a look at my current code and see if this is correct?
I feel like the logic if off.
The code is as follows. More questions at the bottom of the code.
Stack Class:
public class ArrayStack
private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10;
public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0; }
public void push(E value) { if (isFull() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Full"); } else this.arrS[this.size++] = value;
}
public E pop() { if (isEmpty() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Empty"); } return this.arrS[--this.size]; }
public boolean isFull() { return (this.size == MAX_STACK_SIZE) ? true : false;
}
public boolean isEmpty() { return (this.size == 0) ? true : false;
}
public int length() { return this.size; }
public E topValue() { if (isEmpty() == true) { throw new IllegalArgumentException(); } return this.arrS[this.size - 1]; }
public void clear() { this.size = 0;
}
public String show() {
StringBuilder sb = new StringBuilder(); sb.append(" "); for (int i = 0; i < this.size; i++) { sb.append(this.arrS[i]); sb.append(" ");
} sb.append(""); return sb.toString();
}
}
Main Driver:
public class a3main {
public static void main(String[] args) { int count = 0;
Scanner scan = new Scanner(System.in);
do { System.out.println("Enter an algebraic or arithmetic expression: ");
String arg = scan.nextLine();
ArrayStack stack = new ArrayStack(); boolean flag = true; for (int i = 0; i < arg.length(); i++) { count++; char current = arg.charAt(i);
if (current == '{' || current == '[' || current == '(') { try { stack.push(current); } catch (Exception e) { System.out.println("Excessive parenthesis combinatins"); flag = false; break; } }
if ((current == '}' || current == ')' || current == ']')) { if (stack.isEmpty() == true) { System.out.println("Excessive right parenthesis"); flag = false; break; } char last = (char) stack.topValue(); if (current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[') stack.pop(); }
} if (flag) { if (stack.isEmpty() == true) { System.out.println("Correct parenthesis: match"); } else { System.out.println("Excessive left parenthesis"); } } } while (count > 0); } }
Here is the previous application guidelines. I want to see if i did that correct as well.
These would be examples of parentheses related syntax errors:
x=((a-b)+(c-d)
x=(a-b))
x= (((((((((((a-b))))))))))) (assuming only 10 levels of parentheses are allowed)
Think of an appropriate error message for each of the above (you will need them later).
Note that simply counting open and closed parentheses is not going to do it, because the 'count' in this expression is fine, but the syntax is not:
a)(b
The way we solve this problem is by using a stack. We employ a procedure which is in 'compiler-speak' called a scanner. There is nothing fancy about it - we simply check every character of an expression (from left to right) to make sure that the expression conforms to some syntax rule
Loosely the loop will look like this:
while (not the last character) and (not error) do
ix++
....do something with parentheses and stack
end while.
ix needs to be properly initialized to point to the first character, and error needs to be set to 0 (meaning- there is no error). Pop and push operations inside this loop will possibly reset the error to some other value (recall 'silent error reporting' from the previous lecture)
So, what is the ....do something part?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
