#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std; const int maxn = ;
//idea : s[0]做符号位, s[0]=0(0),1(>0),-1(<0) struct BigNum
{
int len; //长度
char s[maxn]; //储存ASCII为0~9的字符 BigNum() {
len = ; memset(s, , sizeof(s));
s[] = ; //符号为置为0
}
//重载 '=', 参数为字符串
BigNum operator = (const char *str)
{
len = ; memset(s, , sizeof(s)); //每次赋值前初始化
s[] = ;
len = strlen(str);
for (int i = ; i <= len; i++) //从位置1开始储存
s[i] = str[len-i] - ''; //将ASCII为0~9存入数组,将低位储存到前面
return *this;
}
//重载 '=', 参数为整型数
BigNum operator = (const int num)
{
len = ; memset(s, , sizeof(s));
s[] = ;
char str[maxn];
sprintf(str, "%d", num); //将num以字符'0'~'9'的形式写到str中
*this = str; //调用已经重载的'=',将字符str赋值
return *this;
}
//构造
BigNum(const char* str) { //用字符串做初值
*this = str;
}
BigNum(const int num) { //用整型做初值
*this = num;
} //用于输出函数
string str() const {
string res = "";
for (int i = len; i > ; i--) {
res += char(s[i] + ''); //输出时再转换成字符形式
}
if (res == "") //如果为空字符,即为0
res = "";
return res;
}
//重载 +
BigNum operator + (const BigNum& b)
{
BigNum x; //储存结果
int carry = ; //用来进位
int maxlen = max(len, b.len) + ; //得到最大的那一位 for (int i = ; i <= maxlen; i++)
{
carry = s[i] + b.s[i] + carry; //从低位向高位加
x.s[++x.len] = carry % ; //保存计算结果_保存的是ASCII的0~9
carry /= ; //计算进位
}
//避免高位为0
while (x.s[x.len] == && len >= )
x.len--;
return x;
} BigNum& operator += (const BigNum& b)
{
*this = *this + b;
return *this;
} //重载比较函数
bool operator < (const BigNum& b) const // <
{
if (len != b.len)
return len < b.len;
else {
for (int i = len; i > ; i--)
if (s[i] != b.s[i])
return s[i] < b.s[i];
}
return false;
}
bool operator > (const BigNum& b) const { return b < *this; } // >
bool operator >= (const BigNum& b) const { return !(*this < b); } // >=
bool operator <= (const BigNum& b) const { return ! (b < *this); } // <=
bool operator != (const BigNum& b) const { return (*this < b) || (b < *this); } // !=
bool operator == (const BigNum& b) const { return !(b < *this) && !(*this < b); }// == //重载 -
BigNum operator - (const BigNum& b)
{
//暂只支持 (大 - 小)
if (*this < b)
return -;
else {
if (*this == b)
return ;
BigNum x;
x.len = max(len, b.len); //保存结果的最大位数
int borrow = ; //用于表示是否 被借位 (没借--1,被借位--0) int i;
for (i = ; i <= x.len; i++)
{ //10为默认借位, -1为默认被借位
borrow = + s[i] - + borrow - b.s[i];
x.s[i] = borrow % ;
borrow /= ; //没有借位算出来的borrow大于10,除以10后为1,,借位的小于10--为0
}
x.len = i-;
while (x.s[x.len] == && x.len > ) //消除高位为 0
x.len--;
return x;
}
}
BigNum& operator -= (const BigNum& b)
{
*this = *this - b;
return *this;
} //重载 '*'
BigNum operator * (const long long& b)
{
BigNum x;
int i,
carry = ; //用于借位 for (i = ; i <= maxn; i++)
{
carry = s[i]*b + carry;
x.s[i] = carry % ; //得到低位
carry /= ; //得到进位
}
x.len = i;
while (x.s[len] == && x.len >= ) //消除高位为 0
x.len--;
return x;
} // '*'
BigNum operator * (const BigNum& b)
{
BigNum c; //结果
int carry, i, j, g, lenb = b.len; //carry用于进位
if ( (lenb == && b.s[] == ) || (len == && s[] == ))
{
c.s[] = ;
return c;
} for (i = , g = ; (i <= len + lenb - ) || g != ; i++) //进位为 0时,或者达到最高位时跳出循环
{
carry = g;
for (j = ; j <= i; j++)
if (j <= len && i-j <= lenb)
carry += s[j] * b.s[i-j+]; // i - j 相当于左移位
c.s[i] = carry % ;
g = carry / ; //得到进位
}
c.len = i-; //设置结果的长度为 i-1
while (c.s[c.len] == && c.len > ) //消除高位为 0,,,不能 >= 1---除数不为0
c.len--;
return c;
} BigNum operator ^ (int n)
{
BigNum result = , tmp;
int i;
if (n < )
return -;
if (n == )
return ;
if (n == )
return *this;
int m = n;
while (m > )
{
tmp = *this;
for (i = ; (i<<) <= m; i = i << ) // i<<1 -- > i^2
{
tmp = tmp * tmp;
}
m = m - i;
result = result * tmp;
if (m == )
result = result * (*this);
}
return result;
}
//重载 '/' -------- 试除法
BigNum operator / (const BigNum& b)
{
BigNum ten, one, c , d; // c--用来保存结果, d--用来保存试除数
int i ;
ten = ;
one = ;
for (i = len; i >= ; i--)
{
c = c * ten; //试除
d = d * ten;
d.s[] = s[i];
// cout << d.str() << endl;
while (b <= d) {
// cout << "b: " << b.str() << ", d: " << d.str() << endl;
c += one;
d = d - b;
// cout << "c: " << c.str() << endl;
}
}
while (c.s[c.len] == && c.len > ) //消除高位为 0
c.len--;
return c;
} BigNum operator % (const BigNum &n) // 大数 '%' 大数
{
return (*this - (*this / n) * n);
} BigNum operator % (const int &n) // 大数 '%' int
{
const BigNum tmp = n;
return (*this - (*this / tmp) * tmp);
}
}; //用string类,直接动态写入
istream& operator >> (istream& in, BigNum& b)
{
string s;
in >> s;
b = s.c_str(); //c_str()将string字符串,改成有'\0'的c字符串
return in;
} //重载为普通函数,调用已经写好的成员函数 str() 输出
ostream& operator << (ostream& out, const BigNum& b)
{
out << b.str();
return out;
} int main()
{
BigNum a, b, c; cout << "+ :\n";
cin >> a >> b;
c = a + b; // '+'
cout << c << endl<< '\n'; cout << "- :\n";
cin >> a >> b;
c = a - b; // '-'
cout << c << endl << endl; cout << "* :\n";
cin >> a >> b;
c = a * b; // '*'
cout << c << endl << endl; cout << "/ :\n";
cin >> a >> b;
c = a / b; // '/'
cout << c << endl << endl; cout << "乘方(^): \n";
int n;
cin >> a >> n;
c = a ^ n;
cout << c << endl << endl; cout << "取余(^): \n";
cin >> a >> n;
c = a % n;
cout << c << endl;
return ;
}

