Overflow
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).
Input
An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.
Output
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''.
Sample Input
300 + 3 9999999999999999999999 + 11
Sample Output
300 + 3 9999999999999999999999 + 11 first number too big result too big
=============================
分别看第一个数、第二个数和计算的结果是否超出int
居然用double就能过orz
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
char s1[1111],s2[1111],c;
while(~scanf("%s %c %s",s1,&c,s2))
{
printf("%s %c %s\n",s1,c,s2);
double a=atof(s1);
double b=atof(s2);
if(a>2147483647) printf("first number too big\n");
if(b>2147483647) printf("second number too big\n");
if(c=='+')
{
if(a+b>2147483647)
printf("result too big\n");
}
else
{
if(a*b>2147483647)
printf("result too big\n");
}
}
return 0;
}