65,211
社区成员
发帖
与我相关
我的任务
分享
/*
* exe1102.cpp
* 用类的方法进行求解下题
*
* A + B Problem
*
* Input
* The input will consist of a series of pairs of integers a and b, separated by one line.
* Each integer may consist of less than 79 digits.
*
* Output
* For each pair of input integers a and b you should output the sum of a and b in one line
*
* Sample input
* 1
* 5000000000000000000000000000000000000000000000000000000000
* -1
* 23
*
* Sample output
* 5000000000000000000000000000000000000000000000000000000001
* 22
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class BigInt { //定义大整数类
string s;
int sign;
public:
BigInt (const string &num);
BigInt (int sign, const string &value);
~BigInt () {}
static string numberArrayToString (int a[], int length);
static int numValueCompare (const string &a, const string &b);
string getNumValue () { return s; };
int getSign () { return sign; }
friend BigInt operator + (const BigInt a, const BigInt b); //重载操作符+
// Pre-Condition: a >= b >= 0
friend BigInt operator - (const BigInt a, const BigInt b); //重载操作符-
friend ostream &operator << (ostream &o, const BigInt &a); //重载输出操作符
};
BigInt::BigInt (const string &num) {
if (num[0] == '-') {
sign = -1;
s = num.substr( 1);
} else {
sign = 1;
s = num;
}
}
BigInt::BigInt (int sign, const string &value) {
this->sign = sign;
s = value;
}
string BigInt::numberArrayToString (int a[], int length) {
string result = "";
int i = 0;
while (i < length-1 && a[i] == 0)
++i;
while (i < length) {
result += (char)(a[i] + '0');
++i;
}
return result;
}
int BigInt::numValueCompare (const string &a, const string &b) {
if (a.size() < b.size()) return -1;
if (a.size() > b.size()) return 1;
for (int i= 0; i < a.size(); ++i) {
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return 1;
}
return 0;
}
// Pre-Condition: a >= b >= 0
BigInt operator - (const BigInt a, const BigInt b) {
int shortLen = b.s.size();
int longLen = a.s.size();
string shortNum = b.s;
string longNum = a.s;
int numResult[longLen];
int temp, i, j;
int rent = 0;
for (i= longLen-1, j= shortLen-1; i >= 0 && j >= 0; i--, j--) {
temp = (longNum[i] - '0') - (shortNum[j] - '0') - rent;
rent = ((temp < 0) ? 1 : 0);
numResult[i] = temp + rent * 10;
}
while (i >= 0) {
temp = (longNum[i] - '0') - rent;
rent = ((temp < 0) ? 1 : 0);
numResult[i] = temp + rent * 10;
--i;
}
return BigInt( 1, BigInt::numberArrayToString( numResult, longLen));
}
BigInt operator + (const BigInt a, const BigInt b) {
if (a.sign == b.sign) {
int shortLen, longLen;
string shortNum, longNum;
if (a.s.size() <= b.s.size()) {
shortLen = a.s.size();
longLen = b.s.size();
shortNum = a.s;
longNum = b.s;
} else {
shortLen = b.s.size();
longLen = a.s.size();
shortNum = b.s;
longNum = a.s;
}
int numResult[longLen+1];
int temp, i, j;
int carry = 0;
for (i= longLen-1, j= shortLen-1; i >= 0 && j >= 0; i--, j--) {
temp = longNum[i] - '0' + shortNum[j] - '0' + carry;
numResult[i+1] = temp % 10;
carry = temp / 10;
}
while (i >= 0) {
temp = longNum[i] - '0' + carry;
numResult[i+1] = temp % 10;
carry = temp / 10;
i--;
}
numResult[0] = carry;
return BigInt( a.sign, BigInt::numberArrayToString( numResult, longLen+1));
}
// now a and b in different sign
int compareResult = BigInt::numValueCompare( a.s, b.s );
if (compareResult == 0)
return BigInt( 1, string( "0" ));
if (compareResult < 0)
return BigInt( b.sign, (BigInt( 1, b.s) - BigInt( 1, a.s)).getNumValue());
if (compareResult > 0)
return BigInt( a.sign, (BigInt( 1, a.s) - BigInt( 1, b.s)).getNumValue());
}
ostream &operator << (ostream &o, const BigInt &a) {
return o <<(a.sign == -1 ? "-" : "") <<a.s;
}
int main () {
BigInt a("500000000000000000009999999");
BigInt b("600000000000000000009999999");
cout << a + b << endl;
BigInt c("-500000000000000000009999999");
BigInt d("600000000000000000009999999");
cout << c + d << endl;
}