当前位置:网站首页>Infix expression to suffix expression (computer) code
Infix expression to suffix expression (computer) code
2022-07-02 02:09:00 【Abright_】
The code idea is taken from the kingly data structure
Ideas
Initialize a stack , It is used to save operators whose operation order cannot be determined temporarily .
Process elements from left to right , Till the end . There are three possible situations
① Encountered operand . Add suffix expression directly .
② Delimiter encountered . encounter “(” Direct entry ; encounter “)” Then pop up the operators in the stack in turn and add the suffix expression , until
eject “(” until . Be careful :“("” Do not add suffix expression
③ Operator encountered . Pop up all operators in the stack with priority higher than or equal to the current operator , And add suffix expression ,
If you encounter “(” Or if the stack is empty, stop . Then put the current operator on the stack .
After all characters are processed as described above , Pop up the remaining operators in the stack in turn , And add suffix expression .
flow chart
I drew a flow chart according to my own ideas , Not in accordance with the specifications .

Code
#include <iostream>
#include <string.h>
using namespace std;
#define MaxSize 10
typedef struct{
char data[MaxSize];// Static arrays hold the elements in the stack
int top;// To the top of the stack
} SqStack;
void InitStack(SqStack &S){
S.top = -1;// Initialize the stack top pointer
memset(S.data,'0', MaxSize);
}
bool StackEmpty(SqStack S){
if(S.top==-1)// Empty stack
return true;
else
return false;
}
bool Push(SqStack &S,char x){
if (S.top==MaxSize-1)
return false;
S.top += 1;
S.data[S.top] = x;
return true;
}
bool Pop(SqStack &S,char &x){
if(S.top==-1)
return false;
x = S.data[S.top];
S.top -= 1;
return true;
}
// Get stack header
char GetHead(SqStack S){
if(StackEmpty(S))
return 'c';
return S.data[S.top];
}
/**
* Determine operator priority
*/
int GetPrioraty(char c){
switch (c)
{
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
default:
return 0;
break;
}
}
// Compare the priority of two characters
bool Prioraty(char c,char p){
return GetPrioraty(c) <= GetPrioraty(p);
}
// Determine character type
int StrType(char c){
if(isdigit(c)){
return 1;
}
if(c=='('||c==')'){
return 2;
}
if(c=='+'||c=='*'||c=='-'||c=='/'){
return 3;
}
}
string ZzToHz(string str){
SqStack S;
InitStack(S);
string zzbds;
for (size_t i = 0; i < str.size(); i++)
{
char cur = str.at(i);
int opt = StrType(cur);
if(opt==1){// Operand direct splicing
zzbds.push_back(cur);
continue;
}else if(opt==2){
if(cur=='('){
Push(S, cur);
continue;
}
char follow;
while(Pop(S, follow)&&follow!='('){
zzbds.push_back(follow);
}
}else if(opt==3){
char stackHead = GetHead(S);
if(StackEmpty(S)||stackHead=='('){// Fetch ( Or if you can't get any number and the stack is empty, press the operator directly into the stack
Push(S, cur);
continue;
}
if(Prioraty(cur,stackHead)){
while (true)// Need to pop up
{
stackHead = GetHead(S);
if(StackEmpty(S)||stackHead=='('){
break;
}
if(Prioraty(cur,stackHead)){
zzbds.push_back(stackHead);
S.top--;
}
}
}
Push(S, cur);
}
}
while (!StackEmpty(S))// Splice the remaining elements in the stack
{
char stacHead=GetHead(S);
if(stacHead!='('){
zzbds.push_back(stacHead);
S.top--;
}
}
return zzbds;
}
int main(int argc, char const *argv[])
{
string zz("1+2*(3-4)-5/6");
cout << ZzToHz(zz) << endl;
return 0;
}
边栏推荐
- Sword finger offer 42 Maximum sum of continuous subarrays
- Sword finger offer 31 Stack push in and pop-up sequence
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
- leetcode373. Find and minimum k-pair numbers (medium)
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- Parted command
- Discussion on the idea of platform construction
- What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
猜你喜欢

leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)

Cross domain? Homology? Understand what is cross domain at once

Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory

leetcode373. 查找和最小的 K 对数字(中等)

Architecture evolution from MVC to DDD
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

MySQL主从延迟问题怎么解决

CSDN article underlined, font color changed, picture centered, 1 second to understand

How to use a product to promote "brand thrill"?

How to use redis ordered collection
随机推荐
Data analysis on the disaster of Titanic
How does MySQL solve the problem of not releasing space after deleting a large amount of data
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
321. Chessboard segmentation (2D interval DP)
* and & symbols in C language
JMeter (I) - download, installation and plug-in management
2022 Q2 - résumé des compétences pour améliorer les compétences
剑指 Offer 42. 连续子数组的最大和
flutter 中间一个元素,最右边一个元素
How to debug apps remotely and online?
Open那啥的搭建文档
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
No programming code technology! Four step easy flower store applet
Construction and maintenance of business websites [10]
Post infiltration flow encryption
Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
PR second training
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
Construction and maintenance of business websites [13]
734. Energy stone (greed, backpack)