在 Visual Studio 2008 中执行表达式求值/归约

发布于 2024-07-24 05:39:18 字数 393 浏览 14 评论 0原文

是否可以让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妥活 2024-07-31 05:39:19

谢谢以上各位的回答。

可能有更好的方法,但这里有一个快速而肮脏的宏可以满足我的需要。

需要添加对 System.Data 和 System.XML 命名空间的引用。

突出显示要计算的表达式并运行宏(它使用数据表中的计算列来计算表达式。)它将用简化的结果替换表达式。

编辑 - 更新了下面的代码。 它对于减少大量表达式非常有效。 正如其他人指出的那样,有立即窗口,但这不适用于编辑目的。 该宏是基本表达式“()、+、-、*、/”的独立于语言的解决方案。


Sub Eval()
  Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
  Using dt As New DataTable()
    dt.Columns.Add("Expression", GetType(Double), ts.Text)
    dt.Rows.Add(dt.NewRow)
    ts.Text = CDbl(dt.Rows(0).Item("Expression"))
  End Using
End Sub

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 "(), +, -, *, /".


Sub Eval()
  Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
  Using dt As New DataTable()
    dt.Columns.Add("Expression", GetType(Double), ts.Text)
    dt.Rows.Add(dt.NewRow)
    ts.Text = CDbl(dt.Rows(0).Item("Expression"))
  End Using
End Sub
岁吢 2024-07-31 05:39:19

默认情况下,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.

听,心雨的声音 2024-07-31 05:39:19

答案是:是的,可以使用以下步骤。 (虽然从技术上讲,执行您所要求的操作,但我不确定它是否非常有用。:-)

  1. 在程序中设置一个断点,当您调试程序时,该断点可能会被命中。
  2. 然后,在 Visual Studio 调试器下运行您的程序。
  3. 当命中断点时,打开“监视”窗口。
  4. 在“监视”窗口中,通过单击“名称”列添加新监视。
  5. 输入表达式“-0.005 + -0.345”(不带引号),然后按 [Enter]。
    ...您应该看到“值”列填充了 -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. :-)

  1. Set a breakpoint in your program that's likely to get hit when you debug the program.
  2. Then, run your program under the Visual Studio debugger.
  3. When the breakpoint is hit, open the Watch window.
  4. In the Watch window, add a new watch by clicking in the Name column.
  5. Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter].
    ... 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.

ゃ人海孤独症 2024-07-31 05:39:19

您只需转到立即窗口并输入“?

You could just go to the immediate window and type "?<yourExpression>"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文