integer of unlimited size(大数类的加减乘)

闲着,于是写了个大数类。加减乘都不难,所以实现了。除法得以后有空再慢慢研究的。
// bigintJ.h
//

#pragma once

#include "link_list.h"

class BigInt
{
public:
BigInt(const char* nums);
BigInt(const char* nums, const int c);
BigInt(const int c);
BigInt(LinkList<char>& list);
BigInt(BigInt& bigInt);
~BigInt(void);


BigInt operator+(BigInt& operand);
BigInt operator-(BigInt& operand);
BigInt operator*(BigInt& operand);
BigInt operator=(BigInt operand);
bool operator==(BigInt operand);


char& operator[](const int position);
const char at(const int position);
int size() const;
protected:
LinkList<char> valueList();
const int compare(BigInt& b);


protected:
LinkList<char> m_value;
};


// bigintJ.cpp
//

#include "BigIntJ.h"

BigInt::BigInt(const char* nums)
{
int c = ::strlen(nums);
for (int idx = 0; idx < c; ++idx)
{
m_value.append(nums[idx]);
}
}

BigInt::BigInt(const char* nums, const int c)
{
for (int idx = 0; idx < c; ++idx)
{
m_value.append(nums[idx]);
}
}

BigInt::BigInt(LinkList<char>& list)
{
list.setStart();
char temp;
while (list.getValue(temp))
{
m_value.append(temp);
list.next();
}
}

BigInt::BigInt(BigInt& bigInt)
{
bigInt.m_value.setStart();
char temp;
while (bigInt.m_value.getValue(temp))
{
m_value.append(temp);
bigInt.m_value.next();
}
}

BigInt::BigInt(const int c)
{
for (int idx = 0; idx < c; ++idx)
{
m_value.append('0');
}
}

BigInt::~BigInt(void)
{
}

BigInt BigInt::operator+(BigInt& operand)
{
int c = ::max(size(), operand.size());
LinkList<char> tempList;
int carray = 0;
for (int l = size() - 1, r = operand.size() - 1; l >= 0 || r >= 0; --l, --r)
{
int temp = (at(l) - '0') + (operand.at(r) - '0') + carray;
carray = temp / 10;
tempList.append(temp % 10 + '0');
}
if (carray > 0)
tempList.append(carray + '0');
tempList.reverse();
return BigInt(tempList);
}

BigInt BigInt::operator-(BigInt& operand)
{
const int compareResult = compare(operand);
if (compareResult == 0)
return BigInt(1);

LinkList<char> tempList;

int carray = 0;
for (int l = compareResult > 0 ? (size() - 1) : (operand.size() - 1),
r = compareResult > 0 ? (operand.size() - 1) : size() - 1;
l >= 0 || r >= 0; 
--l, --r)
{
int lValue = (compareResult > 0 ? (at(l) - '0') : (operand.at(l) - '0'));
int rValue = (compareResult > 0 ? (operand.at(r) - '0') : (at(r) - '0'));
int temp = lValue - rValue + carray;
carray = temp < 0 ? -1 : 0;
temp = (temp + 10) % 10;
tempList.append(temp + '0');
}

if (compareResult < 0)
tempList.append('-');
tempList.reverse();

return BigInt(tempList);
}

BigInt BigInt::operator*(BigInt& operand)
{
BigInt zero(1);
if ((zero == *this) || (zero == operand))
return zero;


int resultSize = size() + operand.size() - 1;
BigInt result(resultSize);

int carray = 0;
for (int l = size() - 1; l >= 0; --l)
{
for (int r = operand.size() - 1; r >= 0; --r)
{
int temp = (at(l) - '0') * (operand.at(r) - '0');
temp = (result[l + r] - '0') + temp + carray;
result[l + r] = (temp % 10) + '0';
carray = temp / 10;
}
if (carray > 0)
{
if (0 != l)
result[l - 1] = carray + '0';
else
{
result.m_value.setStart();
result.m_value.insert(carray + '0');
}

carray = 0;
}
}

return result;
}

BigInt BigInt::operator=(BigInt operand)
{
m_value.clear();
operand.m_value.setStart();
char temp;
while (operand.m_value.getValue(temp))
{
m_value.append(temp);
operand.m_value.next();
}
return *this;
}

bool BigInt::operator==(BigInt operand)
{
if (size() != operand.size())
return false;

for (int idx = 0; idx < size(); ++idx)
{
if (at(idx) != operand.at(idx))
return false;
}

return true;
}

char& BigInt::operator[](const int position)
{
return m_value[position];
}

const char BigInt::at(const int position)
{
if (0 > position || position >= size())
return '0';
return m_value[position];
}

int BigInt::size() const
{
return m_value.leftLenght() + m_value.rightLenght();
}

LinkList<char> BigInt::valueList()
{
return m_value;
}

const int BigInt::compare(BigInt& b)
{
if (size() > b.size())
return 1;
if (size() < b.size())
return -1;

int sz = size();
for (int idx = 0; idx < sz; ++idx)
{
if (at(idx) == b.at(idx))
continue;
if (at(idx) < b.at(idx))
return -1;
if (at(idx) > b.at(idx))
return 1;
}
return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值