多倍長整数クラス add / sub 全体
現状の全体のコード
■ヘッダ
default.h
#pragma once #include <string> #include <iostream> ///////////////////////////////////////////////////////////////////////// // 自作ヘッダをインクルード ///////////////////////////////////////////////////////////////////////// #include "Plate.h" #include "NumberPlace.h"
#pragma once #include <string> using namespace std; namespace BoardGames { /// 数独の数字1つに対する情報 class BigUInt { private: unsigned long bits[3]; // 初期化 void init(void); // ビットの左右逆転 inline unsigned long rev(unsigned long x); public: BigUInt(); BigUInt(int bit1, int bit2, int bit3); BigUInt(const BigUInt &plate); // テストコード用 string getAscii(); // インデクサ代わり(?) unsigned long operator[](int num); // 代入operator void operator=(const BigUInt& plate); // 代入演算子 BigUInt& operator>>=(int n); BigUInt& operator<<=(int n); BigUInt& operator&=(const BigUInt& p); BigUInt& operator|=(const BigUInt& p); BigUInt& operator^=(const BigUInt& p); BigUInt& operator+=(const BigUInt& p); BigUInt& operator-=(const BigUInt& p); BigUInt& operator*=(const BigUInt& p); BigUInt& operator/=(const BigUInt& p); //BigUInt& operator%=(const BigUInt& p); // 単項演算子 friend BigUInt operator!(const BigUInt& p); friend BigUInt operator~(const BigUInt& p); // 二項演算子 BigUInt operator>>(int n) const; BigUInt operator<<(int n) const; friend BigUInt operator&(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator|(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator^(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator+(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator-(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator*(const BigUInt& pl, const BigUInt& pr); friend BigUInt operator/(const BigUInt& pl, const BigUInt& pr); //friend BigUInt operator%(const BigUInt& pl, const BigUInt& pr); // 比較 bool operator<=(const BigUInt& p) const; bool operator>=(const BigUInt& p) const; bool operator<(const BigUInt& p) const; bool operator>(const BigUInt& p) const; bool operator==(const BigUInt& p) const; bool operator!=(const BigUInt& p) const; }; }
■コード
#include "default.h" using namespace std; using namespace BoardGames; ///////////////////////////////////////////////////////////////////////////////////// // コンストラクタ ///////////////////////////////////////////////////////////////////////////////////// BigUInt::BigUInt() { init(); } BigUInt::BigUInt(int bit1, int bit2, int bit3) { init(); bits[2] = bit1; bits[1] = bit2; bits[0] = bit3; } BigUInt::BigUInt(const BigUInt &plate) { init(); bits[0] = plate.bits[0]; bits[1] = plate.bits[1]; bits[2] = plate.bits[2]; } ///////////////////////////////////////////////////////////////////////////////////// // その他内部関数 ///////////////////////////////////////////////////////////////////////////////////// string BigUInt::getAscii() { string ascii; unsigned long temp = 0; for (int i = 2; 0 <= i; i--) { temp = rev(bits[i]); for (int j = 0; j < 32; j++) { ascii += (char)((temp & 1) + 0x30); temp >>= 1; } } return ascii; } // 初期化 void BigUInt::init(void) { bits[0] = 0; bits[1] = 0; bits[2] = 0; } // bitの左右逆転 inline unsigned long BigUInt::rev(unsigned long x) { x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555; x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333; x = (x & 0x0f0f0f0f) << 4 | (x >> 4) & 0x0f0f0f0f; x = (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24); return x; } ///////////////////////////////////////////////////////////////////////////////////// // operator 関係演算子以外 ///////////////////////////////////////////////////////////////////////////////////// unsigned long BigUInt::operator[](int num) { if (0 <= num && num <= 2) { return this->bits[num]; } return 0; } // 代入演算子 void BigUInt::operator=(const BigUInt& plate) { bits[0] = plate.bits[0]; bits[1] = plate.bits[1]; bits[2] = plate.bits[2]; } ///////////////////////////////////////////////////////////////////////////////////// // operator 代入演算子 ///////////////////////////////////////////////////////////////////////////////////// BigUInt& BigUInt::operator>>=(int n) { for (;31<n;n-=31) { ((*this) >>= 31); } bits[0] = (bits[0] >> n) | (bits[1] << (32-n)); bits[1] = (bits[1] >> n) | (bits[2] << (32-n)); bits[2] = bits[2] >> n; return *this; } BigUInt& BigUInt::operator<<=(int n) { for (;31<n;n-=31) { ((*this) <<= 31); } bits[0] = bits[0] << n; bits[1] = (bits[1] << n) | (bits[0] >> (32-n)); bits[2] = (bits[2] << n) | (bits[1] >> (32-n)); return *this; } BigUInt& BigUInt::operator&=(const BigUInt& p) { bits[0] &= p.bits[0]; bits[1] &= p.bits[1]; bits[2] &= p.bits[2]; return *this; } BigUInt& BigUInt::operator|=(const BigUInt& p) { bits[0] |= p.bits[0]; bits[1] |= p.bits[1]; bits[2] |= p.bits[2]; return *this; } BigUInt& BigUInt::operator^=(const BigUInt& p) { bits[0] ^= p.bits[0]; bits[1] ^= p.bits[1]; bits[2] ^= p.bits[2]; return *this; } BigUInt& BigUInt::operator+=(const BigUInt& p) { unsigned long long b0, b1, b2; b0 = bits[0]; b1 = bits[1]; b2 = bits[2]; b0 += p.bits[0]; b1 += p.bits[1]; b2 += p.bits[2]; b1 += (b0 >> 16 >> 16); b2 += (b1 >> 16 >> 16); bits[0] = (unsigned long)b0; bits[1] = (unsigned long)b1; bits[2] = (unsigned long)b2; return *this; } BigUInt& BigUInt::operator-=(const BigUInt& p) { unsigned long long b0, b1, b2; b0 = bits[0]; b1 = bits[1]; b2 = bits[2]; b0 -= p.bits[0]; b1 -= p.bits[1]; b2 -= p.bits[2]; b1 -= ((b0 >> 16 >> 16) & 1); b2 -= ((b1 >> 16 >> 16) & 1); bits[0] = (unsigned long)b0; bits[1] = (unsigned long)b1; bits[2] = (unsigned long)b2; return *this; } //BigUInt& BigUInt::operator*=(const BigUInt& p) { // return *this; //} //BigUInt& BigUInt::operator/=(const BigUInt& p) { // return *this; //} ///////////////////////////////////////////////////////////////////////////////////// // operator 単項演算子 ///////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// // operator 二項演算子 ///////////////////////////////////////////////////////////////////////////////////// BigUInt BigUInt::operator>>(int n) const { BigUInt temp(*this); temp >>= n; return temp; } BigUInt BigUInt::operator<<(int n) const { BigUInt temp(*this); temp <<= n; return temp; }