当前位置:网站首页>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;
}
边栏推荐
- PR second training
- Sword finger offer 47 Maximum value of gifts
- Electronic Association C language level 1 33, odd even number judgment
- Architecture evolution from MVC to DDD
- D discard the virtual recovery method
- C language 3-7 daffodils (enhanced version)
- 【OpenCV】-5种图像滤波的综合示例
- 分卷压缩,解压
- Sword finger offer II 031 Least recently used cache
- 【LeetCode 43】236. The nearest common ancestor of binary tree
猜你喜欢

MySQL中一条SQL是怎么执行的

5g/4g pole gateway_ Smart pole gateway
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/ba/dcb276768b1a9cc84099f093677d29.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

golang---锁

If you want to rewind the video picture, what simple methods can you use?

如何用一款产品推动「品牌的惊险一跃」?

Medical management system (C language course for freshmen)

大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里

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

leetcode2305. 公平分发饼干(中等,周赛,状压dp)
随机推荐
[graduation season] graduate seniors share how to make undergraduate more meaningful
DNS domain name resolution
Implementation principle of city selector component
Construction and maintenance of business websites [10]
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
How to debug apps remotely and online?
Types of exhibition items available in the multimedia interactive exhibition hall
SQL server calculates the daily average and annual average of the whole province
[question] - why is optical flow not good for static scenes
Redis环境搭建和使用的方法
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
Email picture attachment
Laravel artisan common commands
Number of palindromes in C language (leetcode)
分卷压缩,解压
Discussion on the idea of platform construction
Construction and maintenance of business websites [13]
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
JMeter (II) - install the custom thread groups plug-in
Post infiltration flow encryption