逆波兰计算器

#include<iostream>
#include<algorithm>
#include<stack> 
#include<string>
using namespace std;
int level(char ch);
int main()
{
    stack <char> s1, s2, s3, s4; //
    char str[10];
    int p1, p2;
    cin>>str;
    char ch;
    for(int i = 0;str[i]!='\0';i++)
    {

         if(str[i] >='0' && str[i]<='9')
         {
            s1.push(str[i]);
         }
        if(str[i] == '(')
        {
            s2.push(str[i]);//左括号放入运算符堆栈
        }
        else if(str[i]== ')')
        {

            while(s2.top() != '(')
            {
                ch = s2.top();
                s2.pop();
                s1.push(ch);
            }
            s2.pop();
        }
        else if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
        {
             if(s2.empty())
            {
                s2.push(str[i]);
            }
            else if(!s2.empty() && s2.top() == '(' || s2.top() == ')')
            {
                s2.push(str[i]);
            }
            else if(!s2.empty() && level(str[i]) > level(s2.top()))
            {
                s2.push(str[i]);
            }
            else if(!s2.empty() && level(str[i]) <= level(s2.top()))
            {
                ch = s2.top();
                s2.pop();
                s1.push(ch);
                s2.push(str[i]);
            }   
        }
    }
    while(!s2.empty())
    {
        ch = s2.top();
        s2.pop();
        s1.push(ch);
    }//s1 从左到又数逆波兰
    cout<<endl;
    cout<<"逆波兰从右往左看"<<endl;
    while(!s1.empty())
    {
        ch = s1.top();
        cout<<ch<<' ';
        s3.push(ch);
        s1.pop();
    }
    cout<<endl;
    /*while(!s3.empty())
    {
        cout<<s3.top()<<' ';
        s3.pop();
    }*/
    while(!s3.empty())
    {
        if(s3.top() >= '0' && s3.top() <= '9')
        {
            s4.push(s3.top());
            s3.pop();
        }
        else if(s3.top() == '+' || s3.top() == '-' || s3.top() == '*' || s3.top() == '/')
        {
            if(s3.top() == '+')
            {s3.pop();
                p1 = s4.top() - '0';
                s4.pop();
                p2 = s4.top() - '0';
                s4.pop();
                if(s3.empty())
                {
                    cout<<p1+p2<<endl;
                }
                else
                {
                    s4.push(char(p1+p2+48));
                }
            }
            else if(s3.top() == '-')
            {s3.pop();
                p1 = s4.top() - '0';
                s4.pop();
                p2 = s4.top() - '0';
                s4.pop();
                if(s3.empty())
                {
                    cout<<p2-p1<<endl;
                }
                else
                {
                    s4.push(char(p2-p1+48));
                }
            }
            else if(s3.top() == '*')
            {s3.pop();
                p1 = s4.top() - '0';
                s4.pop();
                p2 = s4.top() - '0';
                s4.pop();
                if(s3.empty())
                {
                    cout<<p1*p2<<endl;
                }
                else
                {
                    s4.push(char(p1*p2+48));
                }
            }
            else if(s3.top() == '/')
            {
                s3.pop();
                p1 = s4.top() - '0';
                s4.pop();
                p2 = s4.top() - '0';
                s4.pop();
                if(s3.empty())
                {
                    cout<<p2/p1<<endl;
                }
                else
                {
                    s4.push(char(p2/p1+48));
                }
            }

        }
    }
    return 0;
}
 int level(char ch)     //优先级判断 
 {   
      if(ch=='(')   
      return   0;   
      if(ch=='+'||ch=='-') 
      return   1;   
      if (ch=='*'||ch=='/')  
      return   2;   
 }
posted @ 2017-08-12 22:19  云胡同学  阅读(250)  评论(0编辑  收藏  举报