-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP465.cpp
53 lines (49 loc) · 1.01 KB
/
P465.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
#include <iostream>
#include <stdio.h>
#define MAX 2147483647
typedef unsigned long long ul;
bool readInt(int &i, ul &a, char *w) {
a = 0;
char c;
bool overflow = false;
for(; isdigit(c = w[i]); ++i) {
a = 10*a + (c-'0');
if(a > MAX)
overflow = true;
}
return overflow;
}
int main() {
char c, w[10000];
while(gets(w)) {
printf("%s\n", w);
bool mult = true;
int i = 0;
ul a, b;
bool overflowA = readInt(i, a, w);
if(overflowA)
printf("first number too big\n");
while(!isdigit(c = w[i])) {
if(c == '+')
mult = false;
++i;
}
bool overflowB = readInt(i, b, w);
if(overflowB)
printf("second number too big\n");
ul result = mult ? (a*b) : (a+b);
if(mult) {
if(!overflowB && b == 0)
continue;
if(!overflowA && a == 0)
continue;
if(overflowA || overflowB || result > MAX)
printf("result too big\n");
}
else {
if(overflowA || overflowB || result > MAX)
printf("result too big\n");
}
}
return 0;
}