Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class myStack<T>{
- private ArrayList<T> stack = new ArrayList<T>();
- public int size = 0;
- public boolean isEmpty(){
- return this.size==0?true:false;
- };
- public void push(T element){
- if(this.stack.size()<=this.size)this.stack.add(element);
- else this.stack.set(this.size, element);
- //this.isEmpty = false;
- this.size++;
- };
- public T pop(){
- T last = this.stack.get(this.size - 1);
- this.size--;
- return last;
- }
- public T peek(){
- return this.stack.get(this.size - 1);
- }
- }
- class Stack<T>{
- private Object[] array = new Object[1];
- }
- class myError{
- private int inLine, inColumn, typeOfError;
- //typeOfError equals -1 if there are not errors
- //typeOfError equals 1 if there is wrong type of bracket
- //typeOfError equals 2 if there is unexpected closing
- //typeOfError equals 3 if there is unexpected end of input
- private char expectedSign, gotSign;
- public myError(){
- this.inLine = -1;
- this.inColumn=-1;
- this.typeOfError = -1;
- }
- public myError(int inLine, int inColumn, char expectedSign, char gotSign, int typeOfError){
- this.inLine = inLine;
- this.inColumn = inColumn;
- this.expectedSign = expectedSign;
- this.gotSign = gotSign;
- this.typeOfError = typeOfError;
- }
- private boolean errorExists(){
- return this.inLine == -1;
- }
- @Override
- public String toString() {
- if(errorExists())
- System.out.println("");
- else System.out.println("");
- }
- }
- public class Main {
- public static void main(String[] args){
- myError error = new myError();
- Scanner scanner = new Scanner(System.in);
- String input;
- myStack<Character> stack = new myStack<>();
- stack.push('?');//We put it to avoid additional check if the stack will be empty
- int line = 1;
- while(scanner.hasNextLine()){
- input = scanner.nextLine();
- for(int i=0;i<input.length();++i){
- switch(input.charAt(i)){
- case ('('):
- stack.push('(');
- break;
- case ('['):
- stack.push('[');
- break;
- case ('{'):
- stack.push('{');
- break;
- case (')'):
- if(stack.peek()=='?'){
- error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
- }
- else if(stack.peek()!='(' && stack.peek()=='['){
- error = new myError(line,i+1,']',input.charAt(i), 1)
- }
- else if(stack.peek()!='(' && stack.peek()=='{'){
- error = new myError(line,i+1,'}',input.charAt(i), 1)
- }
- break;
- case (']'):
- if(stack.peek()=='?'){
- error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
- }
- else if(stack.peek()!='[' && stack.peek()=='('){
- error = new myError(line,i+1,')',input.charAt(i), 1)
- }
- else if(stack.peek()!='[' && stack.peek()=='{'){
- error = new myError(line,i+1,'}',input.charAt(i), 1)
- }
- break;
- case ('}'):
- if(stack.peek()=='?'){
- error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
- }
- else if(stack.peek()!='{' && stack.peek()=='['){
- error = new myError(line,i+1,']',input.charAt(i), 1)
- }
- else if(stack.peek()!='{' && stack.peek()=='('){
- error = new myError(line,i+1,')',input.charAt(i), 1)
- }
- break;
- }
- }
- line++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement