This is an easy big integer. Your task is to finish the class big integer:
Tips:
size_ means the size of the big integer
friend function should have been declared outside;
when you declare the friend function, you should forward declare the class.
when you cannot compile, try to read the informations or google it.
overload “<<” : please output like this format: “123456”, no wrap.
overload “-”: we promise that a > b when a - b .
Hint
operator overloading ( we promise that left will bigger than right when doing subtract operation )
friend function
deep copy
From: 欧文杰
main.cpp
#include "Biginteger.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1, s2, s3, s4;
cin >> s1 >> s2;
cin >> s4;
BigInteger a(s1);
BigInteger b(s2);
BigInteger c(a);
BigInteger d(s4);
BigInteger sum(a+b);
BigInteger diff(c-d);
cout << sum << endl;
cout << diff << endl;
return 0;
}
Biginteger.h
#ifndef _BIG_INTEGER_
#define _BIG_INTEGER_
#include <iostream>
#include <string>
using std::string;
using std::ostream;
// forward declaration
class BigInteger;
// operators overloading
BigInteger operator+(const BigInteger& left, const BigInteger& right);
BigInteger operator-(const BigInteger& left, const BigInteger& right);
ostream& operator<<(ostream& out, const BigInteger& Bint);
class BigInteger {
public:
BigInteger();
explicit BigInteger(const string& number);
BigInteger(const BigInteger& other);
friend BigInteger operator+(const BigInteger& left,
const BigInteger& right);
// promise left >= right
friend BigInteger operator-(const BigInteger& left,
const BigInteger& right);
friend ostream& operator<<(ostream& out, const BigInteger& Bint);
~BigInteger();
private:
int *data_;
int size_;
};
#endif // _BIG_INTEGER_
Biginteger.cpp
# include "Biginteger.h"
BigInteger :: BigInteger() {
data_ = NULL;
size_ = 0;
}
BigInteger :: BigInteger(const string& number) {
data_ = NULL;
size_ = 0;
data_ = new int[number.size()];
int j = 0;
for (int i = number.size()-1; i >= 0; i--) {
data_[j] = number[i] - '0';
j++;
}
size_ = number.size();
}
BigInteger :: BigInteger(const BigInteger& other) {
data_ = NULL;
size_ = other.size_;
data_ = new int[size_];
for (int i = 0; i < size_; i++) {
data_[i] = other.data_[i];
}
}
BigInteger operator+(const BigInteger& left,
const BigInteger& right) {
BigInteger copy;
if (left.size_ < right.size_) {
copy.size_ = right.size_+1;
copy.data_ = new int[copy.size_];
copy.data_[copy.size_-1] = 0;
for (int i = 0; i < right.size_; i++) {
if (i < left.size_) {
copy.data_[i] = left.data_[i] + right.data_[i];
} else {
copy.data_[i] = right.data_[i];
}
}
for (int i = 0; i < right.size_; i++) {
if (copy.data_[i] >= 10) {
copy.data_[i+1]++;
copy.data_[i] -= 10;
}
}
} else {
copy.size_ = left.size_+1;
copy.data_ = new int[copy.size_];
copy.data_[copy.size_-1] = 0;
for (int i = 0; i < left.size_; i++) {
if (i < right.size_) {
copy.data_[i] = left.data_[i] + right.data_[i];
} else {
copy.data_[i] = left.data_[i];
}
}
for (int i = 0; i < left.size_; i++) {
if (copy.data_[i] >= 10) {
copy.data_[i+1]++;
copy.data_[i] -= 10;
}
}
}
return copy;
}
// promise left >= right
BigInteger operator-(const BigInteger& left,
const BigInteger& right) {
BigInteger copy;
copy.size_ = left.size_;
copy.data_ = new int[left.size_];
for (int i = 0; i < left.size_; i++) {
if (i < right.size_) {
copy.data_[i] = left.data_[i] - right.data_[i];
} else {
copy.data_[i] = left.data_[i];
}
}
for (int i = 0; i < left.size_; i++) {
if (copy.data_[i] < 0) {
copy.data_[i+1]--;
copy.data_[i] += 10;
}
}
return copy;
}
ostream& operator<<(ostream& out, const BigInteger& Bint) {
int key = 0;
for (int i = Bint.size_-1; i >= 0; i--) {
if (Bint.data_[i] != 0) key = 1;
if (Bint.data_[i] == 0 && key == 0) continue;
out << Bint.data_[i];
}
int j;
for (j = 0; j < Bint.size_; j++) {
if (Bint.data_[j] != 0) break;
}
if (j == Bint.size_) {
out << 0;
}
return out;
}
BigInteger :: ~BigInteger() {
if (data_ != NULL)
delete[] data_;
}