C - Overflow
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Appoint description:
Description
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
这道题目有点坑。。。。
套模板调了半天死活过不了。后来据说用double就可以。。
因为double的范围是10^308。
然后套用atof函数,就是将一个字符串转变成为double型浮点数。
郁闷的是,直接用double输入好像不行。
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INT 2147483647
int main()
{
char s1[1000],c,s2[1000];
int l1,l2,i,j,k;
while(scanf("%s %c %s",s1,&c,s2)!=EOF)
{
double a,b;
a=atof(s1);
b=atof(s2);
printf("%s %c %s\n",s1,c,s2);
if(a>INT)printf("first number too big\n");
if(b>INT)printf("second number too big\n");
double cc;
if(c=='+')cc=a+b;
else if(c=='*')cc=a*b;
// printf("%.0f\n",c);
if(cc>INT)printf("result too big\n");
}
return 0;
}