当前位置:网站首页>【栈的应用】--- 中缀表达式转后缀表达式
【栈的应用】--- 中缀表达式转后缀表达式
2022-07-28 10:12:00 【努力的张张】
问题描述:
如何将中缀表达式转为后缀表达式,首先我们看看他们两者的联系与区别:
- 中缀表达式(或中缀记法)是一个通用的算术或逻辑公式表示方法, 操作符是以中缀形式处于操作数的中间(例:3 + 4),中缀表达式是人们常用的算术表示方法。
- 但是,这种表达让计算机来做运算是不方便的,因此,有后缀表达式:
一个表达式E的后缀形式可以如下定义:
(1)如果E是一个变量或常量,则E的后缀式是E本身。
(2)如果E是E1 op E2形式的表达式,这里op是任何二元操作符,则E的后缀式为E1’ E2’ op,这里E1’和E2’分别为E1和E2的后缀式。
(3)如果E是(E1)形式的表达式,则E1的后缀式就是E的后缀式。
如:我们平时写a+b,这是中缀表达式,写成后缀表达式就是:ab+
原因分析:
我们很容易找到如何将中缀表达式转为后缀表达式,不过用栈怎么实现呢?
主要是处理下面这几种特殊情况即可:
代码实现:
#ifndef __STACK_H
#define __STACK_H
typedef char ElemType;
typedef struct Stack
{
ElemType *stack_low;
int top;
int size;
}Stack;
void InitStack(Stack *st, int init_size);
bool Empty(Stack *st);
bool Push(Stack *st, ElemType value);
bool Pop(Stack *st);
bool Top(Stack *st, ElemType *reval);
void DestroyStack(Stack *st);
#include "stack.h"
#include <string.h>
#include<stdio.h>
#include <stdlib.h>
#include<ctype.h>
static bool Full(Stack *st)
{
return st->top == st->size;
}
static bool AppendSpace(Stack *st)
{
ElemType *new_space = (ElemType*)malloc(sizeof(ElemType) * st->size * 2);
if(new_space == NULL) return false;
for(int i = 0; i < st->size; ++i)
{
new_space[i] = st->stack_low[i];
}
free(st->stack_low);
st->stack_low = new_space;
st->size *= 2;
return true;
}
void InitStack(Stack *st, int init_size)
{
if(st == NULL) exit(0);
init_size = init_size > 0 ? init_size : 10;
st->stack_low = (ElemType*)malloc(sizeof(ElemType) * init_size);
if(st->stack_low == NULL) exit(0);
st->top = 0;
st->size = init_size;
}
bool Empty(Stack *st)
{
if(st == NULL) exit(0);
return st->top == 0;
}
bool Push(Stack *st, ElemType value)
{
if(st == NULL) exit(0);
if(Full(st))
{
if(!AppendSpace(st))
{
return false;
}
}
st->stack_low[st->top++] = value;
return true;
}
bool Pop(Stack *st)
{
if(st == NULL) exit(0);
if(Empty(st)) return false;
st->top--;
return true;
}
bool Top(Stack *st, ElemType *reval)
{
if(st == NULL) exit(0);
if(Empty(st)) return false;
*reval = st->stack_low[st->top - 1];
return true;
}
void DestroyStack(Stack *st)
{
if(st == NULL) exit(0);
free(st->stack_low);
st->stack_low = NULL;
st->top = st->size = 0;
}
//中缀转后缀
void InfixToSuffix(char *str)
{
if(str==NULL) return;
Stack s;
InitStack(&s,10);
int i=0; //开始遍历表达式字符串的下标
while(str[i]!='\0')
{
if(str[i]==' ')
{
i++;
continue;
}
if(isdigit(str[i])) //如果是数字的情况
{
printf("%c",str[i]);
if(!isdigit(str[i+1]))
{
printf(" ");
}
}
else if(str[i]=='(') //如果是左括号的情况
{
Push(&s,str[i]);
}
else if(str[i]==')') //如果是右括号的情况
{
int flag=0;
while(!Empty(&s))
{
ElemType val;
Top(&s,&val);
Pop(&s);
if(val=='(')
{
flag=1;
break;
}
printf("%c ",val);
}
if(!flag)
{
printf("括号不匹配\n");
}
}
else if(str[i]=='*' || str[i]=='/')
{
ElemType val;
while(!Empty(&s))
{
Top(&s,&val);
if(val=='(' ||
val=='+' ||
val=='-') break;
Pop(&s);
printf("%c ",val);
}
Push(&s,str[i]);
}
else if(str[i]=='+' || str[i]=='-')
{
ElemType val;
while(!Empty(&s))
{
Top(&s,&val);
if(val=='(' ) break;
Pop(&s);
printf("%c ",val);
}
Push(&s,str[i]);
}
else
{
printf("输入内容有误!\n");
return;
}
i++;
}
while(!ElemType(&s))
{
ElemType val;
Top(&s,&val);
printf("%c ",val);
Pop(&s);
}
printf("\n");
DestroyStack(&s);
}
int main()
{
char str[]="12*4+34/5-(56+67*4)+32";
printf("\n");
InfixToSuffix(str);
return 0;
}
边栏推荐
- Qt | 信号和槽的一些总结
- 7、二分法——寻找一组重复或者有序但是旋转的数组
- Get to know SuperMap idesktop for the first time
- 5. Dynamic programming -- Fibonacci series
- 配置树莓派,过程和遇到问题
- Ie compatibility problem handling
- Xu Ziyang, President of ZTE: 5nm chip will be launched in 2021
- PHP生成二维码(学习)
- 13. Hash table - the first common node of two linked lists
- MySQL架构原理
猜你喜欢

IDEA创建我的第一个项目

Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control

利用正则表达式从文件路径中匹配文件名

记录一次idea中的父子项目修改project与module名称,亲测!

14、双指针——盛最多水的容器

语音聊天app——如何规范开发流程?

Get to know SuperMap idesktop for the first time

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试

剑指offer

配置树莓派,过程和遇到问题
随机推荐
C language secondary pointer explanation and example code
基于ModelArts续写最伟大的作品【玩转华为云】
13. Hash table - the first common node of two linked lists
PL/SQL server语法详解
Massive data topn problem
初识SuperMap iDesktop
[cloud co creation] enterprise digital transformation, Huawei cloud consulting is with you
银行入职考试要点汇总
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
字符串匹配
pt-kill 查询中包含中文字符 导致工具失效的排查
Summary of key points of bank entry examination
Aqua Data Studio 18.5.0导出insert语句
10、链表中倒数第k个节点
2021-10-13arx
3. Print the linked list in reverse order with the array
Sword finger offer
不登高山,不知天之高也;不临深溪,不知地之厚也
传全球半导体设备巨头或将于上海建合资工厂!
剑指offer