uva 465 Overflow(借助double型进行比较)

737 篇文章 0 订阅

 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, typeint 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
题目大意:高精度乘法+加法,并且判断num1、num2、sum是否超过Int存储的最大值。

解题思路:题目数据比较水,用atof转化成double 型的进行比较。

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

#define N 62505

struct bign{
	int len, s[N];
    
	bign(){
		memset(s, 0, sizeof(s));
		len = 1;
	}
    
	bign operator = (const char *num){
		len = strlen(num);
		for (int i = 0; i < len; i++)
			s[i] = num[len - i - 1] - '0';
		return *this;
	}
    
	bign operator * (const bign &c){
		bign sum;
		sum.len = 0;
        
		for (int i = 0; i < len; i++){
            
			int g = 0;
			for (int j = 0; j < c.len; j++){
                
				int x = s[i] * c.s[j] + g + sum.s[i + j];
                
				sum.s[i + j] = x % 10;
                
				g = x / 10;
			}
            
			sum.len = i + c.len;
            
			while (g){
				sum.s[sum.len++] = g % 10;
				g = g / 10;
				
			}
		}
        
		return sum;
	}
    
    bign operator + (const bign &c){
		bign sum;
		sum.len = 0;
        
		for (int i = 0, g = 0; g || i < len || i < c.len; i++){
			int x = g;
			if (i < len) x += s[i];
			if (i < c.len) x += c.s[i];
			sum.s[sum.len++] = x % 10;
			g = x / 10;
		}
        
		return sum;
	}
    
};

int main(){
	char str1[N], str2[N], str3[N], c;
	double tem = 2147483647;
	while (cin >> str1 >> c >> str2){
        
		bign num1, num2, sum;
        
		num1 = str1;
		num2 = str2;

		if (c == '*')
			sum = num1 * num2;
		else if (c == '+')
			sum = num1 + num2;

		cout << str1 << " " << c << " " << str2 << endl;

		double t;

		t = atof(str1);

		if (t > tem)
			cout << "first number too big" << endl;

		t = atof(str2);

		if (t > tem)
			cout << "second number too big" << endl;

		for (int i = 0; i < sum.len; i++)
			str3[i] = sum.s[sum.len - i - 1] + '0';

		t = atof(str3);

		if (t > tem)
			cout << "result too big" << endl;
		
		memset(str1, 0, sizeof(str1));
		memset(str2, 0, sizeof(str2));
		memset(str3, 0, sizeof(str3));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值