用法

把头文件和源代码文件放在同一目录下,然后#include"INT"即可使用。你能对int类的变量进行a=2,a+=3,a%8,a--等等操作,那你就也能对INT进行。INT基于vector,可以实现存储任意大的整数,且利用动态内存机制不会多浪费一点空间

类声明

class INT
{
public: INT();
template <class T>
INT(T a);
INT(const string & s);
INT(const INT &a);
INT(const char * s); template <class T>
INT & operator=(const T obj);
INT & operator=(const INT & obj); friend istream & operator>>(istream & in , INT &a);
friend ostream & operator<<(ostream & out , INT &a); operator bool(); friend INT operator+(INT a,INT b);
friend INT operator-(INT a,INT b);
friend INT operator*(INT a,INT b);
friend INT operator/(INT a,INT b);
friend INT operator%(INT a,INT b);
template <class T1>
friend INT operator+(T1 a,INT b);
template <class T1>
friend INT operator-(T1 a,INT b);
template <class T1>
friend INT operator*(T1 a,INT b);
template <class T1>
friend INT operator/(T1 a,INT b);
template <class T1>
friend INT operator%(T1 a,INT b);
template <class T2>
friend INT operator+(INT a,T2 b);
template <class T2>
friend INT operator-(INT a,T2 b);
template <class T2>
friend INT operator*(INT a,T2 b);
template <class T2>
friend INT operator/(INT a,T2 b);
template <class T2>
friend INT operator%(INT a,T2 b); friend bool operator<(INT a,INT b);
friend bool operator>(INT a,INT b);
friend bool operator<=(INT a,INT b);
friend bool operator>=(INT a,INT b);
friend bool operator!=(INT a,INT b);
friend bool operator==(INT a,INT b); template <class T>
inline INT& operator+=(T b);
template <class T>
inline INT& operator-=(T b);
template <class T>
inline INT& operator*=(T b);
template <class T>
inline INT& operator/=(T b);
template <class T>
inline INT& operator%=(T b); inline INT& operator++();
inline INT& operator--();
inline INT operator++(int);
inline INT operator--(int);
inline INT operator-(); private: vector<int> data;
bool sign;
static const long long int BIT=100000000;
static const short int BITb=8; void print();
void clear();//去除多余的前导0
};

Code

