Pemdas.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 using pemdas
  11. public static String 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. //create a duplicate of the method below, this one should have no print statemnts - testing purposes only.
  23. //instead of the above, remove everyting except for assignment and initiailization
  24. //this will work because getUpdateString2 cannot return null.
  25. if(getUpdateString2(s,"^") != null)
  26. {
  27. //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
  28. e = getUpdateString2(s,"^");
  29. }
  30. String m = null;
  31. System.out.println("mult");
  32. if(getUpdateString2(e,"*") != null)
  33. {
  34. m = getUpdateString2(e,"*");
  35. }
  36. String d = null;
  37. System.out.println("div");
  38. if(getUpdateString2(m,"/") != null)
  39. {
  40. d = getUpdateString2(m,"/");
  41. }
  42. String a = null;
  43. System.out.println("add");
  44. if(getUpdateString2(d,"+") != null)
  45. {
  46. a = getUpdateString2(d,"+");
  47. }
  48. String ss = null;
  49. System.out.println("sub");
  50. if(getUpdateString2(a,"-") != null)
  51. {
  52. ss = getUpdateString2(a,"-");
  53. }
  54. System.out.println("calculate 1.) Answer: " + ss);
  55. return ss;
  56. }
  57. /**
  58. //search a string (s) and return all occurences of r (regex pattern)
  59. public static ArrayList<String> searchReturnAll(String s, String r){
  60. //create a regex pattern
  61. Pattern pattern = Pattern.compile(r);
  62. //create an arraylist to store all occurences of exponentation
  63. ArrayList<String> occurences = new ArrayList<String>();
  64. //create a matcher to match the pattern to the string
  65. Matcher matcher = pattern.matcher(s);
  66. //while the last search exists
  67. while(matcher.find())
  68. {
  69. //add the occurences to the list
  70. occurences.add(matcher.group());
  71. }
  72. //if(occurences.size() > 0)
  73. //{
  74. return occurences;
  75. //}
  76. }*/
  77. //search a string ( s ) for the pattern ( r ) and return a string resemblence of the first occurrence
  78. public static String searchReturnFirstString(String s, String r){
  79. Pattern pattern = Pattern.compile(r);
  80. Matcher matcher = pattern.matcher(s);
  81. if(matcher.find())
  82. {
  83. System.out.println("searchReturnFirstString 1.) perform on " + s + ": " + matcher.group());
  84. return matcher.group();
  85. }
  86. return null;
  87. }
  88. //search the string (target) and replace the first occrence of (pattern) with replacement.
  89. public static String searchReplaceFirst(String target, String replacement, String pattern)
  90. {
  91. //if nothing is replaced, return null
  92. if(target.equals(target.replaceFirst(pattern, replacement))){
  93. return null;
  94. //if anything is replaced, return an updated string with the appropriate replacements
  95. } else {
  96. String result = target.replaceFirst(pattern, replacement);
  97. System.out.println("searchReplaceFirst 1.) target | pattern | replacement | result");
  98. System.out.println("searchReplaceFirst 2.) " + target + " | " + pattern + " | " + replacement + " | " + result);
  99. return result;
  100. }
  101. }
  102. /**
  103. - Method Name: PEMDAS
  104. - The method takes in two strings, one string ( s ) - the string being modified and the operator ( r )
  105. - Search the string ( s ) for the first occurrence of the operator being performed.
  106. - If no occurrence is found
  107. - return the original string
  108. - If an occurrence of - this operation being performed - exists.
  109. - store the occurrence as a string ( occ )
  110. - store the digit left of the operator ( left ) as a double
  111. - store the digit right of the operator ( right ) as a double
  112. - perform switch case based on the operator ( r )
  113. - case statement for each operator allowed
  114. - calculate the product of left and right ( result )
  115. - replace ( occ ) in ( s ) with ( result ), store this in ( f )
  116. - if ( f ) and ( s ) do not have the same value
  117. - run PEMDAS on ( f ) and assign it to ( f )
  118. - if the original ( s ) has the same value as ( f )
  119. - return ( f )
  120. NOTES
  121. - The logic is there, and the method behaves properly, the regex is inaccurate and will have to resolved.
  122. - regex needs to pick up two numbers on either side of a given operation - no matter what the operation is.
  123. - values need to be matched for both integers and floaring point.
  124. */
  125. public static String getUpdateString2(String s, String operator)
  126. {
  127. String f = s;
  128. System.out.println("getUpdateString2 1.) Input is: " + f);
  129. String pattern = "\\s?\\.?\\d+\\.?(\\d+)?\\s?\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?";
  130. System.out.println("getUpdateString2 2.) Pattern is " + pattern);
  131. String occ = searchReturnFirstString(s, pattern);
  132. System.out.println("getUpdateString2 3.) the occurance is " + occ);
  133. //if we recieve a result
  134. if(occ != null)
  135. {
  136. //store the first and last digits as left and right (based on relationship to operator)
  137. String leftString = searchReturnFirstString(occ, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
  138. String rightStringOpp = searchReturnFirstString(occ, "\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
  139. String rightString = searchReturnFirstString(rightStringOpp, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
  140. //create a double to store the digit on the left and right, double to store the product of right and left
  141. Double left = 0.0;
  142. Double right = 0.0;
  143. Double doubleResult = 0.0;
  144. //if the left digit is found as a string, convert to double
  145. //if the right digit is found as a string, convert to double
  146. if(leftString != null){left = Double.parseDouble(leftString);}
  147. if(rightString != null){right = Double.parseDouble(rightString);}
  148. //switch statement for the operator to conduct proper arithmetics
  149. switch(operator)
  150. {
  151. case "^":
  152. doubleResult = Math.pow(left, right);
  153. System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
  154. break;
  155. case "+":
  156. doubleResult = left + right;
  157. System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
  158. break;
  159. case "-":
  160. doubleResult = left - right;
  161. System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
  162. break;
  163. case "*":
  164. doubleResult = left * right;
  165. System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
  166. break;
  167. case "/":
  168. doubleResult = left / right;
  169. System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
  170. break;
  171. default:
  172. doubleResult = 0.0;
  173. break;
  174. }
  175. //convert the double result to a string result
  176. String result = String.valueOf(doubleResult);
  177. //String result = String.format("%.9f", doubleResult);
  178. //replace the occurence of occ in s with result
  179. f = searchReplaceFirst(s, result, pattern);
  180. System.out.println("getUpdateString2 5.) iteration completed, f: " + f + "\n");
  181. if(!f.equals(s))
  182. {
  183. f = getUpdateString2(f, operator);
  184. } else {
  185. return f;
  186. }
  187. } else {
  188. return f;
  189. }
  190. return f;
  191. }
  192. public static String getBetweenBraces(String s){
  193. //int to hold the opening braces index in the string
  194. //int to hold the closing brace index in the string
  195. //set a position counter = 0 to determine if the brace is closed.
  196. //boolean to determine if the first opening brace has been captured = false
  197. //convert string to str array
  198. //for each letter in char array
  199. //if the char is an opening brace
  200. //if the boolean varriable to determine of the first opening brace != true
  201. //store i from the for loop in the variable to hold the opening index
  202. //pos --
  203. //if the char is not an opening brace
  204. //pos ++
  205. //if the counter = 0
  206. //store i in the varriable to hold the closing brace
  207. int openBrace = 0;
  208. int closeBrace = 0;
  209. int position = 0;
  210. boolean firstBrace = false;
  211. String[] strArr = s.split("(?!^)");
  212. for(int i = 0; i < strArr.length; i ++)
  213. {
  214. String letter = strArr[i];
  215. if(letter.equals("("))
  216. {
  217. if(firstBrace == false)
  218. {
  219. firstBrace = true;
  220. openBrace = i + 1;
  221. }
  222. position --;
  223. } else if(letter.equals(")"))
  224. {
  225. position ++;
  226. }
  227. if(position == 0)
  228. {
  229. closeBrace = i;
  230. break;
  231. } else {
  232. return null;
  233. }
  234. }
  235. String result = s.substring(openBrace, closeBrace);
  236. return result;
  237. }
  238. }