当前位置:网站首页>Expression evaluation
Expression evaluation
2022-07-29 01:15:00 【Ma cute's Ma cute】
1、 Infix expression to prefix expression ( Manual calculation )

notes : In the process of infix expression to prefix expression , Scan expression from right to left , As long as the operator on the right can calculate , Give priority to the one on the right . This ensures that the resulting suffix expression is unique , And it takes effect from right to left !
2、 Calculation of prefix expression ( Computer calculation )
Use stack to calculate prefix expression ( The first thing out of the stack is the left operand )
(1)、 Scan the next element from right to left , Until all the elements are processed
(2)、 If the operand is scanned and pushed onto the stack , And back to (1); otherwise (3)
(3)、 If operator is scanned , Then two stack top elements pop up , Perform the corresponding operation , Push the result back to the top of the stack , And back to (1)
(4)、 Cycle the above steps back and forth , Until all the elements are processed , There must be only one element in the stack , Otherwise, the prefix expression is incorrect , It's illegal
Be careful , Because the next element is scanned from right to left to perform the operation , So the right operand is the most advanced stack , The backward is left-wing math , However, due to the characteristics of stack first in and last out , So the left operand comes out of the stack first when encountering the operator !
3、 Infix expression to suffix expression ( Manual calculation )

In the process of infix expression to suffix expression , Scan the scan expression from left to right , As long as the operator on the left can calculate , Give priority to the one on the left . This ensures that the resulting suffix expression is unique , And it takes effect from left to right !
4、 The calculation of suffix expression ( Manual calculation )

5、 Suffix expression evaluation ( Computer calculation )

(4)、 Cycle the above steps back and forth , Until all the elements are processed , There must be only one element in the stack , Otherwise, the suffix expression is incorrect , It's illegal
6、 Infix expression to suffix expression ( Computer calculation )
Initialize a stack , It is used to save operators whose operation order cannot be determined temporarily
Scan the elements from left to right , Till the end , There are three possible situations :
(1)、 Encountered operand . Add directly to the suffix expression
(2)、 Delimiter encountered
encounter “(” Add directly to the stack
encounter “)” Then pop up the operators in the stack in turn and add the suffix expression , Until it pops up “(” until ( Be careful :“(” Is not added to the suffix expression ), Because the current delimiter “)” It must be better than the stack top operator +、-、*、) The priority is low , Therefore, the priority is higher than “)” All operators of , Until the top element with the same priority pops up “(”
(3)、 Operator encountered ( View operator stack )
If the current operator has a lower priority than the top of the stack operator , Pop up all operators in the stack with priority higher than or equal to the current operator , And add it to the suffix expression , Then press the current operator onto the stack
If the current operator has higher priority than the stack top operator , Then directly stack the current operator
If the stack top element is “(”, Then directly press the current meta operator onto the stack ( Because the current operator +、-、*、/、( The priority of is definitely higher than the top element “) High priority ”)
if Empty in stack , Then directly push the current operator onto the stack
(4)、 After processing all the characters according to the above method , Pop up the remaining operators in the stack in turn , And add suffix expression !



7、 The calculation of infix expression ( While generating suffix expression , At the same time, the computer method of suffix expression is used to calculate the suffix expression )
First, you need to initialize two stacks , One is the operand stack , One is the operator stack
Scan each character in the expression from left to right
If the operand is scanned , Push directly into the operand stack
If you scan to an operator or delimiter , according to “ Infix expression to suffix expression ” The same logic is pushed into the operator stack ( But operators will pop up during , Whenever an operator pops up , You need to pop up the top elements of the two operand stacks and perform the corresponding operations , The result of the operation is pushed back to the operand stack again )
After scanning from left to right , If the operator stack is empty and there is only one element at the bottom of the operand stack , Then the infix expression is legal , Otherwise, the infix expression is illegal !


8、 Algorithm implementation of infix expression (c edition )
边栏推荐
- Introduction to FLV documents
- 【Leetcode-滑动窗口问题】
- APP接入Kakaotalk三方登录
- Talk about the cross end technical scheme
- 【刷题笔记】从链表中删去总和值为零的连续节点
- Prometheus 的 API 稳定性保障
- Textkit custom uilabel identification link
- Cookie和Session
- Wechat campus bathroom reservation applet graduation design finished product (5) assignment
- “index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
猜你喜欢

How to explain JS' bind simulation implementation to your girlfriend

Tupu software appeared at the 2022 Fuzhou digital Expo to jointly create a new digital era

进程和线程知识点总结 2

新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售

Summary of process and thread knowledge points 1

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income

Thread lock and its ascending and descending levels

Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)

【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
随机推荐
[Commons lang3 topic] 005- objectutils topic
Univariate function integration 1__ Indefinite integral
Rewriting method set
新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
Transfer: cognitive subculture
[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
Self made | a 16 bit RISC architecture CPU is self-made by hand
Cookies and sessions
Spark 3.0 中七个必须知道的 SQL 性能优化
ACM SIGIR 2022 | 美团技术团队精选论文解读
IT硬件故障的主要原因和预防的最佳实践
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
Principle and usage setting of large page memory
How to create a custom 404 error page in WordPress
[Commons lang3 topic] 003- randomstringutils topic
【mysql】多指标历史累计去重问题
How to carry out engineering implementation of DDD Domain Driven Design
Necessary interview skills for Android (including interview questions and learning materials)
[web development] basic knowledge of flask framework
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益