Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 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.Random;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class Parser {
  12. public class Result {
  13. Double num;
  14. String rest;
  15.  
  16. public Result(Double num, String rest) {
  17. this.num = num;
  18. this.rest = rest;
  19. }
  20. }
  21.  
  22. Map<String, Double> vars = new HashMap<String, Double>();
  23.  
  24. public Parser() {
  25.  
  26. }
  27.  
  28. public void setVariable(String var, Double value) {
  29. this.vars.put(var, value);
  30. }
  31.  
  32. public Double getVariable(String var) {
  33. return this.vars.get(var);
  34. }
  35.  
  36. public Result add(String s) throws Exception {
  37. Result current = this.sub(s);
  38. Double num = current.num;
  39.  
  40. while (current.rest.length() > 0) {
  41. if (!current.rest.substring(0, 1).equals("+")) {
  42. break;
  43. }
  44.  
  45. String next = current.rest.substring(1);
  46.  
  47. current = this.sub(next);
  48.  
  49. num = num + current.num;
  50. }
  51.  
  52. return new Result(num, current.rest);
  53. }
  54.  
  55. public Result sub(String s) throws Exception {
  56. Result current = this.mul(s);
  57.  
  58. Double num = current.num;
  59.  
  60. while (current.rest.length() > 0) {
  61. if (!current.rest.substring(0, 1).equals("-")) {
  62. break;
  63. }
  64.  
  65. String next = current.rest.substring(1);
  66. current = this.mul(next);
  67.  
  68. num = num - current.num;
  69. }
  70.  
  71. return new Result(num, current.rest);
  72. }
  73.  
  74. public Result mul(String s) throws Exception {
  75. Result current = this.div(s);
  76.  
  77. Double num = current.num;
  78.  
  79. while (current.rest.length() > 0) {
  80. if (!current.rest.substring(0, 1).equals("*")) {
  81. break;
  82. }
  83.  
  84. String next = current.rest.substring(1);
  85. current = this.div(next);
  86.  
  87. num = num * current.num;
  88. }
  89.  
  90. return new Result(num, current.rest);
  91. }
  92.  
  93. public Result div(String s) throws Exception {
  94. Result current = this.exp(s);
  95.  
  96. Double num = current.num;
  97.  
  98. while (current.rest.length() > 0) {
  99. if (!current.rest.substring(0, 1).equals("/")) {
  100. break;
  101. }
  102.  
  103. String next = current.rest.substring(1);
  104. current = this.exp(next);
  105.  
  106. num = num / current.num;
  107. }
  108.  
  109. return new Result(num, current.rest);
  110. }
  111.  
  112. public Result exp(String s) throws Exception {
  113. Result current = this.brackets(s);
  114. Double num = current.num;
  115.  
  116. while (current.rest.length() > 0) {
  117. if (!current.rest.substring(0, 1).equals("^")) {
  118. break;
  119. }
  120.  
  121. String next = current.rest.substring(1);
  122. current = this.brackets(next);
  123.  
  124. num = Math.pow(num, current.num);
  125. }
  126.  
  127. return new Result(num, current.rest);
  128. }
  129.  
  130. public Result brackets(String s) throws Exception {
  131. if (s.charAt(0) == '(') {
  132. Result result = this.add(s.substring(1));
  133. if (result.rest.length() > 0 && result.rest.charAt(0) == ')') {
  134. result.rest = result.rest.substring(1);
  135. } else {
  136. throw new Exception("Вы забыли закрыть скобку после " + s);
  137. }
  138.  
  139. return result;
  140. }
  141.  
  142. return this.sin(s);
  143. }
  144.  
  145. public Result sin(String s) throws Exception {
  146. Matcher matcher = Pattern.compile("^[sin]+[\\(]+.+").matcher(s);
  147. if (matcher.find()) {
  148. System.out.println("Functions: " + s);
  149. System.out.println("Functions found: " + matcher.group());
  150. Result result = this.add(s.substring(4)); //length("sin(") = 4
  151.  
  152. if (result.rest.length() > 0 && result.rest.charAt(0) == ')') {
  153. result.rest = result.rest.substring(1);
  154. } else {
  155. throw new Exception("Вы забыли закрыть скобку после " + s);
  156. }
  157.  
  158. result.num = Math.sin(result.num);
  159.  
  160. return result;
  161. }
  162.  
  163. return this.cos(s);
  164. }
  165.  
  166. public Result cos(String s) throws Exception {
  167. Matcher matcher = Pattern.compile("^[cos]+[\\(]+.+").matcher(s);
  168. if (matcher.find()) {
  169. System.out.println("Functions: " + s);
  170. System.out.println("Functions found: " + matcher.group());
  171. Result result = this.add(s.substring(4)); //length("sin(") = 4
  172.  
  173. if (result.rest.length() > 0 && result.rest.charAt(0) == ')') {
  174. result.rest = result.rest.substring(1);
  175. } else {
  176. throw new Exception("Вы забыли закрыть скобку после " + s);
  177. }
  178.  
  179. result.num = Math.sin(result.num);
  180.  
  181. return result;
  182. }
  183.  
  184. return this.expFunc(s);
  185. }
  186.  
  187. public Result expFunc(String s) throws Exception {
  188. Matcher matcher = Pattern.compile("^[exp]+[\\(]+.+").matcher(s);
  189. if (matcher.find()) {
  190. System.out.println("Functions: " + s);
  191. System.out.println("Functions found: " + matcher.group());
  192. Result result = this.add(s.substring(4)); //length("sin(") = 4
  193.  
  194. if (result.rest.length() > 0 && result.rest.charAt(0) == ')') {
  195. result.rest = result.rest.substring(1);
  196. } else {
  197. throw new Exception("Вы забыли закрыть скобку после " + s);
  198. }
  199.  
  200. result.num = Math.sin(result.num);
  201.  
  202. return result;
  203. }
  204.  
  205. return this.rand(s);
  206. }
  207.  
  208. public Result rand(String s) throws Exception {
  209. Matcher matcher = Pattern.compile("^[rand]+[\\(]+[\\\\)]").matcher(s);
  210. if (matcher.find()) {
  211. System.out.println("Functions: " + s);
  212. System.out.println("Functions found: " + matcher.group());
  213.  
  214. Result result = new Result(new Random().nextDouble(), s.substring(6));
  215.  
  216. return result;
  217. }
  218.  
  219. return this.variables(s);
  220. }
  221.  
  222. public Result variables(String s) throws Exception {
  223. String f = "";
  224. int i = 0;
  225. while (i < s.length()) {
  226. char letter = s.charAt(i);
  227. if (!this.isLetter("" + letter)) {
  228. break;
  229. }
  230.  
  231. f += letter;
  232. i++;
  233. }
  234.  
  235. String sub = s.substring(f.length());
  236.  
  237. if (sub.length() != s.length())
  238. return new Result(this.getVariable(f), sub);
  239.  
  240. return this.constant(s);
  241. }
  242.  
  243. public Result constant(String s) throws Exception {
  244.  
  245. String regex = "^((\\+|-)?([0-9]+)(\\.[0-9]+)?)|((\\+|-)?\\.?[0-9]+)";
  246.  
  247. Matcher matcher = Pattern.compile(regex).matcher(s);
  248.  
  249. if (matcher.find()) {
  250. String group = matcher.group();
  251.  
  252. return new Result(Double.valueOf(group), s.substring(group.length()));
  253. } else {
  254. return new Result(null, s);
  255. }
  256. }
  257.  
  258. public List<String> findVariables(String s) {
  259. List<String> result = new ArrayList<>();
  260.  
  261. int i = 0;
  262. while (i < s.length()) {
  263. String letter = "" + s.charAt(i);
  264. if (this.isLetter(letter) && result.indexOf(letter) < 0) {
  265. result.add(letter);
  266. }
  267.  
  268. i++;
  269. }
  270.  
  271. return result;
  272. }
  273.  
  274. public boolean isLetter(String c) {
  275. c = c.toLowerCase();
  276. return c != c.toUpperCase();
  277. }
  278.  
  279. public Double eval(String str) throws Exception {
  280. Result result = this.add(str);
  281.  
  282. if (result.rest.length() > 0) {
  283. throw new Exception("Невозможно полностью спарсить. Осталось незакрытым: " + result.rest);
  284. }
  285.  
  286. return result.num;
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement