HDU 2646 栈的应用 STL

Expression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 202    Accepted Submission(s): 61

Problem Description

As a shopkeeper of a restaurant,everyday ,dandelion‘s mother needs to calculate the incomes.Sometimes,she may make some mistakes.So dandelion wants you to write a program to help her calculate the value of some expressions.   You may consider every expression is made of :left parenthesis ‘(‘ ,right parenthesis ‘)‘ , plus sign ‘+‘ , subtraction sign ‘-‘ , multiplication sign ‘*‘ ,positive sign ‘+‘ ,and negative sign ‘-‘.There is no precursor 0.The length of expression will not exceed 100.The value produced during the calculating will not exceed 10^9.Every expression is legal.Ie. ((10+(-100))) ,-(-1),-3*(+5+7*2)-(0) ,-0 ,(-3)*(-5+7) are legal,while 1+-7 ,--3+8 ,-3+() are illegal.

Input

There are several cases,every line contains a expression.

Output

For every case ,print the answer of the expression.

Sample Input

-3*(+5-7*2)-(0)

Sample Output

27

Author

dandelion

Source

曾是惊鸿照影来

栈的运用 考虑的比较多 使用  STL <stack>

详细的看注释

#include<bits/stdc++.h>
using namespace std;
char a[105];
stack<char> m;//运算符栈
stack<int> n;//操作室栈
map<char,int> mp;
//40  (
//41  )
//42  *
//43  +
//45  -
int main()
{
    mp[‘-‘]=1;
    mp[‘+‘]=1;
    mp[‘*‘]=2;
    mp[‘(‘]=-1;
    mp[‘)‘]=-1;
    memset(a,0,sizeof(a));
    while(gets(a))
    {

        while(!m.empty())
            m.pop();
        while(!n.empty())
            n.pop();
        n.push(0);//考虑初始有符号 放在栈底
        int len=strlen(a);
        int exm=0;
        int xx,yy;
        char what;
        for(int i=0; i<len; i++)
        {
            if(a[i]>=48&&a[i]<=57)
                exm=exm*10+a[i]-‘0‘;
            else
            {
                if(a[i]==‘(‘)// 前括号处理 添0 处理紧邻的符号
                {
                    m.push(a[i]);
                    n.push(0);
                    continue;
                }
                if(m.empty())//若运算符栈为空
                {
                    m.push(a[i]);
                    continue;
                }
                else
                {
                    if(mp[a[i]]>mp[m.top()]) //优先级大于栈顶运算符
                        m.push(a[i]);
                    else
                    {//直到优先级大于栈顶 或 栈空 或栈顶为后括号(这个没有验证)
                   while(!m.empty()&&mp[a[i]]<=mp[m.top()]&&m.top()!=‘(‘)// 这里理解
                        {
                            xx=n.top();
                            n.pop();
                            yy=n.top();
                            n.pop();
                            what=m.top();
                            m.pop();
                            if(mp[what]==1)
                            {
                                if(what==‘+‘)
                                    n.push(yy+xx);
                                if(what==‘-‘)
                                    n.push(yy-xx);
                            }
                            if(mp[what]==2)
                                n.push(yy*xx);
                        }
                        if(!m.empty()&&m.top()==‘(‘&&a[i]==‘)‘) //当前后括号相遇pop
                            m.pop();
                        else          //否则插入
                            m.push(a[i]);

                    }
                    continue;
                }
            }
            if(i>=1)//处理前括号后若无符号
            {
                if(a[i-1]==‘(‘)
                    n.pop();
            }
            if(mp[a[i+1]]!=0)//判断exm 积累结束
            {
                n.push(exm);
                exm=0;
            }
            if(i==len-1&&a[i]!=‘)‘)//考虑最后一个操作数
                n.push(exm);

        }
        while(!m.empty())//直到 运算符栈空
        {
            xx=n.top();
            n.pop();
            yy=n.top();
            n.pop();
            what=m.top();
            m.pop();
            if(mp[what]==1)
            {
                if(what==‘+‘)
                    n.push(yy+xx);
                if(what==‘-‘)
                    n.push(yy-xx);
            }
            if(mp[what]==2)
                n.push(yy*xx);
        }
        printf("%d\n",n.top());//输出栈顶值
        memset(a,0,sizeof(a));
    }
    return 0;
}
时间: 2024-11-07 16:17:36

HDU 2646 栈的应用 STL的相关文章

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

HDU 2072 单词数 --- stringstream+STL

/* HDU 2072 单词数 --- stringstream+STL */ #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <set> using namespace std; set<string> k; int main() { string s; while (getline(cin, s) &&a

poj 1363 Rails (栈的应用+STL)

Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24762   Accepted: 9715 Description There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds we

hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场

Magical Forest                                                                       Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is a forest can be seen as N * M grid. In this fore

hdu 1022 Train Problem I(栈的应用+STL)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20521    Accepted Submission(s): 7712 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDU 1022 Train Problem I (STL 栈模拟)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30420    Accepted Submission(s): 11492 Problem Description As the new term comes, the Ignatius Train Station is very busy nowaday

hdu 2072 单词数(STL set写法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 思路简单,但是注意一个细节就是最后可能是空格结束的,就是这儿让我WA啦好多次呀... code: #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<st

HDU 2072.单词数【STL的优势以及字符串流的使用】【8月4】

单词数 Problem Description lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数. Sample Input you are my friend # Sample

UVa - 12096 集合栈计算机(STL)

[题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两者的并集入栈,并输出并集的sizeINTERSECT:出栈两个集合,然后把二者的交集入栈,并输出交集的sizeADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈,并输出结果的size       每次操作后,输出栈顶集合的大小(即元素个数).例如栈顶元素是A={ {}, {{}