例子:
#include "X:\Work\Share\CCode\CPlatform\MathExt\_MathExt_out.h"
using namespace lf;
using namespace std;
int main()
{
_Integer i1 = _t("12345678989764581381214575686787987979234234354364");
_Integer i2 = _t("1234567898976458138121");
_pcn(i1 / i2);
_Integer i3 = 6, i4 = -9, i5 = 234;
_pcn(i3);
if (i3 > i4)
{
std::cout << "i3 > i4" << "\n";
}
if (i3 > i5)
{
std::cout << "i3 > i5" << "\n";
}
for (_Integer i = 0; i < 10; i++)
{
_pcn(i);
}
for (_Integer i = 10; i > 0; --i)
{
_pcn(i);
}
return 0;
}
运行结果:
_Integer.h
/*******************************************************************************************
文件名 : _Integer.h
作者 : 李锋
手机 : 13828778863
Email : ruizhilf@139.com
功能 : 可以表示无限整数的类
创建时间 : 2024-04-15
最后一次修改时间 : 2024-04-28
********************************************************************************************/
#pragma once
#include "_MathExt_in.h"
_LF_BEGIN_
/// <summary>
/// 用链接表示的十进制数整数
/// </summary>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
class _Integer : public _Object
{
private:
/// <summary>
/// 表示当前数字字符串
/// </summary>
_string _num;
/// <summary>
/// 是否为负数
/// </summary>
bool _IsNegative;
void _InitData();
public:
/// <summary>
/// 【中文】:充许的书写格式
/// 【英文】:Allowed writing format
/// 001 不可以
/// -1 不可以
/// +5 不可以
/// 3a 不可以
/// 318,000 不可以
/// </summary>
/// <param name="sNum"></param>
/// <returns></returns>
/// 创建时间:2024-04-26 最后一次修改时间:2024-04-26
static bool IsAllowedWritingFormat(const _string& sNum);
static void Check(const _string& sNum);
/// <summary>
/// 无符号的两个整数相加 (参考自星飞星火)
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-21
static _string Add(const _string& num1, const _string& num2);
/// <summary>
/// 无符号的两个整数相减,部分算法参考自星飞星火,但星火
/// 算的结果只有同位数相减才正确,注意:这里是假定是无符
/// 号两个整数相减。
/// 【中文】:两个数相减
/// 【英文】:Subtract two numbers
/// 百度文心一言和星飞星火结果不对。
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-25 (已测试)
static _string Subtract(const _string& num1, const _string& num2, bool isCheckForamt = true);
/// <summary>
/// 无符号的两个整数相减(使用标准库)
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
static std::string Subtract_std(const std::string& num1, const std::string& num2);
/// <summary>
/// 无符号的两个整数相剩 (参考自星飞星火)
/// 【中文】:两个数相乘
/// 【英文】:Multiplication of two numbers
/// </summary>
/// <param name="sNum1"></param>
/// <param name="sNum2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-21 (已测试)
static _string Multiplication(const _string& sNum1, const _string& sNum2, bool isCheckForamt = true);
/// <summary>
/// sNum1 / sNum2 ,sNum1 的倍数受制于int所能表达的最大值
/// </summary>
/// <param name="sNum1"></param>
/// <param name="sNum2"></param>
/// <param name="isCheckForamt"></param>
/// <returns></returns>
/// 创建时间:2024-04-26 最后一次修改时间:2024-04-27 (已测试)
static _Pair<int, _string> SmallDiv(const _string& sNum1, const _string& sNum2, bool isCheckForamt = true);
/// <summary>
/// 【单词中英文对照】:两个数相除 - Divide two numbers
///
/// 百度文心一言和星飞星火结果不对。
/// </summary>
/// 创建时间:2024-04-25 最后一次修改时间:2024-04-26 (已测式,这个函数用了大约1整天时间,搞死人)
static _string Divide(const _string& sNum1, const _string& sNum2, bool isCheckForamt = true);
public://-----------------------------构造
_Integer();
_Integer(const _Integer& integer);
_Integer(const int& iValue);
_Integer(const _string& sNum);
/// <summary>
///
/// </summary>
/// <param name="pStr"></param>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer(const _char* pStr);
public:
public: //----------------------------运算符重载
/// <summary>
///
/// </summary>
/// <param name="iVlue"></param>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
bool operator==(const int& iVlue);
/// <summary>
///
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer operator*(const _Integer& r)const;
/// <summary>
///
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer operator/(const _Integer& r)const;
/// <summary>
///
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer operator+(const _Integer& r)const;
/// <summary>
///
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer operator-(const _Integer& r)const;
/// <summary>
/// 前置加加
/// </summary>
/// <returns></returns>
/// 创建时间:2024-04-28 最后一次修改时间:2024-04-28
_Integer& operator++();
/// <summary>
///
/// </summary>
/// <returns></returns>
/// 创建时间:2024-04-28 最后一次修改时间:2024-04-28
_Integer& operator--();
/// <summary>
/// 后置加加
/// </summary>
/// <param name=""></param>
/// <returns></returns>
/// 创建时间:2024-04-28 最后一次修改时间:2024-04-28
_Integer operator++(int);
/// <summary>
///
/// </summary>
/// <param name=""></param>
/// <returns></returns>
/// 创建时间:2024-04-28 最后一次修改时间:2024-04-28
_Integer operator--(int);
/// <summary>
/// 表达式:前缀负号 (- obj)
/// </summary>
/// <returns></returns>
/// 创建时间:2024-04-15 最后一次修改时间:2024-04-15
_Integer operator-() const;
/// <summary>
///
/// </summary>
/// <param name="l"></param>
/// <param name="r"></param>
/// <returns></returns>
/// 创建时间:2024-04-19 最后一次修改时间:2024-04-28
friend bool operator>(const _Integer& l, const _Integer& r);
/// <summary>
///
/// </summary>
/// <param name="l"></param>
/// <param name="r"></param>
/// <returns></returns>
/// 创建时间:2024-04-19 最后一次修改时间:2024-04-19
friend bool operator < (const _Integer& l, const _Integer& r);
/// <summary>
///
/// </summary>
/// <param name="l"></param>
/// <param name="r"></param>
/// <returns></returns>
/// 创建时间:2024-04-19 最后一次修改时间:2024-04-19
friend bool operator== (const _Integer& l, const _Integer& r);
friend bool operator!= (const _Integer& l, const _Integer& r);
public: //----------------------------功能函数
virtual _string ToSplitString(const _string& splitString) const override;
public:
};
_LF_END_
_Integer.cpp
#include"_Integer.h"
_LF_BEGIN_
void _Integer::_InitData()
{
_IsNegative = false;
}
bool _Integer::IsAllowedWritingFormat(const _string& sNum)
{
/// 001 不可以
/// -1 不可以
/// +5 不可以
/// 3a 不可以
/// 318,000 不可以
if (!sNum.IsAllArabicDigitString())
return false;
//001
if (sNum[0] == _t('0') && sNum.Length > 1) //排除0
return false;
return true;
}
void _Integer::Check(const _string& sNum)
{
if (sNum.Length == 0)
{
_string sErrorText = _t("sNum.Length == 0 传递空字符串。\n");
ga.Print(sErrorText);
assert(false);
}
if (!IsAllowedWritingFormat(sNum))
{
_string sErrorText = _t(" “") + sNum + _t("” 不是正确的书写格式!\n");
ga.Print(sErrorText);
assert(false);
}
}
/// <summary>
/// 无符号的两个整数相加 (参考自星飞星火)
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-28
_string _Integer::Add(const _string& num1, const _string& num2)
{
/*
std::string add(const std::string &num1, const std::string &num2) {
int len1 = num1.size();
int len2 = num2.size();
int maxLen = std::max(len1, len2);
std::string result(maxLen + 1, '0');
int carry = 0;
for (int i = 0; i < maxLen; i++) {
int sum = carry;
if (i < len1) {
sum += num1[len1 - 1 - i] - '0';
}
if (i < len2) {
sum += num2[len2 - 1 - i] - '0';
}
carry = sum / 10;
sum %= 10;
result[maxLen - i] = sum + '0';
}
if (carry > 0) {
result[0] = carry + '0';
} else {
result.erase(result.begin());
}
return result;
}
*/
int len1 = num1.std_size();
int len2 = num2.std_size();
int maxLen = _Math::Max(len1, len2);
_string result(maxLen + 1, _t('0'));
int carry = 0;
for (int i = 0; i < maxLen; i++) {
int sum = carry;
if (i < len1) {
sum += num1[len1 - 1 - i] - _t('0');
}
if (i < len2) {
sum += num2[len2 - 1 - i] - _t('0');
}
carry = sum / 10;
sum %= 10;
result[maxLen - i] = sum + _t('0');
}
if (carry > 0) {
result[0] = carry + _t('0');
}
else {
//result.std_erase(result.std_begin());
return result.SubStr(1, result.Length - 1);
}
return result;
}
/// <summary>
/// 无符号的两个整数相减,部分算法参考自星飞星火,但星火
/// 算的结果只有同位数相减才正确,注意:这里是假定是无符
/// 号两个整数相减。
/// 【中文】:两个数相减
/// 【英文】:Subtract two numbers
/// 百度文心一言和星飞星火结果不对。
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-25 (已测试)
_string _Integer::Subtract(const _string& num1, const _string& num2, bool isCheckForamt)
{
if (isCheckForamt){ Check(num1); Check(num2);}
/*
* 星火给出的代码:
*
int len1 = num1.size();
int len2 = num2.size();
int maxLen = std::max(len1, len2);
std::string result(maxLen, '0');
int iBorrow = 0;
for (int i = 0; i < maxLen; i++) {
int iDiff = num1[len1 - 1 - i] - num2[len2 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
result[maxLen - 1 - i] = iDiff + '0';
}
result.erase(std::find_if(result.rbegin(), result.rend(), [](char ch) { return ch != '0'; }).base(), result.end());
return result;
*/
int len1 = num1.Length;
int len2 = num2.Length;
int maxLen = _Math::Max(len1, len2);
_string sResult(_t(""), maxLen + 1);
//这里先比较字符串长度(数字位数)的大小
if (len1 > len2){ //num1 > num2
int iBorrow = 0;
for (int i = 0; i < len2; i++) {
int iDiff = num1[len1 - 1 - i] - num2[len2 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
sResult.Add(iDiff + _t('0'));
}
if (iBorrow != 0){
bool bBorrow = false;
for (int i = len2; i < len1; i++){
_char c = num1[len1 - i - 1];
if (!bBorrow){
if (c == _t('0')) {
sResult.Add(_t('9'));
}else{
sResult.Add(c - 1);
bBorrow = true;
}
}else{
sResult.Add(c);
}
}
}else{
for (int i = len2; i < len1; i++){
_char c = num1[len1 - i - 1];
sResult.Add(c);
}
}
sResult.TrimRight(_t('0')); //除去尾部的0
return sResult.Reversal();
}else if(len1 == len2){//字符长度相等
if (num1 > num2){
int iBorrow = 0;
for (int i = 0; i < len2; i++) {
int iDiff = num1[len1 - 1 - i] - num2[len2 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
sResult.Add(iDiff + _t('0'));
}
sResult.TrimRight(_t('0')); //除去尾部的0
return sResult.Reversal();
}else if(num1 == num2){
return _t("0");
}else{
int iBorrow = 0;
for (int i = 0; i < len2; i++) {
int iDiff = num2[len1 - 1 - i] - num1[len2 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
sResult.Add(iDiff + _t('0'));
}
sResult.TrimRight(_t('0')); //除去尾部的0
sResult.Add(_t('-'));
return sResult.Reversal();
}
}else{ //len1 < len2 小数减大数
int iBorrow = 0;
for (int i = 0; i < len1; i++) {
int iDiff = num2[len2 - 1 - i] - num1[len1 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
sResult.Add(iDiff + _t('0'));
}
if (iBorrow != 0) { //还需借一位
bool bBorrow = false;
for (int i = len1; i < len2; i++) {
_char c = num2[len2 - i - 1];
if (!bBorrow) {
if (c == _t('0')) {
sResult.Add(_t('9'));
}
else {
sResult.Add(c - 1);
bBorrow = true;
}
}
else {
sResult.Add(c);
}
}
}
else {
for (int i = len1; i < len2; i++) {
_char c = num2[len2 - i - 1];
sResult.Add(c);
}
}
sResult.TrimRight(_t('0'));
sResult.Add(_t("-"));
return sResult.Reversal();
}
}
std::string _Integer::Subtract_std(const std::string& num1, const std::string& num2)
{
int len1 = num1.size();
int len2 = num2.size();
int maxLen = std::max(len1, len2);
std::string result(maxLen, '0');
int iBorrow = 0;
for (int i = 0; i < maxLen; i++) {
int iDiff = num1[len1 - 1 - i] - num2[len2 - 1 - i] - iBorrow;
if (iDiff < 0) {
iDiff += 10;
iBorrow = 1;
}
else {
iBorrow = 0;
}
result[maxLen - 1 - i] = iDiff + '0';
}
result.erase(std::find_if(result.rbegin(), result.rend(), [](char ch) { return ch != '0'; }).base(), result.end());
return result;
}
/// <summary>
/// 无符号的两个整数相剩 (参考自星飞星火)
/// 【中文】:两个数相乘
/// 【英文】:Multiplication of two numbers
/// </summary>
/// <param name="sNum1"></param>
/// <param name="sNum2"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-21 (已测试)
_string _Integer::Multiplication(const _string& sNum1, const _string& sNum2, bool isCheckForamt)
{
if (isCheckForamt) { Check(sNum2); Check(sNum2); }
/*
int len1 = num1.size();
int len2 = num2.size();
std::vector<int> result(len1 + len2, 0);
for (int i = len1 - 1; i >= 0; i--) {
for (int j = len2 - 1; j >= 0; j--) {
int product = (num1[i] - '0') * (num2[j] - '0');
int sum = result[i + j + 1] + product;
result[i + j + 1] = sum % 10;
result[i + j] += sum / 10;
}
}
std::string res;
for (int num : result) {
if (!res.empty() || num != 0) {
res.push_back(num + '0');
}
}
return res.empty() ? "0" : res;
*/
size_t len1 = sNum1.Length;
size_t len2 = sNum2.Length;
_Array<_byte> result(len1 + len2); //设置缓冲长度为 len1 + len2
result.ZeroBufferAll(); //把缓冲区初始为0
result.ResetLength(len1 + len2); //重设长度
/*
123
11
33 (3) i = 2 j = 1(个位+3) i + j = 3 刚好是第4位 (3) i = 2 j = 0 (十位加3)
22
11
________
1353
*/
for (int i = len1 - 1; i >= 0; i--) {
for (int j = len2 - 1; j >= 0; j--) {
int product = (sNum1[i] - '0') * (sNum2[j] - '0'); //总量 n * n
int sum = result[i + j + 1] + product; //
result[i + j + 1] = sum % 10;
result[i + j] += sum / 10;
}
}
_string sTmp(_t(""), result.Length);
/*
//15*15 = 0225 (第一位是0)
size_t nStart = (*result.Data == 0) ? 1 : 0;
for(size_t n = nStart; n < result.Length; ++n)
sTmp.Add( *(result.Data + n) + 48);
*/
for (_byte b : result) {
if (sTmp.Length > 0 || b != 0) { //如果前面有非零的数字,则添加
sTmp.Add(b + 48);
}
}
return sTmp.Length == 0 ? _t("0") : sTmp;
}
/// <summary>
/// sNum1 / sNum2 ,sNum1 的倍数受制于int所能表达的最大值
/// </summary>
/// <param name="sNum1"></param>
/// <param name="sNum2"></param>
/// <param name="isCheckForamt"></param>
/// <returns></returns>
/// 创建时间:2024-04-26 最后一次修改时间:2024-04-27 (已测试)
_Pair<int, _string> _Integer::SmallDiv(const _string& sNum1, const _string& sNum2, bool isCheckForamt)
{
if (isCheckForamt) { Check(sNum1); Check(sNum2); }
if (sNum2 == _t("0"))
{
d.PrintError(_t("除数不能为零"), _t("_Integer::SmallDiv"));
assert(false);
}
if (sNum1.Length < sNum2.Length)
return _Pair<int, _string>(0, sNum1);
if (sNum1.Length == sNum2.Length)
{
if (sNum1 > sNum2)
{
int nNear = (sNum1[0] - 48) / (sNum2[0] - 48 ) + 1;
for (int n = nNear; n > 0; --n)
{
_string sn = ga.intToString(n);
_string s = _Integer::Multiplication(sNum2, sn, false); //乘积
_string r = _Integer::Subtract(sNum1, s, false); //余数
if (r[0] == _t('-'))
{
}
else if (r == _t("0"))
{
return _Pair<int, _string>(n, _t("0"));
}
else
{
if (r.Length < sNum2.Length)
{
return _Pair<int, _string>(n, r);
}
else if (r.Length == sNum2.Length)
{
if (r < sNum2)
{
return _Pair<int, _string>(n, r);
}
else if (r == sNum2)
{
return _Pair<int, _string>(n + 1, _t("0"));
}
}
}
}
}
else if (sNum1 == sNum2)
{
return _Pair<int, _string>(1, _t("0"));
}
else
{
return _Pair<int, _string>(0, sNum1);
}
}
else //sNum1.Length > sNum2.Length
{
int nNear = _Math::pow(10, sNum1.Length - sNum2.Length) * (sNum1[0] -48 + 1) - 1;
//_pin(nNear);
for (int n = nNear; n > 0; --n)
{
_string sn = ga.intToString(n);
_string s = _Integer::Multiplication(sNum2, sn, false); //乘积
_string r = _Integer::Subtract(sNum1, s, false); //余数
if (r[0] == _t('-'))
{
}
else if (r == _t("0"))
{
return _Pair<int, _string>(n, _t("0"));
}
else
{
if (r.Length < sNum2.Length)
{
return _Pair<int, _string>(n, r);
}
else if (r.Length == sNum2.Length)
{
if (r < sNum2)
{
return _Pair<int, _string>(n, r);
}
else if (r == sNum2)
{
return _Pair<int, _string>(n + 1, _t("0"));
}
}
}
}
}
std::wcout << _t("错误:\n");
_pcn(sNum1);
_pcn(sNum2);
assert(false);
return _Pair<int, _string>(0, _t("错误!"));
}
/// <summary>
/// 【单词中英文对照】:两个数相除 - Divide two numbers
///
/// 百度文心一言和星飞星火结果不对。
/// </summary>
/// 创建时间:2024-04-25 最后一次修改时间:2024-04-26 (已测式,这个函数用了大约1整天时间,搞死人)
_string _Integer::Divide(const _string& sNum1, const _string& sNum2,bool isCheckForamt)
{
if (isCheckForamt) { Check(sNum1); Check(sNum2); }
if (sNum2 == _t("0"))
{
_string sError = _t("_string _Integer::Divide: 除数 sNum2 不能为零不能为 0 !");
_cout << sError <<"\n";
assert(false);
}
_string sResult(_t(""), sNum1.Length);
//2024/4 nPos = 4-1 = 3 sSub = 24 Point = 2 - 1 - 1(2<4) = 0 s = sSub ""
//2124/4 nPos = 4-1 = 3
//2124/23 nPos = 4-2 - 1 = 1
_string sSub = sNum1.SubStr(0, sNum2.Length); //除数
auto p = SmallDiv(sSub, sNum2, false);
int nStart = sNum2.Length;
//获取第一位,确保第一次 p.Fist不等于0
if (p.First == 0) {
sSub = sNum1.SubStr(0, sNum2.Length + 1);
p = SmallDiv(sSub, sNum2, false);
nStart = sNum2.Length + 1;
}
for( int i = nStart; i <= sNum1.Length; ++i){
if (p.First > 0) {//商不为0
sResult.Add(_tostr(p.First));
if (p.Second == _t("0")) { //商不为0,余数为0
sSub = sNum1.SubStr(i, 1);
}else{ //商不为0,余数不为0
sSub = p.Second + sNum1.SubStr(i, 1);
}
if(sSub.Length > 0)
p = SmallDiv(sSub, sNum2, false);
}else { //--------------------------------商为零
sResult.Add(_t("0"));
if (p.Second == _t("0")) { //--------------商为0,余数为0
sSub = sNum1.SubStr(i, 1);
}
else { //----------------------------商为0,余数不为0
sSub = p.Second + sNum1.SubStr(i, 1);
}
if (sSub.Length > 0)
p = SmallDiv(sSub, sNum2, false);
}
}
return sResult;
}
_Integer::_Integer()
{
_InitData();
_num = "0";
}
_Integer::_Integer(const _Integer& integer)
{
_num = integer._num;
_IsNegative = integer._IsNegative;
}
_Integer::_Integer(const int& iValue)
{
if (iValue < 0){
_IsNegative = true;
_num = _tostr(-iValue);
}else{
_IsNegative = false;
_num = _tostr(iValue);
}
}
_Integer::_Integer(const _string& sNum)
{
assert(sNum.Length > 0);
if (sNum[0] == _t('-')){
_IsNegative = true;
_num = sNum.SubStr(1, sNum.Length - 1);
}else{
_IsNegative = false;
_num = sNum;
}
Check(_num);
}
_Integer::_Integer(const _char* pStr)
{
assert(pStr != null);
_string sNum(pStr);
assert(sNum.Length != 0);
if (sNum[0] == _t('-')) {
_IsNegative = true;
_num = sNum.SubStr(1, sNum.Length - 1);
}
else {
_IsNegative = false;
_num = sNum;
}
Check(_num);
}
bool _Integer::operator==(const int& iVlue)
{
if (iVlue > 0) {
_string sNum = _tostr(iVlue);
return _num == sNum && _IsNegative == false;
}else {
_string sNum = _tostr(-iVlue);
return _num == sNum && _IsNegative == true;
}
}
_Integer _Integer::operator*(const _Integer& right) const
{
_Integer sum;
sum._num = Multiplication(_num, right._num);
sum._IsNegative = !((_IsNegative && right._IsNegative) || (!_IsNegative && !right._IsNegative));
return sum;
}
_Integer _Integer::operator/(const _Integer& r) const
{
_Integer tmp;
tmp._num = _Integer::Divide(_num, r._num);
tmp._IsNegative = (_IsNegative != r._IsNegative);
return tmp;
}
_Integer _Integer::operator+(const _Integer& right) const
{
if (_IsNegative == false && right._IsNegative == false) {//丙个正数相加
_Integer tmp;
tmp._num = Add(_num, right._num);
return tmp;
}
else if (_IsNegative == false && right._IsNegative == true) { //正数+负数
return *this - (-right);
}
else if (_IsNegative == true && right._IsNegative == false) { //负数+正数
return right - (-*this);
}
else if (_IsNegative == true && right._IsNegative == true) { //两个负数相加
return -((-*this) + (-right));
}
}
_Integer _Integer::operator-(const _Integer& right) const
{
if (*this < right)
{
return -(right - *this);
}else{
if (_IsNegative == false && right._IsNegative == false) {//丙个正数相减
return Subtract(_num, right._num);
}
else if (_IsNegative == false && right._IsNegative == true) { //正数-负数
return *this + (-right);
}
else if (_IsNegative == true && right._IsNegative == false) { //负数-正数
return -((-*this) + right);
}
else if (_IsNegative == true && right._IsNegative == true) { //两个负数相减
return (-right) - *this;
}
}
}
/// <summary>
/// 前置加加
/// </summary>
/// <returns></returns>
_Integer& _Integer::operator++()
{
_num = Add(_num, "1");
return *this;
}
_Integer& _Integer::operator--()
{
_num = Subtract(_num, "1");
return *this;
}
_Integer _Integer::operator++(int)
{
_string sNum = _num;
_num = Add(_num, "1");
return sNum;
}
_Integer _Integer::operator--(int)
{
_string sNum = _num;
_num = Subtract(_num, "1");
return sNum;
}
_Integer _Integer::operator-() const
{
//前缀负号
_Integer iResult = *this;
iResult._IsNegative = !iResult._IsNegative;
return iResult;
}
_string _Integer::ToSplitString(const _string& splitString) const
{
if (_IsNegative)
return _t("-") + _num;
else
return _num;
}
bool operator>(const _Integer& l, const _Integer& r)
{
if (l._IsNegative == true && r._IsNegative == true) // - - 两个负数
{
if (l._num.Length > r._num.Length)
return false;
else if (l._num.Length == r._num.Length)
return l._num < r._num;
else
return true;
}
else if (l._IsNegative == true && r._IsNegative == false) //- +
{
return false;
}
else if (l._IsNegative == false && r._IsNegative == true) //+ -
{
if (l._num != _t("") || r._num != _t(""))
return true;
else
return false;
}
else if (l._IsNegative == false && r._IsNegative == false) //+ +
{
if (l._num.Length > r._num.Length)
return true;
else if (l._num.Length == r._num.Length)
return l._num > r._num;
else
return false;
}
return false;
}
bool operator<(const _Integer& l, const _Integer& r)
{
if (l > r)
return false;
if (l == r)
return false;
return true;
}
bool operator==(const _Integer& l, const _Integer& r)
{
return l._IsNegative == r._IsNegative && l._num == r._num;
}
bool operator!=(const _Integer& l, const _Integer& r)
{
if (l == r)
return false;
return true;
}
_LF_END_