Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class myStack<T>{
  4. private ArrayList<T> stack = new ArrayList<T>();
  5. public int size = 0;
  6. public boolean isEmpty(){
  7. return this.size==0?true:false;
  8. };
  9. public void push(T element){
  10. if(this.stack.size()<=this.size)this.stack.add(element);
  11. else this.stack.set(this.size, element);
  12. //this.isEmpty = false;
  13. this.size++;
  14. };
  15. public T pop(){
  16. T last = this.stack.get(this.size - 1);
  17. this.size--;
  18. return last;
  19. }
  20. public T peek(){
  21. return this.stack.get(this.size - 1);
  22. }
  23. }
  24.  
  25. class Stack<T>{
  26. private Object[] array = new Object[1];
  27.  
  28. }
  29.  
  30. class myError{
  31. private int inLine, inColumn, typeOfError;
  32. //typeOfError equals -1 if there are not errors
  33. //typeOfError equals 1 if there is wrong type of bracket
  34. //typeOfError equals 2 if there is unexpected closing
  35. //typeOfError equals 3 if there is unexpected end of input
  36. private char expectedSign, gotSign;
  37. public myError(){
  38. this.inLine = -1;
  39. this.inColumn=-1;
  40. this.typeOfError = -1;
  41. }
  42. public myError(int inLine, int inColumn, char expectedSign, char gotSign, int typeOfError){
  43. this.inLine = inLine;
  44. this.inColumn = inColumn;
  45. this.expectedSign = expectedSign;
  46. this.gotSign = gotSign;
  47. this.typeOfError = typeOfError;
  48. }
  49. private boolean errorExists(){
  50. return this.inLine == -1;
  51. }
  52. @Override
  53. public String toString() {
  54. if(errorExists())
  55. System.out.println("");
  56. else System.out.println("");
  57. }
  58. }
  59.  
  60. public class Main {
  61. public static void main(String[] args){
  62. myError error = new myError();
  63. Scanner scanner = new Scanner(System.in);
  64. String input;
  65. myStack<Character> stack = new myStack<>();
  66. stack.push('?');//We put it to avoid additional check if the stack will be empty
  67. int line = 1;
  68. while(scanner.hasNextLine()){
  69. input = scanner.nextLine();
  70. for(int i=0;i<input.length();++i){
  71. switch(input.charAt(i)){
  72. case ('('):
  73. stack.push('(');
  74. break;
  75.  
  76. case ('['):
  77. stack.push('[');
  78. break;
  79.  
  80. case ('{'):
  81. stack.push('{');
  82. break;
  83.  
  84. case (')'):
  85. if(stack.peek()=='?'){
  86. error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
  87. }
  88. else if(stack.peek()!='(' && stack.peek()=='['){
  89. error = new myError(line,i+1,']',input.charAt(i), 1)
  90. }
  91. else if(stack.peek()!='(' && stack.peek()=='{'){
  92. error = new myError(line,i+1,'}',input.charAt(i), 1)
  93. }
  94. break;
  95. case (']'):
  96. if(stack.peek()=='?'){
  97. error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
  98. }
  99. else if(stack.peek()!='[' && stack.peek()=='('){
  100. error = new myError(line,i+1,')',input.charAt(i), 1)
  101. }
  102. else if(stack.peek()!='[' && stack.peek()=='{'){
  103. error = new myError(line,i+1,'}',input.charAt(i), 1)
  104. }
  105. break;
  106.  
  107. case ('}'):
  108. if(stack.peek()=='?'){
  109. error = new myError(line, i+1,input.charAt(i),input.charAt(i),2);
  110. }
  111. else if(stack.peek()!='{' && stack.peek()=='['){
  112. error = new myError(line,i+1,']',input.charAt(i), 1)
  113. }
  114. else if(stack.peek()!='{' && stack.peek()=='('){
  115. error = new myError(line,i+1,')',input.charAt(i), 1)
  116. }
  117. break;
  118.  
  119. }
  120. }
  121. line++;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement