Main.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5. public class Main{
  6. public static ArrayList<String> pemdas = new ArrayList<String>();
  7. public static boolean pClose(String input){
  8. int tracker = 0;
  9. for(int i =0; i < input.length(); i++){
  10. if(input.charAt(i) == '(') tracker++;
  11. else if (input.charAt(i) == ')') tracker--;
  12. if(tracker < 0) return false;
  13. }
  14. return tracker == 0;
  15. }
  16. public static Double calc(String input, int num){ // p =0, e = 1, md = 2, as=3
  17. //System.out.println(input);
  18. Double result = null;
  19. String next;
  20. Pattern filter = Pattern.compile(pemdas.get(num));
  21. Matcher calcMatch = filter.matcher(input);
  22. if(!calcMatch.find()){
  23. if(num < 3) return calc(input, num+1);
  24. return null;
  25. }
  26. if(num == 0){
  27. Double pSol = calc(calcMatch.group(2), 1);
  28. if(pSol !=null){ result = pSol;
  29. next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
  30. }
  31. else{
  32. next = input.substring(0, calcMatch.start()) + input.substring(calcMatch.end());
  33. }
  34. }
  35. else{
  36. switch(calcMatch.group(2)){
  37. case "+":
  38. result = Double.parseDouble(calcMatch.group(1)) + Double.parseDouble(calcMatch.group(3));
  39. break;
  40. case "-":
  41. result = Double.parseDouble(calcMatch.group(1)) - Double.parseDouble(calcMatch.group(3));
  42. break;
  43. case "/":
  44. result = Double.parseDouble(calcMatch.group(1)) / Double.parseDouble(calcMatch.group(3));
  45. break;
  46. case "*":
  47. result = Double.parseDouble(calcMatch.group(1)) * Double.parseDouble(calcMatch.group(3));
  48. break;
  49. case "^":
  50. result = Math.pow(Double.parseDouble(calcMatch.group(1)), Double.parseDouble(calcMatch.group(3)));
  51. break;
  52. }
  53. next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
  54. }
  55. //for(int i = num; i <= 3; i++){
  56. //if(calc(next, i) != null) {return calc(next, i);}
  57. //}
  58. if(filter.matcher(next).find()) return calc(next, num);
  59. if(num < 3){
  60. Double nextResult = calc(next, num+1);
  61. if(nextResult !=null) return nextResult;
  62. }
  63. return result;
  64. }
  65. public static void main(String args[]){
  66. 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.");
  67. Scanner scanner = new Scanner(System.in);
  68. String input;
  69. pemdas.add("(\\()([^\\(\\)]*)(\\))");//detects anything in parenthesis
  70. pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\^)\\D*?(-?\\d+\\.?\\d*)"); //exponents
  71. pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\*|\\/|%)\\D*?(-?\\d+\\.?\\d*)"); //multiplication, division,
  72. pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\+|-)\\D*?(-?\\d+\\.?\\d*)"); // addition, subraction
  73. while(true){
  74. input = scanner.nextLine();
  75. if(input.toLowerCase().equals("stop")) break;
  76. if(!pClose(input)) System.out.println("Error: There's a problem with your parenthesis, so the result may be different from what you expect.");
  77. System.out.println(input + " is " + calc(input, 0));
  78. }
  79. }
  80. }