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

来源:互联网 发布:protel手机版软件 编辑:程序博客网 时间:2024/05/30 02:26

Problem Description

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

Input

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

Output

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

Example Input

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

Example Output

ab*cde/-f*+

Hint

#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){    int i,j,n,top=0;    char g[10000],s;    while(scanf("%c",&s)&&s!='#')    {        if(s>='a'&&s<='z') printf("%c",s);        else if(s==')')        {            while(g[top]!='(')            {                printf("%c",g[top--]);            }            top--;        }        else if(s=='(')            g[++top]=s;        else if(s=='+'||s=='-')        {            while(top>0&&g[top]!='(')            {                printf("%c",g[top--]);            }            g[++top]=s;        }        else if(s=='*'||s=='/')        {            while(top>0&&(g[top]=='*'||g[top]=='/'))            {                printf("%c",g[top--]);            }            g[++top]=s;        }    }    for(i=top;i>0;i--)    printf("%c",g[i]);    return 0;}


0 0
原创粉丝点击