当前位置:网站首页>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 )
边栏推荐
- Consumer unit 消费单元
- Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
- 时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
- System Verilog common syntax
- [unity] configure unity edit C as vscode
- ThinkPHP high imitation blue cloud disk system program
- solidity实现智能合约教程(5)-NFT拍卖合约
- FLV文件简介
- 面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?
- 20220728-不纯为数字的字符串排序
猜你喜欢

How to carry out engineering implementation of DDD Domain Driven Design

Canal实时解析mysql binlog数据实战

solidity实现智能合约教程(5)-NFT拍卖合约

数字孪生轨道交通:“智慧化”监控疏通城市运行痛点

时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测

ThinkPHP high imitation blue cloud disk system program

DDD领域驱动设计如何进行工程化落地

大页内存原理及使用设置

New pseudo personal guide page source code

How to create a custom 404 error page in WordPress
随机推荐
Method of converting inline elements to block elements
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
20220728-不纯为数字的字符串排序
Connect with Alipay payment
RHCE命令练习(二)
C language bracket matching (stack bracket matching C language)
Recursion and divide and conquer
Oozie工作调度
SDRAM Controller Design (two design methods of digital controller)
DDD领域驱动设计如何进行工程化落地
SystemVerilog join and copy operators
Transfer: cognitive subculture
如何执行建设项目的时间影响分析?
Interview shock 69: is TCP reliable? Why?
Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
[Commons lang3 topic] 002 randomutils topic
How to carry out engineering implementation of DDD Domain Driven Design
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
Linux Redis 源码安装
机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定