Split string from math operation

Hello,

I'm making a calculator with socket.

I have the server and the client.

Before to send an operation, I need to know if the operation is correct or not.

I count '(' and ')', but I need a regex or a String[], to split the string.

My string is : ((1+5)*(7*5))-3. The aim is to have : (,(1+5),*,(7*5),),-,3

Or, I search for the last '(', find the next ')', solve it, and replace with the before and after part.

And this again and again, until there is one or more signs and more than 2 numbers.

The numbers can be float, so if there is a ',' , I need to add it in the previous string.

Can you help me, because I try the seond solution without success ?

Re: Split string from math operation

Evaluation of expressions is typically done by converting the expression to Reverse Polish Notation (RPN - aka as postfix notation). So your expression

((1+5)*(7*5))-3 is changed to 1 5 + 7 5 * * 3 -. Then the evaluation is trivial using a stack. There are lots of articles on it so you can search the web for RPN.

Also, I would suggest you split the tokens using the operators and parentheses as delimiters (but preserving the delimiters). This can be accomplished using a regex. It also precludes you from having to explicitly parse floating point numbers which can be a bit nasty.

Regards,

Jim

Re: Split string from math operation

Maybe my old blog article can be of any help: it describes a rather complete expression parser and several ways to evaluate the expressions. See the link on the top right side of this reply.

kind regards,

Jos

Re: Split string from math operation

I haven't read your article but I knew you had written one from previous posts. Unfortunately, I didn't know how to reference it.

Regards,

Jim

Re: Split string from math operation

Quote:

Originally Posted by

**jim829** I haven't read your article but I knew you had written one from previous posts. Unfortunately, I didn't know how to reference it.

Neither do I ;-) I simply mention the 'blog entries' link near the top right corner of my replies.

kind regards,

Jos (<--- lazy bum)

Re: Split string from math operation

Hello,

I have the code to solve RPN : Reverse Polish Notation : AlgorithmsÂ*Â«Â*Collections Data StructureÂ*Â«Â*Java

I use this code to make the postfix change : http://stackoverflow.com/questions/1...fix-to-postfix

Can you help me to cath all exceptions from the second link, because if the input is '((()', I have java.util.EmptyStackException.

I mean to not return the string without the good notation.

Regards

Re: Split string from math operation

The JDK Stack class has an empty() method to tell if the stack is empty. So you can couple that with parentheses counting to ensure that you have the correct number. However, what if someone types in (3-5+) or (*3-5)? If you really want to ensure the expression is syntactically correct then you may need to write a state machine to help parse the expression. If carefully done it can be integrated into your RPN conversion.

Regards,

Jim