Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. package kek;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class Parser {
  11. public class Result {
  12. Double num;
  13. String rest;
  14.  
  15. public Result(Double num, String rest) {
  16. this.num = num;
  17. this.rest = rest;
  18. }
  19. }
  20.  
  21. Map<String, Double> vars = new HashMap<String, Double>();
  22.  
  23. public Parser() {
  24.  
  25. }
  26.  
  27. public void setVariable(String var, Double value) {
  28. this.vars.put(var, value);
  29. }
  30.  
  31. public Double getVariable(String var) {
  32. return this.vars.get(var);
  33. }
  34.  
  35. public Result add(String s) throws Exception {
  36. Result current = this.sub(s);
  37. Double num = current.num;
  38.  
  39. while (current.rest.length() > 0) {
  40. if (!current.rest.substring(0, 1).equals("+")) {
  41. break;
  42. }
  43.  
  44. String next = current.rest.substring(1);
  45.  
  46. current = this.sub(next);
  47.  
  48. num = num + current.num;
  49. }
  50.  
  51. return new Result(num, current.rest);
  52. }
  53.  
  54. public Result sub(String s) throws Exception {
  55. Result current = this.mul(s);
  56.  
  57. Double num = current.num;
  58.  
  59. while (current.rest.length() > 0) {
  60. if (!current.rest.substring(0, 1).equals("-")) {
  61. break;
  62. }
  63.  
  64. String next = current.rest.substring(1);
  65. current = this.mul(next);
  66.  
  67. num = num - current.num;
  68. }
  69.  
  70. return new Result(num, current.rest);
  71. }
  72.  
  73. public Result mul(String s) throws Exception {
  74. Result current = this.div(s);
  75.  
  76. Double num = current.num;
  77.  
  78. while (current.rest.length() > 0) {
  79. if (!current.rest.substring(0, 1).equals("*")) {
  80. break;
  81. }
  82.  
  83. String next = current.rest.substring(1);
  84. current = this.div(next);
  85.  
  86. num = num * current.num;
  87. }
  88.  
  89. return new Result(num, current.rest);
  90. }
  91.  
  92. public Result div(String s) throws Exception {
  93. Result current = this.exp(s);
  94.  
  95. Double num = current.num;
  96.  
  97. while (current.rest.length() > 0) {
  98. if (!current.rest.substring(0, 1).equals("/")) {
  99. break;
  100. }
  101.  
  102. String next = current.rest.substring(1);
  103. current = this.exp(next);
  104.  
  105. num = num / current.num;
  106. }
  107.  
  108. return new Result(num, current.rest);
  109. }
  110.  
  111. public Result exp(String s) throws Exception {
  112. Result current = this.brackets(s);
  113. Double num = current.num;
  114.  
  115. while (current.rest.length() > 0) {
  116. if (!current.rest.substring(0, 1).equals("^")) {
  117. break;
  118. }
  119.  
  120. String next = current.rest.substring(1);
  121. current = this.brackets(next);
  122.  
  123. num = Math.pow(num, current.num);
  124. }
  125.  
  126. return new Result(num, current.rest);
  127. }
  128.  
  129. public Result brackets(String s) throws Exception {
  130. if (s.charAt(0) == '(') {
  131. Result result = this.add(s.substring(1));
  132. if (result.rest.length() > 0 && result.rest.charAt(0) == ')') {
  133. result.rest = result.rest.substring(1);
  134. } else {
  135. throw new Exception("Вы забыли закрыть скобку после " + s);
  136. }
  137.  
  138. return result;
  139. }
  140.  
  141. return this.variables(s);
  142. }
  143.  
  144. public Result variables(String s) throws Exception {
  145. String f = "";
  146. int i = 0;
  147. while (i < s.length()) {
  148. char letter = s.charAt(i);
  149. if (!this.isLetter("" + letter)) {
  150. break;
  151. }
  152.  
  153. f += letter;
  154. i++;
  155. }
  156.  
  157. String sub = s.substring(f.length());
  158.  
  159. if (sub.length() != s.length())
  160. return new Result(this.getVariable(f), sub);
  161.  
  162. return this.constant(s);
  163. }
  164.  
  165. public Result constant(String s) throws Exception {
  166.  
  167. String regex = "^((\\+|-)?([0-9]+)(\\.[0-9]+)?)|((\\+|-)?\\.?[0-9]+)";
  168.  
  169. Matcher matcher = Pattern.compile(regex).matcher(s);
  170.  
  171. if (matcher.find()) {
  172. String group = matcher.group();
  173.  
  174. return new Result(Double.valueOf(group), s.substring(group.length()));
  175. } else {
  176. return new Result(null, s);
  177. }
  178. }
  179.  
  180. public List<String> findVariables(String s) {
  181. List<String> result = new ArrayList<>();
  182.  
  183. int i = 0;
  184. while (i < s.length()) {
  185. String letter = "" + s.charAt(i);
  186. if (this.isLetter(letter) && result.indexOf(letter) < 0) {
  187. result.add(letter);
  188. }
  189.  
  190. i++;
  191. }
  192.  
  193. return result;
  194. }
  195.  
  196. public boolean isLetter(String c) {
  197. c = c.toLowerCase();
  198. return c != c.toUpperCase();
  199. }
  200.  
  201. public Double eval(String str) throws Exception {
  202. Result result = this.add(str);
  203.  
  204. if (result.rest.length() > 0) {
  205. throw new Exception("Невозможно полностью спарсить. Осталось незакрытым: " + result.rest);
  206. }
  207.  
  208. return result.num;
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement