465 Overflow

这题没做,直接在网上看了别人的代码,对数字的一些问题不是很了解

学习一下istringstream的用法:

#include <iostream>
#include <string>
#include <sstream>//使用istringstream()需要调入的头文件

using namespace std;

string getMaxStr()
{
    int n=0x7fffffff;
    string s="";
    while(n!=0)
    {
        s=(char)(n%10+'0')+s;
        n=n/10;
    }
    return s;
}

bool cmp(const string &a,const string &b)
{
    int alen=a.size();
    int blen=b.size();
    if(alen==blen)
    {
        for(int i=0;i<alen;i++)
            if(a[i]!=b[i])
                return a[i]>b[i];
    }
    else
    {
        return alen>blen;
    }
    return false;
}

int main()
{
    string a,op,b;
    string max_str=getMaxStr();
    while(cin>>a>>op>>b)
    {
        bool over=false;
        cout<<a<<" "<<op<<" "<<b<<endl;
        while(a.size()>1 && a[0]=='0')//###,wa n多次,大整数问题记着清除前置零,判断为‘0’而不是0.
            a.erase(0,1);
        while(b.size()>1 && b[0]=='0')
            b.erase(0,1);
        if(cmp(a,max_str))
        {
            cout<<"first number too big"<<endl;
            over=true;
        }
        if(cmp(b,max_str))
        {
            cout<<"second number too big"<<endl;
            over=true;
        }
        if(over)
        {
            if(op=="+")
                cout<<"result too big"<<endl;
            else if(op=="*" && a!="0" && b!="0")
                cout<<"result too big"<<endl;
        }
        else
        {
            long long aa,bb,cc;
            istringstream(a)>>aa;
            istringstream(b)>>bb;
            if(op=="+")
                cc=aa+bb;
            else if(op=="*")
                cc=aa*bb;
            if(cc>0x7fffffff)
                cout<<"result too big"<<endl;
        }
    }
    return 0;
}

学习一下sscanf的使用:

#include<stdio.h>
#include<string.h>
#define MAXN 20000
char a[MAXN], b[MAXN],s[MAXN],y[2];
int A[MAXN], B[MAXN], S[MAXN];
void decide()
{
long long a1, b1, s1;    
sscanf(a,"%lld",&a1);
sscanf(b,"%lld",&b1);
sscanf(s,"%lld",&s1);
printf("%s %s %s\n",a,y,b);
if(a1 > 0x7fffffff) printf("first number too big\n");
if(b1 > 0x7fffffff) printf("second number too big\n");
if(s1 > 0x7fffffff) printf("result too big\n");
}
void a_b_mult()
{
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    memset(S, 0, sizeof(S));
    int len1 = strlen(a);
    for(int i = 0; i < len1; i ++)
        A[len1-1-i] = a[i] - '0';
    int len2 = strlen(b);
    for(int j = 0; j < len2; j ++)
        B[len2-1-j] = b[j] - '0';
    for(int i = 0; i <= len2; i ++)
    {
        int c = 0;
        for(int j = 0; j <= len1; j ++)
        {
            S[i + j] += B[i] * A[j] +c;
            c = S[i + j] / 10;
            S[i + j] %= 10;
        }
    }    
    int i;
    for(i = MAXN; i > 0; i --)
    if(S[i]) break;
    for(int j = 0; j <= i; j ++)
    s[j] = S[i - j] + '0';
    s[i+1] = '\0';
    decide();
}
void a_b_sum()
{
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    memset(S, 0, sizeof(S));
    int len1 = strlen(a);
    for(int i = 0; i < len1; i ++)
        A[len1-1-i] = a[i] - '0';
    int len2 = strlen(b);
    for(int j = 0; j < len2; j ++)
        B[len2-1-j] = b[j] - '0';
    int t;
    if(len1>len2) t = len1;
    else t = len2;
    int c = 0;
    for(int k = 0; k <= t; k ++)
    {
        S[k] = A[k] + B[k] + c;
        c = S[k] / 10;
        S[k] %= 10; 
    }
    int i;
    for(i = MAXN; i > 0; i --)
        if(S[i]) break;
    for(int j = 0; j <= i; j ++)
    s[j] = S[i - j] + '0';
    s[i+1] = '\0';
    decide();
}
void input()
{
    while(scanf("%s%s%s",a,y,b) == 3)
    {
        if(y[0] == '+') a_b_sum();
        else     a_b_mult();
    }
}
int main()
{
    input();
    return 0;
}

别人的,代码好短哦!
 

#include<stdio.h>
 #include<stdlib.h>
 int main()
 {
     char str1[1001],str2[1001],c;
     double a,b;
     while(scanf("%s %c %s",str1,&c,str2)!=EOF)
     {
         a=atof(str1);
         b=atof(str2);
         printf("%s %c %s\n",str1,c,str2);
         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");
         }
         if(c=='*')
         {
             if(a*b>2147483647)
             printf("result too big\n");
         }
     }
     return 0;
 }
原文地址:https://www.cnblogs.com/UnGeek/p/2566495.html