大數類模板

2021-07-12 01:40:16 字數 3267 閱讀 2641

大數類模板

#define maxn 9999

#define maxsize 10

#define dlen 4

class bignum

//建構函式

bignum(const

int); //將乙個int型別的變數轉化為大數

bignum(const

char*); //將乙個字串型別的變數轉化為大數

bignum(const bignum &); //拷貝建構函式

bignum &operator=(const bignum &); //過載賦值運算子,大數之間進行賦值運算

friend istream& operator>>(istream&, bignum&); //過載輸入運算子

friend ostream& operator

<<(ostream&, bignum&); //過載輸出運算子

bignum operator+(const bignum &) const; //過載加法運算子,兩個大數之間的相加運算

bignum operator-(const bignum &) const; //過載減法運算子,兩個大數之間的相減運算

bignum operator*(const bignum &) const; //過載乘法運算子,兩個大數之間的相乘運算

bignum operator/(const

int &) const; //過載除法運算子,大數對乙個整數進行相除運算

bignum operator^(const

int &) const; //大數的n次方運算

intoperator%(const

int &) const; //大數對乙個int型別的變數進行取模運算

bool

operator>(const bignum & t)const; //大數和另乙個大數的大小比較

bool

operator>(const

int & t)const; //大數和乙個int型別的變數的大小比較

void print(); //輸出大數

};bignum::bignum(const

int b) //將乙個int型別的變數轉化為大數

a[len++] = d;

}bignum::bignum(const

char*s) //將乙個字串型別的變數轉化為大數

}bignum::bignum(const bignum & t) : len(t.len) //拷貝建構函式

bignum & bignum::operator=(const bignum & n) //過載賦值運算子,大數之間進行賦值運算

istream& operator>>(istream & in, bignum & b) //過載輸入運算子

b.a[count] = sum;

count++;

}b.len = count++;

return in;

}ostream& operator

<<(ostream& out, bignum& b) //過載輸出運算子

return out;

}bignum bignum::operator+(const bignum & t) const

//兩個大數之間的相加運算

}if (t.a[big] != 0)

t.len = big + 1;

else

t.len = big;

return t;

}bignum bignum::operator-(const bignum & t) const

//兩個大數之間的相減運算

else

big = t1.len;

for (i = 0; i < big; i++)

else

t1.a[i] -= t2.a[i];

}t1.len = big;

while (t1.a[t1.len - 1] == 0 && t1.len > 1)

if (flag)

t1.a[big - 1] = 0 - t1.a[big - 1];

return t1;

}bignum bignum::operator*(const bignum & t) const

//兩個大數之間的相乘運算

else

}if (up != 0)

ret.a[i + j] = up;

}ret.len = i + j;

while (ret.a[ret.len - 1] == 0 && ret.len > 1)

ret.len--;

return ret;

}bignum bignum::operator/(const

int & b) const

//大數對乙個整數進行相除運算

ret.len = len;

while (ret.a[ret.len - 1] == 0 && ret.len > 1)

ret.len--;

return ret;

}int bignum::operator %(const

int & b) const

//大數對乙個int型別的變數進行取模運算

return d;

}bignum bignum::operator^(const

int & n) const

//大數的n次方運算

m -= i;

ret = ret*t;

if (m == 1)

ret = ret*(*this);

}return ret;

}bool bignum::operator>(const bignum & t) const

//大數和另乙個大數的大小比較

else

return

false;

}bool bignum::operator >(const

int & t) const

//大數和乙個int型別的變數的大小比較

void bignum::print() //輸出大數

cout

<< endl;

}

c 大數類 大數模板

分類 大數 hdoj 分別使用c 中的運算子過載的方法來實現大數之間的數 算,包括加法 減法 乘法 除法 n次方 取模 大小比較 賦值以及輸入流 輸出流的過載。並且使用這個大數模板,順利ac了hdoj上的1134這個題目的catalan數計數問題。大數模板的 如下 cpp view plain co...

c 大數類 大數模板

分別使用c 中的運算子過載的方法來實現大數之間的數 算,包括加法 減法 乘法 除法 n次方 取模 大小比較 賦值以及輸入流 輸出流的過載。並且使用這個大數模板,順利ac了hdoj上的1134這個題目的catalan數計數問題。大數模板的 如下 cpp view plain copy include ...

C 大數模板類

include include include include using namespace std define maxn 9999 define maxsize 10 define dlen 4 class bignum 建構函式 bignum const int 將乙個int型別的變數轉化為...