NYOJ_35 表达式求值

来源:互联网 发布:高中英语语法视频 知乎 编辑:程序博客网 时间:2024/06/05 08:17
描述
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


分析: 先将前缀表达式换成后缀表达式(http://blog.csdn.net/s031302306/article/details/51867937),由后缀表达式求值是非常简单的,从前往后进行扫描,碰到数字,就进栈,碰到运算符,就从栈中弹出两个数字,进行运算,将运算结果压栈。


代码:

#include<cstdlib>
#include<string.h>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
char ss[1009],post[1009];
int post1;
int cmp(char c)
{
    switch(c)
    {
        case '+':
        case '-': return 1;
        case '*':
        case '/': return 2;        
        default : return -1;        
    }
}

void fun()
{
    int len = strlen(ss);
    stack<char> sta;
    
    for(int i=0;i<len;)
    {
        if(isdigit(ss[i]))
        {
            
            while( i<len && (isdigit(ss[i]) || ss[i]=='.') )
            {
                post[post1++]=ss[i++];
            }
            post[post1++]=' ';
            
        }
        else if(ss[i]=='=')
        {
            i++;            
        }
        else
        {
            if(ss[i]==')')             //碰到一个右括号,直接弹到与它匹配的左括号结束
            {
                while(sta.top()!='(')
                {
                    char temp = sta.top();
                    //cout<<temp<<' ';
                    post[post1++]=temp;
                    post[post1++]=' ';
                    sta.pop();                    
                }
                sta.pop();       //将左括号从栈中弹出    
                i++;            
            }
            else if(ss[i]=='(')
            {
                sta.push(ss[i]);
                i++;                
            }
            
            else if(sta.empty() || cmp(ss[i]) > cmp(sta.top()) )
            {
                sta.push(ss[i]);
                i++;
            }
            else
            {
                while(!sta.empty() && cmp( sta.top() ) >= cmp(ss[i]) )
                {
                    char temp = sta.top();
                    sta.pop();
                    //cout<<temp<<' ';
                    post[post1++]=temp;
                    post[post1++]=' ';                
                }
                sta.push(ss[i]);
                i++;                
            }            
        }        
    }
    while(!sta.empty())           //将剩余的符号全部弹出栈
    {
        char temp = sta.top();
        sta.pop();        
//        cout<<temp<<' ';    
        post[post1++]=temp;    
        post[post1++]=' ';
    }        
}

double calc(double a, double b,char cc)
{
    if(cc=='+')   return a+b;
    if(cc=='-')   return a-b;
    if(cc=='*')   return a*b;
    return a/b;    
}

void fun2()
{
    stack<double> sta;
    for(int i=0;i<post1;)
    {
        if(isdigit(post[i]))
        {
            double val;
            int n;
            sscanf(post+i,"%lf%n",&val,&n);
            i+=n;
            sta.push(val);
        //    cout<<val<<endl;            
        }
        else if(post[i]=='+'  || post[i]=='-' || post[i]=='*' || post[i]== '/')
        {
            double val1,val2;
            val1=sta.top();
            sta.pop();
            val2=sta.top();
            sta.pop();
            val1 = calc(val2,val1,post[i]);
            sta.push(val1);    
            i++;    
        //    cout<<val1<<endl;        
        }    
        else
        {
            i++;
        }    
    }    
    printf("%.2lf\n",sta.top());
}


int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%s",ss);
        post1=0;
        fun();
//        for(int i=0;i<post1;i++)
//        {
//            cout<<post[i];
//        }
        fun2();
        
    }    
    return 0;
}



0 0
原创粉丝点击