//1.HugeInt.h
#ifndef HUGEINT_H
#define HUGEINT_H
#include<array>
#include<iostream>
#include<string>
class HugeInt
{
friend std::ostream& operator<<(std::ostream&, const HugeInt&);
public:
static const int digits = 30;
HugeInt(long = 0);
HugeInt(const std::string&);
HugeInt operator+(const HugeInt&)const;
HugeInt operator+(int)const;
HugeInt operator+(const std::string&)const;
HugeInt operator-(const HugeInt&)const;
HugeInt operator-(int)const;
HugeInt operator-(const std::string&)const;
HugeInt operator*(const HugeInt&)const;
HugeInt operator*(int)const;
HugeInt operator*(const std::string&)const;
HugeInt operator/(const HugeInt&)const;
HugeInt operator/(int)const;
HugeInt operator/(const std::string&)const;
bool operator<(const HugeInt&)const;
bool operator<(int)const;
bool operator<(const std::string&)const;
bool operator>(const HugeInt&)const;
bool operator>(int)const;
bool operator>(const std::string&)const;
bool operator<=(const HugeInt&)const;
bool operator<=(int)const;
bool operator<=(const std::string&)const;
bool operator>=(const HugeInt&)const;
bool operator>=(int)const;
bool operator>=(const std::string&)const;
bool operator==(const HugeInt&)const;
bool operator==(int)const;
bool operator==(const std::string&)const;
private:
std::array<short, digits>integer;
};
#endif // !HUGEINT_H
//2.HugeInt.cpp
#include<cctype>
#include"HugeInt.h"
using namespace std;
HugeInt::HugeInt(long value)
{
for (short& element : integer)
element = 0;
for (size_t j = digits - 1;value != 0 && j >= 0;j--)
{
integer[j] = value % 10;
value /= 10;
}
}
HugeInt::HugeInt(const string& number)
{
for (short& elements : integer)
elements = 0;
size_t length = number.size();
for (size_t j = digits - length, k = 0;j < digits;++j, ++k)
if (isdigit(number[k]))
integer[j] = number[k] - '0';
}
HugeInt HugeInt::operator+(const HugeInt& op2)const
{
HugeInt temp;
int carry = 0;
for (int i = digits - 1;i >= 0;i--)
{
temp.integer[i] = integer[i] + op2.integer[i] + carry;
if (temp.integer[i] > 9)
{
temp.integer[i] %= 10;
carry = 1;
}
else
carry = 0;
}
return temp;
}
HugeInt HugeInt::operator+(int op2)const
{
return *this + HugeInt(op2);
}
HugeInt HugeInt::operator+(const string& op2)const
{
return *this + HugeInt(op2);
}
HugeInt HugeInt::operator-(const HugeInt& op2)const
{
HugeInt temp;
int carry = 0;
for (int i = digits-1;i >= 0;i--)
{
temp.integer[i] = integer[i] - op2.integer[i] - carry;
carry = 0;
if (temp.integer[i] < 0)
{
temp.integer[i] += 10;
carry = 1;
}
}
return temp;
}
HugeInt HugeInt::operator-(int op2)const
{
return *this - HugeInt(op2);
}
HugeInt HugeInt::operator-(const std::string& op2)const
{
return *this - HugeInt(op2);
}
ostream& operator<<(ostream& output, const HugeInt& num)
{
int i;
for (i = 0;(i < HugeInt::digits) && (0 == num.integer[i]);++i)
;
if (i == HugeInt::digits)
output << 0;
else
for (;i < HugeInt::digits;++i)
output << num.integer[i];
return output;
}
HugeInt HugeInt::operator*(const HugeInt& op2)const
{
HugeInt temp(0);
int carry = 0;
for (int i = digits-1;i >= 0;i--)
{
for (int j = digits-1;j >=0;j--)
{
if(29-(29-i+29-j)<=digits-1&&0<= 29 - (29 - i + 29 - j))
temp.integer[29-((29-i)+(29-j))] += (integer[i] * op2.integer[j]);
}
}
for (int k = digits-1;k >= 0;k--)
{
temp.integer[k] += carry;
if (temp.integer[k] > 9)
{
carry = temp.integer[k] / 10;
temp.integer[k] %= 10;
}
else
carry = 0;
}
return temp;
}
HugeInt HugeInt::operator*(int c)const
{
HugeInt classC(c);
return *this * classC;
}
HugeInt HugeInt::operator*(const std::string& c)const
{
HugeInt classC(c);
return *this * classC;
}
HugeInt HugeInt::operator/(const HugeInt& c)const
{
HugeInt temp(0);
HugeInt temp2(*this);
for (int j = 1;j <= 10;j++)
{
HugeInt temp1(0);
if (temp2 < c)
break;
if (temp2 < c * pow(10,j) )//必须是temp2<c*pow(10,j),否则会出现负数,导致程序炸掉
{
for (int p=1;p <= 9;p++)
{
if (temp2 < c * pow(10, j - 1) * p )
{
temp1 = temp2 - c * pow(10, j - 1)*(p-1);
temp2 = temp1;
temp.integer[29-j + 1] = p - 1;
break;
}
if (temp2 == c * pow(10, j - 1) * p )
{
temp.integer[29-j + 1] = p ;
return temp;
}
}
j = 1;
}
if (temp2 == c * pow(10, j) )
{
temp.integer[29-j] = j;
return temp;
}
}
return temp;
}
HugeInt HugeInt::operator/(int op)const
{
return *this / HugeInt(op);
}
HugeInt HugeInt::operator/(const std::string&op)const
{
return *this / HugeInt(op);
}
bool HugeInt::operator<(const HugeInt& op)const
{
for (int i = 0;i <= digits-1;i++)
{
if (integer[i] > op.integer[i])
{
return 0;
}
if (integer[i] < op.integer[i])
{
return 1;
}
}
return 0;
}
bool HugeInt::operator<(int op)const
{
return(*this < HugeInt(op));
}
bool HugeInt::operator<(const std::string& op)const
{
return(*this < HugeInt(op));
}
bool HugeInt::operator>(const HugeInt& op)const
{
for (int i = digits-1;i >=0 ;i)
{
if (integer[i] < op.integer[i])
return 0;
if (integer[i] > op.integer[i])
return 1;
}
return 0;
}
bool HugeInt::operator>(int op)const
{
return(*this > HugeInt(op));
}
bool HugeInt::operator>(const std::string& op)const
{
return(*this > HugeInt(op));
}
bool HugeInt::operator<=(const HugeInt& op)const
{
return((*this < op) || (*this == op));
}
bool HugeInt::operator<=(int op)const
{
return((*this < op) || (*this == op));
}
bool HugeInt::operator<=(const std::string& op)const
{
return((*this < op) || (*this == op));
}
bool HugeInt::operator>=(const HugeInt& op)const
{
return((*this > op) || (*this == op));
}
bool HugeInt::operator>=(int op)const
{
return((*this > op) || (*this == op));
}
bool HugeInt::operator>=(const std::string& op)const
{
return((*this > op) || (*this == op));
}
bool HugeInt::operator==(const HugeInt& op)const
{
bool flag = 1;
for (int i = digits - 1;i >= 0;i--)
{
if (integer[i] != op.integer[i])
flag = 0;
}
return flag;
}
bool HugeInt::operator==(int op)const
{
return(*this == HugeInt(op));
}
bool HugeInt::operator==(const std::string& op)const
{
return(*this == HugeInt(op));
}
//3.main.cpp
#include<iostream>
#include"Hugeint.h"
using namespace std;
int main()
{
HugeInt n1(7654321);
HugeInt n11(7654321);
HugeInt n2(7891234);
HugeInt n3("99999999999999999999999999999");
HugeInt n4("1");
HugeInt n5;
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\n\n";
n5 = n1 + n2;
cout << n1 << "+" << n2 << "=" << n5 << "\n\n";
cout << n3 << "+" << n4 << "\n=" << (n3 + n4) << "\n\n";
n5 = n1 + 9;
cout << n1 << "+" << 9 << "=" << n5 << "\n\n";
n5 = n2 + "10000";
cout << n2 << "+" << "10000" << "=" << n5 << endl;
cout << "n2 - n1 = " << n2 - n1 << endl;
cout << "n1 * 3 = " << n1 * 3 << endl;
cout << "n2 / n1 = " << n2 / n1 << endl;
cout << (n1 < n2) << endl;
cout << (n1 > n2) << endl;
HugeInt a1(900); HugeInt a2(45);HugeInt a3(90);HugeInt a4(1318902);
cout << a1 / a2 << endl;
cout << a1 / a3 << endl;
cout << a1 / a4 << endl;
return 0;
}
大概是一个初学者的作业存档
大整数类的乘法:
设a*b
则temp[i+j]=a[i]*b[j];
再将temp每一位判断是否有carry,再输出就行了。
大整数类的除法
这是全篇最难的地方,采用的方法是例如10000/45
则10000-4500*2=1000
商1=4500*2的那个2
商2=1000-450*2的那个2
商3=100-45*2的那个2
余10<45 over~
你看这个构造函数,integer是反着排的