﻿// longnum.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
#include "complex.h"
using namespace std;
const int N = 1024;
class longint {
private:
    char *m;//m[N] масив символів цифр в оберн. порядку, що закінчується символом '\0'
    int sign;
public:
    longint() { m = new char[N]; };
    longint(const longint& l);
    longint(const char* s);
    longint& operator=(const longint& l);
    friend longint operator+(longint& l1, longint& l2);
    friend longint operator-(longint& l1, longint& l2);
    //longint& operator+=(longint& l2);
    friend ostream& operator<<(ostream& out, const longint& l);
    friend istream& operator>>(istream& in, longint& l);
    ~longint() { delete[] m; m = nullptr; }
};

int main()
{
    /*
    complex c1,c2(1.5,7.1),c3;
    cin >> c1;
    c3 = c1 + c2;
    cout << "c1+c2=" << c3 << endl;
    c3 = c1 - c2;
    cout << "c1-c2=" << c3 << endl;
    c3 = c1 * c2;
    cout << "c1*c2=" << c3 << endl;
    c3 = c1 / c2;
    cout << "c1/c2=" << c3 << endl;
    */

    longint a("1000000"), b,c;
    cout << ">";
    cin >> b;
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    c = a - b;
    cout << "c=a-b=" << c << endl;
    system("pause");
    return 0;
}

longint::longint(const longint& l)
{
    m = new char[N];
    strcpy_s(m,N,l.m);
}

longint::longint(const char* s)
{
    int l = strlen(s);
    m = new char[N];
    for (int i = l - 1; i >= 0; i--) {
        m[l - i - 1] = s[i];
    }
    m[l] = '\0';
}

longint& longint::operator=(const longint& l)
{
    strcpy_s(m, N, l.m);
    return *this;
}

longint operator+(longint& l1, longint& l2)
{
    longint t;
    int len1 = strlen(l1.m);
    int len2 = strlen(l2.m);
    if (len1 > len2) {
        for (int i = 0; i < len1 - len2; i++)
            l2.m[len2 + i] = '0';
        len2 = len1;
        l2.m[len2] = '\0';
    }else
        if (len2 > len1) {
            for (int i = 0; i < len2 - len1; i++)
                l1.m[len1 + i] = '0';
            len1 = len2;
            l1.m[len1] = '\0';
        }

    int r = 0,i;
    for (int i = 0; i < len1; i++) {
        t.m[i] = (l1.m[i] - '0') + (l2.m[i] - '0') + r;
        r = t.m[i] / 10;
        t.m[i] = t.m[i] % 10;
        t.m[i] += '0';
    }
    if (r > 0) {
        t.m[len1] = r + '0';
        t.m[len1 + 1] = '\0';
    }
    else
        t.m[len1] = '\0';
    return t;
}

longint operator-(longint& l1, longint& l2)
{
    longint t;
    if (l1.sign >= 0 && l2.sign < 0) {
        l2.sign = 1;
        t = l1 + l2;
        l2.sign = -1;
        return t;
    }
    if (l1.sign < 0 && l2.sign >= 0) {
        l1.sign = 1;
        t = l1 + l2;
        l1.sign = -1;
        t.sign = -1;
        return t;
    }
    //Порівняти модулі чисел
    //Якщо |l2|>|l1|, то 1. від l2 віднімати l1; 2. t.sign=-1
    //TODO

    //Нехай l1, l2 >0, l1>l2.
    int len1 = strlen(l1.m);
    int len2 = strlen(l2.m);
    if (len1 > len2) {
        for (int i = 0; i < len1 - len2; i++)
            l2.m[len2 + i] = '0';
        len2 = len1;
        l2.m[len2] = '\0';
    }
    else
        if (len2 > len1) {
            for (int i = 0; i < len2 - len1; i++)
                l1.m[len1 + i] = '0';
            len1 = len2;
            l1.m[len1] = '\0';
        }

    int r = 0, i;
    for (int i = 0; i < len1; i++) {
        t.m[i] = (l1.m[i] - '0') - (l2.m[i] - '0') + r;
        if (t.m[i] < 0) {
            r = -1;
            t.m[i] += 10;
        }
        t.m[i] += '0';
    }
    //if (r<0) {}
    t.m[len1] = '\0';
    return t;

}

ostream& operator<<(ostream& out, const longint& l)
{
    int len = strlen(l.m);
    if (l.sign < 0)
        out << "-";
    for (int i = 0; i < len; i++)
        out << l.m[len - i - 1];
    return out;
}

istream& operator>>(istream& in, longint& l)
{
    char buf[2048];
    in >> buf;
    int len = strlen(buf);
    strcpy_s(l.m, N, buf);
    for (int i = 0; i < len; i++)
        l.m[i] = buf[len - i - 1];
    return in;
}
