如何分割正数和负数? [英] how to split positive and negative numbers?

查看:112
本文介绍了如何分割正数和负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框中输入数字:

1 + 24 + 32-4 + 2-12

然后显示结果。

所以我怎么能把正数和负数分开?

还是有其他办法吗?

解决方案

当然,您可以解析,解释并显示结果。但这有点无聊。



我可以建议一个异国情调的选择:用C#代码包装输入,用CodeDOM编译,执行(你必须使用单独的Application Domain,以避免内存泄漏!见下文),并显示结果,如果成功,否则显示编译错误。



请参阅我的过去答案:

使用CodeDom生成代码 [ ^ ] ,

创建WPF应用程序使用Reloadable Plugins ... [ ^ ]。



这不是一个非常简单的解决方案,但你可以做更多的事情。



如果事情不是很清楚,我会'我很乐意解释。它有效。



-SA


你最好使用或写一个表达式评估器。 />
您可以使用此处描述的经典方法:构建表达式在C#中使用表达式树的评估程序 - 改进API [ ^ ]

使用第三方库作为这一个: http: //csharpeval.codeplex.com/ [ ^ ]

或使用反射来发出代码: http://www.daniweb.com/software-development/csharp/code/351471/simple-math-expression-evaluator-usi ng-emit [ ^ ]

或者使用CodeDom在运行时运行动态代码,如下所述: http://devreminder.wordpress.com/net/net-framework-fundamentals/c-dynamic-math-expression-evaluation/ [ ^ ]

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 ?

解决方案

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


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


这篇关于如何分割正数和负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