1

I want to process this polynomial

String str = "2*x^2 + x + 2*x^2 + 8 - x^3";

I have split the string with plus and minus operator and got the 2*x^2. I am finding it difficult to work out this operation.

I know first i will need to work out value of x lets say x=2, just don't know how can I parse string to integer as there are other parts of the string to be parsed as well.

do i need to decompose "2*x^2" too?

How do I find * in string. I know i can use str.contains("*"), for this purticular operation i know whats coming after 2*x^2 but if user enters the polynomial thats the tricky bit.

Output is so far Operation 2*x^2 + x + 2*x^2 + 8 - x^3
Plus Splits
0 2*x^2
1 x
2 2*x^2
3 8 - x^3
Minus Splits
0 2*x^2 + x + 2*x^2 + 8
1 x^3

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 2
    What are you trying to achieve? What is the goal of your program? – Jack Jan 04 '17 at 13:07
  • 1
    When evaluating such an expression it comes down to parsing the expression and build up an abstract syntax tree that represents the expression with it's constants, variables and operation in the correct precedence. The evaluation then usually is done by walking through the tree. – hotzst Jan 04 '17 at 13:13
  • A regexp like `"((\\d+)\\*)?x(\\^(\\d+))?"`, perhaps? – Ole V.V. Jan 04 '17 at 13:17
  • Hi Guys, Thanks for your response,i have to write a program to solve any given polynomial by giving the range, range should be in double, interval should be in double. So far i have split the Polynomial String by + & - Operator now, I need to count the remaining part, I am checking if the string contain x and then sub if is checking if there is "^" to check the degree of x but i dont know how do i find "*" on the first part – Java_Learner Jan 04 '17 at 16:54
  • as in 2*x^2, lets say x=2, it should calculate x^2 so x=x^x and then 2*x(value of x =4) and so on then i will have to store all the values in to and array upto the endrange. – Java_Learner Jan 04 '17 at 16:58

2 Answers2

2

Try to use this: http://projects.congrace.de/exp4j/

Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)")
        .variables("x", "y")
        .build()
        .setVariable("x", 2.3)
        .setVariable("y", 3.14);
double result = e.evaluate();
Saulius Next
  • 1,340
  • 10
  • 16
0

In general this operation called "parse an expression" and covered in many books, articles and questions on so like How to parse a mathematical expression given as a string and return a number?.

In this particular case it looks like format is very limited and you can get by with manually splitting string and constructing corresponding data structure:

  1. split by plus and minus first similar to what you already do (How to split a string, but also keep the delimiters?)
  2. in resulting groups split by * if present.
  3. if got 3 groups (number, *, power) - split last by ^.

If you can't use regular expression to split strings - use indexOf to find particular characters (like * in your question - s.indexOf('*') ) and use substring to cut out parts of the string.

As part of building AST for that expression you'll need to How to convert a String to an int in Java? too.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Hi Alexei, thank you very much for your response and tips about i am going to try it now and will let you know! yes i will have to convert the string into Int too. – Java_Learner Jan 04 '17 at 22:05
  • Hi I have tried what you have suggested but i am stuck at the point 2*x^2 now if i use for loop to split this, then It will not evaluate the other parts of my main split! I am using for loop.for (int i = 0; i < str.length(); i++) { // Plus Splits str plus_opr = str.split("\\+"); System.out.println(" " + plus_opr[i]); for (int k = 0; k < plus_opr.length; k++) { // Multiplication coeff = plus_opr[k].split("\\*"); – Java_Learner Jan 05 '17 at 00:13
  • for (int l = 0; l < plus_opr.length; l++) { carat = plus_opr[l].split("\\^"); // System.out.println(" " + carat[l]); for (int m = 0; m < plus_opr.length; m++) { minus_opr = str.split("\\-"); // System.out.println(" " + minus_opr[m]); } – Java_Learner Jan 05 '17 at 00:14