1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Main{
- public static ArrayList<String> pemdas = new ArrayList<String>();
- public static boolean pClose(String input){
- int tracker = 0;
- for(int i =0; i < input.length(); i++){
- if(input.charAt(i) == '(') tracker++;
- else if (input.charAt(i) == ')') tracker--;
- if(tracker < 0) return false;
- }
- return tracker == 0;
- }
-
- public static Double calc(String input, int num){ // p =0, e = 1, md = 2, as=3
- //System.out.println(input);
- Double result = null;
- String next;
- Pattern filter = Pattern.compile(pemdas.get(num));
- Matcher calcMatch = filter.matcher(input);
- if(!calcMatch.find()){
- if(num < 3) return calc(input, num+1);
- return null;
- }
- if(num == 0){
- Double pSol = calc(calcMatch.group(2), 1);
- if(pSol !=null){ result = pSol;
- next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
- }
- else{
- next = input.substring(0, calcMatch.start()) + input.substring(calcMatch.end());
- }
- }
- else{
- switch(calcMatch.group(2)){
- case "+":
- result = Double.parseDouble(calcMatch.group(1)) + Double.parseDouble(calcMatch.group(3));
- break;
- case "-":
- result = Double.parseDouble(calcMatch.group(1)) - Double.parseDouble(calcMatch.group(3));
- break;
- case "/":
- result = Double.parseDouble(calcMatch.group(1)) / Double.parseDouble(calcMatch.group(3));
- break;
- case "*":
- result = Double.parseDouble(calcMatch.group(1)) * Double.parseDouble(calcMatch.group(3));
- break;
- case "^":
- result = Math.pow(Double.parseDouble(calcMatch.group(1)), Double.parseDouble(calcMatch.group(3)));
- break;
-
- }
- next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
- }
- //for(int i = num; i <= 3; i++){
- //if(calc(next, i) != null) {return calc(next, i);}
- //}
- if(filter.matcher(next).find()) return calc(next, num);
- if(num < 3){
- Double nextResult = calc(next, num+1);
- if(nextResult !=null) return nextResult;
- }
- return result;
- }
-
- public static void main(String args[]){
- System.out.println("PEMDAS calculator! Just write an expression to solve how you normally would, like \"(2+3)*3\"! Supports + - * / ^ % as its operators. \"stop\" will stop the program.");
- Scanner scanner = new Scanner(System.in);
- String input;
- pemdas.add("(\\()([^\\(\\)]*)(\\))");//detects anything in parenthesis
- pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\^)\\D*?(-?\\d+\\.?\\d*)"); //exponents
- pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\*|\\/|%)\\D*?(-?\\d+\\.?\\d*)"); //multiplication, division,
- pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\+|-)\\D*?(-?\\d+\\.?\\d*)"); // addition, subraction
- while(true){
- input = scanner.nextLine();
- if(input.toLowerCase().equals("stop")) break;
- if(!pClose(input)) System.out.println("Error: There's a problem with your parenthesis, so the result may be different from what you expect.");
- System.out.println(input + " is " + calc(input, 0));
- }
- }
- }
|