# C++ How to program 9E<10.9 hugeInt Class 函数运算符重载>

C++ 专栏收录该内容
51 篇文章 0 订阅

a = 9000
a + 100 = 9100
a + 834 = 9834
100 + a = 9100
bye = 9000
Overflow, Product is over than 4-digits.

main.cpp

#include <iostream>
#include <stdexcept>
#include "Huge.hpp"
#include <string>

int main() {
try {
HugeInt a(9000);
HugeInt sum;
int b = 100;
std::string s = "834";

std::cout << "a = " << a << '\n';

sum = a + b;
std::cout <<  "a + " << b  << " = " << sum << '\n';

sum = a + s;
std::cout <<  "a + " << s  << " = " << sum << '\n';

sum = b + a;
std::cout << b  << " + " << "a = " << sum << '\n';

HugeInt bye(a);
std::cout << "bye = " << bye << '\n';

HugeInt x(111);
HugeInt y(132);
HugeInt z;
z =  x * y;
std::cout << x << " * " << y << " = " << z << '\n';

//        HugeInt x(4280);
//        std::string y = "8239";
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(4321);
//        int y = 1234;
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(4321);
//        int y = 1234;
//        HugeInt z;
//        z =  y - x;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(4321);
//        int y = 1234;
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(1234);
//        int y = 4321;
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(1234);
//        HugeInt y(3227);
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(3227);
//        HugeInt y(1234);
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

//        HugeInt x(1234);
//        HugeInt y(1234);
//        HugeInt z;
//        z =  x - y;
//        std::cout << x << " - " << y << " = " << z << '\n';

} catch (std::overflow_error &e) {
std::cout << e.what();
}

return 0;
}

Huge.hpp

#ifndef Huge_hpp
#define Huge_hpp

#include <iostream>
#include <array>
#include <string>

class HugeInt {
friend std::ostream& operator << (std::ostream&, const HugeInt&);
friend HugeInt operator + (const int, const HugeInt&);
friend HugeInt operator - (const int, const HugeInt&);
public:
static const int digits = 4;

HugeInt(long = 0);
HugeInt(const std::string&);

HugeInt operator + (const int) const;
HugeInt operator + (const HugeInt&) const;
HugeInt operator + (const std::string&) const;

HugeInt operator - (const int) const;
HugeInt operator - (const HugeInt&) const;
void difference(const HugeInt&);
HugeInt operator - (const std::string&) const;

HugeInt operator * (const HugeInt&) const;

private:
std::array<short, digits> integer;
char negative;  // 存储数字正负号，默认为正
};

#endif

Huge.cpp

#include "Huge.hpp"
#include <ostream>
#include <stdexcept> // 异常处理
#include <string>
#include <cctype>   // isdigit(), 检测（）内的参数是否为数字
#include <cstdlib>  // abs()，将（）内的参数转换为正数

HugeInt:: HugeInt(long value) {
// 将integer数组内容全部初始化为0
for (short& element : integer)
element = 0;

negative = '+';  // 默认设置为正数

for (int j = digits - 1; j >= 0 && value != 0; j--) {
integer[j] = value % 10;
value /= 10;
}
if (value > 0)
throw std::overflow_error ("Overflow, It is over than 4-digits number.\n");
}

HugeInt::HugeInt(const std::string& number) {
for (short& element : integer)
element = 0;

negative = '+';

if (number.size() > integer.size())
throw std::overflow_error("Overflow, It is over than 4-digits number.\n");
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';
else
throw std::invalid_argument("Not a digit number.\n");
}
}

HugeInt HugeInt::operator + (const HugeInt& obj) const {
HugeInt sum;
short k = 0;

for (int i = digits - 1; i >= 0; i--) {
sum.integer[i] = obj.integer[i] + integer[i] + k;
if (sum.integer[i] > 9) {
if (i == 0)
throw std::overflow_error("Overflow, Sum is over than 4-digits.\n");
k = 1;
sum.integer[i] %= 10;
} else
k = 0;
}

return sum;
}

HugeInt HugeInt::operator + (const int n) const {return *this + HugeInt(n);}

HugeInt operator + (const int n, const HugeInt& obj) {return obj + n;}

HugeInt HugeInt::operator + (const std::string& s) const {return *this + HugeInt(s);}

HugeInt HugeInt::operator - (const HugeInt& obj) const {
HugeInt diff(*this);
//比较对象*this与obj，始终使大的对象减去小的对象
for (int j = 0; j < digits; j++) {
if (integer[j] > obj.integer[j]) {
diff.difference(obj);
break;
}
else if (integer[j] < obj.integer[j]) {
diff = obj;
diff.negative = '-';
diff.difference(*this);
break;
}
}

return diff;
}

void HugeInt::difference(const HugeInt& obj) {
int j;
for (int i = digits - 1; i >= 0 ; i--) {
if (integer[i] >= obj.integer[i])
integer[i] -= obj.integer[i];
else {
for (j = i - 1; j >= 0; j--) {
if (integer[j] > 0) {
integer[j]--;
break;
}
}
if (j < 0) {
integer[i] = abs(integer[i] - obj.integer[i]);
negative = '-';
break;
} else {
for (int k = j + 1; k < i; k++)
integer[k] += 9;
integer[i]  += 10 - obj.integer[i];
}
}
}
}

HugeInt HugeInt::operator - (const int n) const {return *this - HugeInt(n);}

HugeInt operator - (const int n, const HugeInt& obj) {return obj - n;}

HugeInt HugeInt::operator - (const std::string& s) const {return *this - HugeInt(s);}

HugeInt HugeInt::operator * (const HugeInt& obj) const {
HugeInt product;

for (int i = digits - 1; i >= 0; i--) {
static int m = 0;
int k = 0;

for (int j = digits - 1; j >= 0; j--) {
if (j - m < 0) {
if (integer[j] * obj.integer[i] + k > 0)
throw std::overflow_error("Overflow, Product is over than 4-digits.\n");
else
break;
}
product.integer[j - m] += integer[j] * obj.integer[i] + k;
if (product.integer[j] > 9) {
if (j == 0)
throw std::overflow_error("Overflow, Product is over than 4-digits.\n");
k = product.integer[j] /10;
product.integer[j] %= 10;
} else
k = 0;
}
m++;
}

return product;
}

std::ostream& operator << (std::ostream& out, const HugeInt& obj) {
if (obj.negative == '-')
out << '-';

size_t key = 0;

for (size_t i = 0, j = 0; i < obj.integer.size(); i++) {
if (obj.integer[j] == 0) {
j++;
continue;
} else {
key++;
out << obj.integer[i];
}
}

if (key == 0)
out << key;

return out;
}
• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

03-09 1358

05-14 27
10-12 751
08-21 119
08-23 192
07-13 979
11-08 456
08-06 3789

Okwarry

¥2 ¥4 ¥6 ¥10 ¥20

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