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

think:1.压栈、出栈、栈空、栈满基本操作
hope:1.进行C++的学习

数据结构实验之栈二:一般算术表达式转换成后缀式
Time Limit: 1000MS Memory Limit: 65536KB

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

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

Output
输出该表达式转换所得到的后缀式。
Example Input
a*b+(c-d/e)*f#

Example Output
ab*cde/-f*+

Hint

Author

以下为accepted代码

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


/***************************************************
User name: jk160630
Result: Accepted
Take time: 0ms
Take Memory: 120KB
Submit time: 2017-02-01 21:44:10
****************************************************/

以下为参考的博客的参考代码,衷心感谢作者的启迪,谢谢

    #include <stdio.h>  
    #include <string.h>  
    #include <stdlib.h>  
    #include <iostream>  
    #include <algorithm>  
    #include <stack>  
    using namespace std;  
    int main()  
    {  
        stack<int >q;  
        char str[110];  
        int i;  
        scanf("%s",str);  
        for(i=0;str[i]!='#';i++)  
        {  
            if(str[i]>='a'&&str[i]<='z')//当遇到字母的时候直接输出;  
                printf("%c",str[i]);  
            else if(str[i]=='(')//当碰到左括号直接压进栈;  
                q.push(str[i]);  
            else if(str[i]==')')//当遇到右括号的时候;  
            {  
                while(q.top()!='(')//如果栈顶不是左括号,就证明括号里面的数值没有输完,就一直输出  
                {  
                    printf("%c",q.top());  
                    q.pop();  
                }  
                q.pop();//把左括号直接删除;  
            }  
            else if(str[i]=='+'||str[i]=='-')  
            {  
                while(!q.empty()&&q.top()!='(')  
                {  
                    printf("%c",q.top());  
                    q.pop();  
                }  
                q.push(str[i]);  
            }  
            else if(str[i]=='*'||str[i]=='/')  
            {  
                while(!q.empty()&& q.top()!= '('&&(q.top()== '*'||q.top() == '/'))  
                {  
                    printf("%c",q.top());  
                    q.pop();  
                }  
                q.push(str[i]);  
            }  
        }  
        while(!q.empty())//当结束的时候如果栈不为空,证明栈里还有积压的数,此时输出;  
        {  
            printf("%c",q.top());  
            q.pop();  
        }  
        cout<<endl;  
        return 0;  
    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值