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

部署运行你感兴趣的模型镜像

运行结果:

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;
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

// Lab 2: Hugeint.cpp // HugeInt member-function and friend-function definitions. #include <iostream> #include <cctype> // isdigit function prototype #include <cstring> // strlen function prototype using namespace std; #include "Hugeint.h" // HugeInt class definition // default constructor; conversion constructor that converts // a long integer into a HugeInt object HugeInt::HugeInt( long value ) { // initialize array to zero for ( int i = 0; i <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array for ( int j = 29; value != 0 && j >= 0; j-- ) { integer[ j ] = value % 10; value /= 10; } // end for } // end HugeInt default/conversion constructor // conversion constructor that converts a character string // representing a large integer into a HugeInt object HugeInt::HugeInt( const char *string ) { // initialize array to zero for ( int i = 0; i <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array int length = strlen( string ); for ( int j = 30 - length, k = 0; j <= 29; j++, k++ ) if ( isdigit( string[ k ] ) ) integer[ j ] = string[ k ] - &#39;0&#39;; } // end HugeInt conversion constructor // get function calculates length of integer int HugeInt::getLength() const { for ( int i = 0; i <= 29; i++ ) if ( integer[ i ] != 0 ) break; // break when first digit is reached return 30 - i; // length is from first digit (at i) to end of array } // end function getLength // addition operator; HugeInt + HugeInt HugeInt HugeInt::operator+( const HugeInt &op2 ) const { HugeInt temp; // temporary result int carry = 0; for ( int i = 29; i >= 0; i-- ) { temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry; // determine whether to carry a 1 if ( temp.integer[ i ] > 9 ) { temp.integer[ i ] %= 10; // reduce to 0-9 carry = 1; } // end if else // no carry carry = 0; } // end for return temp; // return copy of temporary object } // end function operator+ // addition operator; HugeInt + int HugeInt HugeInt::operator+( int op2 ) const { // convert op2 to a HugeInt, then invoke // operator+ for two HugeInt objects return *this + HugeInt( op2 ); } // end function operator+ // addition operator; // HugeInt + string that represents large integer value HugeInt HugeInt::operator+( const char *op2 ) const { // convert op2 to a HugeInt, then invoke // operator+ for two HugeInt objects return *this + HugeInt( op2 ); } // end function operator+ // equality operator; HugeInt == HugeInt /* Write a definition for the == operator */ // inequality operator; HugeInt != HugeInt /* Write a definition for the != operator by calling the == operator */ // less than operator; HugeInt < HugeInt /* Write a definition for the < operator */ // less than or equal operator; HugeInt <= HugeInt /* Write a definition for the <= operator by calling the < and == operators */ // greater than operator; HugeInt > HugeInt /* Write a definition for the > operator by calling the <= operator */ // greater than or equal operator; HugeInt >= HugeInt /* Write a definition for the >= operator by calling the > and == operators */ // overloaded output operator ostream& operator<<( ostream &output, const HugeInt &num ) { int i; for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ ) ; // skip leading zeros if ( i == 30 ) output << 0; else for ( ; i <= 29; i++ ) output << num.integer[ i ]; return output; } // end function operator<<
11-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值