现在的位置: 首页 > 综合 > 正文

大整数类的实现(2)(原创)

2013年12月13日 ⁄ 综合 ⁄ 共 10530字 ⁄ 字号 评论关闭
 3.SignedHugeLong.h

————————————————————————————————————

/*
 * Copyright (c) 2005 by DoZerg.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 */
#pragma once
#include "HugeNumberBase.h"

namespace DoZerg{
//For operator +,-,*,/ and % implementation
#define SIGNED_MEMBER_DEFINE_OPERATOR(oper)    /
    template<typename __ValueType>    /
        const SignedHugeLong operator ##oper##(const __ValueType & value) const{    /
            return SignedHugeLong<N,__Alloc>(*this).operator ##oper##=(value);    /
        }    /
    const SignedHugeLong operator ##oper##(const SignedHugeLong & value) const{    /
        return SignedHugeLong<N,__Alloc>(*this).operator ##oper##=(value);    /
    }   
#define SIGNED_GLOBAL_DEFINE_OPERATOR(oper)    /
    template<typename __ValueType,long N,class T>    /
        inline const SignedHugeLong<N,T> operator ##oper##(const __ValueType & value1,const SignedHugeLong<N,T> & value2){    /
            return SignedHugeLong<N,T>(value1).operator ##oper##=(value2);    /
        }
//declaration.
    template<
        long N,
        template<long,typename>class __Alloc = DataAutoSelect
    >class SignedHugeLong:public HugeNumberBase<N,__Alloc>
    {
    public:
        SignedHugeLong(){}
        template<typename __ValueType>
            SignedHugeLong(const __ValueType & value):HugeNumberBase<N,__Alloc>(value){}
        const SignedHugeLong & operator =(const std::string &);
        const SignedHugeLong AbsValue() const;
        const SignedHugeLong operator +() const;
        const SignedHugeLong operator -() const;
        const SignedHugeLong operator ~() const;
        const SignedHugeLong operator >>(long) const;
        const SignedHugeLong operator <<(long) const;
        const SignedHugeLong & operator ++();
        const SignedHugeLong operator ++(int);
        const SignedHugeLong & operator --();
        const SignedHugeLong operator --(int);
        const SignedHugeLong & operator >>=(long);
        const SignedHugeLong & operator <<=(long);
        const SignedHugeLong & operator +=(const SignedHugeLong &);
        const SignedHugeLong & operator -=(const SignedHugeLong &);
        const SignedHugeLong & operator *=(const SignedHugeLong &);
        const SignedHugeLong & operator /=(const SignedHugeLong &);
        const SignedHugeLong & operator &=(const SignedHugeLong &);
        const SignedHugeLong & operator |=(const SignedHugeLong &);
        const SignedHugeLong & operator ^=(const SignedHugeLong &);
        const SignedHugeLong & operator %=(const SignedHugeLong &);
        bool operator ==(const SignedHugeLong &) const;
        bool operator !=(const SignedHugeLong &) const;
        const SignedHugeLong Power(const SignedHugeLong &);
        const std::string ToString(int = 10) const;
        SIGNED_MEMBER_DEFINE_OPERATOR(+);
        SIGNED_MEMBER_DEFINE_OPERATOR(-);
        SIGNED_MEMBER_DEFINE_OPERATOR(*);
        SIGNED_MEMBER_DEFINE_OPERATOR(/);
        SIGNED_MEMBER_DEFINE_OPERATOR(%);
//friend functions. for several reasons they can't be implemented outside
        friend bool operator >(const SignedHugeLong & value1,const SignedHugeLong & value2){
            bool t(value2.GetSignBit());
            if(t^value1.GetSignBit())
                return t;
            return value1.OperatorLarger(value2);
        }
        friend bool operator <(const SignedHugeLong & value1,const SignedHugeLong & value2){
            bool t(value1.GetSignBit());
            if(t^value2.GetSignBit())
                return t;
            return value1.OperatorSmaller(value2);
        }
        friend bool operator >=(const SignedHugeLong & value1,const SignedHugeLong & value2){
            bool t(value2.GetSignBit());
            if(t^value1.GetSignBit())
                return t;
            return value1.OperatorNotSmaller(value2);
        }
        friend bool operator <=(const SignedHugeLong & value1,const SignedHugeLong & value2){
            bool t(value1.GetSignBit());
            if(t^value2.GetSignBit())
                return t;
            return value1.OperatorNotLarger(value2);
        }
        template<typename __OutStreamType>
            inline friend __OutStreamType & operator <<(__OutStreamType & os,const SignedHugeLong & value){
                return os<<value.ToString();
            }
        inline friend std::ostream & operator <<(std::ostream & os,const SignedHugeLong & value){
            return os<<value.ToString((0xc00&os.flags())>>7);
        }
    };
