当前位置:网站首页>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;
}
边栏推荐
- The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
- Construction and maintenance of business websites [14]
- MySQL constraints and multi table query example analysis
- The difference between new and malloc
- How to execute an SQL in MySQL
- leetcode2305. 公平分发饼干(中等,周赛,状压dp)
- 2022 Q2 - Summary of skills to improve skills
- pytest 测试框架
- PR second training
- MySQL约束与多表查询实例分析
猜你喜欢

leetcode2309. The best English letters with both upper and lower case (simple, weekly)

1222. Password dropping (interval DP, bracket matching)

研发中台拆分过程的一些心得总结

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

How to debug apps remotely and online?

What is AQS and its principle
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

MySQL中一条SQL是怎么执行的

软件开发生命周期 --瀑布模型

分卷压缩,解压
随机推荐
2022 Q2 - 提昇技能的技巧總結
Architecture evolution from MVC to DDD
479. Additive binary tree (interval DP on the tree)
MySQL约束与多表查询实例分析
321. Chessboard segmentation (2D interval DP)
This is the form of the K-line diagram (pithy formula)
[deep learning] Infomap face clustering facecluster
Discussion on the idea of platform construction
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
Construction and maintenance of business websites [11]
The middle element and the rightmost element of the shutter
Redis有序集合如何使用
pytest 测试框架
How to solve MySQL master-slave delay problem
Exception handling of class C in yyds dry goods inventory
Ar Augmented Reality applicable scenarios
Number of palindromes in C language (leetcode)
2022 Q2 - résumé des compétences pour améliorer les compétences
Post infiltration flow encryption
MySQL中一条SQL是怎么执行的