Cod sursa(job #3194846)

Utilizator aeandreescuAndreescu Ana-Eliza aeandreescu Data 19 ianuarie 2024 16:22:00
Problema A+B Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <string>
#include <vector>

using namespace std;

ifstream fin("adunare.in");
ofstream fout("adunare.out");

const int base= 10;

class Big {
private:
    vector <int> digits;
public:
    Big( string s );

    Big &operator+(const Big &add);

    void print() const;
};

Big::Big ( string s ) {
    for ( int i= (int)s.size()-1; i>=0; --i ) {
        digits.push_back((int)s[i]-'0');
    }
}

Big &Big::operator+(const Big &add) {
    for ( int i= 0; i<(int)(this->digits).size() || i<(int)(add.digits).size(); ++i ) {
        if ( i>=(int)(this->digits).size() ) {
            (this->digits).push_back(0);
        }

        if ( i<(int)(add.digits).size() ) {
            (this->digits)[i]+= (add.digits)[i];

            if ( (this->digits)[i]>=base ) {
                (this->digits)[i]%= base;
                if ( i==(int)(this->digits).size()-1 ) {
                    (this->digits).push_back(0);
                }
                ++(this->digits)[i+1];
            }
        }
    }

    return *this;
}

void Big::print () const {
    for ( int i= (int)digits.size()-1; i>=0; --i ) {
        fout<<digits[i];
    }
    fout<<"\n";
}

int main() {
    string a, b;
    fin>>a>>b;

    Big x(a), y(b);

    x= x+y;
    x.print();

    return 0;
}