SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式
数据结构实验之栈与队列二:一般算术表达式转换成后缀式
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。
Input
输入一个算术表达式,以‘#’字符作为结束标志。
Output
输出该表达式转换所得到的后缀式。
Sample Input
a*b+(c-d/e)*f#
Sample Output
ab*cde/-f*+
在做这道题之前首先要了解什么是后缀表达式
百度百科—后缀表达式
这是一种适合计算机计算的表达式,具体步骤:
- 遇到操作数:直接输出(添加到后缀表达式中)
- 栈为空时,遇到运算符,直接入栈
- 遇到左括号:将其入栈
- 遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,括号不输出。
- 遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
- 最终将栈中的元素依次出栈,输出。
引用自 Casionx 的CSDN 博客——原表达式转换为后缀表达式
附上代码:
非线性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char data;
struct node *next;
}Node;
typedef struct stack
{
Node *base,*top;
}Stack;
Node *newnode()
{
Node *t;
t = (Node *)malloc(sizeof(Node));
t->next = NULL;
return t;
};
Stack *Newstack()
{
Stack *t;
t = (Stack *)malloc(sizeof(Stack));
t->top = newnode();
t->base = t->top;
return t;
}
void push(Stack *t,char x)
{
Node *p = newnode();
p->data = x;
p->next = t->top->next;
t->top->next = p;
t->base = p;
}
char top(Stack *t)
{
return t->top->next->data;
}
void pop(Stack *t)
{
Node *p;
p = t->top->next;
t->top->next = t->top->next->next;
free(p);
}
int empty(Stack *t)
{
if(t->top->next==NULL)
return 1;
return 0;
}
int judge(char a,char b)
{
if(b=='(')
return 1;
if(a=='*'||a=='/')
{
if(b=='+'||b=='-')
return 1;
}
return 0;
}
int main()
{
Stack *t;
char s[100050],s2[100050];
int i,num = 0;
scanf("%s",s);
t = Newstack();
for(i=0;s[i]!='#';i++)
{
if(s[i]=='(')
push(t,s[i]);
else if(s[i]==')')
{
while(top(t)!='(')
{
s2[num++] = top(t);
pop(t);
}
pop(t);
}
else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
{
if(empty(t))
push(t,s[i]);
else
{
while(!empty(t)&&!judge(s[i],top(t)))
{
s2[num++] = top(t);
pop(t);
}
push(t,s[i]);
}
}
else
s2[num++] = s[i];
}
while(!empty(t))
{
s2[num++] = top(t);
pop(t);
}
s2[num] = '\0';
printf("%s\n",s2);
return 0;
}
线性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stack
{
char *top,*base;
int len;
}Stack;
Stack newstack()
{
Stack t;
t.top = (char *)malloc(100050*sizeof(char));
t.base = t.top;
t.len = 0;
return t;
}
char top(Stack t)
{
return *(t.top-1);
}
void pop(Stack *t)
{
t->top--;
t->len--;
}
void push(Stack *t,char x)
{
*(t->top) = x;
t->top++;
t->len++;
}
int judge(char a,char b)
{
if(b=='(')
return 1;
if(a=='*'||a=='/')
{
if(b=='+'||b=='-')
return 1;
}
return 0;
}
int main()
{
Stack t;
char s[100050],s2[100050];
int num = 0,i;
t = newstack();
scanf("%s",s);
for(i=0;s[i]!='#';i++)
{
if(s[i]=='(')
push(&t,s[i]);
else if(s[i]==')')
{
while(top(t)!='(')
{
s2[num++] = top(t);
pop(&t);
}
pop(&t);
}
else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
{
if(!t.len)
push(&t,s[i]);
else
{
while(t.len&&!judge(s[i],top(t)))
{
s2[num++] = top(t);
pop(&t);
}
push(&t,s[i]);
}
}
else
s2[num++] = s[i];
}
while(t.len)
{
s2[num++] = top(t);
pop(&t);
}
s2[num] = '\0';
printf("%s\n",s2);
return 0;
}
分类:
数据结构-栈和队列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现