当前位置:网站首页>中缀表达式转换为后缀表达式(C语言代码+详解)
中缀表达式转换为后缀表达式(C语言代码+详解)
2022-07-02 18:37:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
中缀表达式转换为后缀表达式(思路)
1.创建栈2.从左向右顺序获取中缀表达式
a.数字直接输出b.运算符情况一:遇到左括号直接入栈,遇到右括号将栈中左括号之后入栈的运算符全部弹栈输出,同时左括号出栈但是不输出。
情况二:遇到乘号和除号直接入栈,直到遇到优先级比它更低的运算符,依次弹栈。
情况三:遇到加号和减号,如果此时栈空,则直接入栈,否则,将栈中优先级高的运算符依次弹栈(注意:加号和减号属于同一个优先级,所以也依次弹栈)直到栈空或则遇到左括号为止,停止弹栈。(因为左括号要匹配右括号时才弹出)。
情况四:获取完后,将栈中剩余的运算符号依次弹栈输出
例:比如将:2*(9+6/3-5)+4转化为后缀表达式 2 9 6 3 / +5 – * 4 +
转换算法代码如下:
/*中缀转后缀函数*/
void Change(SqStack *S,Elemtype str[])
{
int i=0;
Elemtype e;
InitStack(S);
while(str[i]!='\0')
{
while(isdigit(str[i]))
{/*过滤数字字符,直接输出,直到下一位不是数字字符打印空格跳出循环 */
printf("%c",str[i++]);
if(!isdigit(str[i]))
{
printf(" ");
}
}
/*加减运算符优先级最低,如果栈顶元素为空则直接入栈,否则将栈中存储
的运算符全部弹栈,如果遇到左括号则停止,将弹出的左括号从新压栈,因为左
括号要和又括号匹配时弹出,这个后面单独讨论。弹出后将优先级低的运算符压入栈中*/
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]);
}
}
/*当遇到右括号是,把括号里剩余的运算符弹出,直到匹配到左括号为止
左括号只弹出不打印(右括号也不压栈)*/
else if(str[i]==')')
{
PopStack(S,&e);
while(e!='(')
{
printf("%c ",e);
PopStack(S,&e);
}
}
/*乘、除、左括号都是优先级高的,直接压栈*/
else if(str[i]=='*'||str[i]=='/'||str[i]=='(')
{
PushStack(S,str[i]);
}
else if(str[i]=='\0')
{
break;
}
else
{
printf("\n输入格式错误!\n");
return ;
}
i++;
}
/*最后把栈中剩余的运算符依次弹栈打印*/
while(StackLength(S))
{
PopStack(S,&e);
printf("%c ",e);
}
}
完整代码如下:
#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)
/*栈的动态分配存储结构*/
typedef char Elemtype;
typedef struct{
Elemtype *base;
Elemtype *top;
int StackSize;
}SqStack;
/*初始化栈*/
void InitStack(SqStack *S)
{
S->base=(Elemtype*)malloc(LEN*INITSIZE);
assert(S->base !=NULL);
S->top=S->base;
S->StackSize=INITSIZE;
}
/*压栈操作*/
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;
}
/*求栈长*/
int StackLength(SqStack *S)
{
return (S->top - S->base);
}
/*弹栈操作*/
int PopStack(SqStack *S,Elemtype *c)
{
if(!StackLength(S))
{
return 0;
}
*c=*--S->top;
return 1;
}
/*中缀转后缀函数*/
void Change(SqStack *S,Elemtype str[])
{
int i=0;
Elemtype e;
InitStack(S);
while(str[i]!='\0')
{
while(isdigit(str[i]))
{/*过滤数字字符,直接输出,直到下一位不是数字字符打印空格跳出循环 */
printf("%c",str[i++]);
if(!isdigit(str[i]))
{
printf(" ");
}
}
/*加减运算符优先级最低,如果栈顶元素为空则直接入栈,否则将栈中存储
的运算符全部弹栈,如果遇到左括号则停止,将弹出的左括号从新压栈,因为左
括号要和又括号匹配时弹出,这个后面单独讨论。弹出后将优先级低的运算符压入栈中*/
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]);
}
}
/*当遇到右括号是,把括号里剩余的运算符弹出,直到匹配到左括号为止
左括号只弹出不打印(右括号也不压栈)*/
else if(str[i]==')')
{
PopStack(S,&e);
while(e!='(')
{
printf("%c ",e);
PopStack(S,&e);
}
}
/*乘、除、左括号都是优先级高的,直接压栈*/
else if(str[i]=='*'||str[i]=='/'||str[i]=='(')
{
PushStack(S,str[i]);
}
else if(str[i]=='\0')
{
break;
}
else
{
printf("\n输入格式错误!\n");
return ;
}
i++;
}
/*最后把栈中剩余的运算符依次弹栈打印*/
while(StackLength(S))
{
PopStack(S,&e);
printf("%c ",e);
}
}
int main()
{
Elemtype str[MAXBUFFER];
SqStack S;
gets(str);
Change(&S,str);
return 0;
}
运行效果截图如下:
如何实现将中缀表达式转换成后缀表达式后计算值
(https://blog.csdn.net/qq_42552533/article/details/86562791)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148663.html原文链接:https://javaforall.cn
边栏推荐
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- AcWing 1125. 牛的旅行 题解(最短路、直径)
- Emmet basic syntax
- 2022.7.1-----leetcode. two hundred and forty-one
- 电脑使用哪个录制视频软件比较好
- In pytorch function__ call__ And forward functions
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- AcWing 1131. Saving Private Ryan (the shortest way)
- 移动机器人路径规划:人工势场法[通俗易懂]
- Fastdfs installation
猜你喜欢
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Machine learning notes - time series prediction research: monthly sales of French champagne
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
High frequency interview questions
搭建主从模式集群redis
《架构整洁之道》读书笔记(下)
Refactoring: improving the design of existing code (Part 1)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Markdown basic grammar
随机推荐
AcWing 383. Sightseeing problem solution (shortest circuit)
Machine learning notes - time series prediction research: monthly sales of French champagne
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
安装单机redis详细教程
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Golang并发编程——goroutine、channel、sync
Chapter 7 - class foundation
《重构:改善既有代码的设计》读书笔记(下)
Which video recording software is better for the computer
Use cheat engine to modify money, life and stars in Kingdom rush
PyTorch函数中的__call__和forward函数
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
《架构整洁之道》读书笔记(下)
高级性能测试系列《24. 通过jdbc执行sql脚本》
Data dimensionality reduction principal component analysis
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
Emmet basic syntax
Is there any security guarantee for the ranking of stock and securities companies
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
虚拟机初始化脚本, 虚拟机相互免秘钥