表达式求值(数据结构书上栈的应用之一)

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

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

我的代码如下(比较简洁):


#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]='\0';
               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\n",ly.top());
   }
   return 0;
}

/*
2
((-2+3)*1.2+2)=
((-2+3)*10/2)=
*/

后来ac了看了别人用书上的方法进行分装,但是觉得太麻烦了,一直不知道到底哪种方法好。。。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值