• 大整数处理类(头文件)


    /////////////////////integer.h

    #include <iostream>

    #include <string>

    #include <cstring>

    #include <math.h>

    using namespace std;

    #define MAXSIZE   256

    /***************************

    * @class name: BigInteger

    * @author: LoongFee

    * @date: 2009-11-03

    ****************************/

    class BigInteger

    {

    public:

        bool sign;

        char modulus[MAXSIZE];

        int ndigit;

    public:

        BigInteger();                      //Defaulted constructor

        BigInteger(const char* a);                //Constructor with a value

        BigInteger(const int& a);

        BigInteger(const BigInteger& another); //Copy constructor

        ~BigInteger();                         //Destructor

     

        BigInteger abs(const BigInteger& another);

        BigInteger mod(const BigInteger& another);

        BigInteger gcd(const BigInteger& another);    //the greatest common divisor

        BigInteger gcd(const BigInteger& a1, const BigInteger& a2);

        BigInteger lcm(const BigInteger& another);    //the least common multiple

        BigInteger lcm(const BigInteger& a1, const BigInteger& a2);

     

        ////Overload operators

        BigInteger& operator = (const BigInteger& another);

        BigInteger& operator = (const string& another);

        BigInteger& operator = (const int& another);

        BigInteger operator + (const BigInteger& another);

        BigInteger& operator += (const BigInteger& another);

        BigInteger operator - (const BigInteger& another);

        BigInteger& operator -= (const BigInteger& another);

        BigInteger operator - ();

        BigInteger operator * (const BigInteger& another);

        BigInteger& operator *= (const BigInteger& another);

        BigInteger operator / (const BigInteger& another);

        BigInteger& operator /= (const BigInteger& another);

        BigInteger operator % (const BigInteger& another);

        BigInteger& operator %= (const BigInteger& another);

        BigInteger& operator ++ ();

        BigInteger& operator -- ();

     

        BigInteger operator ++(int);

        BigInteger operator --(int);

     

        bool operator >= (const BigInteger& another);

        bool operator <= (const BigInteger& another);

        bool operator == (const BigInteger& another);

        bool operator != (const BigInteger& another);

        bool operator > (const BigInteger& another);

        bool operator < (const BigInteger& another);

     

        friend ostream& operator <<(ostream& o, const BigInteger& myInt)

        {

           if(1 == myInt.sign)

               cout<<'-';

           cout<<myInt.modulus;

     

           return o;

        }

        friend istream& operator >>(istream& i, BigInteger& myInt)

        {

           char tmp[MAXSIZE];

           cin>>tmp;

     

           myInt.sign = (tmp[0] == '-') ? 1 : 0;

     

           int pos = myInt.sign;

     

           while('\0' != tmp[pos])

           {

               myInt.modulus[pos - myInt.sign] = tmp[pos++];

           }

     

           myInt.ndigit = (pos - myInt.sign);

           myInt.modulus[myInt.ndigit] = '\0';

     

           return i;

        }

    };
  • 相关阅读:
    单例模式
    leetcode:Minimum Subarray
    leetcode:Minimum Path Sum
    内存分配以及优化
    完整性检查
    类型定义之可选的一些笔记
    CSS|CSS module
    TS 操作符
    React|虚拟 Dom、render函数、shouldComponentUpdate
    React|常用相关框架
  • 原文地址:https://www.cnblogs.com/loongfee/p/1595664.html
Copyright © 2020-2023  润新知