1

I have a series of advanced math I want to convert to string implemented by Java for example :

String mymath="45+√4+√5+sin(6)+6^3";

i used

 String res=  mymath.replace("√", 
 "Math.sqrt(").replace("sin","Math.sin").replace("^","pow(");

Get the result:

45+Math.sqrt(4+Math.sqrt(5+Math.sin(6)+6pow(3

Now I have a problem in closing parentheses when the conversion

vs97
  • 5,765
  • 3
  • 28
  • 41
mhd
  • 103
  • 1
  • 9
  • Possible duplicate of [How to evaluate a math expression given in string form?](https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form) – geco17 Aug 13 '19 at 14:35
  • Please attempt to solve the problem first, and edit your answer if you encounter any errors. If you don't know how to attempt this, look into parsers/parse trees, or libraries for algebraic calculations. – cameron1024 Aug 13 '19 at 14:35
  • @WilliamBurnham please read my question have root and pow – mhd Aug 13 '19 at 14:39
  • @mhd you simply can't do with with regular expressions. You need a parser. – Andy Turner Aug 13 '19 at 14:42
  • @AndyTurner Can you give an example? – mhd Aug 13 '19 at 14:47
  • See the link in [William Burnham's comment](https://stackoverflow.com/questions/57479995/convert-advanced-mathematics-including-root-string-to-code-execute-by-java?noredirect=1#comment101431668_57479995) – Andy Turner Aug 13 '19 at 14:48
  • @AndyTurner I used 'ScriptEngine engine', but this needs a Java string ready .. I have a string with user input like "√" .. so it must be converted into a string to understand by ScriptEngine engine like Math.sqrt() rathar than '√' – mhd Aug 13 '19 at 14:53

1 Answers1

2

I would like to suggest a regex approach. For example, take a look at this regex:

√(\d+)|sin\((\d+)\)|(\d+)\^(\d+)

I don't have an access to an IDE at the moment, but this works in Notepad++ and the idea is the same. Essentially what I am proposing is to capture e.g. √4, and then capture the digit(s) under the √ sign as a group. Then, replace with the actual sqrt() function, so Math.sqrt\(\1\), for example, where we are simply inserting the group between the parenthesis. Same for sine - capture sin(6) with 6 being in a group, and replace with Math.sin\(\2\). Same idea for pow(), but this time have two groups - Math.pow\(\3, \4\). So hopefully you get the idea.

The problem is that you will have to separately think about every math symbol/operation and write out a separate regex/replacement function for it. So using a parser would save you a lot of time.

Regex demo to play around.

Java Demo (written out verbose for understanding):

public class MathRegex {
    public static void main( String args[] ) {
        // String to be scanned to find the pattern.
        String line = "45+√4+√5+sin(6)+6^3";
        String patternSqrt = "√(\\d+)";         // Pattern to find √digit(s)
        String patternSine = "sin\\((\\d+)\\)"; // Pattern to find sin(digit(s))
        String patternPow = "(\\d+)\\^(\\d+)";  // Pattern to capture digit(s)^digit(s)

        // Create a Pattern object
        Pattern sqrtPattern = Pattern.compile(patternSqrt);
        Pattern sinPattern = Pattern.compile(patternSine);
        Pattern powPattern = Pattern.compile(patternPow);

        // Now create matcher object for each operation.
        Matcher sqrtMatch = sqrtPattern.matcher(line);
        String stringSqrt = sqrtMatch.replaceAll("Math.sqrt($1)");

        Matcher sinMatch = sinPattern.matcher(stringSqrt); // notice feeding to updated string
        String stringSine = sinMatch.replaceAll("Math.sin($1)");

        Matcher powMatch = powPattern.matcher(stringSine);
        String output = powMatch.replaceAll("pow($1, $2)");

        System.out.println(output);

        // 45+Math.sqrt(4)+Math.sqrt(5)+Math.sin(6)+pow(6, 3)
    }
}
vs97
  • 5,765
  • 3
  • 28
  • 41
  • I did not understand your answer! Can you give me a active java code example of this? I understand the examples instead of the explanation .. thank you for answer – mhd Aug 13 '19 at 15:10
  • @mhd added some code for you to play around with. I've written it out so it's easier to understand. – vs97 Aug 13 '19 at 15:22
  • or how i can capture the digit(s) under the √ sign as a group?? – mhd Aug 13 '19 at 15:22
  • ok i see your edit, but your code only for Constant value , the string must entered by user and it will changing – mhd Aug 13 '19 at 15:29
  • @mhd aren't you anyway storing user input in a string? Instead of the constant input just take the input string? – vs97 Aug 13 '19 at 15:32
  • Thank you I got the idea .. Please rate my question .. I will get back to you if I got a problem :D – mhd Aug 13 '19 at 15:34