大数计算_BigNum优化_加减乘除乘方取余_带注释_数组的更多相关文章

  1. JAVA大数类—基础操作(加减乘除、取模、四舍五入、设置保留位数)

    当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecima ...

  2. Luogu P1226 取余运算||快速幂_快速幂

    超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...

  3. 为什么Java的hash表的长度一直是2的指数次幂?为什么这个(hash&(h-1)=hash%h)位运算公式等价于取余运算?

    1.什么是hash表? 答:简单回答散列表,在hash结构散列(分散)存放的一种数据集结构. 2.如何散列排布,如何均匀排布? 答:取余运算 3.Java中如何实现? 答:hash&(h-1) ...

  4. java 大数计算

    这几天做了几道用大数的题,发现java来做大数运算十分方便.对acmer来说是十分实用的 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger ...

  5. [Pytorch]深度模型的显存计算以及优化

    原文链接:https://oldpan.me/archives/how-to-calculate-gpu-memory 前言 亲,显存炸了,你的显卡快冒烟了! torch.FatalError: cu ...

  6. MySQL的性能指标计算和优化方法

    MySQL的性能指标计算和优化方法1 QPS计算(每秒查询数) 针对MyISAM引擎为主的DB mysql> show global status like 'questions';+----- ...

  7. 使用PHP语言制作具有加减乘除取余功能的简单计算器

    准备工作: 使用环境 :PHPStudy 开启Apache和Mysql 打开代码编辑器 <!DOCTYPE html> <html lang="en"> & ...

  8. PHP大数(浮点数)取余

    一般我们进行取余运算第一个想到的就是用百分号%,但当除数是个很大的数值,超出了int范围时,这样取余就不准确了. php大数(浮点数)取余函数 /** * php大数取余 * * @param int ...

  9. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

随机推荐

  1. 【PHP面向对象(OOP)编程入门教程】9.封装性(var与public,protected,private的关系)

    封装性是面象对象编程中的三大特性之一,封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 1. 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独 ...

  2. mysql索引优化

    mysql 索引优化 >mysql一次查询只能使用一个索引.如果要对多个字段使用索引,建立复合索引. >越小的数据类型通常更好:越小的数据类型通常在磁盘.内存和CPU缓存中都需要更少的空间 ...

  3. Android在TextView中实现RichText风格

    参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...

  4. ReactiveCocoa源码拆分解析(一)

    (整个关于ReactiveCocoa的工程可以在https://github.com/qianhongqiang/QHQReactive下载) ReactiveCocoa的介绍我就不说了,可以自行百度 ...

  5. Redis学习笔记二:单机数据库的实现

    1. 数据库 服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构的db数组中,db数组的每个项都是一个redis.h/redisDb结构,每个r ...

  6. bug-android之app:mergeDebugResources

    bug描述:Error:Execution failed for task ':app:mergeDebugResources'. > Crunching Cruncher seekbar_th ...

  7. PingUtil in Android

    Ping a host in Android:“ping -c 1 127.0.0.1”-c 1: The ping times. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  8. 读书笔记-JVM

    局部变量表(虚拟机栈中的一部分)在编译期完成分配,运行期不会再改变大小: 每个方法对应一个栈帧(存储局部变量表.操作数栈.动态链接.方法出口等),栈帧被存储到虚拟机栈中,每个线程对应一个虚拟机栈,方法 ...

  9. 将Apache加入到linux系统service

    将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...

  10. div 自动满屏

    通常通过jq来做,类似这样: $('#navigation').css({ height: $(window).innerHeight() }); css3后,只需要用 下面这段样式即可 #navig ...