forked from Ouditchya/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARITH2.cpp
86 lines (73 loc) · 2.64 KB
/
ARITH2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// AC, ALGO : Adhoc.
/*
Helpful Link: http://www.cplusplus.com/reference/cstdlib/atoi/
*/
// For any clarifications, contact me at : [email protected]
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std ;
int main( )
{
int i , j , n , t , ctr = 0 ;
long long ans , a , b ;
char op , str[1000000] , buffer[2500] , buffer2[2500] ;
scanf("%d",&t) ;
t += 1 ;
while( t-- )
{
ctr++ ;
gets( str ) ;
n = strlen( str ) ;
if( n == 0 )
{
gets( str ) ;
n = strlen( str ) ;
}
for( i = 0 ; i < n ; )
{
memset( buffer , 0 , sizeof( buffer ) ) ;
j = 0 ;
while( str[i] == ' ' )
i++ ;
while( str[i] != ' ' )
buffer[j++] = str[i++] ;
buffer[j] = '\0' ;
a = atoi( buffer ) ;
ans = a ;
while( str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' )
i++ ;
op = str[i] ;
i++ ;
while( str[i] == ' ' )
i++ ;
break ;
}
for( ; i < n ; )
{
j = 0 ;
memset( buffer2 , 0 , sizeof( buffer2 ) ) ;
while( str[i] != ' ' && str[i] != '=' )
buffer2[j++] = str[i++] ;
buffer2[j] = '\0' ;
b = atoi( buffer2 ) ;
if( op == '+' )
ans += b ;
else if( op == '-' )
ans -= b ;
else if( op == '*' )
ans *= b ;
else if( op == '/' )
ans /= b ;
while( str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' )
i++ ;
op = str[i] ;
i++ ;
while( str[i] == ' ' )
i++ ;
}
if( ctr != 1 )
printf("%lld\n",ans) ;
}
return 0 ;
}