123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- import java.util.ArrayList;
-
- public class Pemdas
- {
- //empty constructor function
- public Pemdas()
- {
-
- }
- //function to perform main calculations
- public static void calculate(String s)
- {
-
- System.out.println("Original " + s);
- //String p = null;
- //if(getBetweenBraces(s) != null)
- //{
- // p = getBetweenBraces(s);
- //}
- //System.out.println(p);
-
- String e = null;
- System.out.println("expo");
- //if the string contains this operator
- if(getUpdatedString(s,"^") != null)
- {
- //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
- e = getUpdatedString(s,"^");
- }
-
- String m = null;
- System.out.println("mult");
- if(getUpdatedString(e,"*") != null)
- {
- m = getUpdatedString(e,"*");
- }
-
-
- String d = null;
- System.out.println("div");
- if(getUpdatedString(m,"/") != null)
- {
- d = getUpdatedString(m,"/");
- }
-
-
- String a = null;
- System.out.println("add");
- if(getUpdatedString(d,"+") != null)
- {
- a = getUpdatedString(d,"+");
- }
-
-
- String ss = null;
- System.out.println("sub");
- if(getUpdatedString(a,"-") != null)
- {
- ss = getUpdatedString(a,"-");
- }
- System.out.println(ss);
-
- }
-
- //search a string (s) and return all occurences of r (regex pattern)
- public static ArrayList<String> searchReturnAll(String s, String r){
- //create a regex pattern
- Pattern pattern = Pattern.compile(r);
- //create an arraylist to store all occurences of exponentation
- ArrayList<String> occurences = new ArrayList<String>();
- //create a matcher to match the pattern to the string
- Matcher matcher = pattern.matcher(s);
- //while the last search exists
- while(matcher.find())
- {
- //add the occurences to the list
- occurences.add(matcher.group());
- }
- return occurences;
- }
-
- //search the string (target) and replace the first occrence of (pattern) with replacement.
- public static String searchReplaceFirst(String target, String replacement, String pattern)
- {
- //if nothing is replaced, return null
- if(target.equals(target.replaceFirst(pattern, replacement))){
- return null;
- //if anything is replaced, return an updated string with the appropriate replacements
- } else {
- String result = target.replaceFirst(pattern, replacement);
- System.out.println("target | pattern | replacement | result");
- System.out.println(target + " | " + pattern + " | " + replacement + " | " + result);
- return result;
- }
- }
-
- //extract operations from a string
- //perform operations and replace notation in the string with product of the operation
- public static String getUpdatedString(String s, String opperator){
- //create an array of all occurences where this opperation takes place
- ArrayList<String> expoStrings = searchReturnAll(s, "\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
- //create an array to hold the results of each occurence
- ArrayList<String> results = new ArrayList<String>();
- String result = "";
- //if any strings return with the operator specified
- if(expoStrings.size() > 0)
- {
- //for each occurence in the list
- for(int i = 0; i < expoStrings.size(); i ++)
- {
- String occ = expoStrings.get(i);
- System.out.println("amount of occurences" + expoStrings.size());
- System.out.println("occurence at " + i);
- System.out.println(occ);
- //find the number on the left
- ArrayList<String> leftString = searchReturnAll(occ, "\\d+\\.?\\d?\\s?\\" + opperator + "\\s?");
- System.out.println(leftString.get(0));
- //take the carrot off the end of the string
- double left = 0;
-
- if(leftString.size() > 0)
- {
- ArrayList<String> leftNumString = searchReturnAll(leftString.get(0), "\\d+\\.?\\d?");
- left = Double.valueOf(leftNumString.get(0));
- //left = Double.valueOf(leftString.get(0).split(opperator));
- }
- //extract the number and store as a double
- //find the number on the right
- ArrayList<String> rightString = searchReturnAll(occ, "\\"+opperator+"\\s?\\d+\\.?\\d?");
- double right = 0;
- if(rightString.size() > 0)
- {
- //ectrct the digit from the second half of the string, without the carrot.
- ArrayList<String> rightNumString = searchReturnAll(rightString.get(0), "\\d+\\.?\\d?");
- right = Double.parseDouble(rightNumString.get(0));
- }
- //extract the number and store as a double
- //store the result as a double
- double doubleResult = 0;
- switch(opperator){
- case "^":
- //System.out.println(left + " | " + right);
- doubleResult = Math.pow(left, right);
- break;
-
- case "+":
- //System.out.println("Here");
- System.out.println("left:" + left + " | " + "right:" + right);
- doubleResult = left + right;
- break;
-
- case "*":
- doubleResult = left * right;
- break;
-
- case "/":
- doubleResult = left / right;
- break;
-
- default:
- doubleResult = 0;
- break;
- }
- results.add(String.valueOf(doubleResult));
- //if there is anything to replace on result, replace on that
- if(searchReplaceFirst(result,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?") != null)
- {
- result = searchReplaceFirst(result,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
- } else {
- //other wise replace on s, and set this = to result;
- result = searchReplaceFirst(s,results.get(i),"\\d+\\.?\\d?\\s?\\"+opperator+"\\s?\\d+\\.?\\d?\\s?");
- }
- }
- return result;
- } else {
- return s;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String getExpo(String s){
- //create an array of all occurences where exponentation takes place
- ArrayList<String> expoStrings = searchReturnAll(s, "\\d+\\^\\d+");
- ArrayList<String> results = new ArrayList<String>();
- String result = "";
- //for each occurence in the list
- for(int i = 0; i < expoStrings.size(); i ++)
- {
- String occ = expoStrings.get(i);
- //find the number on the left
- ArrayList<String> leftString = searchReturnAll(occ, "\\d+\\^");
- //take the carrot off the end of the string
- ArrayList<String> leftNumString = searchReturnAll(leftString.get(0), "\\d+");
- //extract the number and store as a double
- double left = Double.valueOf(leftNumString.get(0));
- //find the number on the right
- ArrayList<String> rightString = searchReturnAll(occ, "\\^\\d+");
- //ectrct the digit from the second half of the string, without the carrot.
- ArrayList<String> rightNumString = searchReturnAll(rightString.get(0), "\\d+");
- //extract the number and store as a double
- double right = Double.parseDouble(rightNumString.get(0));
- //use math.pow to find the exponenation
- //store the result as a double
- double doubleResult = Math.pow(left, right);
- //add the result to the results as a string
- results.add(String.valueOf(doubleResult));
- result = searchReplaceFirst(s,results.get(i),"\\d+\\^\\d+");
- }
- return result;
- }
-
- public static String getBetweenBraces(String s){
- //int to hold the opening braces index in the string
- //int to hold the closing brace index in the string
- //set a position counter = 0 to determine if the brace is closed.
- //boolean to determine if the first opening brace has been captured = false
- //convert string to str array
- //for each letter in char array
- //if the char is an opening brace
- //if the boolean varriable to determine of the first opening brace != true
- //store i from the for loop in the variable to hold the opening index
- //pos --
- //if the char is not an opening brace
- //pos ++
-
- //if the counter = 0
- //store i in the varriable to hold the closing brace
-
- int openBrace = 0;
- int closeBrace = 0;
- int position = 0;
- boolean firstBrace = false;
- String[] strArr = s.split("(?!^)");
-
- for(int i = 0; i < strArr.length; i ++)
- {
- String letter = strArr[i];
- if(letter.equals("("))
- {
- if(firstBrace == false)
- {
- firstBrace = true;
- openBrace = i + 1;
- }
- position --;
- } else if(letter.equals(")"))
- {
- position ++;
- }
-
- if(position == 0)
- {
- closeBrace = i;
- break;
- } else {
- return null;
- }
- }
-
- String result = s.substring(openBrace, closeBrace);
- return result;
- }
- }
|