Overflow |
Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal'' signed integer (type integer if you are working Pascal, type int if you are working in C).
An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.
For each line of input, print the input followed by 0-3 lines containing as many of these three messages as are appropriate: ``first number too big'', ``second number too big'', ``result too big''.
300 + 3 9999999999999999999999 + 11
300 + 3 9999999999999999999999 + 11 first number too big result too big
题意。。输入一个式子。先输出原式子。然后判断: 第一个数大于INT_MAX,就输出first number too big, 第二个数大于INT_MAX,就输出second number too big, 如果他们运算结果大于INT_MAX,就输出result too big。。
这题乍一看是高精度加法和乘法的结合。。。
其实。。其实!!! 这题数据挺水的。。。
我们知道double型可以表示300多位。。这就足够。。。。
把数据转换成double型。在和INT_MAX进行比较。。。。 就可以了- - 好坑爹
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> char a[255]; char b[255]; char c[1]; int m = INT_MAX; double aa, bb; int main() { while (scanf("%s%s%s", a, c, b) != EOF) { printf("%s %s %s\n", a, c, b); aa = atof(a); bb = atof(b); if (aa > m) printf("first number too big\n"); if (bb > m) printf("second number too big\n"); if (c[0] == '+') { if (aa + bb > m) printf("result too big\n"); } if (c[0] == '*') { if (aa * bb > m) printf("result too big\n"); } } return 0; }