【软件18-栈的应用】

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
using namespace std;
stack<char> sign;
char s[22];
map<char, int> mp;
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    mp['+'] = mp['-'] = 1;
    mp['*'] = mp['/'] = 2;
    mp['('] = mp[')'] = 3;
    scanf("%s", s);
    int len = strlen(s);
    bool isfirst = true;
    for(int i = 0; i < len; i++)
    {
        if((!i || (i && s[i-1] == '(')) && (s[i] == '+' || s[i] == '-'))
        {
            if(!isfirst)
                printf(" ");
            if(s[i] != '+')
                printf("%c",s[i]);
            while(i + 1 < len && (s[i+1] == '.' || (s[i+1] >= '0' && s[i+1] <= '9')))
            {
                ++i;
                printf("%c", s[i]);
            }
            if(isfirst)
                isfirst = false;
        }
        else if(s[i] >= '0' && s[i] <= '9')
        {
            if(!isfirst)
                printf(" ");
            printf("%c", s[i]);
            while(i + 1 < len && (s[i+1] == '.' || (s[i+1] >= '0' && s[i+1] <= '9')))
            {
                ++i;
                printf("%c", s[i]);
            }
            if(isfirst)
                isfirst = false;
        }
        else if(s[i] == ')')
        {
            while(!sign.empty() && sign.top() != '(')
            {
                printf(" %c", sign.top());
                sign.pop();   
            }
            if(!sign.empty() && sign.top() == '(')
                sign.pop();
        }
        else if(sign.empty() || (mp[s[i]] > mp[sign.top()]))
            sign.push(s[i]);
        else 
        {
            while(!sign.empty() && sign.top() != '(')
            {
                printf(" %c", sign.top());
                sign.pop();
            }
            sign.push(s[i]);
        }
    }
    while(!sign.empty())
    {
        printf(" %c", sign.top());
        sign.pop();
    }
} 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1000000;
char s[maxn];
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    stack<char> p;
    int flag = 0;
    while(scanf("%s", s) != EOF)
    {
        int len = strlen(s);
        if(len == 1 && s[0] == '.')
            break;
        for(int i = 0; i < len; i++)
        {
            if(s[i] == '(' || s[i] == '[' || s[i] == '{')
                p.push(s[i]);
            else if(i + 1 < len && s[i] == '/' && s[i+1] == '*')
            {
                p.push('<');
                ++i;
            }
            else if(s[i] == ')' || s[i] == ']' || s[i] == '}')
            {
                char ss;
                if(s[i] == ')') ss = '(';
                else if(s[i] == ']') ss = '[';
                else if(s[i] == '}') ss = '{';
                if(p.size())
                {
                    char top = p.top();
                    if(top != ss && !flag)
                    {
                        flag = 1;
                        printf("NO\n");
                        if(top == '<')
                            printf("/*-?\n");
                        else 
                            printf("%c-?\n", top);
                    }
                    else 
                        p.pop();
                }
                else if(!flag)
                {
                    flag = 1;
                    printf("NO\n");
                    printf("?-%c\n", s[i]);
                }
            }
            else if(i + 1 < len && s[i] == '*' && s[i+1] == '/')
            {
                ++i;
                char ss = '<';
                if(p.size())
                {
                    char top = p.top();
                    if(top != ss && !flag)
                    {
                        flag = 1;
                        printf("NO\n");
                        printf("%c-?\n", top);
                    }
                    else 
                        p.pop();
                }
                else if(!flag)
                {
                    flag = 1;
                    printf("NO\n");
                    printf("?-*/\n");
                }
            }
            
        }
    }
    
    if(!flag)
    {
        if(p.empty())
        {
            printf("YES\n");
        }
        else 
        {
            char top = p.top();
            p.pop();
            flag = 1;
            printf("NO\n");
            if(top == '(' || top == '[' || top == '{')
                printf("%c-?\n", top);
            else 
                printf("/*-?\n");
        }
    }
    while(!p.empty())
        p.pop();
        
} 
posted @ 2019-09-21 16:12  DIY-Z  阅读(106)  评论(0编辑  收藏  举报