Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to type the numbers in a textbox:
1+24+32-4+2-12
and then display the result.
so how can i split the positive numbers and the negatives?
or is there any other way to do it ?
Posted
Updated 19-Feb-13 10:59am
v2
Comments
Richard C Bishop 19-Feb-13 16:51pm    
You can use the .Split() that returns an array and then do what you want with them.
Sergey Alexandrovich Kryukov 19-Feb-13 17:10pm    
I suggested a pretty unusual but quite practical solution, please see...
—SA
Maciej Los 19-Feb-13 16:53pm    
I think, you mean "operators" ("+", "-")...
Sergey Alexandrovich Kryukov 19-Feb-13 17:09pm    
I have that very funny solution I just promised. It's quite reasonable, depending on the purpose of all that, at least much more interesting than the boring parsing...
—SA
Zoltán Zörgő 19-Feb-13 17:00pm    
Note, that this is an expression, there are no negative numbers, only substraction operators.

Of course you can parse, interpret it, and show the result. But this is kind of boring.

I can advise one "exotic" alternative: wrap the input in C# code, compile it using CodeDOM, execute (you will have to use a separate Application Domain, to avoid memory leak! see below), and show the result, if it was successful, otherwise show compilation error.

Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

Not a very simple solution, but you can do a lot more with that.

If something is not quite clear, I'll gladly explain. It works.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 19-Feb-13 17:14pm    
This is not funny answer, but full professional answer! 5^5!
Sergey Alexandrovich Kryukov 19-Feb-13 17:19pm    
Nor "full" neither "professional" is a barrier for being funny. :-)
Thank you, Maciej.
—SA
appleduardo 19-Feb-13 18:19pm    
would you help me to do it by the "boring" way ? please! i dont understand much the way you said is a good way 2do it.
Sergey Alexandrovich Kryukov 19-Feb-13 18:31pm    
I say, boring... :-)

Help with what? Just use logic. This is just work, no secrets to share. Don't use string.Split, loop by all charters, have 3 "modes": digits, blank spaces, sign, invalid (for other characters; throw exception when you face them). This way, split. Remove blank spaces why splitting, just don't add them to the list. Find numeric values using, say uint.Parse. Do not consider negative values, treat '-' as operators, not a part of the number. Summarize taking sign into consideration. Simple, boring... The biggest thing: this is one-time work. Should you add operators, brackets, and you will need to do it all again.
OK, I described the solution, but I should have asked: what did you try so far?..

Better use my approach.

—SA
You better use or write an expression evaluator.
You can use the classical approach as described here: Building Expression Evaluator with Expression Trees in C# – Improving the API[^]
Use a third party library as this one: http://csharpeval.codeplex.com/[^]
Or use the reflection to emit code: http://www.daniweb.com/software-development/csharp/code/351471/simple-math-expression-evaluator-using-emit[^]
Or use CodeDom to run dynamic code at runtime, like described here: http://devreminder.wordpress.com/net/net-framework-fundamentals/c-dynamic-math-expression-evaluation/[^]
 
Share this answer
 

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