您的位置:首页 > 理论基础 > 数据结构算法

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

2015-06-02 21:31 453 查看

题目描述

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

输入

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

输出

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

示例输入

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


示例输出

ab*cde/-f*+


最近在看栈的内容,但对于这道题,没有想法,看到网上的题解,才明白怎么回事,思路就是,建立一个栈,是数字就输出,是运算符的话,要先比较现在栈顶的运算符和我当前的优先级,现在的高,进栈,原栈顶的高,让他出栈,让现在的进栈,如果遇见“(”的话,进栈,在遇见“)”之前的运算符都进栈,当遇见“)”,把“(”的运算符号都出栈,最后把栈中所有符号输出,栈空

[code]#include<stdio.h>
#include<string.h>

char cmp(char a)
{
    if(a == '-' || a == '+') return 1;
    if(a == '*' || a == '/') return 2;
    if(a == ')') return 3;
    if(a == '(') return 4;
}

int main()
{
    char a[1000], ls[1000];
    while(~scanf("%s",a))
    {
        int t = 0, i = 0;
        for(i = 0; a[i] != '#';i++)
        {
            if(a[i] >= 'a' && a[i] <= 'z') printf("%c",a[i]);
            else
            {
                if(t == 0)
                {
                    ls[t] = a[i];
                    t++;
                }
                else
                {
                    if(cmp(ls[t-1]) >= cmp(a[i]))
                     {
                         if(ls[t-1] != '(')
                        {
                            printf("%c",ls[t-1]);
                            ls[t-1] = a[i];
                        }
                        else
                        {
                            ls[t++] = a[i];
                        }
                     }
                    else
                    {
                        if(a[i] == ')')
                        {
                            int n = t- 1;
                            for(; ls
 != '(';n--)
                            {
                                printf("%c",ls
);
                            }
                            t = n;
                        }
                        else
                        {
                            ls[t++] = a[i];
                        }
                    }
                }
            }
        }
        while(t--)
        {
            printf("%c",ls[t]);
        }
        printf("\n");
    }
    return 0;
}

<pre name="code" class="cpp">#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char cmp(char a)
{
    if(a == '-' || a == '+') return 1;
    if(a == '*' || a == '/') return 2;
    if(a == ')') return 3;
    if(a == '(') return 4;
}
typedef struct
{
   char * base;
    char  *top;
}SqStack;
char a[1000];
void InitStack(SqStack &S)
{
    S.base = (char *)malloc(1000*sizeof(char));
    S.top = S.base;
}
void Push(SqStack &S, char  e)
{
    *S.top++  = e;
}
void Out(SqStack &S)
{
    char e;
    e = *--S.top;
    cout<<e;
}
int main()
{
    while(~scanf("%s",a))
    {
        int i;
        SqStack S;
        InitStack(S);
        for(i = 0; a[i]!='#'; i++)
        {
            if(a[i] >= 'a' && a[i] <= 'z')
                cout<<a[i];
            else
            {
                if(S.base == S.top)
                {
                    Push(S,a[i]);
                }
                else
                {
                    if(cmp(*(S.top-1)) >= cmp(a[i]))
                    {
                        if(*(S.top-1) != '(')
                        {
                              Out(S);
                        Push(S,a[i]);
                        }
                       else
                       {
                             Push(S,a[i]);
                       }
                    }
                    else
                    {
                       if(a[i]== ')')
                       {
                           while(*(S.top-1) != '(')
                           {
                               Out(S);
                           }
                           S.top--;

                       }
                       else
                       {
                           Push(S,a[i]);
                       }
                    }
                }

            }
        }
        while(S.top != S.base)
        {
            Out(S);
        }
        cout<<endl;
    }
    return 0;
}

[/code]


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: