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

来源:互联网 发布:象过河软件怎么使用 编辑:程序博客网 时间:2024/06/04 01:17
数据结构实验之栈二:一般算术表达式转换成后缀式

Time Limit: 1000MS Memory limit: 65536K

题目描述

对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。

输入

输入一个算术表达式,以‘#’字符作为结束标志。

输出

输出该表达式转换所得到的后缀式。

示例输入

a*b+(c-d/e)*f#

示例输出

ab*cde/-f*+
#include <stdio.h>#include <string.h>char s[1100];int top;void push(char n);int pop();int empty();int main(){    int l,i;    char c;    top=-1;    while(~scanf("%c",&c))    {        if(c!='*'&&c!='+'&&c!='-'&&c!='/'&&c!='('&&c!=')'&&c!='#')            printf("%c",c);        if(c=='(')            push(c);        if(c==')')        {            while(s[top]!='(')                if(!empty())                    printf("%c", pop());            pop();        }        if(c=='*'||c=='/')            push(c);        if(c=='+'||c=='-')        {            while(s[top]!='('&&s[top]!=')'&&!empty())            {                printf("%c",pop());            }            push(c);        }        if(c=='#')        {            while(!empty())                printf("%c",pop());            break;        }    }    printf("\n");}void push(char n){    s[++top]=n;}int pop(){    return s[top--];}int empty(){    return top==-1;}

0 0