目录
完整版源代码
源.cpp
#include <iostream>
#include "Hugeint.h"
using namespace std;
int main()
{
HugeInt n1(7654321);
HugeInt n2(7891234);
HugeInt n3("99999999999999999999999999999");
HugeInt n4("1");
HugeInt n5("12341234");
HugeInt n6("7888");
HugeInt result;
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\nn6 is " << n6
<< "\nresult is " << result << "\n\n";
// test relational and equality operators
if (n1 == n2)
cout << "n1 equals n2" << endl;
if (n1 != n2)
cout << "n1 is not equal to n2" << endl;
if (n1 < n2)
cout << "n1 is less than n2" << endl;
if (n1 <= n2)
cout << "n1 is less than or equal to n2" << endl;
if (n1 > n2)
cout << "n1 is greater than n2" << endl;
if (n1 >= n2)
cout << "n1 is greater than or equal to n2" << endl;
result = n1 + n2;
cout << n1 << " + " << n2 << " = " << result << "\n\n";
cout << n3 << " + " << n4 << "\n= " << (n3 + n4) << "\n\n";
result = n1 + 9;
cout << n1 << " + " << 9 << " = " << result << endl;
result = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << result << endl;
result = n5 * n6;
cout << n5 << " * " << n6 << " = " << result << endl;
result = n5 - n6;
cout << n5 << " - " << n6 << " = " << result << endl;
result = n5 / n6;
cout << n5 << " / " << n6 << " = " << result << endl;
}
HugeInt.h
#pragma once
#include <iostream>
#ifndef HUGEINT_H
#define HUGEINT_H
using namespace std;
class HugeInt
{
friend ostream& operator<<(ostream&, const HugeInt&);
public:
HugeInt(long x= 0); // conversion/default constructor
HugeInt(const char*); // conversion constructor
// addition operator; HugeInt + HugeInt
HugeInt operator=(const HugeInt&);
HugeInt operator+(const HugeInt&) const;
// addition operator; HugeInt + int
HugeInt operator+(long) const;
// addition operator;
// HugeInt + string that represents large integer value
HugeInt operator+(const char*) const;
bool operator==(const HugeInt&) const; // equality operator
bool operator!=(const HugeInt&) const; // inequality operator
bool operator<(const HugeInt&) const; // less than operator
// less than or equal to operator
bool operator<=(const HugeInt&) const;
bool operator>(const HugeInt&) const; // greater than operator
// greater than or equal to operator
bool operator>=(const HugeInt&) const;
HugeInt operator-(const HugeInt&) const; // subtraction operator
HugeInt operator-(long) const;
HugeInt operator-(const char*) const;
HugeInt operator*(const HugeInt&) const; // multiply two HugeInts
HugeInt operator*(long) const;
HugeInt operator*(const char*) const;
HugeInt operator/(const HugeInt&) const; // divide two HugeInts
int getLength() const;
void updatelength();
private:
int integer[30];
int length = 0;
}; // end class HugeInt
#endif
HugeInt.cpp
#include "HugeInt.h"
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
HugeInt::HugeInt(long x)// conversion/default constructor
{
memset(integer, 0, sizeof(integer));
//int num;
for (int i = 29; x != 0; i--)
{
integer[i] = x % 10;
x = x / 10;
length++;
}
}
HugeInt::HugeInt(const char* x) // conversion constructor
{
memset(integer, 0, sizeof(integer));
length = strlen(x);
int len = length;
for (int i = 29; len > 0; i--)
{
integer[i] = x[len - 1] - '0';
len--;
}
}
// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator=(const HugeInt& x)
{
if (this != &x)
{
for (int i = 0; i <= 29; i++)
{
integer[i] = x.integer[i];
}
length = x.length;
}
return *this;
}
HugeInt HugeInt::operator+(const HugeInt& x) const
{
HugeInt temp;
int carry=0;
for (int i = 29; i >= 0; i--)
{
temp.integer[i] = integer[i] + x.integer[i]+carry;
carry = 0;
if (temp.integer[i] > 9)
{
temp.integer[i] %= 10;
carry = 1;
}
}
temp.updatelength();
return temp;
}
// addition operator; HugeInt + int
HugeInt HugeInt::operator+(long x) const
{
return *this + HugeInt(x);
}
// addition operator;
// HugeInt + string that represents large integer value
HugeInt HugeInt::operator+(const char* x) const
{
return *this + HugeInt(x);
}
bool HugeInt::operator==(const HugeInt& x) const // equality operator
{
for (int i = 0; i <= 29; i++)
{
if (integer[i] != x.integer[i])
return false;
}
return true;
}
bool HugeInt::operator!=(const HugeInt& x) const // inequality operator
{
return !(*this == x);
}
bool HugeInt::operator<(const HugeInt& x) const // less than operator
{
for (int i = 0; i <= 29; i++)
{
if (integer[i] == x.integer[i])
continue;
else if (integer[i] > x.integer[i])
return false;
else
return true;
}
return false;
}
/*bool HugeInt::operator<(const HugeInt& x) const // less than operator
{
for (int i = 0; i <= 29; i++)
{
if (integer[i] > x.integer[i])
return false;
}
if (*this == x)
return false;
return true;
}*/
// less than or equal to operator
bool HugeInt::operator<=(const HugeInt& x) const
{
return ((*this == x) || (*this < x));
}
bool HugeInt::operator>(const HugeInt& x) const // greater than operator
{
return !(*this <= x);
}
// greater than or equal to operator
bool HugeInt::operator>=(const HugeInt& x) const
{
return !(*this < x);
}
HugeInt HugeInt::operator-(const HugeInt& x) const // subtraction operator
{
HugeInt temp;
int borrow = 0;
for (int i = 29; i >= 0; i--)
{
temp.integer[i] = integer[i] - x.integer[i] - borrow;
borrow = 0;
if (temp.integer[i] < 0)
{
temp.integer[i] += 10;
borrow = 1;
}
}
temp.updatelength();
return temp;
}
HugeInt HugeInt::operator-(long x) const
{
return *this - HugeInt(x);
}
HugeInt HugeInt::operator-(const char* x) const
{
return *this - HugeInt(x);
}
HugeInt HugeInt::operator*(const HugeInt& x) const // multiply two HugeInts
{
HugeInt temp;
for (int i = 29; i >= 29-length+1; i--)
{
int len = 0;
//cout << x.length << "\n\n";
for (int j = 29; j >= 29 - x.length + 1; j--)
{
HugeInt temp2;
int temp3 = integer[i] * x.integer[j];
//cout << temp3 << "\n\n";
if (temp3 < 10)
{
temp2.integer[i-len] = temp3;
//cout << temp2 << "\n\n";
}
else
{
temp2.integer[i-len ] = temp3 % 10;
temp3 = temp3 / 10;
temp2.integer[i -len - 1] = temp3;
}
//cout << temp2 << "\n\n";
temp = temp + temp2;
len++;
}
}
temp.updatelength();
return temp;
}
HugeInt HugeInt::operator*(long x) const
{
return *this * HugeInt(x);
}
HugeInt HugeInt::operator*(const char* x) const
{
return *this * HugeInt(x);
}
HugeInt HugeInt::operator/(const HugeInt& x) const// divide two HugeInts
{
HugeInt temp;
HugeInt temp1(*this);
HugeInt num("10");
//cout << num << "\n\n";
/*HugeInt aa;
HugeInt bb;
aa = x * num;
bb = aa;
cout << aa << "\n\n";
cout << num << "\n\n";
cout << bb*num << "\n\n";*/
if (length < x.length)
{
cout << "Invalid input" << endl;
exit(0);
}
//cout << length << " " << x.length << endl;
for (int i = 29 - length+x.length ; i <= 29; i++)
{
//cout << i << " 123" << "\n";
HugeInt temp2;
temp2 = x;
//cout << temp2 << "\n\n";
for (int j = i; j<29; j++)
{
temp2 = temp2 * num;
//cout << num << "\n";
}
int cnt = 1;
//cout << temp2 << "\n";
HugeInt value;
//cout << value << "\n\n";
for (; cnt <= 10; cnt++)
{
value = temp2 * HugeInt(cnt);
if (temp1<value)
{
break;
}
}
temp1 = temp1 - value + temp2;;
temp.integer[i] = cnt-1;
//cout << temp2 * HugeInt(cnt - 1) << " 123\n\n";
//cout << temp1 << " 123\n\n";
}
temp.updatelength();
return temp;
}
int HugeInt::getLength() const
{
return length;
}
void HugeInt::updatelength()
{
int i = 0;
for (; i <= 29; i++)
{
if (integer[i] != 0)
break;
}
length = 29 - i + 1;
}
ostream& operator<<(ostream& cout, const HugeInt& x)
{
int i = 0;
for (; i <= 29; i++)
{
if (x.integer[i] != 0)
break;
}
if (i == 30)
cout << "0";
for (; i <= 29; i++)
{
cout << x.integer[i];
}
return cout;
}