//definition
//public:
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator =(const std::string & value){
            FromString(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::AbsValue() const{
            return GetSignBit()?operator -():*this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator +() const{
            return SignedHugeLong<N,__Alloc>(*this);
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator -() const{
            return SignedHugeLong<N,__Alloc>(*this).Minus();
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator ~() const{
            return SignedHugeLong<N,__Alloc>(*this).Complement();
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator >>(long value) const{
            return SignedHugeLong<N,__Alloc>(*this).RightShift(value);
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator <<(long value) const{
            return SignedHugeLong<N,__Alloc>(*this).LeftShift(value);
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator ++(){
            Increase();
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator ++(int){
            UnsignedHugeLong<N> tmp(*this);
            operator ++();
            return tmp;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator --(){
            Decrease();
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::operator --(int){
            UnsignedHugeLong<N> tmp(*this);
            operator --();
            return tmp;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator >>=(long value){
            RightShift(value,true);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator <<=(long value){
            LeftShift(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator +=(const SignedHugeLong & value){
            OperatorAdd(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator -=(const SignedHugeLong & value){
            OperatorSub(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator *=(const SignedHugeLong & value){
            OperatorMult(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator /=(const SignedHugeLong & value){
            bool sign(GetSignBit());
            if(sign)
                Minus();
            OperatorDiv(value.AbsValue());
            if(sign^value.GetSignBit())
                Minus();
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator &=(const SignedHugeLong & value){
            OperatorAnd(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator |=(const SignedHugeLong & value){
            OperatorOr(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator ^=(const SignedHugeLong & value){
            OperatorXor(value);
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        const SignedHugeLong<N,__Alloc> & SignedHugeLong<N,__Alloc>::operator %=(const SignedHugeLong & value){
            bool sign(GetSignBit());
            if(sign)
                Minus();
            OperatorDiv(value.AbsValue(),false);
            if(sign)
                Minus();
            return *this;
        }
    template<long N,template<long,typename>class __Alloc>
        inline bool SignedHugeLong<N,__Alloc>::operator ==(const SignedHugeLong & value) const{
            return OperatorEqual(value);
        }
    template<long N,template<long,typename>class __Alloc>
        inline bool SignedHugeLong<N,__Alloc>::operator !=(const SignedHugeLong & value) const{
            return !operator ==(value);
        }
    template<long N,template<long,typename>class __Alloc>
        const SignedHugeLong<N,__Alloc> SignedHugeLong<N,__Alloc>::Power(const SignedHugeLong & value){
            return SignedHugeLong<N,__Alloc>(*this).OperatorPower(value);
        }
    template<long N,template<long,typename>class __Alloc>
        inline const std::string SignedHugeLong<N,__Alloc>::ToString(int BaseValue) const{
            return HugeNumberBase<N,__Alloc>::ToString(BaseValue,true);
        }
//others:
    template<typename __ValueType,long N,class __Alloc>
        inline bool operator ==(const __ValueType & value1,const SignedHugeLong<N,__Alloc> & value2){
            return value2.operator ==(value1);
        }
    template<typename __ValueType,long N,class __Alloc>
        inline bool operator !=(const __ValueType & value1,const SignedHugeLong<N,__Alloc> & value2){
            return value2.operator !=(value1);
        }
    SIGNED_GLOBAL_DEFINE_OPERATOR(+);
    SIGNED_GLOBAL_DEFINE_OPERATOR(-);
    SIGNED_GLOBAL_DEFINE_OPERATOR(*);
    SIGNED_GLOBAL_DEFINE_OPERATOR(/);
    SIGNED_GLOBAL_DEFINE_OPERATOR(%);
#undef SIGNED_MEMBER_DEFINE_OPERATOR
#undef SIGNED_GLOBAL_DEFINE_OPERATOR
}    //namespace DoZerg

————————————————————————————————————

这段代码基本是UnsignedHugeLong的重复,模版参数的意义也相同。

不过请注意它与UnsignedHugeLong不同的地方,往往也是涉及到有符号特性的部分(移位,比较,......)。

抱歉!评论已关闭.