Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have
C++
String res="120*10-50%5";

The answer is 230.

How can I parse this as double?
How can I calculate this?

Please help me.
Posted
Updated 20-Nov-11 20:03pm
v3
Comments
André Kraak 20-Nov-11 5:07am    
Edited question:
Added pre tags
Sergey Alexandrovich Kryukov 20-Nov-11 19:04pm    
I really wonder "why". For compiled question it's not so easy and lead for such a big fields as "Computer Algebra Systems (CAS)". (And yes, it's quite possible to develop one using Java). Just arithmetic evaluation is a highly simplified case of such thing. Again, why going into it? I would say, development of CAS is very difficult but would make sense, but just arithmetic expression evaluation -- hardly. Can you justify such work?
--SA
TorstenH. 21-Nov-11 2:03am    
did anyone figure out the pure failure of this calculation?

(120*10)-(50%5) = 1190

The rest of the homework should be done by the questioner. I added tags for that

What you are looking for is Arithmetic Evaluation functions.

See this question and answers over at SO; http://stackoverflow.com/questions/4173623/mathematical-expression-string-to-number-in-java[^]

There are various articles for this and also libraries you could use if you just search around.
 
Share this answer
 
Comments
RaviRanjanKr 21-Nov-11 2:13am    
My 5+
 
Share this answer
 
Comments
RaviRanjanKr 21-Nov-11 2:13am    
My 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900