This question demonstrates the advantages of floating point numbers. That is, the 64-bit integer is used (note that long requires i64d output). Overflow also turns into a negative number, making it difficult to judge. If the double type is used, even if the number of digits of a string exceeds bits, the sscanf () function is also used for processing, but it will not become a negative number. The reason is that floating point numbers can be expressed exponentially. For example, the positive value of double ranges from 4.94065645841246544e-324 to 1.79769313486231570e + 308. This means that the upper 300-bit string can be converted to a double-precision floating point number, and the precision will be lost at most. After knowing this nature, it is very easy to solve this problem with double-precision floating point numbers.
#include <stdio.h>const int MINT = 0x7fffffff;int main() { char str1[1000], str2[1000], ch; while (scanf("%s %c %s", str1, &ch, str2) != EOF) { printf("%s %c %s\n", str1, ch, str2); double a, b; sscanf(str1, "%lf", &a); sscanf(str2, "%lf", &b); if (a > MINT) printf("first number too big\n"); if (b > MINT) printf("second number too big\n"); if ('+'==ch && a+b>MINT) printf("result too big\n"); if ('*'==ch && a*b>MINT) printf("result too big\n"); } return 0;}
Previously, I used long integer processing, even though various processing would still be wrong. Finally, let's start with the article http://blog.sina.com.cn/s/blog_8ee62ecb0100wpjm.html. Thanks to the concise and elegant code of the original author.