Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 19-Feb-13 10:48am
Edited 19-Feb-13 10:59am
v2
Comments
richcb at 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 at 19-Feb-13 17:10pm
   
I suggested a pretty unusual but quite practical solution, please see... —SA
Maciej Los at 19-Feb-13 16:53pm
   
I think, you mean "operators" ("+", "-")...
Sergey Alexandrovich Kryukov at 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ő at 19-Feb-13 17:00pm
   
Note, that this is an expression, there are no negative numbers, only substraction operators.
Sergey Alexandrovich Kryukov at 19-Feb-13 17:10pm
   
I suggested a pretty unusual but quite practical solution, please see... —SA
Sergey Alexandrovich Kryukov at 19-Feb-13 17:01pm
   
First of all, why parsing such thing? And what's the problem; what did you try so far? —SA
Jibesh at 19-Feb-13 17:10pm
   
it may be possible but if you can tell whats the need of such thing we can able to provide you a better way of doing it.
Sergey Alexandrovich Kryukov at 19-Feb-13 17:14pm
   
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Maciej Los at 19-Feb-13 17:14pm
   
This is not funny answer, but full professional answer! 5^5!
Sergey Alexandrovich Kryukov at 19-Feb-13 17:19pm
   
Nor "full" neither "professional" is a barrier for being funny. :-) Thank you, Maciej. —SA
appleduardo at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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/[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 375
1 Abhinav S 333
2 OriginalGriff 330
3 sanket saxena 305
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid