#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. Python学习————字典的增删改查

    增加:dic1['KEY'] = value -->若之前有KEY,则会覆盖.若没有KEY,则新增至尾处dic.setdefault('KEY',value/None) --->若之前有K ...

  2. POJ——T 3687 Labeling Balls

    http://poj.org/problem?id=3687 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14842   ...

  3. 使用Qt.labs.settings来存储应用的设置

    我在曾经的文章中,讲述了怎样使用U1db及SQLite offline storage API来存储应用的一些状态.在这篇文章中,我将介绍怎样使用Qt.labs.settings来存储应用的状态.更加 ...

  4. LeetCode【8】. String to Integer (atoi) --java实现

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. mysql-过程与函数

    一.过程与函数简介 过程与函数是命名的PL/SQL块(也是用户的方案对象),被编译后存储在数据库中,以备执行.因此,其他PL/SQL块可以按名称来使用他们.所以可以将商业逻辑.企业规划写成函数或过程保 ...

  6. Compile OpenCASCADE7.3 with VS2008

    Compile OpenCASCADE7.3 with VS2008 eryar@163.com 概述 在OpenCASCADE的源码文件夹中有个adm文件夹,里面提供了各个平台中编译源码的项目文件. ...

  7. 关于有的Apk无法反编译的探究

    Android的apk包,其实就是zip包,只不过后缀名换了而已!使用“好压”等解压缩工具解压,就可以看到里面的内容了.简单介绍一下吧. 以下就是解压出来的apk的内容: 其中: assets     ...

  8. Laravel-错误调试与记录日志

    Laravel-错误调试与记录日志 标签(空格分隔): php 错误调试 配置 修改/config/app.php 'debug' => env('APP_DEBUG', true), 开启de ...

  9. CMake入门之创建一个基于PCL的最小工程

    最近在学习PCL,借助Cmake可省去繁琐的添加包含目录和依赖库操作. 一个典型的CMakeLists.txt内容通常为: cmake_minimum_required(VERSION 2.6 FAT ...

  10. UVa 208 Firetruck【回溯】

    题意:给出一个n个节点的无向图,以及某个节点k,按照字典序从小到大输出从节点1到节点k的所有路径 看的题解 http://blog.csdn.net/hcbbt/article/details/975 ...