C#运算符优先级 [英] C# Operator Precedence

查看:102
本文介绍了C#运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在用C#写一个计算器,在设置运算符优先级时遇到问题.
例如,如果用户输入8 * 8-9,则程序给出的答案是-8而不是55.
我的程序使用两个堆栈来存储数字和运算符,然后求值(尝试使用调车场的想法).
有谁知道如何设置运算符优先级或如何在堆栈中搜索并选择优先级最高的运算符?
如果需要的话,我可以发送我拥有的代码,
问候,
Andrew

Hello,
I am writing a calculator in C# and am having problems setting the operator precedence.
For example if the user enters 8*8-9 the program gives an answer of -8 instead of 55.
My program uses two stacks to store the numbers and operators and then evaluate (trying to use the shunting yard idea).
Does anyone know how to set the operator precedence or search through a stack and pick the operator with the highest precedence?
I can send the code I have if it is needed,
Regards,
Andrew

推荐答案

这是一个有趣的问题.我不了解所有详细信息,但我可能会自己实施此操作.无论如何,这是我所获得的,并大量引用了此内容:

http://en.wikipedia.org/wiki/Shunting_yard_algorithm [
如果我理解正确,那就错了(如果我误会了,表示歉意).调车场的目的是重新编写一个公式,使其以波兰文相反的符号出现,其中
3 2 +
将3加2(通常是 Operand2 Operand1 Operator ,其中操作数本身可以被评估为函数的位).您的公式8 * 8-9-> 9-8 8 *我认为.反向波兰语表示法非常令人困惑,我必须使用波兰语,如果您理解我的意思,那么请反向使用波兰语.

分流算法具有一个堆栈和一个队列.运算符堆栈实际上是临时的",算法完成后,它应该为空.一切都应该结束在输出队列中,而不仅仅是值.如果正确遵循算法,则无需任何额外工作即可在输出队列中获得RPN公式.您不应该在输出队列和运算符堆栈的末尾进行遍历,而是优先处理优先级.即使您可以执行此操作(并且我认为您无法像描述的那样进行),它也会比适当的算法慢.

该算法在Wikipedia页面上有详细说明,但简单地:
This is an interesting problem. I don''t understand the full details but I might have a bash at implementing this myself. Anyway this is what I have garnered, referencing this heavily:

http://en.wikipedia.org/wiki/Shunting_yard_algorithm[^]

"My program uses two stacks to store the numbers and operators and then evaluate (trying to use the shunting yard idea)."

If I understand correctly, this is wrong (apologies if I misunderstand). The purpose of the shunting yard is to re-write a formula so that it appear in reverse Polish notation where
3 2 +
adds 3 to 2 (in general Operand2 Operand1 Operator where the operands themselves can be evaluated bits of the function). Your formula 8*8-9 --> 9 - 8 8 * I think. Reverse Polish notation is very confusing, I have to use Polish, then reverse it if you get my meaning.

The shunting algorithm has one stack and one queue. The operator stack is really "temporary", when the algorithm is finished it should be empty. Everything should end up in the output queue rather than just the values. If you follow the algorithm correctly you get a RPN formula in the output queue without any extra work. You shouldn''t go through the output queue and operator stack at the end and work through the precedences. Even if you got this working (and I don''t think it is possible as you described it) it would be slower than the proper algorithm.

The algorithm is detailed in the wikipedia page, but simplistically:

  1. 如果下一个术语是数字,则将其推入输出队列.
  2. 如果下一个术语(运算符)的优先级高于运算符stack()或op上的优先级堆栈为空,将其推入堆栈.否则,将运算符堆栈弹出到输出队列中并将术语推入运算符堆栈
  3. 重复直到没有更多的术语:将运算符堆栈中的每个项目弹出到输出队列中

  1. The Next term, if it is a number push it onto the output queue.
  2. If the next term (an operator) has a higher precedence than the one on the operator stack() or the op stack is empty, push it to the stack. Otherwise pop the operator stack into the output queue and push the term into the operator stack
  3. Repeat until there are no more terms: pop each item in the operator stack into the output queue


显然,这不涉及方括号(与Wiki页面不同).



Obviously this doesn''t deal with brackets (unlike the wiki page).


[edit: I have started to implement this alogrithm, I thought the ouptut was a stack, it isn''t, it is a queue. I have corrected this.]


这很奇怪,当然应该在-之前执行*,这也是它们的写入顺序. br/>
您是否尝试过使用括号?

此处 [
That is odd, it ought to perform the * before the - as a matter of course, and that is the order in which they are written as well.

Have you tried using parentheses?

Here[^] may help.


这篇关于C#运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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