• 高精度 加减乘


    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    using namespace std;
    
    const int MAXN = 2007;
    
    struct BigNum
    {///数值保存从0位开始
    
        int num[MAXN];///数值,逆序保存
        int op;///表示符号位,1表示正数,-1表示负数
        int size;///数的位数
    
        BigNum(){
            size=1, op=1;
            memset(num, false, sizeof(num));
        }
        void Cin()
        {
            char s[MAXN];
    
            scanf("%s", s);
            size = strlen(s);
    
            for(int i=0, k=size-1; i<size; i++)
            {
                if(s[i]=='-')
                {
                    k--;
                    op = -1;
                }
                else
                    num[k--] = s[i] - '0';
            }
    
            if(op == -1)
                size--;
        }
        bool operator <= (const BigNum &b)const
        {///去符号比较大小
            if(size > b.size)return false;
            if(size < b.size)return true;
    
            for(int i=size-1; i>=0; i--)
            {
                if(num[i] > b.num[i])return false;
                if(num[i] < b.num[i])return true;
            }
    
            return true;
        }
        void operator = (const BigNum &b)
        {
            size = b.size, op=b.op;
    
            for(int i=0; i<b.size; i++)
                num[i] = b.num[i];
        }
        void operator = (const int &b)
        {
            int x = b;
    
            if(x < 0)
            {
                op = -1;
                x = -x;
            }
    
            size = 0;
            do
            {
                num[size++] = x % 10;
                x /= 10;
            }
            while(x);
        }
        BigNum operator * (const BigNum &b)const
        {
            BigNum res;
    
            res.op = b.op * op;
            res.size = size + b.size - 1;
    
            for(int i=0; i<b.size; i++)
            for(int j=0; j<size; j++)
            {
                res.num[i+j] += num[j] * b.num[i];
            }
    
            res.CarryBit();
    
            return res;
        }
        BigNum operator * (const int &b)const
        {
            int x = b;
            BigNum res;
    
            res.size = size;
            if(x < 0)
            {
                res.op = res.op * -1;
                x = -x;
    
            }
    
            for(int i=0; i<size; i++)
            {
                res.num[i] = num[i] * x;
            }
    
            res.CarryBit();
    
            return res;
        }
        friend BigNum operator + (BigNum a, BigNum b)
        {
            BigNum res;
    
            if(a.op != b.op)
            {
                res = a - b;
                b.op = b.op * -1;
            }
            else
            {
                res.op = a.op;
                res.size = max(a.size, b.size);
    
                for(int i=0; i<res.size; i++)
                    res.num[i] = a.num[i] + b.num[i];
                res.CarryBit();
            }
    
            return res;
        }
        friend BigNum operator + (BigNum a, int b)
        {
            BigNum res;
            res = b;
    
            res = res + a;
    
            return res;
        }
        friend BigNum operator - (BigNum a, BigNum b)
        {///b值小,先比较在进行相减
            BigNum res;
    
            b.op = b.op * -1;
    
            if(a.op == b.op)
                res = a + b;
            else
            {
                if(a <= b)
                    swap(a, b);
    
                res.size = a.size;
                res.op = a.op;
    
                for(int i=0; i<res.size; i++)
                {
                    if(i < b.size)
                        res.num[i] = a.num[i] - b.num[i];
                    else
                        res.num[i] = a.num[i];
                }
                res.BorrowBit();
            }
            return res;
        }
        friend BigNum operator - (BigNum a, int b)
        {
            BigNum res;
            res = b;
    
            res = a - res;
    
            return res;
        }
        void CarryBit()
        {///进位,注意减法的时候进位结果需要是非负数
            for(int i=0; i<size; i++)
            {
                if(num[i] >= 10)
                {
                    if(i+1==size)
                    {
                        num[i+1] = 0;
                        size += 1;
                    }
                    num[i+1] += num[i]/10;
                    num[i] %= 10;
                }
            }
            while(size > 1 && !num[size-1])
                size--;
            if(size == 1 && !num[0])
                op = 1;
        }
        void BorrowBit()
        {
            for(int i=0; i<size; i++)
            {
                if(num[i] < 0)
                {
                    num[i] += 10;
                    num[i+1] -= 1;
                }
            }
    
            while(size > 1 && !num[size-1])
                size--;
    
            if(size == 1 && !num[0])
                op = 1;
        }
        void Out()
        {
            if(op == -1)
                printf("-");
            for(int i=size-1; i>=0; i--)
                printf("%d", num[i]);
            printf("
    ");
        }
    };
    
    int main()
    {
        BigNum a, b;
    
        a.Cin();
        b.Cin();
    
        a = a * b;
    
        a.Out();
    
        return 0;
    }
  • 相关阅读:
    委托事件学习笔记
    开发小技巧:C#逐个输出字符
    存储过程实例总结(开发中的错误与总结,调试,数据库函数DATEDIFF计算当前日期是否在本周内)
    一个简单的通用面板和菜单类
    PHP配置图文教程
    LaTeX——代码框风格设置
    layui获取弹出层内容
    argis android sdk配置备忘一下
    记录下-两点角度计算
    注册dll命令
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4864619.html
Copyright © 2020-2023  润新知