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, 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;
}