数据结构实验之栈与队列二:一般算术表达式转换成后缀式

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*+

在做这道题之前首先要了解什么是后缀表达式

百度百科—后缀表达式

这是一种适合计算机计算的表达式,具体步骤:

  1. 遇到操作数:直接输出(添加到后缀表达式中)
  2. 栈为空时,遇到运算符,直接入栈
  3. 遇到左括号:将其入栈
  4. 遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,括号不输出。
  5. 遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
  6. 最终将栈中的元素依次出栈,输出。

引用自 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;
}

SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式的更多相关文章

  1. 数据结构实验之栈与队列二:一般算术表达式转换成后缀式(SDUT 2132)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; int ok(char ch, char ...

  2. SDUT-2131_数据结构实验之栈与队列一:进制转换

    数据结构实验之栈与队列一:进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制非负整数,将其转换成对 ...

  3. 数据结构实验之栈与队列一:进制转换(SDUT 2131)

    题目链接 题解: 特判一下n==0的时候. #include <bits/stdc++.h> using namespace std; int a[1000]; int main() { ...

  4. SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...

  5. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  6. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  7. SDUT-3335_数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...

  8. SDUT-3334_数据结构实验之栈与队列七:出栈序列判定

    数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...

  9. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

随机推荐

  1. datetime模块常用函数

    import datetime import time # 当前时间戳 now = time.time() print(now) # 时间戳转换成时间元祖 now = time.localtime(n ...

  2. FFT初步代码分析和逼近曲线

    FFT:快速傅里叶变换 文章从两个方面来写,一个是FFT的基础知识,也就是将时域信号转换为频域信号,另一个是合成时域信号. 将时域信号转换为频域信号 代码来源于http://bigsec.net/b5 ...

  3. Spring 的初次见面

    简介: Spring Framework 是一个开源的企业级应用程序框架,为构建满足企业级需求的应用程序提供了大量的工具集.推出该框架的原因是在时候用J2EE进行开发是会提高复杂性. Spring三大 ...

  4. 极简bootstrap file 美化样式(无需第三方插件)

    原本的file上传表单非常的丑,但是又不想使用第三方插件,Bootstrap也没有相关的美化,于是用纯CSS完成,美化,JS实现功能,连BootStrap都不需要,十分简单 1.给原版丑表单隐藏了di ...

  5. 错觉-Info:让你难以置信的视错觉

    ylbtech-错觉-Info:让你难以置信的视错觉 1.返回顶部 1. 看下图:如果你看到舞者逆时针旋转说明你用左脑,如果看到顺时针旋转说明你用右脑思维. 据说这是耶鲁大学五年的研究成果.   下图 ...

  6. Spring_Hibernate

    Spring与Hiberante整合 通过hibernate的学习,我们知道,hibernate主要在hibernate.cfg.xml配置文件中 接下来我们看一下hibernate的一个配置文件 h ...

  7. PyCharm使用之配置SSH Interpreter

      在文章PyCharm使用之利用Docker镜像搭建Python开发环境中,该文章介绍了在PyCharm中如何利用Docker镜像搭建Python开发环境.在本文中,将会介绍如何使用PyCharm来 ...

  8. SQLSERVER 时间函数汇总

    1.求当天的年份 (getdate(): 2012/05/08 18:07:26) SELECT YEAR(GETDATE())     --2012 2. 求当天的月份       SELECT M ...

  9. 生成mysql数据字典

    data_dictionary.php <?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf- ...

  10. 从零开始写一个npm包,一键生成react组件(偷懒==提高效率)

    前言 最近写项目开发新模块的时候,每次写新模块的时候需要创建一个组件的时候(包含组件css,index.js,组件js),就只能会拷贝其他组件修改名称 ,但是写了1-2个后发现效率太低了,而且极容易出 ...