biginteger用法c语言,C++ 大数算术 BigInteger

#include "string"

using namespace std;

class BigInteger{

private:

string s;

string subtraction(string s1, string s2){

if (s1 == s2)

return "0";

while (s1.length() < s2.length())

s1 = '0' + s1;

while (s2.length() < s1.length())

s2 = '0' + s2;

s1 = '0' + s1;

s2 = '0' + s2;

for (size_t i = 1; i < s1.length(); i++){

if (s1[i] > s2[i])

s1[i] = s1[i] - s2[i] + '0';

else{

s1[i] = s1[i] + 10 - s2[i] + '0';

s1[i - 1]--;

}

}

return clearZero(s1);

return s1;

}

void strToInt(int *a, string b){ //b = "1234" then a = {4,3,2,1}

int length = b.length();

for (int i = 0; i < length; i++){

a[length - i - 1] = b[i] - '0';

}

}

string intToStr(int *a, int length){

string b;

for (int i = 0; i < length; i++){

b = char(a[i] + '0') + b;

}

return b;

}

//清楚数字开头多余的0

string clearZero(string a){

while (a.size() > 1 && a[0] == '0')

a.erase(a.begin());

return a;

}

//比较两个字符串的大小

bool Compare(string first, string second){

first = clearZero(first);

second = clearZero(second);

if (first.size() < second.size())

return false;

if (first.size() == second.size() && first < second)

return false;

return true;

}

public:

BigInteger(){

s = "";

}

BigInteger(string a) :s(a){}

void setInteger(string integer){

this->s = integer;

}

string getInteger(){

return this->s;

}

BigInteger operator+(const BigInteger& other){

string s2 = other.s;

string s1 = this->s;

while (s1.length() < s2.length())

s1 = '0' + s1;

while (s1.length() > s2.length())

s2 = '0' + s2;

s1 = '0' + s1;

s2 = '0' + s2;

for (int i = s1.length() - 1; i > 0; i--){

s1[i] = s1[i] + s2[i] - '0';

if (s1[i] > '9'){

s1[i] -= 10;

s1[i - 1] += 1;

}

}

return clearZero(s1);

}

BigInteger operator-(const BigInteger& other){

string s2 = clearZero(other.s);

string s1 = clearZero(this->s);

if (s1 == s2)

return "0";

else if (s1.length() > s2.length()){

return subtraction(s1, s2);

}

else if (s1.length() < s2.length()){

return '-' + subtraction(s2, s1);

}

else if (s1 > s2){

return subtraction(s1, s2);

}

else{

return '-' + subtraction(s2, s1);

}

}

BigInteger operator*(const BigInteger& other){

string s1 = this->s;

string s2 = other.s;

const int MAX = 100;

int a[MAX]; //保存操作数1

int b[MAX]; //保存操作数2

int c[MAX]; //保存计算结果

memset(a, 0, sizeof(a));

memset(b, 0, sizeof(b));

memset(c, 0, sizeof(c));

strToInt(a, s1);

strToInt(b, s2);

for (int i = 0; i < MAX; i++){ //借鉴手算乘法

for (int j = 0; j < MAX; j++){

c[i + j] += (a[i] * b[j]);

}

}

//处理进位

for (int i = 0; i < MAX - 1; i++){

c[i + 1] += (c[i] / 10);

c[i] = c[i] % 10;

}

return clearZero(intToStr(c, MAX));

}

BigInteger operator/(const BigInteger& other){

string first = clearZero(this->s); //被除数

string second = clearZero(other.s); //除数

if ("0" == second){

return "Error, divisor can no be zero.";

}

if (!Compare(first, second))

return "0";

string result = "", remain = "";

for (int i = 0; i < first.length(); i++){ //借鉴手算除法的步骤

remain += first[i];

result += '0';

while (Compare(remain, second)){

result[result.length() - 1] += 1;

remain = subtraction(remain, second);

}

}

return clearZero(result);

}

friend ostream& operator<

out << b.s;

return out;

}

friend istream& operator>>(istream &in, BigInteger &b){

in >> b.s;

return in;

}

};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值