美文网首页
算法训练 表达式计算 (中缀表达式转后缀表达式)

算法训练 表达式计算 (中缀表达式转后缀表达式)

作者: ClockworkTree | 来源:发表于2017-09-09 16:38 被阅读0次

问题描述
  输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。
输入格式
  输入一行,包含一个表达式。
输出格式
  输出这个表达式的值。
样例输入
1-2+3*(4-5)
样例输出
-4
数据规模和约定
  表达式长度不超过100,表达式运算合法且运算过程都在int内进行。

http://lx.lanqiao.cn/problem.page?gpid=T419

#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
#include<stack>
#include<cstdio>
#include<vector>
#include<fstream>
#include<ctime>
#include<queue> 
#define ALL(x) x.begin(),x.end()
using namespace std;

string s1;
queue<string> q1;
stack<string> stk1;
stack<int> stk2;

int f2(string s)
{
    if(s=="+") return 2;
    else if(s=="-") return 2;
    else if(s=="*") return 3;
    else if(s=="/") return 3;

    
}

void f1()
{
    string temp;
    cin>>s1;
    for(int i=0;i<s1.size();++i)
    {
        temp.clear();
        if(isdigit(s1[i]))
        {
            while(i<s1.size()&&isdigit(s1[i]))
            {
                temp+=s1[i];
                ++i;
            }
            --i;
            q1.push(temp);
        }
        else
        {
            temp+=s1[i];
            if(temp=="(")
            {
                stk1.push(temp);
            }
            else if(temp==")")
            {
                while(!stk1.empty()&&stk1.top()!="(")
                {
                    q1.push(stk1.top());
                    stk1.pop();
                }
                stk1.pop();             
            }
            else if(!stk1.empty()&&f2(temp)>f2(stk1.top()))
            {
                stk1.push(temp);
            }
            else
            {
                while(!stk1.empty()&&f2(temp)<=f2(stk1.top()))
                {
                    q1.push(stk1.top());
                    stk1.pop();
                }
                stk1.push(temp);
            }
            
        }
        
    }
    while(!stk1.empty())
    {
        q1.push(stk1.top());
        stk1.pop();
    }
    return ;
}

void f3()
{
    int temp=0,x,y;
    while(!q1.empty())
    {
        temp=0;
        string s1=q1.front();
        if(s1.size()>1)
        {
            for(int j=0;j!=s1.size();++j)
            {
                temp=temp*10+(s1[j]-'0');
            }
            stk2.push(temp);
        }
        else if(isdigit(s1[0]))
        {
            temp=s1[0]-'0';
            stk2.push(temp);
        }
        else
        {
            if(s1=="+")
            {
                y=stk2.top();
                stk2.pop();
                x=stk2.top();
                stk2.pop();
                stk2.push(x+y);
            }
            else if(s1=="-")
            {
                y=stk2.top();
                stk2.pop();
                x=stk2.top();
                stk2.pop();
                stk2.push(x-y);
            }
            else if(s1=="*")
            {
                y=stk2.top();
                stk2.pop();
                x=stk2.top();
                stk2.pop();
                stk2.push(x*y);
            }
            else if(s1=="/")
            {
                y=stk2.top();
                stk2.pop();
                x=stk2.top();
                stk2.pop();
                stk2.push(x/y);
            }
        }
        q1.pop();
    }
}
int main()
{
    f1();
//  while(!q1.empty())
//  {
//      cout<<q1.front();
//      q1.pop();
//  }
//  cout<<endl;
    f3();
    cout<<stk2.top();

    return 0;
    
}

相关文章

  • 堆栈

    堆栈中常见的问题: 问题1: 后缀表达式怎么计算?问题2: 中缀表达式怎么转换成后缀表达式?问题3: 回溯算法问题...

  • day06-逆波兰表达式的计算器

    目标:完成一个逆波兰表达式的计算器(分为两个步骤)计算后缀表达式:中缀表达式转成后缀表达式: 1.计算后缀表达式:...

  • Python 简单计算器-逆波兰后缀表达式实现

    中缀表达式 后缀表达式 简易计算器,可以通过栈来实现。然而如果直接使用中缀表达式,需要处理括号,而使用后缀表达式则...

  • 深度透析逆波兰表达式

    逆波兰表达式 1、概念 标准四则运算表达式---中缀表达式 计算机采用一种计算,变成后缀表达式: 2、计算机进行转...

  • 栈的应用

    中缀表达式转换为后缀表达式 后缀表达式 做数学运算时,经常使用的是中缀表达式,即“操作数 运算符 操作数”。在计算...

  • 表达式树

    表达式树中缀表达式转换为后缀表达式后缀表达式总结

  • 逆波兰计算器

    中缀表达式转换成后缀表达式 后缀表达式的计算 逆波兰计算器 先挖坑,学年设计之后再来填坑。

  • 数据结构与算法--后缀表达式

    中缀表达式转后缀表达式 中缀表达式转后缀表达式的思路步骤分析。 初始化一个栈和一个队列,运算符栈 S1 和存储中间...

  • 中缀表达式转后缀表达式

    算法: 中缀表达式转后缀表达式的方法: 1.遇到操作数:直接输出(添加到后缀表达式中) 2.栈为空时,遇到运算符,...

  • 中缀表达式转后缀表达式并求值

    1.什么是中缀表达式?中缀表达式示例 2.什么是后缀表达式?后缀表达式示例 3.代码

网友评论

      本文标题: 算法训练 表达式计算 (中缀表达式转后缀表达式)

      本文链接:https://www.haomeiwen.com/subject/jazfnttx.html