Cod sursa(job #2421931)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 16 mai 2019 18:41:12
Problema A+B Scor 100
Compilator cpp-64 Status done
Runda Lista lui wefgef Marime 1.16 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int DIGITMAX = 1000;

class Huge
{
public:
    int lg;
    int v[DIGITMAX];

    Huge()
    {
        lg = 0;
        memset(v, 0, sizeof(v));
    }

    void Read()
    {
        char s[DIGITMAX];
        fin >> s;

        int x = strlen(s) - 1;

        while(x >= 0)
        {
            v[++lg] =  s[x] - '0';
            x--;
        }
    }

    void Write()
    {
        if(lg == 0)
        {
            fout << 0 << '\n';
            return;
        }

        for(int i = lg; i >= 1; i--)
            fout << v[i];
        fout << '\n';
    }

    Huge operator + (const Huge B) const
    {
        Huge ans;
        int t = 0;

        for(int i = 1; i <= max(lg, B.lg) || t != 0; i++)
        {
            ans.lg++;
            ans.v[i] = v[i] + B.v[i] + t;
            t = ans.v[i] / 10;
            ans.v[i] %= 10;
        }

        return ans;
    }
};

Huge A, B;
Huge S;

int main()
{
    A.Read();
    B.Read();

    S = A + B;
    S.Write();

    return 0;
}