C++: 高精度加法与高精度减法
标签:C++ 高精度加法 高精度减法
by 小威威
在学C语言的时候,我有实现过高精度乘法(想了解的戳它)。
其实高精度加法、减法比乘法简单多了,所以可以通过高精度乘法这篇文章来入门高精度的算法。
现在是用C++实现,显然有了string类,一切的操作都变的简单多了。
题目如下:(Author: 欧文杰(TA))
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 .
其中,main.cpp 与 BigInteger.h已给出。
\\ 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