字符串笔试题2:大数的加减法计算

题目:
大数的加减法

class BigInteger
{
public:
    BigInteger(char* number);
private:
    char *_number;
    friend BigInteger operator+(const BigInteger &left, const BigInteger &right);
    friend BigInteger operator-(const BigInteger &left, const BigInteger &right);
};
BigInteger operator+(const BigInteger &left, const BigInteger &right);
BigInteger operator-(const BigInteger &left, const BigInteger &right);
请完成以上代码,实现大数的加减法(减法考虑负数情况),例如输入表达式如下:
23761798265789632409851927836478916279384+6756857986094375190283749126412347861297834
打印计算结果: xxxxx
输入表达式如下:
7892136478712036589789347438971627836487125478-907987897213894782174234
打印计算结果: xxxxx

代码实现:

class BigInteger
{
public:
    BigInteger(char* number) :_number(number) {}
    void Print()
    {
        cout << _number << endl;
    }
private:
    char *_number;
    friend BigInteger operator+(const BigInteger &left, const BigInteger &right);
    friend BigInteger operator-(const BigInteger &left, const BigInteger &right);
};

BigInteger operator+(const BigInteger &left, const BigInteger &right)
{
    char* s1 = left._number;
    char* s2 = right._number;
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    if (len2 == 0 && len1 == 0)
    {
        return NULL;
    }
    int max = (len1 > len2) ? len1 : len2;//选取两个字符串长度的最大值,作为加的次数(最多)
    static char s[50];//保存相加后的结果
    if (max == len1)
    {
        strcpy_s(s,len1+1,s1);
    }
    else
    {
        strcpy_s(s,len2+1,s2);
    }

    int flag = 0;//设置进位
    int len = max;
    for (int i = 0; i < max; ++i)//
    {
        //保存每一位相加的结果
        int temp;
        if (len2 == 0 && len1 > 0)
        {
            temp = (s1[len1 - 1] - '0') + flag;
            break;
        }
        else if (len2 > 0 && len1 == 0)
        {
            temp = (s2[len2 - 1] - '0') + flag;
            break;
        }
        else
        {
            temp = (s1[len1 - 1] - '0') + (s2[len2 - 1] - '0') + flag;
        }

        if (temp >= 10)
        {
            flag = 1;
            s[len - 1] = temp - 10 + '0';
        }
        else
        {
            flag = 0;
            s[len - 1] = temp + '0';
        }

        len1--;
        len2--;
        len--;
    }

    if (flag == 1 && len == 0)//如果两个字符串长度相同,并且最后加有进位,则在最高位添加
    {
        char t[50] = "1";
        strcat_s(t,strlen(s)+1,s);
        return BigInteger(t);
    }
    else
    {
        return BigInteger(s);
    }
}

BigInteger operator-(const BigInteger &left, const BigInteger &right)
{
    char *s1 = left._number;
    char *s2 = right._number;
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int max = (len1 > len2) ? len1 : len2;//选取两个字符串长度的最大值,作为减的次数(最多)
    static char s[50];//保存相减后的结果
    if (max == len1)
    {
        strcpy_s(s, len1 + 1, s1);
    }
    else
    {
        strcpy_s(s, len2 + 1, s2);
    }
    int flag = 0;//设置借位
    int len = max;
    for (int i = 0; i < max; ++i)//
    {
        //保存每一位相减后的结果
        int temp;
        if (len2 == 0 && len1 > 0)
        {
            temp = (s1[len1 - 1] - '0') + flag;
            break;
        }
        else if (len2 > 0 && len1 == 0)
        {
            temp = (s2[len2 - 1] - '0') + flag;
            break;
        }
        else
        {
            temp = (s1[len1 - 1] - '0') - (s2[len2 - 1] - '0') + flag;
        }

        if (temp < 0)
        {
            flag = -1;
            s[len - 1] = temp + 10 + '0';
        }
        else
        {
            flag = 0;
            s[len - 1] = temp + '0';
        }
        len1--;
        len2--;
        len--;
    }
    if (flag == 1 && len == 0)//如果两个字符串长度相同,并且最后加有借位,则在最高位添加-
    {
        char t[50] = "-";
        strcat_s(t, strlen(s) + 1, s);
        return BigInteger(t);
    }
    else
    {
        return BigInteger(s);
    }
}

测试程序:

int main()
{
    char* str1 = "123761798265789632409851927836478916279384";
    char* str2 = "6756857986094375190283749126412347861297834";

    BigInteger s1(str1),s2(str2);
    BigInteger result1 = s1 + s2;
    result1.Print();

    char* str3 = "7892136478712036589789347438971627836487125478";
    char* str4 = "907987897213894782174234";

    BigInteger s3(str3), s4(str4);
    BigInteger result2 = s3 - s4;
    result2.Print();

    return 0;
}

你可能感兴趣的:(笔试面试)