Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. public abstract class AbstractBinaryOperation implements Expression {
  2. public Expression firstOperand;
  3. public Expression secondOperand;
  4.  
  5. public AbstractBinaryOperation(Expression left, Expression right) {
  6. this.firstOperand = left;
  7. this.secondOperand = right;
  8. }
  9.  
  10. protected abstract boolean calculate(boolean left, boolean right);
  11.  
  12. public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
  13. boolean evaluatedFirstOperand = false, evaluatedSecondOperand = false;
  14. try {
  15. evaluatedFirstOperand = firstOperand.evaluate(a, b, c, d, e, f, g, h, i, j);
  16. evaluatedSecondOperand = secondOperand.evaluate(a, b, c, d, e, f, g, h, i, j);
  17. } catch (NullPointerException ex) {
  18. System.out.println("Parsing error!");
  19. System.exit(1);
  20. }
  21. return calculate(evaluatedFirstOperand, evaluatedSecondOperand);
  22. }
  23. }
  24.  
  25. public abstract class AbstractUnaryOperation implements Expression {
  26. public Expression singleOperand;
  27.  
  28. public AbstractUnaryOperation(Expression currentOperand) {
  29. this.singleOperand = currentOperand;
  30. }
  31.  
  32. protected abstract boolean calculate(boolean operand);
  33.  
  34. public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
  35. return calculate(singleOperand.evaluate(a, b, c, d, e, f, g, h, i, j));
  36. }
  37. }
  38.  
  39. public class And extends AbstractBinaryOperation {
  40. public And(Expression left, Expression right) {
  41. super(left, right);
  42. }
  43.  
  44. protected boolean calculate(boolean left, boolean right) {
  45. return left && right;
  46. }
  47. }
  48.  
  49. public class Const implements Expression {
  50. private boolean value;
  51.  
  52. public Const(boolean value) {
  53. this.value = value;
  54. }
  55.  
  56. public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
  57. return value;
  58. }
  59. }
  60.  
  61. public interface Expression {
  62. boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j);
  63. }
  64.  
  65. public class ExpressionParser implements Parser {
  66. private String expression;
  67. private char name;
  68. private int currentIndex = 0;
  69. private boolean value = false;
  70. private int differenceInBrackets = 0;
  71. private enum Token {
  72. AND, CLOSE_BRACE, CONST, BEGIN, ERR, NOT, OPEN_BRACE, OR, VAR
  73. }
  74.  
  75. private Token currentToken = Token.ERR;
  76.  
  77. private void skipSpaces() {
  78. while (currentIndex < expression.length() && Character.isWhitespace(expression.charAt(currentIndex))) {
  79. ++currentIndex;
  80. }
  81. }
  82.  
  83. private void nextToken() {
  84. skipSpaces();
  85. if (currentIndex >= expression.length()) {
  86. currentToken = Token.BEGIN;
  87. return;
  88. }
  89. char currentSymbol = expression.charAt(currentIndex);
  90. switch (expression.charAt(currentIndex)) {
  91. case '(':
  92. currentToken = Token.OPEN_BRACE;
  93. differenceInBrackets++;
  94. break;
  95. case ')':
  96. currentToken = Token.CLOSE_BRACE;
  97. differenceInBrackets--;
  98. break;
  99. case '&':
  100. currentToken = Token.AND;
  101. break;
  102. case '|':
  103. currentToken = Token.OR;
  104. break;
  105. case '~':
  106. currentToken = Token.NOT;
  107. break;
  108. default:
  109. if (Character.isDigit(currentSymbol)) {
  110. if (currentIndex + 1 < expression.length()) {
  111. if (Character.isDigit(expression.charAt(currentIndex + 1)) || Character.isLetter(expression.charAt(currentIndex + 1))) {
  112. System.out.println("Parsing error!");
  113. System.exit(1);
  114. }
  115. }
  116. if (currentSymbol == '0') {
  117. value = false;
  118. } else {
  119. value = true;
  120. }
  121. currentToken = Token.CONST;
  122. } else if (Character.isLetter(expression.charAt(currentIndex))) {
  123. if (currentIndex + 1 < expression.length()) {
  124. if (Character.isDigit(expression.charAt(currentIndex + 1)) || Character.isLetter(expression.charAt(currentIndex + 1))) {
  125. System.out.println("Parsing error!");
  126. System.exit(1);
  127. }
  128. }
  129. name = currentSymbol;
  130. currentToken = Token.VAR;
  131. } else {
  132. System.out.println("Parsing error!");
  133. System.exit(1);
  134. }
  135. }
  136. ++currentIndex;
  137. }
  138.  
  139. private Expression unary() {
  140. nextToken();
  141. Expression res = null;
  142.  
  143. switch (currentToken) {
  144. case CONST:
  145. res = new Const(value);
  146. nextToken();
  147. break;
  148. case VAR:
  149. res = new Variable(name);
  150. nextToken();
  151. break;
  152. case OPEN_BRACE:
  153. res = or();
  154. nextToken();
  155. break;
  156. case NOT:
  157. res = new Not(unary());
  158. break;
  159. }
  160. return res;
  161. }
  162.  
  163. private Expression and() {
  164. Expression res = unary();
  165. while (true) {
  166. if (currentToken == Token.AND) {
  167. res = new And(res, and());
  168. } else {
  169. return res;
  170. }
  171. }
  172. }
  173.  
  174. private Expression or() {
  175. Expression res = and();
  176. while (true) {
  177. if (currentToken == Token.OR) {
  178. res = new Or(res, and());
  179. } else {
  180. return res;
  181. }
  182. }
  183. }
  184.  
  185. public Expression parse(final String expression) {
  186. assert expression != null : "Expression is null";
  187. currentIndex = 0;
  188. this.expression = expression;
  189. currentToken = Token.ERR;
  190. Expression result = or();
  191. if (differenceInBrackets != 0) {
  192. System.out.println("Parsing error!");
  193. System.exit(1);
  194. }
  195. return result;
  196. }
  197. }
  198.  
  199. public class Not extends AbstractUnaryOperation {
  200. public Not(Expression operand) {
  201. super(operand);
  202. }
  203.  
  204. @Override
  205. protected boolean calculate(boolean operand) {
  206. return !operand;
  207. }
  208. }
  209.  
  210. public class Or extends AbstractBinaryOperation {
  211. public Or(Expression left, Expression right) {
  212. super(left, right);
  213. }
  214.  
  215. @Override
  216. protected boolean calculate(boolean left, boolean right) {
  217. return left || right;
  218. }
  219. }
  220.  
  221. public interface Parser {
  222. Expression parse(String expression);
  223. }
  224.  
  225. public class Variable implements Expression {
  226. private char name;
  227.  
  228. public Variable(char name) {
  229. this.name = name;
  230. }
  231.  
  232. public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
  233. switch(name) {
  234. case 'a':
  235. return a;
  236. case 'b':
  237. return b;
  238. case 'c':
  239. return c;
  240. case 'd':
  241. return d;
  242. case 'e':
  243. return e;
  244. case 'f':
  245. return f;
  246. case 'g':
  247. return g;
  248. case 'h':
  249. return h;
  250. case 'i':
  251. return i;
  252. case 'j':
  253. return j;
  254. default:
  255. System.out.println("Parsing error!");
  256. System.exit(1);
  257. }
  258. return false;
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement