高精度四则运算

5 篇文章 0 订阅
class HighPrecisionOperation
{
public:
    static int Compare(string a, string b) //a>b 1  a==b 0  a<b -1
    {
        if (a[0] == '-'&&b[0] == '-') return -Compare(a.substr(1), b.substr(1));
        else if (a[0] == '-'&&b[0] != '-') return -1;
        else if (a[0] != '-'&&b[0] == '-') return 1;
        else
        {
            a = IntegerSimplify(a);
            b = IntegerSimplify(b);
            if (a.length() > b.length()) return 1;
            else if (a.length() < b.length()) return -1;
            else
            {
                if (a > b) return 1;
                else if (a < b) return -1;
                else return 0;
            }
        }
    }

    static string Add(string a, string b)
    {
        if (a[0] == '-'&&b[0] == '-') return "-" + Add(a.substr(1), b.substr(1));
        else if (a[0] == '-' &&b[0] != '-') return Sub(b, a.substr(1));
        else if (a[0] != '-'&& b[0] == '-') return Sub(a, b.substr(1));
        else
        {
            a = IntegerSimplify(a);
            b = IntegerSimplify(b);
            int M = a.length(), N = b.length();
            int L = max(M, N);
            int *result = new int[L + 1]{ 0 };
            for (size_t i = 0; i < L; i++)
            {
                if (i < a.length()) result[i] += a[M - 1 - i] - '0';
                if (i < b.length()) result[i] += b[N - 1 - i] - '0';
                if (result[i] >= 10)
                {
                    result[i + 1] += result[i] / 10;
                    result[i] %= 10;
                }
            }
            string c("");
            for (int i = result[L] == 0 ? L - 1 : L; i >= 0; i--)
                c += result[i] + '0';
            delete[] result;
            return c;
        }
    }
    static string Sub(string a, string b)
    {
        if (a[0] == '-'&&b[0] == '-') //-A-(-B)= B-A
            return Sub(b.substr(1), a.substr(1));
        else if (a[0] == '-'&&b[0] != '-') //-A-B=-(A+B)
            return "-" + Add(a.substr(1), b);
        else if (a[0] != '-' && b[0] == '-') //A-(-B)=A+B
            return Add(a, b.substr(1));
        else //A-B
        {
            a = IntegerSimplify(a);
            b = IntegerSimplify(b);
            int Comp = Compare(a, b);
            if (Comp == 0)
                return "0";
            else if (Comp < 0)
                return "-" + Sub(b, a);
            else
            {
                int M = a.length(), N = b.length();
                int L = M, highestDigit = 0;
                int *result = new int[L] {0};
                for (size_t i = 0; i < L; i++)
                {
                    if (i < b.length())
                        result[i] += a[M - 1 - i] - b[N - 1 - i];
                    else
                        result[i] += a[M - 1 - i] - '0';
                    if (result[i] < 0)
                    {
                        result[i + 1] -= 1;
                        result[i] += 10;
                    }
                    if (result[i] != 0)
                        highestDigit = i;
                }
                string c("");
                for (int i = highestDigit; i >= 0; i--)
                    c += result[i] + '0';
                delete[] result;
                return c;
            }
        }
    }
    static string Multiply(string a, string b)
    {
        if (a[0] == '-'&&b[0] != '-') return "-" + Multiply(a.substr(1), b);
        else if (a[0] != '-'&&b[0] == '-') return "-" + Multiply(a, b.substr(1));
        else if (a[0] == '-' &&b[0] == '-') return Multiply(a.substr(1), b.substr(1));
        else
        {
            a = IntegerSimplify(a);
            b = IntegerSimplify(b);
            int M = a.length(), N = b.length();
            int L = M + N;
            int *result = new int[L] {0};
            for (size_t i = 0; i < N; i++)
                for (size_t j = 0; j < M; j++)
                    result[i + j] += (b[N - 1 - i] - '0')*(a[M - 1 - j] - '0');
            int highestDigit = 0;
            for (size_t i = 0; i < L; i++)
            {
                if (result[i] >= 10)
                {
                    result[i + 1] += result[i] / 10;
                    result[i] %= 10;
                }
                if (result[i])
                    highestDigit = i;
            }
            string c = "";
            for (int i = highestDigit; i >= 0; i--)
                c += result[i] + '0';
            delete[] result;
            return c;
        }
    }
    static string Divide(string a, string b)
    {
        if (b == "0")
        {
            throw string("Divisor Is 0");
        }
        if (a[0] == '-'&&b[0] != '-') return "-" + Divide(a.substr(1), b);
        else if (a[0] != '-' && b[0] == '-') return "-" + Divide(a, b.substr(1));
        else if (a[0] == '-'&&b[0] == '-') return Divide(a.substr(1), b.substr(1));
        else 
        {
            int Comp = Compare(a, b);
            if (Comp<0) return "0";
            else
            {
                int M, N ,L=a.length()-b.length()+1;
                string tmpDividend=a.substr(0,b.length()), tmpDifference, multiResult, c;
                while (L--)
                {
                    if (Compare(tmpDividend, b) < 0)
                    {
                        c += "0";
                        tmpDividend = a.substr(0, tmpDividend.length() + 1);
                        continue;
                        if (N + 1 <= M)
                            tmpDividend = a.substr(0, ++N);
                    }
                    char i;
                    for (i = '9'; i >= '0'; i--)
                    {
                        multiResult = Multiply(b, string() + i);
                        if (Compare(multiResult, tmpDividend) <= 0)
                            break;
                    }
                    c += i;
                    tmpDifference = Sub(tmpDividend, multiResult);
                    if (tmpDifference != "0")
                    {
                        a = tmpDifference + a.substr(tmpDividend.length());
                        tmpDividend = a.substr(0, tmpDifference.length() + 1);
                    }
                    else
                    {
                        a = a.substr(tmpDividend.length());
                        tmpDividend = a.substr(0, 1);
                    }
                }
                return IntegerSimplify(c);
            }
        }
    }
    private:
    static string IntegerSimplify(string a)
    {
        return a.erase(0, a.find_first_not_of('0'));
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值