#ifndef __gk_BigInt
#define __gk_BigInt #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std; class INT
{
public: INT();
template <class T>
INT(T a);
INT(const string & s);
INT(const INT &a);
INT(const char * s); template <class T>
INT & operator=(const T obj);
INT & operator=(const INT & obj); friend istream & operator>>(istream & in , INT &a);
friend ostream & operator<<(ostream & out , INT &a); operator bool(); friend INT operator+(INT a,INT b);
friend INT operator-(INT a,INT b);
friend INT operator*(INT a,INT b);
friend INT operator/(INT a,INT b);
friend INT operator%(INT a,INT b);
template <class T1>
friend INT operator+(T1 a,INT b);
template <class T1>
friend INT operator-(T1 a,INT b);
template <class T1>
friend INT operator*(T1 a,INT b);
template <class T1>
friend INT operator/(T1 a,INT b);
template <class T1>
friend INT operator%(T1 a,INT b);
template <class T2>
friend INT operator+(INT a,T2 b);
template <class T2>
friend INT operator-(INT a,T2 b);
template <class T2>
friend INT operator*(INT a,T2 b);
template <class T2>
friend INT operator/(INT a,T2 b);
template <class T2>
friend INT operator%(INT a,T2 b); friend bool operator<(INT a,INT b);
friend bool operator>(INT a,INT b);
friend bool operator<=(INT a,INT b);
friend bool operator>=(INT a,INT b);
friend bool operator!=(INT a,INT b);
friend bool operator==(INT a,INT b); template <class T>
inline INT& operator+=(T b);
template <class T>
inline INT& operator-=(T b);
template <class T>
inline INT& operator*=(T b);
template <class T>
inline INT& operator/=(T b);
template <class T>
inline INT& operator%=(T b); inline INT& operator++();
inline INT& operator--();
inline INT operator++(int);
inline INT operator--(int);
inline INT operator-(); private: vector<int> data;
bool sign;
static const long long int BIT=100000000;
static const short int BITb=8; void print();
void clear();//去除多余的前导0
};
INT Null(0);
INT::INT(const INT &a)
{
data=a.data;
sign=a.sign;
}
INT::INT()
{
data.resize(1);
sign=1;
}
INT::INT(const string & s)
{
int p=0,no=0;
data.resize(1);
sign=1;
for(int i=s.length()-1;i>=0;i--)
{
if(s[i]=='-')
{
sign=0;
continue;
}
no++;
int t=(s[i]-'0');
for(int j=1;j<no;j++)t*=10;
data[p]+=t;
if(no>=BITb)
{
data.push_back(0);
p++;
no=0;
}
}
clear();
}
INT::INT(const char * s)
{
int p=0,no=0;
data.resize(1);
sign=1;
for(int i=strlen(s)-1;i>=0;i--)
{
if(s[i]=='-')
{
sign=0;
continue;
}
no++;
int t=(s[i]-'0');
for(int j=1;j<no;j++)t*=10;
data[p]+=t;
if(no>=BITb)
{
data.push_back(0);
p++;
no=0;
}
}
clear();
}
template <class T>
INT::INT(T a)
{
if(a<0)
{
sign=0;
a=-a;
}
else sign=1;
for(short int p=0;a>0;p++)
{
data.push_back(0);
data[p]=a%BIT;
a/=BIT;
}
clear();
} ostream & operator<<(ostream & out , INT &a)
{
a.print();
return out;
} istream & operator>>(istream & in , INT &a)
{
string s;
cin>>s;
a=s;
return in;
}
INT & INT::operator=(const INT & obj)
{
if(this !=&obj)
{
data=obj.data;
sign=obj.sign;
}
return *this;
} template <class T>
INT & INT::operator=(const T obj)
{
*this=INT(obj);
return *this;
} INT operator+(INT a,INT b)
{
if(a.sign==0&&b.sign==0)
{
a.sign=b.sign=1;
INT t=a+b;
t.sign=0;
return t;
}
if(a.sign==1&&b.sign==0)
{
b.sign=1;
return a-b;
}
if(a.sign==0&&b.sign==1)
{
a.sign=1;
return b-a;
}
INT ans;
if(a.data.size()<b.data.size())return (b+a);
for(int i=0;i<b.data.size();i++)
{
ans.data.push_back(0);
ans.data[i]+=(a.data[i]+b.data[i])%INT::BIT;
ans.data[i+1]+=(a.data[i]+b.data[i])/INT::BIT;
}
for(int i=b.data.size();i<a.data.size();i++)
{
ans.data.push_back(0);
ans.data[i]+=a.data[i];
}
ans.clear();
return ans;
}
INT operator-(INT a,INT b)
{
if(a.sign==1&&b.sign==0)
{
b.sign=1;
return a+b;
}
if(a.sign==0&&b.sign==1)
{
a.sign=1;
INT t=a+b;
t.sign=0;
return t;
}
if(a.sign==0&&b.sign==0)
{
a.sign=b.sign=1;
return b-a;
}
if(a<b)
{
INT t=b-a;
t.sign=0;
return t;
}
INT ans;
if(a==b)return ans;
for(int i=1;i<a.data.size();i++)ans.data.push_back(0);
for(int i=0;i<a.data.size();i++)
{
ans.data[i]+=a.data[i]-b.data[i];
if(ans.data[i]<0)
{
ans.data[i]+=INT::BIT;
a.data[i+1]--;
}
}
ans.clear();
return ans;
}
INT operator*(INT a,INT b)
{
if(a.sign==1&&b.sign==0)
{
b.sign=1;
INT t=a*b;
t.sign=0;
return t;
}
if(a.sign==0&&b.sign==1)
{
a.sign=1;
INT t=a*b;
t.sign=0;
return t;
}
if(a.sign==0&&b.sign==0)
{
a.sign=b.sign=1;
return a*b;
}
INT ans;
if(a==INT(0)||b==INT(0))return ans;
for(int i=1;i<=a.data.size()+b.data.size();i++)ans.data.push_back(0);
for(int i=0;i<a.data.size();i++)
{
for(int j=0;j<b.data.size();j++)
{
long long t=(long long)a.data[i]*b.data[j];
ans.data[i+j]+=t%INT::BIT;
ans.data[i+j+1]+=t/INT::BIT;
}
}
ans.clear();
return ans;
}
//能跑,但是有极大优化空间,但是懒得优化了,有空再说
INT operator/(INT a,INT b)
{
if(a.sign==0&&b.sign==0)
{
a.sign=b.sign=1;
return a/b;
}
if(a.sign==0&&b.sign==1)
{
a.sign=1;
INT t=a/b;
t.sign=0;
return t;
}
if(a.sign==1&&b.sign==0)
{
b.sign=1;
INT t=a/b;
t.sign=0;
return t;
}
INT ans;
while(a-b>Null)
{
a=a-b;
ans++;
}
return ans;
} INT operator%(INT a,INT b)
{
if(a.sign==0||b.sign==0)
{
cout<<"error,未定义负数的求模运算"<<endl;
return INT(-1);
}
else
{
while(a-b>Null)
a=a-b;
return a;
}
}
template <class T1>
INT operator+(T1 a,INT b)
{
INT A(a);
return A+b;
}
template <class T1>
INT operator-(T1 a,INT b)
{
INT A(a);
return A-b;
}
template <class T1>
INT operator*(T1 a,INT b)
{
INT A(a);
return A*b;
}
template <class T1>
INT operator/(T1 a,INT b)
{
INT A(a);
return A/b;
}
template <class T1>
INT operator%(T1 a,INT b)
{
INT A(a);
return A%b;
}
template <class T2>
INT operator+(INT a,T2 b)
{
INT B(b);
return a+B;
}
template <class T2>
INT operator-(INT a,T2 b)
{
INT B(b);
return a-B;
}
template <class T2>
INT operator*(INT a,T2 b)
{
INT B(b);
return a*B;
}
template <class T2>
INT operator/(INT a,T2 b)
{
INT B(b);
return a/B;
}
template <class T2>
INT operator%(INT a,T2 b)
{
INT B(b);
return a%B;
} bool operator<(INT a,INT b)
{
if(a.sign==1&&b.sign==0)return false;
if(a.sign==0&&b.sign==1)return true;
if(a.sign==0&&b.sign==0)
{
a.sign=b.sign=1;
return b<a;
}
if(a.data.size()>b.data.size())return false;
else if(a.data.size()<b.data.size())return true;
if(a.data.size()==1&&a.data[0]==b.data[0]&&a.data[0]==0)return false;
for(int i=a.data.size()-1;i>=0;i--)
{
if(a.data[i]<b.data[i])return true;
if(a.data[i]>b.data[i])return false;
}
return false;
}
bool operator==(INT a,INT b)
{
if(a.sign!=b.sign)
{
if(a.data[0]==b.data[0]&&a.data[0]==0&&a.data.size()==b.data.size()&&a.data.size()==1)
return 1;
else
return 0;
}
if(a.data.size()!=b.data.size())return 0;
for(int i=0;i<a.data.size();i++)
if(a.data[i]!=b.data[i])
return 0;
return 1;
}
bool operator!=(INT a,INT b)
{
return !(a==b);
}
bool operator>(INT a,INT b)
{
return a>=b&&a!=b;
}
bool operator<=(INT a,INT b)
{
return (a<b||a==b);
}
bool operator>=(INT a,INT b)
{
return !(a<b);
} template <class T>
inline INT& INT::operator+=(T b)
{
*this=*this+INT(b);
return *this;
}
template <class T>
inline INT& INT::operator-=(T b)
{
*this=*this-INT(b);
return *this;
}
template <class T>
inline INT& INT::operator*=(T b)
{
*this=*this*INT(b);
return *this;
}
template <class T>
inline INT& INT::operator/=(T b)
{
*this=*this/INT(b);
return *this;
}
template <class T>
inline INT& INT::operator%=(T b)
{
*this=*this%INT(b);
return *this;
} inline INT& INT::operator++()
{
*this=*this+INT(1);
return *this;
}
inline INT& INT::operator--()
{
*this=*this-INT(1);
return *this;
}
inline INT INT::operator++(int)
{
INT t= *this;
*this=*this+INT(1);
return t;
}
inline INT INT::operator--(int)
{
INT t=*this;
*this=*this+INT(1);
return t;
} void INT::print()
{
if(data.size()==1&&data[0]==0||data.size()==0)
{
cout<<0;
return;
}
if(sign==0)cout<<'-';
clear();
cout<<data.back();
for(int i=data.size()-2;i>=0;i--)
{
if(data[i]==0)for(int i=1;i< BIT ;i*=10)cout<<'0';
else
{
int x=0;
for(int t=data[i];t>0;t/=10)x++;
for(int i=1;i<=8-x;i++)cout<<'0';
cout<<data[i];
}
}
}
void INT::clear()
{
while(data.size()>1&&data.back()==0)
data.pop_back();
}
INT::operator bool()
{ if(data.size()==1&&data[0]==0||data.size()==0)
return false;
else
return true;
}
inline INT INT::operator-()
{
INT newthis(*this);
if(newthis.sign==0)newthis.sign=1;
else newthis.sign=0;
return newthis;
} #endif

