反转数据并解析堆栈中的数据 [英] reversing data and parsing data in a stack

查看:89
本文介绍了反转数据并解析堆栈中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是反转数据并解析堆栈中的数据

what is reversing data and parsing data in a stack
and how can v reverse and pars data in a stack?

推荐答案

表达式可以用前缀,后缀或中缀表示法表示,并且可以完成从一种形式到另一种形式的转换使用堆栈.许多编译器在转换为低级代码之前都会使用堆栈来解析表达式,程序块等的语法.大多数编程语言都是无上下文语言,因此可以使用基于堆栈的计算机进行解析.

完全括号的 中缀 表达式的求值
输入:((((2 * 5)-(1 * 2))/(11-9))
输出:4
分析:五种输入字符
开口支架
数字
运算符
闭合支架
换行符
数据结构要求:字符堆栈
算法
1.读取一个输入字符
2.每个输入末尾的动作
打开支架(2.1)推入堆栈,然后转到步骤(1)
数字(2.2)推入堆栈,然后转到步骤(1)
操作员(2.3)推入堆栈,然后转到步骤(1)
右括号(2.4)从字符堆栈弹出
(2.4.1)如果在打开支架,则将其丢弃,请转到步骤(1)
(2.4.2)流行音乐使用了四次
第一个弹出的元素分配给op2
第二个弹出的元素分配给op
弹出的第三个元素分配给op1
弹出的第四个元素是剩余的左括号,可以将其丢弃
评估op1 op op2
将结果转换为字符并
推入堆栈
转到步骤(2.4)
新行字符(2.5)从堆栈弹出并打印答案
停止

前缀 表达式的求值
输入:/-* 2 5 * 1 2-11 11
输出:4
分析:共有三种输入字符
数字
运算符
换行符(\ n)
数据结构要求:字符堆栈和整数堆栈
算法:
1.一次读取一个字符输入,并不断将其推入字符堆栈,直到新输入
达到线型字符
2.从字符堆栈中执行弹出.如果堆栈为空,请转到步骤(3)
数字(2.1)推入整数堆栈,然后转到步骤(1)
运算符(2.2)将运算符分配给op
从整数堆栈中弹出一个数字并将其分配给op1
从整数堆栈中弹出另一个数字
并将其分配给op2
计算op1 op op2并将输出推入整数
堆.转到步骤(2)
3.从整数堆栈中弹出结果并显示结果
后缀表达式的求值
计算形式:1 + 2 * 4 + 3可以这样写成后缀符号,其优点是不需要优先级规则和括号:
1 2 4 * + 3 +
该表达式是使用堆栈从左到右求值的:
遇到操作数时:按下
遇到运算符时:弹出两个操作数,评估结果并将其推入.
Expressions can be represented in prefix, postfix or infix notations and conversion from one form to another may be accomplished using a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code. Most programming languages are context-free languages, allowing them to be parsed with stack based machines.

Evaluation of an infix expression that is fully parenthesized
Input: (((2 * 5) - (1 * 2)) / (11 - 9))
Output: 4
Analysis: Five types of input characters
Opening bracket
Numbers
Operators
Closing bracket
New line character
Data structure requirement: A character stack
Algorithm
1. Read one input character
2. Actions at end of each input
Opening brackets (2.1) Push into stack and then Go to step (1)
Number (2.2) Push into stack and then Go to step (1)
Operator (2.3) Push into stack and then Go to step (1)
Closing brackets (2.4) Pop from character stack
(2.4.1) if it is opening bracket, then discard it, Go to step (1)
(2.4.2) Pop is used four times
The first popped element is assigned to op2
The second popped element is assigned to op
The third popped element is assigned to op1
The fourth popped element is the remaining opening bracket, which can be discarded
Evaluate op1 op op2
Convert the result into character and
push into the stack
Go to step (2.4)
New line character (2.5) Pop from stack and print the answer
STOP

Evaluation of prefix expression
Input: / - * 2 5 * 1 2 - 11 9
Output: 4
Analysis: there are three types of input characters
Numbers
Operators
New line character (\n)
Data structure requirement: a character stack and an integer stack
Algorithm:
1. Read one character input at a time and keep pushing it into the character stack until the new
line character is reached
2. Perform pop from the character stack. If the stack is empty, go to step (3)
Number (2.1) Push in to the integer stack and then go to step (1)
Operator (2.2) Assign the operator to op
Pop a number from integer stack and assign it to op1
Pop another number from integer stack
and assign it to op2
Calculate op1 op op2 and push the output into the integer
stack. Go to step (2)
3. Pop the result from the integer stack and display the result
Evaluation of postfix expression
The calculation: 1 + 2 * 4 + 3 can be written down like this in postfix notation with the advantage of no precedence rules and parentheses needed:
1 2 4 * + 3 +
The expression is evaluated from the left to right using a stack:
when encountering an operand: push it
when encountering an operator: pop two operands, evaluate the result and push it.


这篇关于反转数据并解析堆栈中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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