1. 程式人生 > >大數加減

大數加減

class BigData
{
    public:
        BigData(int data)
        {
            int tmp = 0;
            while(data)
            {
                tmp = data % 10;
                _data.push_back(tmp);
                data/=10;
            }
        }

        vector<int> operator+(BigData &src)
        {
            vector<int>::iterator it1 = _data.begin();
            vector<int>::iterator it2 = scr.begin();
            vector<int> tmp;
            int carry = 0;
            for(; it1!= _data.end() && it2 != src.end(); it1++,it2++)
            {
                tmp.push_back((*it1+*it2+carry)%10);
                carry = (*it1+*it2+carry)/10;
            }
            while(it1 != _data.end())
            {
                tmp.push_back(*it1++ +carry);
                carry = 0;
            }
            while(it2 != src.end())
            {
                tmp.push_back(*it2++ +carry);
                carry = 0;
            }
            return tmp;
        }
    private:
        vector<int> _data;
}