2014
03-23

# 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.

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

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;
}