测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试

C++大整数类的更多相关文章

  1. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  2. N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...

  3. C++高精度计算(大整数类)

    Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...

  4. 大整数类BIGN的设计与实现 C++高精度模板

    首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...

  5. 用Java的大整数类BigInteger来实现大整数的一些运算

    关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...

  6. [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换

    [原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...

  7. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  8. C++大整数类模板

    参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...

  9. 【Java编程】Java中的大整数计算

    在上一篇文章中,我们实现了c语言中的大整数的运算,并且用Miller-Rabin算法实现了对大素数的测试.本来我准备用Java代码实现大整数的运算,查了一下资料发现Java中java.math的Big ...

  10. Ural 1158. Censored! 有限状态自动机+DP+大整数

    Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...

随机推荐

  1. Pattern类和Matcher类的使用

    1.先看好数据源 先将一个String对象确定为程序要对其进行操作的数据源. String b="hello,good morning"; 2.建立Pattern类的对象 Stri ...

  2. [ABC282F] Union of Two Sets

    Problem Statement This is an interactive task, where your and the judge's programs interact via Stan ...

  3. 解决OpenCV3+VS2015(VS2017)运行时出现debug error abort()has been called的问题

    问题描述: 在windows平台上安装opencv后,测试一张图片时,出现了debug error abort()has been called的问题 环境: vs2015 windows 10 op ...

  4. MySQL运维12-Mycat分库分表之按天分片

    一.按天分片 指定一个时间周期,将数据写入一个数据节点中,例如:第1-10天的数据,写入到第一个数据节点中,第2-20天的数据写入到第二个节点中,第3-30天的数据节点写入到第三个数据节点中. 说明1 ...

  5. django自带的cache缓存框架使用

    https://docs.djangoproject.com/zh-hans/4.2/topics/cache/#top 主要步骤官网也写得很清楚了,包含怎么区使用. 这里就展示一些配置django- ...

  6. Javascript Ajax总结——XMLHttpRequest对象

    Ajax技术能向服务器异步请求额外的数据,会带来更好的用户体验.Ajax技术核心:XMLHttpRequest对象(简称XHR).XHR为向服务器发送请求和解析服务器响应提供了流畅的接口.1.创建XM ...

  7. 从零玩转系列之微信支付实战PC端支付微信退款接口搭建 | 技术创作特训营第一期

    一.前言 从零玩转系列之微信支付实战PC端支付微信退款接口搭建 | 技术创作特训营第一期 继前文章取消订单接口和查询订单接口此篇为申请退款流程,此篇文章过长我将分几个阶段的文章发布(项目源码都有,小程 ...

  8. 从零玩转Websocket实时通讯服务之前后端分离版本-websocket

    title: 从零玩转Websocket实时通讯服务之前后端分离版本 date: 2021-10-25 00:47:12.945 updated: 2021-12-26 17:43:10.496 ur ...

  9. OpenFeign:Spring Cloud声明式服务调用组件

    OpenFeign:Spring Cloud声明式服务调用组件 问题总结 OpenFeign? Feign VS OpenFeign? OpenFeign实现远程服务调用? OpenFeign超时控制 ...

  10. hwclock详解

    linux下hwclock命令详解 hwclock(hardware clock) 功能说明:显示与设定硬件时钟. 语 法:hwclock [--adjust][--debug][--directis ...