123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- 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 using pemdas
- public static String 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");
- //create a duplicate of the method below, this one should have no print statemnts - testing purposes only.
- //instead of the above, remove everyting except for assignment and initiailization
- //this will work because getUpdateString2 cannot return null.
- if(getUpdateString2(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 = getUpdateString2(s,"^");
- }
-
- String m = null;
- System.out.println("mult");
- if(getUpdateString2(e,"*") != null)
- {
- m = getUpdateString2(e,"*");
- }
-
- String d = null;
- System.out.println("div");
- if(getUpdateString2(m,"/") != null)
- {
- d = getUpdateString2(m,"/");
- }
-
- String a = null;
- System.out.println("add");
- if(getUpdateString2(d,"+") != null)
- {
- a = getUpdateString2(d,"+");
- }
-
- String ss = null;
- System.out.println("sub");
- if(getUpdateString2(a,"-") != null)
- {
- ss = getUpdateString2(a,"-");
- }
-
- System.out.println("calculate 1.) Answer: " + ss);
- return 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());
- }
-
- //if(occurences.size() > 0)
- //{
- return occurences;
- //}
-
- }*/
-
- //search a string ( s ) for the pattern ( r ) and return a string resemblence of the first occurrence
- public static String searchReturnFirstString(String s, String r){
-
- Pattern pattern = Pattern.compile(r);
- Matcher matcher = pattern.matcher(s);
-
- if(matcher.find())
- {
- System.out.println("searchReturnFirstString 1.) perform on " + s + ": " + matcher.group());
- return matcher.group();
- }
-
- return null;
- }
-
-
- //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("searchReplaceFirst 1.) target | pattern | replacement | result");
- System.out.println("searchReplaceFirst 2.) " + target + " | " + pattern + " | " + replacement + " | " + result);
- return result;
- }
- }
-
- /**
- - Method Name: PEMDAS
- - The method takes in two strings, one string ( s ) - the string being modified and the operator ( r )
- - Search the string ( s ) for the first occurrence of the operator being performed.
- - If no occurrence is found
- - return the original string
- - If an occurrence of - this operation being performed - exists.
- - store the occurrence as a string ( occ )
- - store the digit left of the operator ( left ) as a double
- - store the digit right of the operator ( right ) as a double
- - perform switch case based on the operator ( r )
- - case statement for each operator allowed
- - calculate the product of left and right ( result )
- - replace ( occ ) in ( s ) with ( result ), store this in ( f )
- - if ( f ) and ( s ) do not have the same value
- - run PEMDAS on ( f ) and assign it to ( f )
- - if the original ( s ) has the same value as ( f )
- - return ( f )
-
-
- NOTES
- - The logic is there, and the method behaves properly, the regex is inaccurate and will have to resolved.
- - regex needs to pick up two numbers on either side of a given operation - no matter what the operation is.
- - values need to be matched for both integers and floaring point.
- */
-
- public static String getUpdateString2(String s, String operator)
- {
- String f = s;
- System.out.println("getUpdateString2 1.) Input is: " + f);
- String pattern = "\\s?\\.?\\d+\\.?(\\d+)?\\s?\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?";
- System.out.println("getUpdateString2 2.) Pattern is " + pattern);
-
- String occ = searchReturnFirstString(s, pattern);
- System.out.println("getUpdateString2 3.) the occurance is " + occ);
- //if we recieve a result
- if(occ != null)
- {
- //store the first and last digits as left and right (based on relationship to operator)
- String leftString = searchReturnFirstString(occ, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
- String rightStringOpp = searchReturnFirstString(occ, "\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
- String rightString = searchReturnFirstString(rightStringOpp, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
- //create a double to store the digit on the left and right, double to store the product of right and left
- Double left = 0.0;
- Double right = 0.0;
- Double doubleResult = 0.0;
- //if the left digit is found as a string, convert to double
- //if the right digit is found as a string, convert to double
- if(leftString != null){left = Double.parseDouble(leftString);}
- if(rightString != null){right = Double.parseDouble(rightString);}
- //switch statement for the operator to conduct proper arithmetics
- switch(operator)
- {
- case "^":
- doubleResult = Math.pow(left, right);
- System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
- break;
-
- case "+":
- doubleResult = left + right;
- System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
- break;
-
- case "-":
- doubleResult = left - right;
- System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
- break;
-
- case "*":
- doubleResult = left * right;
- System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
- break;
-
- case "/":
- doubleResult = left / right;
- System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
- break;
-
- default:
- doubleResult = 0.0;
- break;
- }
- //convert the double result to a string result
- String result = String.valueOf(doubleResult);
- //String result = String.format("%.9f", doubleResult);
- //replace the occurence of occ in s with result
- f = searchReplaceFirst(s, result, pattern);
- System.out.println("getUpdateString2 5.) iteration completed, f: " + f + "\n");
- if(!f.equals(s))
- {
- f = getUpdateString2(f, operator);
- } else {
- return f;
- }
- } else {
- return f;
- }
- return f;
- }
-
- 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;
- }
- }
|