프로그램은 크게 외부에서 연산자 오버로딩을 이용하여 편하게 이용할 수 있는 함수들과 내부 함수부(실제 연산이 이루어지는 부분들)로 나뉘어져있습니다.
연산자 오버로딩을 처리하는 쪽 함수들은 모두 내부함수를 호출하기 때문에 별로 크게 설명할 내용이 없습니다.
#include "NxBigInt.h" #include <stdio.h> #include <memory.h> #define ALLOCSIZE 4 const NxBigInt NxBigInt::Zero = 0; const NxBigInt NxBigInt::One = 1; const NxBigInt NxBigInt::Undefined; NxBigInt::NxBigInt(int value) { if( value < 0 ) { m_bNegative = true; value = -value; } else { m_bNegative = false; } m_nLen = 1; m_nSize = ALLOCSIZE; m_pnData = new uint32_t[m_nSize]; m_pnData[0] = value; } NxBigInt::NxBigInt(const NxBigInt &value) { m_bNegative = value.m_bNegative; m_nLen = value.m_nLen; m_nSize = value.m_nSize; m_pnData = new uint32_t[m_nSize]; memcpy(m_pnData, value.m_pnData, m_nLen*sizeof(uint32_t)); } NxBigInt::NxBigInt(const char *value) { m_nLen = 1; m_nSize = ALLOCSIZE; m_pnData = new uint32_t[m_nSize]; ConvertFromString(value); } NxBigInt::NxBigInt(const wchar_t *value) { m_nLen = 1; m_nSize = ALLOCSIZE; m_pnData = new uint32_t[m_nSize]; ConvertFromString(value); } NxBigInt::~NxBigInt() { if( m_pnData != 0 ) { delete[] m_pnData; } } NxBigInt NxBigInt::operator+(int value) const { NxBigInt out(*this); out.AddTo(value); return out; } NxBigInt NxBigInt::operator+(const NxBigInt &value) const { NxBigInt out(*this); out.AddTo(value); return out; } NxBigInt operator+(int first, const NxBigInt &second) { return second + first; } NxBigInt NxBigInt::operator-() const { NxBigInt out(*this); out.m_bNegative = ~out.m_bNegative; return out; } NxBigInt NxBigInt::operator-(int value) const { NxBigInt out(*this); out.SubtractTo(value); return out; } NxBigInt NxBigInt::operator-(const NxBigInt &value) const { NxBigInt out(*this); out.SubtractTo(value); return out; } NxBigInt operator-(int first, const NxBigInt &second) { NxBigInt out(first); out.SubtractTo(second); return out; } NxBigInt NxBigInt::operator*(int value) const { NxBigInt out(*this); out.MultiplyTo(value); return out; } NxBigInt NxBigInt::operator*(const NxBigInt &value) const { NxBigInt out(*this); out.MultiplyTo(value); return out; } NxBigInt operator*(int first, const NxBigInt &second) { return second * first; } NxBigInt NxBigInt::operator/(int value) const { NxBigInt out(*this); out.DivideTo(value); return out; } NxBigInt NxBigInt::operator/(const NxBigInt &value) const { NxBigInt out(*this); out.DivideTo(value); return out; } NxBigInt NxBigInt::operator%(int value) const { NxBigInt out(*this); out.ModulaTo(value); return out; } NxBigInt NxBigInt::operator<<(int value) const { NxBigInt out = *this; out.ShiftToLeft(value); return out; } NxBigInt NxBigInt::operator>>(int value) const { NxBigInt out = *this; out.ShiftToRight(value); return out; } NxBigInt &NxBigInt::operator=(int value) { if( value < 0 ) { m_bNegative = true; value = -value; } else { m_bNegative = false; } m_nLen = 1; m_pnData[0] = value; return *this; } NxBigInt &NxBigInt::operator=(int64_t value) { if( value < 0 ) { m_bNegative = true; value = -value; } else { m_bNegative = false; } m_nLen = 2; m_pnData[0] = value & 0xffffffff; m_pnData[1] = value >> 32; return *this; } NxBigInt &NxBigInt::operator=(const char *value) { ConvertFromString(value); return *this; } NxBigInt &NxBigInt::operator=(const wchar_t *value) { ConvertFromString(value); return *this; } NxBigInt &NxBigInt::operator+=(int value) { AddTo(value); return *this; } NxBigInt &NxBigInt::operator+=(const NxBigInt &value) { AddTo(value); return *this; } NxBigInt &NxBigInt::operator*=(int value) { MultiplyTo(value); return *this; } NxBigInt &NxBigInt::operator*=(const NxBigInt &value) { MultiplyTo(value); return *this; } NxBigInt &NxBigInt::operator/=(int value) { DivideTo(value); return *this; } NxBigInt &NxBigInt::operator/=(const NxBigInt &value) { DivideTo(value); return *this; } NxBigInt &NxBigInt::operator%=(int value) { DivideTo(value); return *this; } NxBigInt &NxBigInt::operator<<=(int value) { ShiftToLeft(value); return *this; } NxBigInt &NxBigInt::operator>>=(int value) { ShiftToRight(value); return *this; } NxBigInt::operator bool() const { return !(m_nLen <= 1 && m_pnData[0] == 0); } std::ostream &operator <<(std::ostream &output, const NxBigInt &number) { char buf[2048]; number.ConvertToString(buf, 2048); output << buf; return output; }
반응형
'NeoAxiom' 카테고리의 다른 글
NxBigInt 클래스 구현 : DivideAndStoreRemain() 함수 (2) | 2015.01.19 |
---|---|
NxBigInt 클래스 설계 (0) | 2015.01.15 |
댓글