Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. // 63180383
  2.  
  3. abstract class Izraz {
  4. public static void main(String[] args) {
  5. Izraz x = Izraz.zgradi("3+4");
  6. System.out.println(x.steviloOperatorjev());
  7. }
  8. public static Izraz zgradi(String niz) {
  9. int I = 0;
  10. int J = niz.length() - 1;
  11. while (1 == 1) {
  12. int zag = 0;
  13. boolean cc = true;
  14. if (niz.charAt(I) == '(' && niz.charAt(J) == ')') {
  15. for (int i = I + 1; i < J; i++) {
  16. if (niz.charAt(i) == '(')
  17. zag++;
  18. else if(niz.charAt(i) == ')')
  19. zag--;
  20. if (zag < 0) {
  21. cc = false;
  22. break;
  23. }
  24. }
  25. }
  26. else {
  27. cc = false;
  28. }
  29.  
  30. if (zag != 0)
  31. cc = false;
  32.  
  33. if (cc == true) {
  34. I++;
  35. J--;
  36. }
  37. if (I >= J) {
  38. I--;
  39. J++;
  40. break;
  41. }
  42. }
  43. String nniz = niz.substring(I, J + 1);
  44. char a = 'a';
  45. int index = -1;
  46. for (int i = nniz.length() - 1; i > 0; i--) {
  47. int zag = 0;
  48. if (nniz.charAt(i) == '/' || nniz.charAt(i) == '*' || nniz.charAt(i) == '+' || nniz.charAt(i) == '-') {
  49. if (a == 'a') {
  50. a = nniz.charAt(i);
  51. }
  52. else if (a != nniz.charAt(i))
  53. continue;
  54. for (int j = 0; j < i; j++) {
  55. if (nniz.charAt(j) == '(')
  56. zag++;
  57. else if(nniz.charAt(j) == ')')
  58. zag--;
  59. if (zag < 0)
  60. break;
  61. }
  62. if (zag != 0)
  63. continue;
  64. zag = 0;
  65. for (int j = i + 1; j < nniz.length(); j++) {
  66. if (nniz.charAt(j) == '(')
  67. zag++;
  68. else if(nniz.charAt(j) == ')')
  69. zag--;
  70. if (zag < 0)
  71. break;
  72. }
  73. if (zag != 0)
  74. continue;
  75. else {
  76. index = i;
  77. break;
  78. }
  79. }
  80. }
  81. if (index == -1) {
  82. return new Stevilo (Integer.valueOf(nniz));
  83. }
  84.  
  85. return new SestavljeniIzraz(zgradi(nniz.substring(0, index)), nniz.charAt(index), zgradi(nniz.substring(index + 1, nniz.length())));
  86. }
  87.  
  88. public int steviloOperatorjev() {
  89. if (this instanceof Stevilo) return 0;
  90. return 1 + ((SestavljeniIzraz) this).left.steviloOperatorjev() + ((SestavljeniIzraz) this).right.steviloOperatorjev();
  91. }
  92.  
  93. public String postfiksno() {
  94. if (this instanceof Stevilo) return Integer.toString(((Stevilo) this).value);
  95. return ((SestavljeniIzraz) this).left.postfiksno() + ((SestavljeniIzraz) this).right.postfiksno() + Character.toString(((SestavljeniIzraz) this).op);
  96. }
  97.  
  98. public int vrednost() {
  99. if (this instanceof Stevilo) return ((Stevilo) this).value;
  100.  
  101. char ch = ((SestavljeniIzraz) this).op;
  102. int left = ((SestavljeniIzraz) this).left.vrednost();
  103. int right = ((SestavljeniIzraz) this).right.vrednost();
  104.  
  105. if (ch == '+') return left + right;
  106. if (ch == '-') return left - right;
  107. if (ch == '*') return left * right;
  108. if (ch == '/') return left / right;
  109. return -1;
  110.  
  111. return -1;
  112. }
  113. }
  114.  
  115. class Stevilo extends Izraz {
  116. int value;
  117. public Stevilo(int n) {
  118. this.value = n;
  119. }
  120. }
  121.  
  122. class SestavljeniIzraz extends Izraz {
  123. char op;
  124. Izraz left, right;
  125.  
  126. public SestavljeniIzraz(Izraz levi, char operator, Izraz desni) {
  127. this.left = levi;
  128. this.right = desni;
  129. this.op = operator;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement