当前位置:网站首页>Infix expression is converted to suffix expression (C language code + detailed explanation)
Infix expression is converted to suffix expression (C language code + detailed explanation)
2022-07-02 19:38:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Infix expression to suffix expression ( Ideas )
1. Create a stack 2. Get infix expressions from left to right
a. Digital direct output b. Operator Situation 1 : Left parenthesis encountered Direct stack , encounter Right bracket In the stack Left parenthesis After that, all operators on the stack pop up and output , meanwhile The left bracket comes out of the stack but does not output .
Situation two : Encounter multiplication sign and division sign Direct stack , Until you encounter an operator with a lower priority , Play the stack in turn .
Situation three : Plus and minus signs encountered , If at this time The stack is empty , be Direct stack , otherwise , In the stack High priority The operators of pop-up stack in turn ( Be careful : The plus and minus signs belong to the same priority , So play the stack in turn ) until Stack empty or encounter left parenthesis until , Stop playing stack .( Because the left bracket will pop up only when it matches the right bracket ).
Situation four : After obtaining , Pop up the remaining operation symbols in the stack and output
example : For example, will :2*(9+6/3-5)+4 Convert to suffix expression 2 9 6 3 / +5 – * 4 +
The conversion algorithm code is as follows :
/* Infix to suffix function */
void Change(SqStack *S,Elemtype str[])
{
int i=0;
Elemtype e;
InitStack(S);
while(str[i]!='\0')
{
while(isdigit(str[i]))
{/* Filter numeric characters , Direct output , Until the next non numeric character print space jumps out of the loop */
printf("%c",str[i++]);
if(!isdigit(str[i]))
{
printf(" ");
}
}
/* Addition and subtraction operators have the lowest priority , If the element at the top of the stack is empty, it will be directly put on the stack , Otherwise, it will be stored in the stack
All operators of play stack , If you encounter an open parenthesis, stop , Press the pop-up left bracket from the new stack , Because left
The parentheses pop up when they want to match the parentheses again , This will be discussed separately later . After popping up, press the operator with low priority into the stack */
if(str[i]=='+'||str[i]=='-')
{
if(!StackLength(S))
{
PushStack(S,str[i]);
}
else
{
do
{
PopStack(S,&e);
if(e=='(')
{
PushStack(S,e);
}
else
{
printf("%c ",e);
}
}while( StackLength(S) && e != '(' );
PushStack(S,str[i]);
}
}
/* When you encounter the right parenthesis , Pop up the remaining operators in brackets , Until the left bracket is matched
The left bracket only pops up and does not print ( The right bracket does not press the stack )*/
else if(str[i]==')')
{
PopStack(S,&e);
while(e!='(')
{
printf("%c ",e);
PopStack(S,&e);
}
}
/* ride 、 except 、 The left parentheses are high priority , Press the stack directly */
else if(str[i]=='*'||str[i]=='/'||str[i]=='(')
{
PushStack(S,str[i]);
}
else if(str[i]=='\0')
{
break;
}
else
{
printf("\n Input format error !\n");
return ;
}
i++;
}
/* Finally, flip the remaining operators in the stack in turn and print */
while(StackLength(S))
{
PopStack(S,&e);
printf("%c ",e);
}
}
The complete code is as follows :
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<assert.h>
#define INITSIZE 20
#define INCREMENT 10
#define MAXBUFFER 20
#define LEN sizeof(Elemtype)
/* Dynamic allocation storage structure of stack */
typedef char Elemtype;
typedef struct{
Elemtype *base;
Elemtype *top;
int StackSize;
}SqStack;
/* Initialization stack */
void InitStack(SqStack *S)
{
S->base=(Elemtype*)malloc(LEN*INITSIZE);
assert(S->base !=NULL);
S->top=S->base;
S->StackSize=INITSIZE;
}
/* Stack pressing operation */
void PushStack(SqStack *S,Elemtype c)
{
if(S->top - S->base >= S->StackSize)
{
S->base=(Elemtype*)realloc(S->base,LEN*(S->StackSize+INCREMENT));
assert(S->base !=NULL);
S->top =S->base+S->StackSize;
S->StackSize+=INCREMENT;
}
*S->top++ = c;
}
/* Ask for stack length */
int StackLength(SqStack *S)
{
return (S->top - S->base);
}
/* Pop stack operation */
int PopStack(SqStack *S,Elemtype *c)
{
if(!StackLength(S))
{
return 0;
}
*c=*--S->top;
return 1;
}
/* Infix to suffix function */
void Change(SqStack *S,Elemtype str[])
{
int i=0;
Elemtype e;
InitStack(S);
while(str[i]!='\0')
{
while(isdigit(str[i]))
{/* Filter numeric characters , Direct output , Until the next non numeric character print space jumps out of the loop */
printf("%c",str[i++]);
if(!isdigit(str[i]))
{
printf(" ");
}
}
/* Addition and subtraction operators have the lowest priority , If the element at the top of the stack is empty, it will be directly put on the stack , Otherwise, it will be stored in the stack
All operators of play stack , If you encounter an open parenthesis, stop , Press the pop-up left bracket from the new stack , Because left
The parentheses pop up when they want to match the parentheses again , This will be discussed separately later . After popping up, press the operator with low priority into the stack */
if(str[i]=='+'||str[i]=='-')
{
if(!StackLength(S))
{
PushStack(S,str[i]);
}
else
{
do
{
PopStack(S,&e);
if(e=='(')
{
PushStack(S,e);
}
else
{
printf("%c ",e);
}
}while( StackLength(S) && e != '(' );
PushStack(S,str[i]);
}
}
/* When you encounter the right parenthesis , Pop up the remaining operators in brackets , Until the left bracket is matched
The left bracket only pops up and does not print ( The right bracket does not press the stack )*/
else if(str[i]==')')
{
PopStack(S,&e);
while(e!='(')
{
printf("%c ",e);
PopStack(S,&e);
}
}
/* ride 、 except 、 The left parentheses are high priority , Press the stack directly */
else if(str[i]=='*'||str[i]=='/'||str[i]=='(')
{
PushStack(S,str[i]);
}
else if(str[i]=='\0')
{
break;
}
else
{
printf("\n Input format error !\n");
return ;
}
i++;
}
/* Finally, flip the remaining operators in the stack in turn and print */
while(StackLength(S))
{
PopStack(S,&e);
printf("%c ",e);
}
}
int main()
{
Elemtype str[MAXBUFFER];
SqStack S;
gets(str);
Change(&S,str);
return 0;
}
The operation effect screenshot is as follows :
How to calculate the value after converting infix expression into suffix expression
(https://blog.csdn.net/qq_42552533/article/details/86562791)
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148663.html Link to the original text :https://javaforall.cn
边栏推荐
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- Understanding and function of polymorphism
- Advanced performance test series "24. Execute SQL script through JDBC"
- Memory management of C
- AcWing 1134. 最短路计数 题解(最短路)
- MySQL
- 横向越权与纵向越权[通俗易懂]
- AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
- Bubble sort array
- Juypter notebook modify the default open folder and default browser
猜你喜欢
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Detailed tutorial on installing stand-alone redis
搭建哨兵模式reids、redis从节点脱离哨兵集群
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
《重构:改善既有代码的设计》读书笔记(下)
Juypter notebook modify the default open folder and default browser
450-深信服面经1
《重构:改善既有代码的设计》读书笔记(上)
SQLite 3.39.0 发布,支持右外连接和全外连接
随机推荐
GMapping代码解析[通俗易懂]
452-strcpy、strcat、strcmp、strstr、strchr的实现
《代碼整潔之道》讀書筆記
程序猿入门攻略(十二)——数据的存储
使用xml文件打印mybaties-log插件的方式
横向越权与纵向越权[通俗易懂]
Advanced performance test series "24. Execute SQL script through JDBC"
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
How to avoid duplicate data in gaobingfa?
Codeforces Round #802 (Div. 2) 纯补题
Introduction of Ethernet PHY layer chip lan8720a
JS如何取整数
MySQL表历史数据清理总结
IEDA refactor的用法
Golang concurrent programming goroutine, channel, sync
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
AcWing 1137. 选择最佳线路 题解(最短路)
451 implementation of memcpy, memmove and memset
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
Getting started with typescript