zoukankan      html  css  js  c++  java
  • 表达式计算

    表达式计算模板:

      包含+,-,*,/,(),^ 处理,但没有高精度。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<stack>
    #include<cctype>
    #include<cstdlib>
    using namespace std;
    const int N=1000;
    char _stack[N];//运算符的栈
    int top;//初始化为-1
    int _pow(int x,int y)
    {
        if(y==0) return 1;
        if(y==1) return x;
        int cnt;
        cnt=_pow(x,y/2);
        cnt=cnt*cnt;
        if(y&1) cnt*=x;
        return cnt;
    }
    
    void _change(string &str,string &res)//第一个参数为中缀表达式,第二个为后缀表达式,初始为空
    {
        top=-1;
        int len=str.size();
        for(int i=0;i<len;i++)
        {
            switch(str[i])
            {
                case '(':_stack[++top]='(';break;
                
                case '+':
                case '-':while(top>=0&&_stack[top]!='(')
                               res+=_stack[top--];
                          _stack[++top]=' ';//空格很重要 分隔数字的
                         _stack[++top]=str[i];
                         break;
                         
                case '*':
                case '/':while(top>=0&&_stack[top]!='('&&_stack[top]!='+'&&_stack[top]!='-')
                               res+=_stack[top--];
                         _stack[++top]=' ';
                         _stack[++top]=str[i];
                         break;
    
                case '^':while(top>=0&&_stack[top]!='('&&_stack[top]!='+'&&_stack[top]!='-'&&_stack[top]!='*'&&_stack[top]!='/')
                               res+=_stack[top--];
                         _stack[++top]=' ';
                         _stack[++top]=str[i];
                         break;
    
                case ')':while(_stack[top]!='(')
                               res+=_stack[top--];
                         top--;
                         break;
                default:res+=str[i];
                        if(i==len-1) res+=' ';
                        else if(!isdigit(str[i+1])) res+=' ';
            }
        }
        while(top>=0)
        {
            res+=_stack[top--];
        }
    }
    int _value(string str)//参数为后缀表达式
    {
        int cnt,len=str.size();
        top=-1;
        int _digit[N];
        for(int i=0;i<len;i++)
        {
            switch(str[i])
            {
            case ' ':break;//空格很重要 分隔数字的
            case '+':cnt=_digit[top-1]+_digit[top];
                     _digit[--top]=cnt;
                     break;
            case '-':cnt=_digit[top-1]-_digit[top];
                     _digit[--top]=cnt;
                     break;
            case '*':cnt=_digit[top-1]*_digit[top];
                     _digit[--top]=cnt;
                     break;
            case '/':cnt=_digit[top-1]/_digit[top];//此题为整除,因题而异
                     _digit[--top]=cnt;
                     break;
            case '^':cnt=_pow(_digit[top-1],_digit[top]);
                     _digit[--top]=cnt;
                     break;
            default:string temp;
                    while(i<len&&isdigit(str[i])) temp+=str[i],i++;i--;
                    _digit[++top]=atoi(temp.c_str());
            }
        }
        return _digit[0];
    }
    
    int main()
    {
        string str,res;
        while(cin>>str)
        {
            res.clear();
            _change(str,res);
            int cnt=_value(res);
            printf("%d
    ",cnt);
        }
        return 0;
    }
    /*
    1*2+3*4+5+6*7
    */
    

      

  • 相关阅读:
    使用Python的Mock库进行PySpark单元测试
    库龄报表的相关知识
    使用PlanViz进行ABAP CDS性能分析
    Spark SQL中列转行(UNPIVOT)的两种方法
    Spark中的一些概念
    使用Visual Studio Code进行ABAP开发
    2019年的几个目标
    Dom--样式操作
    Dom选择器--内容文本操作
    Javascript面向
  • 原文地址:https://www.cnblogs.com/forgot93/p/4385281.html
Copyright © 2011-2022 走看看