Pemdas.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import java.util.regex.Pattern;
  2. import java.util.regex.Matcher;
  3. import java.util.ArrayList;
  4. public class Pemdas
  5. {
  6. //empty constructor function
  7. public Pemdas()
  8. {
  9. }
  10. //function to perform main calculations
  11. public static void calculate(String s)
  12. {
  13. System.out.println("Original " + s);
  14. //String p = null;
  15. //if(getBetweenBraces(s) != null)
  16. //{
  17. // p = getBetweenBraces(s);
  18. //}
  19. //System.out.println(p);
  20. String e = null;
  21. System.out.println("expo");
  22. //if the string contains this operator
  23. if(getUpdatedString(s,"^") != null)
  24. {
  25. //assign e to this string, after this operator has been calculated, and the original value of the sting has been modified to replace the occurnece with the calculations
  26. e = getUpdatedString(s,"^");
  27. }
  28. String m = null;
  29. System.out.println("mult");
  30. if(getUpdatedString(e,"*") != null)
  31. {
  32. m = getUpdatedString(e,"*");
  33. }
  34. String d = null;
  35. System.out.println("div");
  36. if(getUpdatedString(m,"/") != null)
  37. {
  38. d = getUpdatedString(m,"/");
  39. }
  40. String a = null;
  41. System.out.println("add");
  42. if(getUpdatedString(d,"+") != null)
  43. {
  44. a = getUpdatedString(d,"+");
  45. }
  46. String ss = null;
  47. System.out.println("sub");
  48. if(getUpdatedString(a,"-") != null)
  49. {
  50. ss = getUpdatedString(a,"-");
  51. }
  52. System.out.println(ss);
  53. }
  54. //search a string (s) and return all occurences of r (regex pattern)
  55. public static ArrayList<String> searchReturnAll(String s, String r){
  56. //create a regex pattern
  57. Pattern pattern = Pattern.compile(r);
  58. //create an arraylist to store all occurences of exponentation
  59. ArrayList<String> occurences = new ArrayList<String>();
  60. //create a matcher to match the pattern to the string
  61. Matcher matcher = pattern.matcher(s);
  62. //while the last search exists
  63. while(matcher.find())
  64. {
  65. //add the occurences to the list
  66. occurences.add(matcher.group());
  67. }
  68. return occurences;
  69. }
  70. //search the string (target) and replace the first occrence of (pattern) with replacement.
  71. public static String searchReplaceFirst(String target, String replacement, String pattern)
  72. {
  73. //if nothing is replaced, return null
  74. if(target.equals(target.replaceFirst(pattern, replacement))){
  75. return null;
  76. //if anything is replaced, return an updated string with the appropriate replacements
  77. } else {
  78. String result = target.replaceFirst(pattern, replacement);
  79. System.out.println("target | pattern | replacement | result");
  80. System.out.println(target + " | " + pattern + " | " + replacement + " | " + result);
  81. return result;
  82. }
  83. }
  84. //extract operations from a string
  85. //perform operations and replace notation in the string with product of the operation
  86. public static String getUpdatedString(String s, String opperator){
  87. //create an array of all occurences where this opperation takes place
  88. ArrayList<String> expoStrings = searchReturnAll(s, "\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
  89. //create an array to hold the results of each occurence
  90. ArrayList<String> results = new ArrayList<String>();
  91. String result = "";
  92. //if any strings return with the operator specified
  93. if(expoStrings.size() > 0)
  94. {
  95. //for each occurence in the list
  96. for(int i = 0; i < expoStrings.size(); i ++)
  97. {
  98. String occ = expoStrings.get(i);
  99. System.out.println("amount of occurences" + expoStrings.size());
  100. System.out.println("occurence at " + i);
  101. System.out.println(occ);
  102. //find the number on the left
  103. ArrayList<String> leftString = searchReturnAll(occ, "\\d+\\.?\\d?\\s?\\" + opperator + "\\s?");
  104. System.out.println(leftString.get(0));
  105. //take the carrot off the end of the string
  106. double left = 0;
  107. if(leftString.size() > 0)
  108. {
  109. ArrayList<String> leftNumString = searchReturnAll(leftString.get(0), "\\d+\\.?\\d?");
  110. left = Double.valueOf(leftNumString.get(0));
  111. //left = Double.valueOf(leftString.get(0).split(opperator));
  112. }
  113. //extract the number and store as a double
  114. //find the number on the right
  115. ArrayList<String> rightString = searchReturnAll(occ, "\\"+opperator+"\\s?\\d+\\.?\\d?");
  116. double right = 0;
  117. if(rightString.size() > 0)
  118. {
  119. //ectrct the digit from the second half of the string, without the carrot.
  120. ArrayList<String> rightNumString = searchReturnAll(rightString.get(0), "\\d+\\.?\\d?");
  121. right = Double.parseDouble(rightNumString.get(0));
  122. }
  123. //extract the number and store as a double
  124. //store the result as a double
  125. double doubleResult = 0;
  126. switch(opperator){
  127. case "^":
  128. //System.out.println(left + " | " + right);
  129. doubleResult = Math.pow(left, right);
  130. break;
  131. case "+":
  132. //System.out.println("Here");
  133. System.out.println("left:" + left + " | " + "right:" + right);
  134. doubleResult = left + right;
  135. break;
  136. case "*":
  137. doubleResult = left * right;
  138. break;
  139. case "/":
  140. doubleResult = left / right;
  141. break;
  142. default:
  143. doubleResult = 0;
  144. break;
  145. }
  146. results.add(String.valueOf(doubleResult));
  147. //if there is anything to replace on result, replace on that
  148. if(searchReplaceFirst(result,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?") != null)
  149. {
  150. result = searchReplaceFirst(result,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
  151. } else {
  152. //other wise replace on s, and set this = to result;
  153. result = searchReplaceFirst(s,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
  154. }
  155. }
  156. return result;
  157. } else {
  158. return s;
  159. }
  160. }
  161. public static String getExpo(String s){
  162. //create an array of all occurences where exponentation takes place
  163. ArrayList<String> expoStrings = searchReturnAll(s, "\\d+\\^\\d+");
  164. ArrayList<String> results = new ArrayList<String>();
  165. String result = "";
  166. //for each occurence in the list
  167. for(int i = 0; i < expoStrings.size(); i ++)
  168. {
  169. String occ = expoStrings.get(i);
  170. //find the number on the left
  171. ArrayList<String> leftString = searchReturnAll(occ, "\\d+\\^");
  172. //take the carrot off the end of the string
  173. ArrayList<String> leftNumString = searchReturnAll(leftString.get(0), "\\d+");
  174. //extract the number and store as a double
  175. double left = Double.valueOf(leftNumString.get(0));
  176. //find the number on the right
  177. ArrayList<String> rightString = searchReturnAll(occ, "\\^\\d+");
  178. //ectrct the digit from the second half of the string, without the carrot.
  179. ArrayList<String> rightNumString = searchReturnAll(rightString.get(0), "\\d+");
  180. //extract the number and store as a double
  181. double right = Double.parseDouble(rightNumString.get(0));
  182. //use math.pow to find the exponenation
  183. //store the result as a double
  184. double doubleResult = Math.pow(left, right);
  185. //add the result to the results as a string
  186. results.add(String.valueOf(doubleResult));
  187. result = searchReplaceFirst(s,results.get(i),"\\d+\\^\\d+");
  188. }
  189. return result;
  190. }
  191. public static String getBetweenBraces(String s){
  192. //int to hold the opening braces index in the string
  193. //int to hold the closing brace index in the string
  194. //set a position counter = 0 to determine if the brace is closed.
  195. //boolean to determine if the first opening brace has been captured = false
  196. //convert string to str array
  197. //for each letter in char array
  198. //if the char is an opening brace
  199. //if the boolean varriable to determine of the first opening brace != true
  200. //store i from the for loop in the variable to hold the opening index
  201. //pos --
  202. //if the char is not an opening brace
  203. //pos ++
  204. //if the counter = 0
  205. //store i in the varriable to hold the closing brace
  206. int openBrace = 0;
  207. int closeBrace = 0;
  208. int position = 0;
  209. boolean firstBrace = false;
  210. String[] strArr = s.split("(?!^)");
  211. for(int i = 0; i < strArr.length; i ++)
  212. {
  213. String letter = strArr[i];
  214. if(letter.equals("("))
  215. {
  216. if(firstBrace == false)
  217. {
  218. firstBrace = true;
  219. openBrace = i + 1;
  220. }
  221. position --;
  222. } else if(letter.equals(")"))
  223. {
  224. position ++;
  225. }
  226. if(position == 0)
  227. {
  228. closeBrace = i;
  229. break;
  230. } else {
  231. return null;
  232. }
  233. }
  234. String result = s.substring(openBrace, closeBrace);
  235. return result;
  236. }
  237. }