中缀表达式 后缀表达式_中缀表达式VS后缀表达式,以及如何构建更好JavaScript计算器...

中缀表达式 后缀表达式

by Pramod Sripada

通过Pramod Sripada

中缀表达式VS后缀表达式,以及如何构建更好JavaScript计算器 (Infix Expressions VS Postfix Expressions, and How to Build a Better JavaScript Calculator)

If you want to make your Simple Calculator a lot smarter, this post is for you.

如果您想使您的“简单计算器”更加智能,那么这篇文章适合您。

You might asking, “What’s wrong with my simple calculator.” Well, it may do all the operations correctly, but the sequence in which it does them is probably wrong.

您可能会问:“我的简单计算器怎么了?” 好吧,它可能正确执行了所有操作,但是执行这些操作的顺序可能是错误的。

The simple calculator contains just four operations: addition, subtraction, division and multiplication. Many of us might have studied in high school about the precedence of operators: division and multiplication have the same priority, and have higher priority than addition and subtraction, which have the same priority.

简单的计算器仅包含四个运算:加法,减法,除法和乘法。 我们中的许多人可能在中学时就已经研究过运算符的优先级:除法和乘法具有相同的优先级,并且比具有相同优先级的加法和减法具有更高的优先级。

A quick recap of operator precedence can be found here: http://www.math.utah.edu/online/1010/precedence/.

可以在这里找到操作员优先级的快速概述: http : //www.math.utah.edu/online/1010/precedence/

The reason I am stressing operator precedence is because a simple calculator performs most of the calculations wrong. For example, 1+2x3 should be equal to 7 according to a normal calculator, but the simple calculator gives a result of 9.

我强调运算符优先级的原因是因为简单的计算器执行的大多数计算错误。 例如,根据普通计算器,1 + 2x3应该等于7,但是简单计算器的结果为9。

The reason simple calculator does it all wrong is because it just multiplies the two operands, with the operator between them, and produces the result.

简单计算器做错所有事情的原因是,它只是将两个操作数相乘,并在它们之间加上了运算符,并产生了结果。

We just can’t blame the simple calculator. After all, it was meant to be simple. So now you might start thinking on how to rearrange the operators, so as to get the correct result. Yes, you are on the right track. For that, we need to know about two more concepts in computer science: Infix expressions and Postfix expressions.

我们不能责怪简单的计算器。 毕竟,这本来很简单。 因此,现在您可以开始考虑如何重新布置运算符,以获取正确的结果。 是的,您的方向正确。 为此,我们需要了解计算机科学中的另外两个概念:中缀表达式和后缀表达式。

In simple words, the arithmetic expressions that we understand are Infix expressions and the arithmetic expression that the computer understands are Postfix expressions.

简而言之,我们理解的算术表达式是Infix表达式,计算机可以理解的算术表达式是Postfix表达式。

Both the infix and postfix expressions create the same results. It’s just humans are used to solving infix expressions, and computers are used to solving postfix expressions.

infix和postfix表达式都创建相同的结果。 只是人类习惯于解决后缀表达式,而计算机习惯于解决后缀表达式。

Another key feature in the postfix expression is that it contains operators succeeding the operands according to precedence, which makes it easy for the computer to evaluate them using stacks, and produce the correct result.

后缀表达式中的另一个关键功能是它包含根据优先级在操作数之后的运算符,这使计算机可以轻松地使用堆栈对它们进行求值,并产生正确的结果。

By now you must be thinking about how to convert the infix expression entered by your user into a postfix expression. There is an algorithm that converts an infix expression into a postfix expression that can be found here.

到目前为止,您必须考虑如何将用户输入的中缀表达式转换为后缀表达式。 有一种算法可以将中缀表达式转换为后缀表达式,可以在此处找到。

Here’s what this process looks like:

这是这个过程的样子:

The postfix expression should be evaluated by an algorithm, which can be found here. It is similar to the evaluation done by a simple calculator, except that the operators succeed the operands in postfix expressions.

后缀表达式应通过算法进行评估,可以在此处找到。 它类似于由简单计算器完成的评估,不同之处在于运算符使后缀表达式中的操作数接替。

Eventually, the primary motive of converting an infix expression into a postfix expression is to preserve the precedence of the operators while the computer evaluates the expression.

最终,将中缀表达式转换为后缀表达式的主要动机是在计算机评估表达式时保留运算符的优先级。

Check out my fully functional calculator that incorporates these principles here.

在这里查看结合了这些原理的全功能计算器

翻译自: https://www.freecodecamp.org/news/zipline-build-a-smart-javascript-calculator-65e21a8b0b0c/

中缀表达式 后缀表达式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值