Sunday, February 27, 2011

465 - Overflow


#include<iostream>
#include<cstdlib>
#include<cstring>
#define range 2147483647ll
using namespace std;
bool tooBig(char[],long long &r);
int main()
    {
        char num1[10000],num2[10000],opt[2],intrange[]="2147483647",op;
        long long a,b,f1,f2,r;
        //freopen("in.txt","r",stdin);
        while(scanf("%s%s%s",num1,opt,num2)==3)
        {
            f1=f2=r=0;
            sscanf(opt,"%c",&op);
            switch(op)
            {
            case '+':
                if(tooBig(num1,a)){f1=r=1;}
                if(tooBig(num2,b)){f2=r=1;}
                if(!r&&(range-a<b))r=1;
                break;
            case '*':
                if(tooBig(num1,a)){f1=r=1;}
                if(tooBig(num2,b)){f2=r=1;}
                if(!a||!b){r=0;break;}
                if(!r&&(range/a<b))r=1;
                break;
            }
            printf("%s %s %s\n",num1,opt,num2);
            if(f1)printf("first number too big\n");
            if(f2)printf("second number too big\n");
            if(r)printf("result too big\n");
        }
    exit(0);
    }
bool tooBig(char num[],long long &r)
{
     long i;
     r=0;
     for(i=0;num[i];i++)
     {
         r=r*10+(num[i]&15);
         if(r>range)return true;
     }
     return false;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.