高精度算法模板-(结构体重载运算符实现+-*)简便

相比之前通过函数的形式实现,结构体重载运算符显得更简便,尤其是在主函数运算的时候,形式与平时无异。
其中mod为数组中每一位的模,若mod=10,则数组中每一个数对应的就是该数的每一位,则数组的长度len就是该数的位数,该模板采用的是四位压缩的方法,每四位存一个数。(mod的的大小决定运算的快慢和输出的方式)
模板如下:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <cstdio>
#define maxn 10000
#define mod 10000
using namespace std;
int n,m,a[maxn];
struct BI{
int l[5000],len;
void print(){
    printf("%d",l[len]);
for(int q=len-1;q>0;q--){
if(l[q]==0){
printf("0000");
continue;
}
for(int w=10;w*l[q]<mod;w*=10){
printf("0");
}
printf("%d",l[q]);
}}}p;
BI operator +(const BI &a,const BI &b)
{
    BI c;
    c.len=max(a.len,b.len);
    int x=0;
    for(int q=1; q<=c.len; q++)
    {
        c.l[q]=a.l[q]+b.l[q]+x;
        x=c.l[q]/mod;
        c.l[q]%=mod;
    }
    if(x>0)
    {
        c.l[++c.len]=x;
    }
    return c;
}
BI operator *(const BI &a,const BI &b)
{
    BI c;
    c.len=max(a.len,b.len);
    int x;
    for(int q=1; q<=a.len; q++)
    {
        x=0;
        for(int w=1; w<=b.len; w++)
        {
            c.l[q+w-1]+=a.l[q]*b.l[w]+x;
            x=c.l[q+w-1]/mod;
            c.l[q+w-1]%=mod;
        }
        c.l[q+b.len]=x;
    }
    c.len=a.len+b.len;
    while((c.l[c.len]==0)&&(c.len>1))
    {
        c.len--;
    }
    return c;
}
BI operator -(const BI &a,const BI &b)
{
    BI x=a,y=b,c;
    c.len=max(a.len,b.len);
    for(int q=1; q<=c.len; q++)
    {
        if(x.l[q]<y.l[q])
        {
            x.l[q+1]--;
            x.l[q]+=mod;
        }
        c.l[q]+=(x.l[q]-y.l[q]);
    }
    while((c.l[c.len]==0)&&(c.len!=1))
    {
        c.len--;
    }
    return c;
}
int main()
{ 
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值