首页 > ACM题库 > HDU-杭电 > HDU 3387-Calculator-模拟-[解题报告]HOJ
2014
03-23

HDU 3387-Calculator-模拟-[解题报告]HOJ

Calculator

问题描述 :

A simple 8-digit electronic calculator contains the following buttons: “1234567890+-*/=” (no quotes). It can be divided into the following categories:
Digit: “1234567890”
You can input any number with digital buttons (ignore leading zero). Notice, the LED of calculator can only display 8 digits. So when the number of digits large than eight, only the first eight digits will be retained. And when the number of one result’s digits large than eight, error occurs.
Operator: “+-*/”
For simplicity, all operations are integer operations. Some continuous operators, the last one is valid. When an operator button is pressed, the calculation before that will be completed.
Hauling Ore

输入:

The first line is number of test cases. After that, each case consists of one line, containing a string means pressed button. The length of string is less than 100. There is no additional space.

输出:

The first line is number of test cases. After that, each case consists of one line, containing a string means pressed button. The length of string is less than 100. There is no additional space.

样例输入:

6
11+/2+
11+*==
3==+5=
5=7=+8=
99999999+1-1=
1-99999999=

样例输出:

Case 1: 5
Case 2: 1331
Case 3: 8
Case 4: 15
Case 5: ERROR
Case 6: -99999998

今天下午这个题wa到死了,厄,不得不说这个实在是太恶心了,没啥算法,就是模拟就好

如果有后人做这个题 注意:它存在一个空行的数据 这个空行要输出 0

还有其他的一些数据:

13
11+/2+

11+*==
3==+5=
5=7=+8++10==
5=7=+8+10==
99999999000+1-1=
1-99999999000==
30000*30001123456789
30000*30001=
==-1000+30==
10/0=
3+8=3=

Case 1: 5
Case 2: 0
Case 3: 1331
Case 4: 8
Case 5: 35
Case 6: 35
Case 7: ERROR
Case 8: ERROR
Case 9: 30001123
Case 10: ERROR
Case 11: -940
Case 12: ERROR
Case 13: 3

 #include <stdio.h>
#include <string.h>
#define OPER 1
#define NUM 2
#define INF 0x3fffffff
#define null ‘x’
#define END -1
#define MUTI 3
int n,T;
char
oper_now,oper_last;
char
in[1000];
__int64
ans,temp,num_equal;
int
num,l;
int
isnum(char a)
{

    if
(a>=’0′&& a<=’9′)
        return
1;
    else return
0;
}

int
findp()
{

    if
(num==l)return END;
    int
i,j;
    int
t=num;
    if
(in[t]==‘=’&& in[t+1]==‘=’)
    {

        num_equal  =0;
        while
(in[num]==‘=’)
        {

            num_equal++;
            num++;
        }

        return
MUTI;
    }

    if
(!isnum(in[num]))
    {

        //oper_last = oper_now;
        //oper_now = in[num];
        num++;
        return
OPER;
    }

    __int64
tt=0;
    int
jj=10;
    int
ttp;
    while
(isnum(in[num]))
    {

        if
((tt*jj+ (in[num]-’0′))>99999999|| (tt*jj+ (in[num]-’0′))<-99999999)
        {

            num++;
            continue
;
        }

        tt= tt*jj+ (in[num]-’0′);
       
        //jj*=10;
        num++;
    }

    temp= tt;
    return
NUM;
}

__int64
cal_op()
{

    __int64
aaa;
    switch
(oper_now)
    {

        case
‘-’:return ans-temp;
        case
‘+’:return temp+ans;
        case
‘*’:aaa= (__int64)ans*temp;if(aaa>99999999|| aaa<-99999999)return INF;else return ans*temp;
        case
‘/’:if(temp!=0)return ans/temp;else return INF;
    }
}

void
cal()
{

    if
(in[num-1]!=‘=’)
    {

        if
(temp!=INF&& oper_now!=null)
        {

            ans= cal_op();
            temp= INF;
        }

        else if
(oper_now==null&& temp!=INF)
        {

             ans= temp;
             temp= INF;
        }

        oper_last= oper_now;
        oper_now= in[num-1];
    }

    else

    {

        if
(oper_now!=null)
        if
(temp!=INF)
        {

            ans= cal_op();
            temp= INF;
        }

        else

        {

            temp= ans;
            ans= cal_op();
            temp= INF;
        }

        oper_now= null;
    }

}
int
main()
{

    int
i,j,t;
    int
op,pp;
    scanf("%d",&T);
    getchar();
    for
(pp=0; pp<T; pp++)
    {

        num=0;
        ans=0;
        temp= INF;
        oper_now= null;
        oper_last= null;
        gets(in);
        l= strlen(in);
        int
ttt;
        int
error=0;
        while
(op= findp(),op!=END&& !error)
        {

            if
(ans>99999999|| ans<-99999999)
            {

                error=1;
                goto
end;
            }

            if
(op==OPER)
            {

                cal();
            }

            else if
(op==MUTI)
            {

                    if
(oper_now!=null)
                    {

                          if
(temp!=INF)
                          {

                              for
(i=0;i<num_equal;i++)
                              {

                                   ans= cal_op();
                                   if
(ans>99999999|| ans<-99999999)
                                   {

                                       error=1;
                                       goto
end;
                                   }
                              }

                              temp= INF;
                          }

                          else

                          {

                              ttt= ans;
                              for
(i=0;i<num_equal;i++)
                              {

                                   temp= ttt;
                                   ans= cal_op();
                                   if
(ans>99999999|| ans<-99999999)
                                   {

                                       error=1;
                                       goto
end;
                                   }
                              }

                              temp= INF;
                          }
                         
                    }

                    oper_now= null;
                   
            }

            if
(ans>99999999|| ans<-99999999)
            {

                error=1;
                goto
end;
            }
        }

end:;
        printf("Case %d: ",pp+1);
        if
(!error)
        printf("%I64d\n",temp==INF?ans:temp);
        else
printf("ERROR\n");
    }

    return
0;
}

参考:http://hi.baidu.com/bobo__bai/item/5fff68374e540af62684f439