大整数加减乘除的实现

为了复习C++基本的东西,自己随便写的,实现的是正的大整数的加减乘除,其中除的实现比较低效,没有找到很好的解决方法。

实现代码:

#include <iostream>   
using namespace std;   

const int MAXLEN = 100;  

class Integer   
{
private:
    int is_neg; //是否为负
    int len; // 位数
    int s[MAXLEN]; // 表示整数的数组
    char str[MAXLEN]; //原字符串

public:
    Integer(const char *string = "")
    {
        memset(s,0,MAXLEN*sizeof(int));
        memset(str,0,MAXLEN*sizeof(char));
        strcpy(str,string);
        is_neg = 0;
        len = strlen(str);   
        for (int i = 0; i < len; i++)   
            s[i] = int(str[len-1-i]) - 48;
    }
    Integer & operator=(const Integer & oth)
    {
        if(this == &oth) return *this;
        memset(s,0,MAXLEN*sizeof(int));
        memset(str,0,MAXLEN*sizeof(char));
        is_neg = oth.is_neg;
        len = oth.len;
        for (int i = 0; i < oth.len; i++)   
            s[i] = oth.s[i];
        strcpy(str,oth.str);
        return *this;
    }

    bool operator==(const Integer & oth)
    {
        if(this == &oth) return true;
        bool ret = true;
        if(len != oth.len || is_neg != oth.is_neg)
            ret = false;
        if(strcmp(str,oth.str)) ret = false;
        for (int i = 0; i < oth.len; i++)
        {
            if(s[i] != oth.s[i]) ret = false;
        }
        return ret;
    }

    bool operator!=(const Integer & oth)
    {
        return !(*this == oth);
    }

    Integer operator+(const Integer & oth)
    {
        Integer c;
        int length = len >= oth.len ? len : oth.len;
        length += 1; //可能会进位
        c.len = length;
        for(int i = 0; i < c.len; i++)
        {
            c.s[i] += s[i] + oth.s[i];   
            c.s[i+1] += c.s[i] / 10;   
            c.s[i] = c.s[i] % 10;
        }
        while ((c.len > 1) && (c.s[c.len-1] == 0))  c.len--; 
        int k = 0;
        for(int i = c.len-1; i >= 0; i--)
        {
            c.str[k++] = (char)(c.s[i]+48);
        }
        return c;
    }

    Integer operator-(const Integer & oth)
    {
        Integer c;
        int flag = 0;

        if(len > oth.len) flag = 1;
        else if(len < oth.len) flag = -1;
        else flag = strcmp(str,oth.str);

        if(flag >= 0)
        {
            c.len = len;
            int borrow = 0;
            for (int i = 0; i < c.len; i++)
            {
                c.s[i] += s[i] - oth.s[i];
                if(borrow) c.s[i] -= 1;
                if(c.s[i] < 0)
                {
                    c.s[i] += 10;
                    borrow = 1;
                }
                else
                    borrow = 0;
            }
            while ((c.len > 1) && (c.s[c.len-1] == 0))  c.len--; 
        }
        else
        {
            c.is_neg = 1;
            c.len = oth.len;
            int borrow = 0;
            for (int i = 0; i < c.len; i++)
            {
                c.s[i] += oth.s[i] - s[i];
                if(borrow) c.s[i] -= 1;
                if(c.s[i] < 0)
                {
                    c.s[i] += 10;
                    borrow = 1;
                }
                else
                    borrow = 0;
            }
            while ((c.len > 1) && (c.s[c.len-1] == 0))  c.len--; 
        }

        int k = 0;
        for(int i = c.len-1; i >= 0; i--)
        {
            c.str[k++] = (char)(c.s[i]+48);
        }
        return c;   
    }

    Integer operator*(const Integer & oth)   
    {
        Integer c;
        c.len = len + oth.len + 1;  
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < oth.len; j++)
            {
                c.s[i+j] += s[i] * oth.s[j];   
                c.s[i+j+1] += c.s[i+j] / 10;   
                c.s[i+j] = c.s[i+j] % 10;   
            }
        }
        while ((c.len > 1) && (c.s[c.len-1] == 0))  c.len--;
        int k = 0;
        for(int i = c.len-1; i >= 0; i--)
        {
            c.str[k++] = (char)(c.s[i]+48);
        }
        return c;
    }

    Integer operator/(const Integer & oth)
    {
        Integer c("0");
        int flag = 0;

        if(len > oth.len) flag = 1;
        else if(len < oth.len) flag = -1;
        else flag = strcmp(str,oth.str);

        Integer one("1");
        Integer zero("0");
        Integer t;
        if(flag >= 0) //被除数大于除数,就一直减除数
        {
            while(this->is_neg == 0 && *this != zero)
            {
                t = *this - oth;
                *this = t;
                c = c + one;
            }
        }
        else //被除数小于除数,就为0
        {
            int k = 0;
            for(int i = c.len-1; i >= 0; i--)
            {
                c.str[k++] = (char)(c.s[i]+48);
            }
            return c;
        }

        int k = 0;
        for(int i = c.len-1; i >= 0; i--)
        {
            c.str[k++] = (char)(c.s[i]+48);
        }
        return c;   
    }

    friend ostream & operator<<(ostream & out, const Integer & oth)
    {
        if(oth.is_neg) out<<"-";
        for (int k = oth.len-1; k >= 0; k--)   
            out<<oth.s[k];
        return out; 
    }
};

int main()   
{
    Integer c;
    char s1[MAXLEN],s2[MAXLEN];   

    cout<<"Input the first number: "<<endl;
    cin>>s1;   

    cout<<"Input the second number: "<<endl;
    cin>>s2;

    Integer a(s1);
    Integer b(s2);

    c = a+b;
    cout<<"The plus result is : "<<c<<endl;

    c = a-b;
    cout<<"The minus result is : "<<c<<endl;


    c = a*b;
    cout<<"The multipy result is : "<<c<<endl;

    c = a/b;
    cout<<"The divide result is : "<<c<<endl;

    return 0;   
}   


这里有另一个实现:http://blog.csdn.net/hackbuteer1/article/details/6595881

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值