大数加法和乘法

不知道错那里的代码

public static String mul(String str1, String str2)
    {
        int minLength = -1;
        int maxLength = -1;
        if (str1.length() > str2.length())
        {
            minLength = str2.length();
            maxLength = str1.length();
        }
        else
        {
            minLength = str1.length();
            maxLength = str2.length();
            String temp = str1;
            str1 = str2;
            str2 = temp;
        }
        int[] cc = new int[maxLength + minLength + 100];
        int maxIndex = -1;
        for (int i = minLength - 1; i >= 0; i--)
        {
            char c2 = str2.charAt(i);
            int cIndex = minLength - 1 - i;
            int dx = 0;
            for (int j = maxLength - 1; j >= 0; j--)
            {
                cc[cIndex] = (str1.charAt(j) - '0') * (c2 - '0') + dx
                        + cc[cIndex];
                dx = cc[cIndex] / 10;
                cc[cIndex] = cc[cIndex] % 10;
                cIndex++;
            }
            if (maxIndex < cIndex)
            {
                maxIndex = cIndex;
            }
            cIndex = maxIndex;
            if (dx != 0)
            {
                while (dx != 0)
                {
                    cc[cIndex] = cc[cIndex] + dx;
                    dx = cc[cIndex] / 10;
                    cc[cIndex] = cc[cIndex] % 10;
                    cIndex++;
                }
                maxIndex = cIndex;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = maxIndex - 1; i >= 0; i--)
        {
            sb.append((char) (cc[i] + '0'));
        }
        return sb.toString();
    }

    public static String add(String str1, String str2)
    {
        int minLength = -1;
        int maxLength = -1;
        if (str1.length() > str2.length())
        {
            minLength = str2.length();
            maxLength = str1.length();
        }
        else
        {
            minLength = str1.length();
            maxLength = str2.length();
            String temp = str1;
            str1 = str2;
            str2 = temp;
        }
        int dx = 0;
        int dl = maxLength - minLength;
        char[] cc = new char[maxLength + 100];
        int cIndex = 0;
        for (int i = minLength - 1; i >= 0; i--)
        {
            int s = str1.charAt(i + dl) - '0' + str2.charAt(i) - '0' + dx;
            dx = s / 10;
            s = s % 10;
            cc[cIndex++] = (char) (s + '0');
        }
        for (int i = maxLength - minLength - 1; i >= 0; i--)
        {
            int s = str1.charAt(i) - '0' + dx;
            dx = s / 10;
            s = s % 10;
            cc[cIndex++] = (char) (s + '0');
        }
        if (dx != 0)
        {
            cc[cIndex++] = (char) (dx + '0');
        }
        StringBuilder sb = new StringBuilder();
        for (int i = cIndex - 1; i >= 0; i--)
        {
            sb.append(cc[i]);
        }
        return sb.toString();
    }

    public static boolean overflow(String str)
    {
        int maxInt = 0x7fffffff;
        String max = maxInt + "";
        if (str.length() > max.length())
        {
            return true;
        }
        if (str.length() < max.length())
        {
            return false;
        }
        for (int i = 0; i < max.length(); i++)
        {
            if (max.charAt(i) < str.charAt(i))
            {
                return true;
            }
            else if (max.charAt(i) > str.charAt(i))
            {
                return false;
            }

        }
        return false;
    }

    public static String trimStr(String str)
    {
        StringBuilder sb = new StringBuilder();
        boolean startNum = false;
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (startNum)
            {
                sb.append(c);
                continue;
            }
            if (!startNum && c != '0')
            {
                startNum = true;
                sb.append(c);
            }
        }
        if(sb.length()==0)
        {
            return "0";
        }
        return sb.toString();
    }
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6287776.html