zoukankan      html  css  js  c++  java
  • 表达式求值(数据结构书上栈的应用之中的一个)

    主要内容:表达式求值。提交nyoj通过。。。

    思路:主要就是一个开两个栈,然后一个操作符栈。一个操作数栈。

    我的代码例如以下(比較简洁):

    /*****
           Author Gery
    ******/
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<vector>
    #include<cmath>
    #include<string>
    #include<stack>
    #include<queue>
    #define eps 1e-9
    #define ll long long
    #define INF 0x3f3f3f3f
    using namespace std;
    
    const int maxn=1000+10;
    
    stack<double>ly;
    stack<char>gery;
    char str[maxn],op[maxn];
    
    char operation[7][7]//运算符的优先级
    {
        {'>','>','<','<','<','>','>'},//'+'
        {'>','>','<','<','<','>','>'},//'-'
        {'>','>','>','>','<','>','>'},//'*'
        {'>','>','>','>','<','>','>'},//'/'
        {'<','<','<','<','<','=',','},//'('
        {'>','>','>','>',',','>','>'},//')'
        {'<','<','<','<','<',',','='},//'='
    };
    
    int get_index(char ch)
    {
        switch(ch)
        {
            case '+':return 0;
            case '-':return 1;
            case '*':return 2;
            case '/':return 3;
            case '(':return 4;
            case ')':return 5;
            case '=':return 6;
        }
    }
    
    char get_prio(char a,char b)
    {
        int c=get_index(a);
        int d=get_index(b);
        return operation[c][d];
    }
    
    double cal_value(double a,double b,char c)
    {
        switch(c)
        {
            case '+':return a+b;
            case '-':return a-b;
            case '*':return a*b;
            case '/':return a*1.0/b;
        }
    }
    
    int main()
    {
       int t,pd,cnt,i;
       double temp,left_value,right_value,val;
       char ch,Op;
       scanf("%d",&t);
       while(t--)
       {
           scanf("%s",str);
           ly.empty();
           gery.empty();
           gery.push('=');
           for(i=0;i<strlen(str);i++)
           {
               cnt=0,pd=i;//pd为字符指针
               if(isdigit(str[i])||str[i]=='.'||str[i]=='-')//拼数过程
               {
                   while(isdigit(str[pd])||str[pd]=='.'||str[pd]=='-')
                   {
                       op[cnt]=str[pd];
                       cnt++,pd++;
                   }
                   op[cnt]='';
                   temp=atof(op);//系统函数是Ascii to float的缩写。。相似的还有atoi,是把字符串转换成float型的函数
                   ly.push(temp);
                   i=pd-1;
               }
               else
               {
                    ch=get_prio(gery.top(),str[i]);
                    switch(ch)
                    {
                        case '<':gery.push(str[i]);break;
                        case '=':gery.pop();break;
                        case '>':
                             Op=gery.top(),gery.pop();
                             right_value=ly.top(),ly.pop();
                             left_value=ly.top(),ly.pop();
                             val=cal_value(left_value,right_value,Op);
                             ly.push(val);
                             i--;break;//表达式运算后字符指针要后移
                    }
               }
           }
           printf("%.2lf
    ",ly.top());
       }
       return 0;
    }
    
    /*
    2
    ((-2+3)*1.2+2)=
    ((-2+3)*10/2)=
    */
    

    后来ac了看了别人用书上的方法进行分装,可是认为太麻烦了。一直不知道究竟哪种方法好。。。



  • 相关阅读:
    Java:抽象类与接口
    OOP编程思想:类的设计原则
    Win10系统下设置Go环境变量和go语言开启go module
    Windows下Golang安装Iris框架
    AOS.JS 和基于Animation.css的收费库WOW.JS相似
    文本比价工具
    MySQL Order By Rand()效率
    datatable分页
    PHP面向对象之魔术方法
    PHP面向对象之序列化与反序列化
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6829045.html
Copyright © 2011-2022 走看看