java - How to split String using regex and insert into ArrayList? -
i trying build calculator converting infix postfix i've been having trouble handling cos, sin, , tan. current approach use regular expressions split input string cos, sin, etc , numbers placing them indexes of arraylist. have been able cos0 split 2 arraylist indexes, index that's supposed hold cos turns empty. don't know if using regular expressions wrong or if it's else.
import java.util.arraylist; import java.util.arrays; import java.util.scanner; import java.util.stack; import java.util.regex.matcher; public class calculator { //string sin = "sin"; //string cos = "cos"; //string tan = "tan"; public static void main(string[] args) { new calculator().run(); } public void run() { calculator eval = new calculator(); scanner keyboard = new scanner(system.in); system.out.println("please give equation."); string input = keyboard.next(); system.out.print(eval.infixtopostfix(input)); } public double infixtopostfix(string input) { stack<string>infixstack = new stack<>(); arraylist<string> exp = new arraylist<>(arrays.aslist(input.split("(cos|tan|sin\\d\\d)"))); string infix = ""; (int = 0; < exp.size(); i++) { if(exp.get(i).equals("")){ continue; } if (exp.get(i).matches("([\\d])")) { infix = infix + exp.get(i); }else if(exp.get(i).matches("([\\d])")) { while(!infixstack.empty() && !infixstack.peek().matches("[\\(\\[\\{\\^]") && operatorpriority(infixstack.peek(), exp.get(i) )) { infix = infix + infixstack.peek(); infixstack.pop(); } infixstack.push(exp.get(i)); }else if(exp.get(i).matches("[(]")) { infixstack.push(exp.get(i)); }else if(exp.get(i).matches("[)]")) { while(!infixstack.empty() && !infixstack.peek().matches("[(]")); { infix = infix + infixstack.peek(); infixstack.pop(); } infixstack.pop(); }else if(exp.get(i).matches("[\\^]")) { infixstack.push(exp.get(i)); }else if(exp.get(i).matches("[\\[]")) { infixstack.push(exp.get(i)); }else if(exp.get(i).matches("[\\]]")) { while(!infixstack.empty() && !infixstack.peek().matches("[\\(\\[]")) { infix = infix + infixstack.peek(); infixstack.pop(); } infixstack.pop(); } else if(exp.get(i).matches("[\\{]")) { infixstack.push(exp.get(i)); }else if(exp.get(i).matches("[\\}]")) { while(!infixstack.empty() && !infixstack.peek().matches("[\\(\\{\\[]")) { infix = infix + infixstack.peek(); infixstack.pop(); } infixstack.pop(); } } while(!infixstack.empty()) { infix = infix + infixstack.peek(); infixstack.pop(); } return evaluatepostfix(infix); } public double evaluatepostfix(string infix) { stack<double> equation = new stack<double>(); arraylist<string> postfixarray = new arraylist<>(arrays.aslist(infix.split("(?<=[\\w'(cos|tan|sin)'\\d])|(?=[\\w'(cos|tan|sin)'\\d])"))); double first; double second; try { (int = 0; < postfixarray.size(); i++) { if (postfixarray.get(i).matches("([\\d])")) { double d = double.parsedouble(postfixarray.get(i)); equation.push(d - '0'); }else if(postfixarray.get(i).matches("([sin])")) { first = equation.pop(); //second = equation.pop(); double result = math.sin(math.toradians(first)); equation.push(result); }else if(postfixarray.get(i).matches("([cos])")) { first = equation.pop(); //second = equation.pop(); double result = math.cos(math.toradians(first)); equation.push(result); }else if(postfixarray.get(i).matches("([tan])")) { first = equation.pop(); //second = equation.pop(); double result = math.tan(math.toradians(first)); equation.push(result); } if (postfixarray.get(i).matches("[*]")) { first = equation.pop(); second = equation.pop(); double result = first * second; equation.push(result); } if (postfixarray.get(i).matches("[/]")) { first = equation.pop(); second = equation.pop(); double result = second / first; equation.push(result); } if (postfixarray.get(i).matches("[+]")) { first = equation.pop(); second = equation.pop(); double result = first + second; equation.push(result); } if (postfixarray.get(i).matches("[-]")) { first = equation.pop(); second = equation.pop(); double result = first - second; equation.push(result); } if (postfixarray.get(i).matches("[(^)]")) { first = equation.pop(); second = equation.pop(); double result = math.pow(first, second); equation.push(result); } } if (!equation.isempty()) { return equation.pop(); } else return 0.0; } catch (exception e ) { return 0.0; } } int operatorweight(string op) { int weight = 1; if(op.equals("+") || op.equals("-")) { weight = 1; }else if(op.equals("*") || op.equals("/") ) { weight = 2; }else if(op.equals("^")) { weight = 3; } return weight; } boolean operatorpriority(string operator1, string operator2) { int weight1 = operatorweight(operator1); int weight2 = operatorweight(operator2); if(weight1 == weight2) { return true; } return weight1 > weight2; } }
assuming following input format:
valid - cos49
invalid - cos43.54
invalid - sin(angle)
you can divide string 2 groups:
- sin|cos|tan
- digits
so regex should this:
pattern p = pattern.compile("(sin|cos|tan)|(\\d+)"); matcher m = p.matcher("cos60"); arraylist<string> = new arraylist<>(); while (m.find()) a.add(m.group(0)); system.out.println(a.tostring());
read how matcher class works. hope helps
Comments
Post a Comment