#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std; #define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
const long long MOD = ;
class BigInteger
{
private:
int a[]; //可以控制大数的位数
int len; //大数长度
public:
BigInteger(){ len = ; memset(a, , sizeof(a)); } //构造函数
BigInteger(const int); //将一个int类型的变量转化为大数
BigInteger(const char*); //将一个字符串类型的变量转化为大数
BigInteger(const BigInteger &); //拷贝构造函数
BigInteger &operator=(const BigInteger &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigInteger&); //重载输入运算符
friend ostream& operator<<(ostream&, BigInteger&); //重载输出运算符 BigInteger operator+(const BigInteger &) const; //重载加法运算符,两个大数之间的相加运算
BigInteger operator-(const BigInteger &) const; //重载减法运算符,两个大数之间的相减运算
BigInteger operator*(const BigInteger &) const; //重载乘法运算符,两个大数之间的相乘运算
BigInteger operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigInteger operator^(const int &) const; //大数的n次方运算
long long operator%(const long long &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigInteger & T)const; //大数和另一个大数的大小比较
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较 void print(); //输出大数
};
BigInteger::BigInteger(const int b) //将一个int类型的变量转化为大数
{
int c, d = b;
len = ;
memset(a, , sizeof(a));
while (d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigInteger::BigInteger(const char*s) //将一个字符串类型的变量转化为大数
{
int t, k, index, l, i;
memset(a, , sizeof(a));
l = strlen(s);
len = l / DLEN;
if (l%DLEN)
len++;
index = ;
for (i = l - ; i >= ; i -= DLEN)
{
t = ;
k = i - DLEN + ;
if (k<)
k = ;
for (int j = k; j <= i; j++)
t = t * + s[j] - '';
a[index++] = t;
}
}
BigInteger::BigInteger(const BigInteger & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = T.a[i];
}
BigInteger & BigInteger::operator=(const BigInteger & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigInteger & b) //重载输入运算符
{
char ch[MAXSIZE * ];
int i = -;
in >> ch;
int l = strlen(ch);
int count = , sum = ;
for (i = l - ; i >= ;)
{
sum = ;
int t = ;
for (int j = ; j< && i >= ; j++, i--, t *= )
{
sum += (ch[i] - '')*t;
}
b.a[count] = sum;
count++;
}
b.len = count++;
return in; }
ostream& operator<<(ostream& out, BigInteger& b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for (i = b.len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << b.a[i];
}
return out;
} BigInteger BigInteger::operator+(const BigInteger & T) const //两个大数之间的相加运算
{
BigInteger t(*this);
int i, big; //位数
big = T.len > len ? T.len : len;
for (i = ; i < big; i++)
{
t.a[i] += T.a[i];
if (t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -= MAXN + ;
}
}
if (t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigInteger BigInteger::operator-(const BigInteger & T) const //两个大数之间的相减运算
{
int i, j, big;
bool flag;
BigInteger t1, t2;
if (*this>T)
{
t1 = *this;
t2 = T;
flag = ;
}
else
{
t1 = T;
t2 = *this;
flag = ;
}
big = t1.len;
for (i = ; i < big; i++)
{
if (t1.a[i] < t2.a[i])
{
j = i + ;
while (t1.a[j] == )
j++;
t1.a[j--]--;
while (j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while (t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if (flag)
t1.a[big - ] = - t1.a[big - ];
return t1;
} BigInteger BigInteger::operator*(const BigInteger & T) const //两个大数之间的相乘运算
{
BigInteger ret;
int i, j, up;
int temp, temp1;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < T.len; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if (temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if (up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigInteger BigInteger::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigInteger ret;
int i, down = ;
for (i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
long long BigInteger::operator %(const long long & b) const //大数对一个int类型的变量进行取模运算
{
int i;
long long d = ;
for (i = len - ; i >= ; i--)
{
d = ((d * (MAXN + )) % b + (long long)(a[i])) % b;
}
return d;
}
BigInteger BigInteger::operator^(const int & n) const //大数的n次方运算
{
BigInteger t, ret();
int i;
if (n<)
exit(-);
if (n == )
return ;
if (n == )
return *this;
int m = n;
while (m>)
{
t = *this;
for (i = ; i << <= m; i <<= )
{
t = t*t;
}
m -= i;
ret = ret*t;
if (m == )
ret = ret*(*this);
}
return ret;
}
bool BigInteger::operator>(const BigInteger & T) const //大数和另一个大数的大小比较
{
int ln;
if (len > T.len)
return true;
else if (len == T.len)
{
ln = len - ;
while (a[ln] == T.a[ln] && ln >= )
ln--;
if (ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigInteger::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigInteger b(t);
return *this>b;
} void BigInteger::print() //输出大数
{
int i;
cout << a[len - ];
for (i = len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << a[i];
}
cout << endl;
}

其实这个也没多大用,大数肯定直接用Java了

C++ BigInteger模板的更多相关文章

  1. 模板-高精度BigInteger

    #include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...

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

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

  3. 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】

    Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...

  4. 模板——BigInteger

    #include <iostream> #include <cstring> #include <string> #include <vector> # ...

  5. BigInteger

    首先上模板(不断更新中...)(根据刘汝佳AOAPCII修改) #include <iostream> #include <sstream> #include <cstd ...

  6. 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem

    P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...

  7. Java 大数、高精度模板

    介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...

  8. 大数模板 poj3982

    1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...

  9. JAVA高精度模板

    刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...

随机推荐

  1. Ubuntu 16.04 Chrome浏览器安装flash player插件

    1:官网下载插件  flash palyer lash_player_npapi_linux_debug.x86_64.tar.gz 2:解压 提取 libpepflashplayer.so 3:手动 ...

  2. python学习笔记:第六天

    一.元组(通用格式a=(1,),结束后面加个逗号,不同与数组是中括号,只能是只读的,不能修改,是有序的): 列表之间可以嵌套(列表之间嵌套,嵌套元组,是有序的):a[b[1,2],c[3,4]],输出 ...

  3. jQuery实现点击图标div循环放大缩小功能

    很基础的一个功能,点击左下角的图标按钮,地图的整个div会变大,变大预览之后,再次点击图标按钮,地图的整个div会变小,恢复原样,两个图标在地图界面的放大和缩小时间不断的切换图标状态(箭头向里面,或者 ...

  4. 将Spring Boot应用程序迁移到Java9:兼容性

    将 Spring Boot 应用程序迁移到 Java 9:兼容性 随着 Java 9 的到来,关于如何迁移应用程序以使用模块系统有很多的讨论.遗憾的是,大多数文章的焦点都集中于简单的 Hello Wo ...

  5. A simpleHttp Proxy

    http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm

  6. java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized

    Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä ...

  7. Python学习之三【对象和类型&amp;&amp;运算符】

    [对象和类型] 学生的属性: 小明 对象 姓名:男 性别: 年龄: 身高: 体重: 籍贯: 五种基本对象类型 字符串 (string),简记为 str 使用 ' ' 或 " " 括 ...

  8. new 对象和Class的getInstance()方法的差别?

    创建对象时的差别 1.new 对象包含2步, 1)载入类: 2)而且实例化. 2.Class的对象.getInstance(),只不过实例化. 也就是说.在运行 Class的对象.getInstanc ...

  9. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

  10. 手机表单验证插件mvalidate的使用

    使用 1.引入js和css <script type="text/javascript" src="../script/jquery-mvalidate.js&qu ...