高精度四则运算

来源:互联网 发布:怎么重新注册知乎 编辑:程序博客网 时间:2024/06/05 08:43
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'));    }};
0 0