zoukankan      html  css  js  c++  java
  • 大整数处理类(头文件)

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

        }

    };
  • 相关阅读:
    zookeeper分布式锁和服务优化配置
    【转】从Mac/OS和iOS开放源码浅谈UNIX家谱
    【转】深入了解CPU两大架构ARM与X86
    【转】volatile关键字。编译器不优化,多线程会改。防止随时变动的
    栈,寄存器,局部变量,内存,语言级别优化程序的方法
    在coursera上有哪些值得推荐的课程
    【转】楼天城楼教主的acm心路历程(作为励志用)
    硬中断软中断
    CPU GPU FPU TPU 及厂商
    C#中使用DLL相关问题
  • 原文地址:https://www.cnblogs.com/loongfee/p/1595664.html
Copyright © 2011-2022 走看看