在 Visual Studio 2008 中执行表达式求值/归约
是否可以让 Visual Studio 进行数学表达式评估/简化?
例如,如果我输入“-0.005 + -0.345”,如何让 Visual Studio 减少该值(即用减少值替换它)? 我必须写宏吗? 如果是这样,是否有任何预先存在的宏可以执行这种类型的表达式缩减?
需要明确的是,我希望能够突出显示一个表达式并将其替换为简化的结果。 许多人建议立即窗口,但我不明白这是否足够?
编辑 我应该指出,这是在编辑而不是运行或调试时。 即时窗口几乎没有用处。 我也认为这是一个语言中立的问题。 我当然有兴趣看到我发布的宏的替代宏。
编辑一次...两次...(即在我考虑接受自己的答案之前还有其他建议吗?)
Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?
For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?
Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?
Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.
Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢以上各位的回答。
可能有更好的方法,但这里有一个快速而肮脏的宏可以满足我的需要。
需要添加对 System.Data 和 System.XML 命名空间的引用。
突出显示要计算的表达式并运行宏(它使用数据表中的计算列来计算表达式。)它将用简化的结果替换表达式。
编辑 - 更新了下面的代码。 它对于减少大量表达式非常有效。 正如其他人指出的那样,有立即窗口,但这不适用于编辑目的。 该宏是基本表达式“()、+、-、*、/”的独立于语言的解决方案。
Thank you for the above answers.
There probably are better ways, but here's a quick and dirty macro that does what I need.
References to the System.Data and System.XML namespaces need to be added.
Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.
Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".
默认情况下,Visual Studio 不会执行任何数学表达式求值/归约。 我不确定您是否可以通过 ReSharper 等项目获得对此的支持,但如果可用,它将在加载项中。
另外,了解您所使用的语言也会有帮助吗?
某些语言可能在这方面有所帮助。 例如,F# 可以通过交互式窗口轻松地在 IDE 中计算表达式并显示结果。 这可以很容易地添加回您的代码中,但它似乎并不正是您正在寻找的东西。
Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.
Also, it would be helpful to know the language you are working in?
Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.
答案是:是的,可以使用以下步骤。 (虽然从技术上讲,执行您所要求的操作,但我不确定它是否非常有用。:-)
...您应该看到“值”列填充了 -0.35。
当然,这不在编辑器窗口的上下文中......这可能是您想要执行缩减的地方。 再说一遍,我想这不是很有用。 加载项是在编辑器窗口中执行此操作的可能方法。
Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)
... You should see the Value column get populated with -0.35.
Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.
您只需转到立即窗口并输入“?”
You could just go to the immediate window and type "?<yourExpression>"