fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Scanner;
  7. import java.util.StringTokenizer;
  8. class steck {
  9. private int size;
  10. private Object[] date;
  11. private int capacity;
  12. public steck(){
  13. date = new Object[2];
  14. capacity = 2;
  15. }
  16. public steck(int x){
  17. date = new Object[x];;
  18. capacity = x;
  19. }
  20.  
  21. public boolean isEmpty(){
  22. return size == 0;
  23. }
  24.  
  25. private void resize(int x){
  26. Object[] newDate = new Object[x];
  27. System.arraycopy(date, 0, newDate, 0, size);
  28. date = null;
  29. date = newDate;
  30. }
  31.  
  32. public void push(Object x){
  33. if(size == capacity){
  34. resize(2*capacity);
  35. }
  36. date[size++] = x;
  37. }
  38.  
  39. public Object pop(){
  40. if(isEmpty()){
  41. return null;
  42. }
  43. if(size < capacity/2){
  44. resize(capacity/2);
  45. }
  46. Object temp = date[size -1];
  47. size = size -1;
  48. return temp;
  49. }
  50.  
  51. public Object peek(){
  52. if(isEmpty()) return null;
  53. return date[size-1];
  54. }
  55.  
  56. public void clear(){
  57. size = 0;
  58. }
  59.  
  60. public int size(){
  61. return size;
  62. }
  63. public int getCapacity(){
  64. return size;
  65. }
  66. }
  67. /**
  68.  * Created by green on 20.12.15.
  69.  */
  70. class Parser{
  71. private String operators = "u+-*/";
  72. private String delimiters = operators + "( )";
  73. public boolean correct = true;
  74. private boolean isOperator(String x){
  75. if(x.length() != 1) return false;
  76. for(int i = 0; i < operators.length(); i++){
  77. if(x.charAt(0) == operators.charAt(i)) return true;
  78. }
  79. return false;
  80. }
  81. private boolean isDelimiter(String x){
  82. if(x.length() != 1) return false;
  83. for(int i = 0; i < delimiters.length(); i++){
  84. if(x.charAt(0) == delimiters.charAt(i)) return true;
  85. }
  86. return false;
  87. }
  88. private int priority(String x) {
  89. if (x.equals("(")) return 1;
  90. if (x.equals("+") || x.equals("-")) return 2;
  91. if (x.equals("*") || x.equals("/")) return 3;
  92. return 4;
  93. }
  94. public String parse(String infix) {
  95. String postfix = "";
  96. steck stack = new steck();
  97. StringTokenizer tokenizer = new StringTokenizer(infix, delimiters, true);
  98. String prev = "", curr = "";
  99. while (tokenizer.hasMoreTokens()) {
  100. curr = tokenizer.nextToken();
  101. if (!tokenizer.hasMoreTokens() && isOperator(curr)) {
  102. postfix = ("Некорректное выражение.");
  103. correct = false;
  104. return postfix;
  105. }
  106. if (curr.equals(" ")) continue;
  107. if (isDelimiter(curr)) {
  108. if (curr.equals("(")) stack.push(curr);
  109. else if (curr.equals(")")) {
  110. while (!stack.peek().equals("(")) {
  111. postfix += stack.pop().toString() + " ";
  112. if (stack.isEmpty()) {
  113. postfix = ("Скобки не согласованы.");
  114. correct = false;
  115. return postfix;
  116. }
  117. }
  118. stack.pop();
  119. }else {
  120. if (curr.equals("-") && (prev.equals("") || (isDelimiter(prev) && !prev.equals(")")))) {
  121. curr = "u";
  122. }else{
  123. while (!stack.isEmpty() && (priority(curr) <= priority(stack.peek().toString()))) {
  124. postfix += stack.pop().toString() + " ";
  125. }
  126.  
  127. }
  128. stack.push(curr);
  129. }
  130.  
  131. }else {
  132. postfix += curr + " ";
  133. }
  134. prev = curr;
  135. }
  136. while (!stack.isEmpty()) {
  137. if (isOperator(stack.peek().toString())) postfix += stack.pop().toString() + " ";
  138. else {
  139. postfix = "Скобки не согласованы.";
  140. correct = false;
  141. return postfix;
  142. }
  143. }
  144. return postfix;
  145. }
  146. public int calc(String postfix) {
  147. if(!correct) return 0;
  148. steck stack = new steck();
  149. for (int i = 0; i < postfix.length() ; i++) {
  150. if(postfix.charAt(i) == ' ') continue;
  151. if (postfix.charAt(i) == '+') stack.push(Integer.valueOf(stack.pop().toString()) + Integer.valueOf(stack.pop().toString()));
  152. else if (postfix.charAt(i) == '-') {
  153. int b = Integer.valueOf(stack.pop().toString()), a = Integer.valueOf(stack.pop().toString());
  154. stack.push(a - b);
  155. }
  156. else if (postfix.charAt(i) == '*') stack.push(Integer.valueOf(stack.pop().toString()) * Integer.valueOf(stack.pop().toString()));
  157. else if (postfix.charAt(i) == '/') {
  158. int b = Integer.valueOf(stack.pop().toString()), a = Integer.valueOf(stack.pop().toString());
  159. stack.push(a / b);
  160. }
  161. else if (postfix.charAt(i) == 'u') stack.push(-Integer.valueOf(stack.pop().toString()));
  162. else{
  163. int g = postfix.indexOf(' ',i);
  164. stack.push(Integer.valueOf(postfix.substring(i, g)));
  165. i = g;
  166. }
  167. }
  168. return Integer.valueOf(stack.pop().toString());
  169. }
  170. }
  171.  
  172. class Ideone
  173. {
  174. public static void main (String[] args) throws java.lang.Exception
  175. {
  176. Scanner in = new Scanner(System.in);
  177. String s = "15 * -(3 - 4)";
  178. Parser x = new Parser();
  179. s = x.parse(s);
  180. if(x.correct){
  181. System.out.println(s);
  182. System.out.println(x.calc(s));
  183. }else{
  184. System.out.println("Ошибка:"+s);
  185. }
  186. }
  187. }
Success #stdin #stdout 0.17s 321344KB
stdin
Standard input is empty
stdout
15 3 4 - u * 
15