栈的运用之表达式求值

NYOJ 35

 

表达式求值

时间限制:3000 ms  |  内存限制:65535 KB

难度:4

 

描述

ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧。
比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)

 

输入
第一行输入一个整数n,共有n组测试数据(n<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。
数据保证除数不会为0
输出
每组都输出该组运算式的运算结果,输出结果保留两位小数。
样例输入

21.000+2/4=((1+2)*5+1)/4=

样例输出

1.504.00

分析:

表达式求值首先考虑优先级问题

1.大家知道要建两个栈,一个存运算符,一个存数字;

2.把数据存入分别存入两个栈中

3.开始遍历遇到( 直接存到字符 栈,遇到数字存到数字栈,遇到 + -,让它与字符栈 定 top进行比较,如果比栈定优先级高,则将它存到字符栈;低则 取 栈 字符 和 数字栈两个元素进行计算,并删除字符栈的栈顶元素和去除的两个数字栈元素。遇到 ) 则观察字符栈,在取到(之前,都要进行运算,从上面的规律我们可以知道,其实在遇到(之前,我们去除的字符都是同一优先级的,所以直观 取数计算即可,最后一个是等于=,看看字符栈清空没有

 

下面是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stack>
#include <math.h>
#define Maxsize 1100
using namespace std;
stack<char> osta;
stack<double> dsta;
char ch[Maxsize];
int main()
{
 int n;
 scanf("%d",&n);
 while(n--)
 {
  scanf("%s",&ch[1]);
  int len=strlen(&ch[1]);
  ch[0]='(';ch[len]=')';
  for(int i=0;i<=len;i++)
  {
   if(ch[i]=='(')
   {
    osta.push(ch[i]);
   }
   else if(ch[i]==')')
   {
    while(osta.top()!='(')
    {
     double a=dsta.top();dsta.pop();
     double b=dsta.top();dsta.pop();
     double c=0;
     switch(osta.top())
     {
      case '+':c=b+a;break;
      case '-':c=b-a;break;
      case '*':c=b*a;break;
      case '/':c=b/a;break;
     }
     //printf("b:%.2lf a:%.2lf c:%.2lf\n",b,a,c);
     dsta.push(c);
     osta.pop();
    }
    osta.pop();
   }
   else if(ch[i]=='+'||ch[i]=='-')
   {
    while(osta.top()!='(')
    {
     double a=dsta.top();dsta.pop();
     double b=dsta.top();dsta.pop();
     double c=0;
     switch(osta.top())
     {
      case '+':c=b+a;break;
      case '-':c=b-a;break;
      case '*':c=b*a;break;
      case '/':c=b/a;break;
     }
     //printf("b:%.2lf a:%.2lf c:%.2lf\n",b,a,c);
     dsta.push(c);
     osta.pop();
    }
    osta.push(ch[i]);
   }
   else if(ch[i]=='*'||ch[i]=='/')
   {
    if(osta.top()=='*')
    {
     double a=dsta.top();dsta.pop();
     double b=dsta.top();dsta.pop();
     //printf("b:%.2lf a:%.2lf c:%.2lf\n",b,a,b*2);
     dsta.push(b*a);
     osta.pop();
    }
    else if(osta.top()=='/')
    {
     double a=dsta.top();dsta.pop();
     double b=dsta.top();dsta.pop();
     //printf("b:%.2lf a:%.2lf c:%.2lf\n",b,a,b/a);
     dsta.push(b/a);
     osta.pop();
    }
    osta.push(ch[i]);
   }
   else if(ch[i]<='9'&&ch[i]>='0')
   {
    double v=0;
    int b=i;
    bool flag=false;
    while(ch[i]<='9'&&ch[i]>='0'||ch[i]=='.')
    {
     if(ch[i]=='.')
      {b=i;flag=true;}
     else v=v*10+ch[i]-'0';
     ++i;
    }
    --i;
    if(flag==true)
    {

     dsta.push(v/pow(10,i-b));
     //printf("%.2lf\n",dsta.top());
    }
    else
    {
     dsta.push(v);
     //printf("%.2lf\n",dsta.top());
    }
   }
  }
  printf("%.2lf\n",dsta.top());
  dsta.pop();
 }
 return 0;
}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值