C++ BigInteger 大整数类模板(转)
#include <deque>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; class DividedByZeroException {}; class BigInteger
{
private:
vector<char> digits;
bool sign; // true for positive, false for negitive
void trim(); // remove zeros in tail, but if the value is 0, keep only one:)
public:
BigInteger(int); // construct with a int integer
BigInteger(string&) ;
BigInteger();
BigInteger (const BigInteger&);
BigInteger operator=(const BigInteger& op2); BigInteger abs() const;
BigInteger pow(int a); //binary operators friend BigInteger operator+=(BigInteger&,const BigInteger&);
friend BigInteger operator-=(BigInteger&,const BigInteger&);
friend BigInteger operator*=(BigInteger&,const BigInteger&);
friend BigInteger operator/=(BigInteger&,const BigInteger&) throw(DividedByZeroException);
friend BigInteger operator%=(BigInteger&,const BigInteger&) throw(DividedByZeroException); friend BigInteger operator+(const BigInteger&,const BigInteger&);
friend BigInteger operator-(const BigInteger&,const BigInteger&);
friend BigInteger operator*(const BigInteger&,const BigInteger&);
friend BigInteger operator/(const BigInteger&,const BigInteger&) throw(DividedByZeroException);
friend BigInteger operator%(const BigInteger&,const BigInteger&) throw(DividedByZeroException); //uniary operators
friend BigInteger operator-(const BigInteger&); //negative friend BigInteger operator++(BigInteger&); //++v
friend BigInteger operator++(BigInteger&,int); //v++
friend BigInteger operator--(BigInteger&); //--v
friend BigInteger operator--(BigInteger&,int); //v-- friend bool operator>(const BigInteger&,const BigInteger&);
friend bool operator<(const BigInteger&,const BigInteger&);
friend bool operator==(const BigInteger&,const BigInteger&);
friend bool operator!=(const BigInteger&,const BigInteger&);
friend bool operator>=(const BigInteger&,const BigInteger&);
friend bool operator<=(const BigInteger&,const BigInteger&); friend ostream& operator<<(ostream&,const BigInteger&); //print the BigInteger
friend istream& operator>>(istream&, BigInteger&); // input the BigInteger public:
static const BigInteger ZERO;
static const BigInteger ONE;
static const BigInteger TEN;
};
// BigInteger.cpp const BigInteger BigInteger::ZERO=BigInteger();
const BigInteger BigInteger::ONE =BigInteger();
const BigInteger BigInteger::TEN =BigInteger(); BigInteger::BigInteger()
{
sign=true;
} BigInteger::BigInteger(int val){// construct with a int integer
if (val >= )
sign = true;
else{
sign = false;
val *= (-);
}
do{
digits.push_back( (char)(val%) );
val /= ;
} while ( val != );
} BigInteger::BigInteger(string& def){
sign=true;
for ( string::reverse_iterator iter = def.rbegin() ; iter < def.rend(); iter++){
char ch = (*iter);
if (iter == def.rend()-){
if ( ch == '+' )
break;
if(ch == '-' ){
sign = false;
break;
}
}
digits.push_back( (char)((*iter) - '' ) );
}
trim();
} void BigInteger::trim(){
vector<char>::reverse_iterator iter = digits.rbegin();
while(!digits.empty() && (*iter) == ){
digits.pop_back();
iter=digits.rbegin();
}
if( digits.size()== ){
sign = true;
digits.push_back();
}
} BigInteger::BigInteger(const BigInteger& op2){
sign = op2.sign;
digits=op2.digits;
} BigInteger BigInteger::operator=(const BigInteger& op2){
digits = op2.digits;
sign = op2.sign;
return (*this);
} BigInteger BigInteger::abs() const {
if(sign) return *this;
else return -(*this);
} BigInteger BigInteger::pow(int a)
{
BigInteger res();
for(int i=; i<a; i++)
res*=(*this);
return res;
} //binary operators
BigInteger operator+=(BigInteger& op1,const BigInteger& op2){
if( op1.sign == op2.sign ){ //只处理相同的符号的情况,异号的情况给-处理
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_add = ; //进位
while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){
(*iter1) = (*iter1) + (*iter2) + to_add;
to_add = ((*iter1) > ); // 大于9进一位
(*iter1) = (*iter1) % ;
iter1++; iter2++;
}
while ( iter1 != op1.digits.end() ){ //
(*iter1) = (*iter1) + to_add;
to_add = ( (*iter1) > );
(*iter1) %= ;
iter1++;
}
while ( iter2 != op2.digits.end() ){
char val = (*iter2) + to_add;
to_add = (val > ) ;
val %= ;
op1.digits.push_back(val);
iter2++;
}
if( to_add != )
op1.digits.push_back(to_add);
return op1;
}
else{
if (op1.sign)
return op1 -= (-op2);
else
return op1= op2 - (-op1);
} } BigInteger operator-=(BigInteger& op1,const BigInteger& op2){
if( op1.sign == op2.sign ){ //只处理相同的符号的情况,异号的情况给+处理
if(op1.sign) {
if(op1 < op2) // 2 - 3
return op1=-(op2 - op1);
}
else {
if(-op1 > -op2) // (-3)-(-2) = -(3 - 2)
return op1=-((-op1)-(-op2));
else // (-2)-(-3) = 3 - 2
return op1= (-op2) - (-op1);
}
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin(); char to_substract = ; //借位 while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){
(*iter1) = (*iter1) - (*iter2) - to_substract;
to_substract = ;
if( (*iter1) < ){
to_substract=;
(*iter1) += ;
}
iter1++;
iter2++;
}
while ( iter1 != op1.digits.end() ){
(*iter1) = (*iter1) - to_substract;
to_substract = ;
if( (*iter1) < ){
to_substract=;
(*iter1) += ;
}
else break;
iter1++;
}
op1.trim();
return op1;
}
else{
if (op1 > BigInteger::ZERO)
return op1 += (-op2);
else
return op1 = -(op2 + (-op1));
}
}
BigInteger operator*=(BigInteger& op1,const BigInteger& op2){
BigInteger result();
if (op1 == BigInteger::ZERO || op2==BigInteger::ZERO)
result = BigInteger::ZERO;
else{
vector<char>::const_iterator iter2 = op2.digits.begin();
while( iter2 != op2.digits.end() ){
if(*iter2 != ){
deque<char> temp(op1.digits.begin() , op1.digits.end());
char to_add = ;
deque<char>::iterator iter1 = temp.begin();
while( iter1 != temp.end() ){
(*iter1) *= (*iter2);
(*iter1) += to_add;
to_add = (*iter1) / ;
(*iter1) %= ;
iter1++;
}
if( to_add != )
temp.push_back( to_add );
int num_of_zeros = iter2 - op2.digits.begin();
while( num_of_zeros--)
temp.push_front();
BigInteger temp2;
temp2.digits.insert( temp2.digits.end() , temp.begin() , temp.end() );
temp2.trim();
result = result + temp2;
}
iter2++;
}
result.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
}
op1 = result;
return op1;
} BigInteger operator/=(BigInteger& op1 , const BigInteger& op2 ) throw(DividedByZeroException) {
if( op2 == BigInteger::ZERO )
throw DividedByZeroException();
BigInteger t1 = op1.abs(), t2 = op2.abs();
if ( t1 < t2 ){
op1 = BigInteger::ZERO;
return op1;
}
//现在 t1 > t2 > 0
//只需将 t1/t2的结果交给result就可以了
deque<char> temp;
vector<char>::reverse_iterator iter = t1.digits.rbegin(); BigInteger temp2();
while( iter != t1.digits.rend() ){
temp2 = temp2 * BigInteger::TEN + BigInteger( (int)(*iter) );
char s = ;
while( temp2 >= t2 ){
temp2 = temp2 - t2;
s = s + ;
}
temp.push_front( s );
iter++;
}
op1.digits.clear();
op1.digits.insert( op1.digits.end() , temp.begin() , temp.end() );
op1.trim();
op1.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) );
return op1;
} BigInteger operator%=(BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
return op1 -= ((op1 / op2)*op2);
} BigInteger operator+(const BigInteger& op1,const BigInteger& op2){
BigInteger temp(op1);
temp += op2;
return temp;
}
BigInteger operator-(const BigInteger& op1,const BigInteger& op2){
BigInteger temp(op1);
temp -= op2;
return temp;
} BigInteger operator*(const BigInteger& op1,const BigInteger& op2){
BigInteger temp(op1);
temp *= op2;
return temp; } BigInteger operator/(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
BigInteger temp(op1);
temp /= op2;
return temp;
} BigInteger operator%(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) {
BigInteger temp(op1);
temp %= op2;
return temp;
} //uniary operators
BigInteger operator-(const BigInteger& op){ //negative
BigInteger temp = BigInteger(op);
temp.sign = !temp.sign;
return temp;
} BigInteger operator++(BigInteger& op){ //++v
op += BigInteger::ONE;
return op;
} BigInteger operator++(BigInteger& op,int x){ //v++
BigInteger temp(op);
++op;
return temp;
} BigInteger operator--(BigInteger& op){ //--v
op -= BigInteger::ONE;
return op;
} BigInteger operator--(BigInteger& op,int x){ //v--
BigInteger temp(op);
--op;
return temp;
} bool operator<(const BigInteger& op1,const BigInteger& op2){
if( op1.sign != op2.sign )
return !op1.sign;
else{
if(op1.digits.size() != op2.digits.size())
return (op1.sign && op1.digits.size()<op2.digits.size())
|| (!op1.sign && op1.digits.size()>op2.digits.size());
vector<char>::const_reverse_iterator iter1,iter2;
iter1 = op1.digits.rbegin();iter2 = op2.digits.rbegin();
while( iter1 != op1.digits.rend() ){
if( op1.sign && *iter1 < *iter2 ) return true;
if( op1.sign && *iter1 > *iter2 ) return false;
if( !op1.sign && *iter1 > *iter2 ) return true;
if( !op1.sign && *iter1 < *iter2 ) return false;
iter1++;
iter2++;
}
return false;
}
}
bool operator==(const BigInteger& op1,const BigInteger& op2){
if( op1.sign != op2.sign || op1.digits.size() != op2.digits.size() )
return false;
vector<char>::const_iterator iter1,iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
while( iter1!= op1.digits.end() ){
if( *iter1 != *iter2 ) return false;
iter1++;
iter2++;
}
return true;
} bool operator!=(const BigInteger& op1,const BigInteger& op2){
return !(op1==op2);
} bool operator>=(const BigInteger& op1,const BigInteger& op2){
return (op1>op2) || (op1==op2);
} bool operator<=(const BigInteger& op1,const BigInteger& op2){
return (op1<op2) || (op1==op2);
} bool operator>(const BigInteger& op1,const BigInteger& op2){
return !(op1<=op2);
} ostream& operator<<(ostream& stream,const BigInteger& val){ //print the BigInteger
if (!val.sign)
stream << "-";
for ( vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++)
stream << (char)((*iter) + '');
return stream;
} istream& operator>>(istream& stream, BigInteger& val){ //Input the BigInteger
string str;
stream >> str;
val=BigInteger(str);
return stream;
} //////////////////////////////////////////////////////////// int main()
{
int n;
cin>>n;
for(int i=; i<n; i++) {
BigInteger A;
BigInteger B;
BigInteger C=;
cin>>A>>B;
cout<<"A-B:"<<A-B<<endl;
cout<<"A+B:"<<A+B<<endl;
cout<<"A*B:"<<A*B<<endl;
cout<<"A/B:"<<A/B<<endl;
cout<<"A%B:"<<A%B<<endl;
cout<<"A.pow(5)"<<A.pow()<<endl;
A++;
cout<<"A++:"<<A<<endl;
A--;
cout<<"A--:"<<A<<endl;
cout<<"++B:"<<++B<<endl;
cout<<"--B:"<<--B<<endl;
cout<<"C:"<<C<<endl;
}
return ;
}
C++ BigInteger 大整数类模板(转)的更多相关文章
- C++大整数类模板
参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
- N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...
- Pollard-Rho大整数拆分模板
随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...
- C++高精度计算(大整数类)
Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- 用Java的大整数类BigInteger来实现大整数的一些运算
关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...
- Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)
先上Java Web图 为了简化叙述,只写Java代码,然后控制台输出 使用[Random类]取得随机数 import java.util.Random; public class Fir { pub ...
- java 常用类库:BigInteger大整数;BigDecimal大小数(解决double精度损失);
大整数BigInteger package com.zmd.common_class_libraries; import java.math.BigInteger; /** * @ClassName ...
随机推荐
- android 时间对话框 TimePickerDialog简介
个人也提醒功能的时候用到了TimePickerDialog对话框,查阅了非常多技术资料,可是感觉非常多东西都说的不是非常具体,而且非常多地方.都有不完好的地方.比方有弹出对话框得到的不是系统当前 ...
- Swift: 继承
为了在属性值改变的时候获得通知,类可以为继承的属性添加属性观察者.属性观察者可以添加到任何属性上,不管这个属性原来是存储属性还是计算属性. Swift中的类没有一个统一的基类. 为了讲明白继承,我们先 ...
- 在cmd窗口下运行Java程序时无法找到主类的解决办法
我是Java的初学者,昨天在cmd窗口下运行一段Java程序时总是有问题,可以编译但无法执行. 也就是javac时正确,一旦java时就不对了,提示找不到或无法加载主类,经百度谷歌再加上自己的摸索终于 ...
- warning : json_decode(): option JSON_BIGINT_AS_STRING not implemented in xxx
先来一段json_decode官方说明 mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, i ...
- bom头惹的祸!
今天使用json_decode函数解析json数据一直出错,最后发现j用浏览器开发者工具发现json数据前面多了个$#65279,查询得知是bom头; 网上找的去除bom头的代码如下: <?ph ...
- OD: Kernel Vulnerabilities Analyze
内核漏洞大多出没于 ring3 到 ring0 的交互中.从 ring3 进入 ring0 的通道,以及操作系统提供的 API 都有可能存在漏洞.例如:驱动程序中 IoControl 的处理函数,SS ...
- OJ常见问题及必须认识的对拍处理水题
HDUOJ: 常见问题及解答 Q: Online Judge(以下简称OJ)支持哪些语言? A: 目前为止,HDOJ支持C.C++.Pascal和Java四种语言. Q: 有什么条件判断我的程序是在O ...
- 关于uploadify 没有显示进度条!!!!
如果你也弄了很久不知道为什么不出现上传进度条!,那就一定要看这里了! 我注释了 queueID 属性后 就出现了!!!!! 就是这么简答! //添加界面的附件管理 $('#file_upload'). ...
- 关于android应用闪屏的几种情况
1.主菜单进入某应用闪屏: 常见是一个空的activity作为launcher属性,实际上它什么事业没干,真正干事情的是从它通过intent启动的activity. 例子: public class ...
- Swift进阶
概述 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反射 扩展—KVO 内存管理 循 ...