Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. package ru.compscicenter.java2019.calculator;
  2.  
  3. class Parser {
  4. public static void main(String[] args) {
  5. Parser pm = new Parser();
  6. //String f = "2^3*10E1";
  7. String f = "1+ 2";
  8. double ans = pm.Parse(f);
  9. System.out.format("%08f", ans);
  10. }
  11.  
  12. public double Parse(String s) {
  13. s = DeleteSpases(s);
  14. Result result = PlusMinus(s);
  15.  
  16. return result.acc;
  17. }
  18.  
  19. private String DeleteSpases(String s) {
  20. String s2 = "";
  21. for(int i = 0; i < s.length(); i++) {
  22. if (s.charAt(i) != ' ') {
  23. s2 += s.charAt(i);
  24. }
  25. }
  26.  
  27. return s2;
  28. }
  29.  
  30. private Result PlusMinus(String s) {
  31. Result current = MulDiv(s);
  32. double acc = current.acc;
  33.  
  34. while (current.rest.length() > 0) {
  35. if (!(current.rest.charAt(0) == '+' || current.rest.charAt(0) == '-'))
  36. break;
  37.  
  38. char sign = current.rest.charAt(0);
  39. String next = current.rest.substring(1);
  40.  
  41. acc = current.acc;
  42.  
  43. current = MulDiv(next);
  44. if (sign == '+') {
  45. acc += current.acc;
  46. } else {
  47. acc -= current.acc;
  48. }
  49. current.acc = acc;
  50. }
  51. return new Result(current.acc, current.rest);
  52. }
  53.  
  54.  
  55. private Result MulDiv(String s) {
  56. Result current = Braket(s);
  57.  
  58. double acc = current.acc;
  59. while (true) {
  60. if (current.rest.length() == 0) {
  61. return current;
  62. }
  63.  
  64. char sign = current.rest.charAt(0);
  65. if ((sign != '*' && sign != '/'))
  66. return current;
  67.  
  68. String next = current.rest.substring(1);
  69. Result right = Braket(next);
  70.  
  71. if (sign == '*') {
  72. acc *= right.acc;
  73. } else {
  74. acc /= right.acc;
  75. }
  76.  
  77. current = new Result(acc, right.rest);
  78. }
  79. }
  80.  
  81.  
  82. private Result Braket(String s) {
  83.  
  84. char bra = s.charAt(0);
  85. if (bra == '(') {
  86. Result resInBrakets = PlusMinus(s.substring(1));
  87.  
  88. if ((resInBrakets.rest.charAt(0) == ')') && (!resInBrakets.rest.isEmpty()) ) {
  89. resInBrakets.rest = resInBrakets.rest.substring(1);
  90. }
  91. return resInBrakets;
  92. }
  93. return Func(s);
  94. }
  95.  
  96.  
  97. private Result Func(String s) {
  98. String function = "";
  99. int i = 0;
  100. // searching for a function
  101. while ((i < s.length() && (Character.isLetter(s.charAt(i)) )) || ( Character.isDigit(s.charAt(i)) && i > 0 ) ) {
  102. if (s.charAt(i) == 'E') {
  103. break;
  104. }
  105. function += s.charAt(i);
  106. i++;
  107. }
  108.  
  109. if (function.length() > 0) {
  110. if ( s.length() > i && s.charAt( i ) == '(') {
  111. Result r = Braket(s.substring(function.length()));
  112. if (function.equals("sin")) {
  113. return new Result(Math.sin(Math.toRadians(r.acc)), r.rest);
  114. } else if (function.equals("cos")) {
  115. return new Result(Math.cos(Math.toRadians(r.acc)), r.rest);
  116. } else if (function.equals("abs")) {
  117. return new Result(Math.abs(Math.toRadians(r.acc)), r.rest);
  118. }
  119. //else if (function.equals("^")) {
  120. // return new Result(Math.pow(NextNumber(s).acc, r.acc), r.rest);
  121. //}
  122. }
  123. }
  124. return NextNumber(s);
  125. }
  126.  
  127.  
  128. private Result NextNumber(String s) {
  129.  
  130. double dPart = 0;
  131. boolean negative = false;
  132.  
  133. if( s.charAt(0) == '-' ){
  134. negative = true;
  135. s = s.substring(1);
  136. }
  137.  
  138. int i = 0;
  139. while (i < s.length() && (Character.isDigit(s.charAt(i)) || s.charAt(i) == '.')) {
  140. i++;
  141. }
  142. dPart = Double.parseDouble(s.substring(0, i));
  143.  
  144. double dPow = 0;
  145. if (i < s.length() && s.charAt(i) == '^') {
  146. int j = i+1;
  147. while (j < s.length() && (Character.isDigit(s.charAt(j)) || s.charAt(j) == '.')) {
  148. j++;
  149. }
  150. if (j == i+1) {
  151. if (s.charAt(j) == '('){
  152. Result Pow = Braket(s.substring(j));
  153. dPart = Math.pow(dPart, Pow.acc);
  154. if( negative ) dPart = -dPart;
  155.  
  156. int len = Pow.rest.length();
  157. String restPart = s.substring(s.length()-len);
  158. return new Result(dPart, restPart);
  159. }
  160. }
  161. dPow = Double.parseDouble(s.substring(i+1, j));
  162. dPart = Math.pow(dPart, dPow);
  163.  
  164. if( negative ) dPart = -dPart;
  165. String restPart = s.substring(j);
  166. return new Result(dPart, restPart);
  167. }
  168.  
  169. boolean neg_pow = false;
  170. if (i < s.length() && s.charAt(i) == 'E') {
  171. int j = i+1;
  172. if (j < s.length() && s.charAt(j) == '-') {
  173. neg_pow = true;
  174. }
  175. j++;
  176. while (j < s.length() && (Character.isDigit(s.charAt(j)) || s.charAt(j) == '.')) {
  177. j++;
  178. }
  179. if (neg_pow) {
  180. dPow = Double.parseDouble(s.substring(i+2, j));
  181. dPart = dPart * Math.pow(10, -dPow);
  182. if( negative ) dPart = -dPart;
  183. String restPart = s.substring(j);
  184. return new Result(dPart, restPart);
  185. }
  186. else {
  187. dPow = Double.parseDouble(s.substring(i+1, j));
  188. dPart = dPart * Math.pow(10, dPow);
  189. if( negative ) dPart = -dPart;
  190. String restPart = s.substring(j);
  191. return new Result(dPart, restPart);
  192. }
  193. }
  194.  
  195.  
  196.  
  197. if( negative ) dPart = -dPart;
  198.  
  199. String restPart = s.substring(i);
  200. Result res = new Result(dPart, restPart);
  201. return res;
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement