nyist0j 35 表达式求值

题目链接:表达式求值

该题以前做过但是WA了,今天终于把他解决了,各种悲剧啊,又是考虑不周到啊。。。。。。。。。。。。。。。。。。。

所以贴出来纪念一下,并作为一个警示

/****
ps:注意当遇到 )时处理到遇到(之前
当一个符号进栈,判断之前的符号是否可以出栈,知道没有可出栈的
*/

#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

bool judge_fuhao(char ch,char ch1)//ch1为将进栈
{
    //printf("fuhao---->%c  %c\n",ch,ch1);
    if(ch == '(' )
        return true;
    else if(ch1 == '+' ||ch1 == '-')
    {
        return false;
    }
    else if(ch1 == '*' || ch1 == '/')
    {
        if(ch == '+' || ch == '-' )
            return true;
        else
        return false;
    }
}

double judge_mtah(char ch,double y,double x)//运算
{
    //printf("%c %lf %lf\n",ch,x,y);
    switch(ch)
    {
        case '+':
                return x + y; break;
        case '-':
                return x - y; break;
        case '/':
                return 1.0*x / y; break;
        case '*':
                return x * y; break;
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    char ch[1003];
    char stack_ch[1003];
    double stack_a[1003];
    int stack_a_t = 0;
    int stack_ch_t = 0;

    while(T--)
    {
        //freopen("2.txt","w",stdout);
        scanf("%s",ch);
        stack_a_t = 0;
        stack_ch_t = 0;
        int len = strlen(ch);
        char ans[1005];
        int ans_t = 0;
        double tmp = 0;
        int i;
        double x,y,z;
        for(i = 0; i < len; i++)
        {
            if(ch[i] == '+'|| ch[i]=='-'||ch[i]=='*'||ch[i]=='/'||ch[i]=='('||ch[i]=='=')
               {
                   if(ans_t != 0)
                    {
                        tmp = atof(ans);
                        ans_t = 0;
                        ans[ans_t] = '\0';
                        stack_a[stack_a_t++] = tmp;
                    }
                     if(ch[i] != '=')
                      {
                    if(stack_ch_t == 0 || ch[i] == '(')
                            {
                                stack_ch[stack_ch_t++] = ch[i];
                            }
                    else
                      while(1)//+-*/进来
                        {
                            if(stack_ch_t-1 < 0 || judge_fuhao(stack_ch[stack_ch_t-1],ch[i])== true)
                                {
                                    stack_ch[stack_ch_t++] = ch[i];
                                    break;
                                }
                            else
                            {
                                x = stack_a[stack_a_t-1];
                                stack_a_t--;
                                y = stack_a[stack_a_t-1];
                                stack_a_t--;
                                z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
                                stack_ch_t--;
                                stack_a[stack_a_t++] = z;
                            }
                        }
                      }
                      else if(ch[i] == '='){
                      {
                          if(stack_ch_t > 1 && judge_fuhao(stack_ch[stack_ch_t-2],stack_ch[stack_ch_t-1]) == true )
                          {
                              x = stack_a[stack_a_t-1];
                              stack_a_t--;
                              y = stack_a[stack_a_t-1];
                              stack_a_t--;
                              z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
                              stack_ch_t--;
                              stack_a[stack_a_t++] = z;

                          }
                      }
                      }
               }
            else if(ch[i] == ')')
                {
                    if(ans_t != 0)
                    {
                        tmp = atof(ans);
                        ans_t = 0;
                        ans[ans_t] = '\0';
                        stack_a[stack_a_t++] = tmp;
                    }

                    while(1)
                    {
                        if(stack_ch[stack_ch_t-1] == '(')
                           {
                               stack_ch_t--;
                               break;
                           }
                        else
                        {
                            x = stack_a[stack_a_t-1];
                            stack_a_t--;
                            y = stack_a[stack_a_t-1];
                            stack_a_t--;
                            z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
                            stack_a[stack_a_t++] = z;
                            stack_ch_t--;
                        }
                    }
                }
            else
            {
                ans[ans_t++] = ch[i];
                ans[ans_t] = '\0';
            }
        }
        for(i = stack_ch_t-1; i >= 0; i--)
        {
            x = stack_a[stack_a_t-1];
            stack_a_t--;
            y = stack_a[stack_a_t-1];
            stack_a_t--;
            z = judge_mtah(stack_ch[i],x,y);
            stack_a[stack_a_t++] = z;
        }
        printf("%0.2lf\n",stack_a[0]);
    }
    return 0;
}

 

posted @ 2013-07-24 10:20  heity  阅读(4935)  评论(0编辑  收藏  举报