Загрузка данных


#include <stdio.h>

#define LEN 100

int read_number(int *a, int *n, int *is_eof);
int is_zero(int *a, int n);
void trim_number(int *a, int n, int *b, int *m);
int compare(int *a, int n, int *b, int m);
void output(int *a, int n);
void sum_numbers(int *a, int n, int *b, int m, int *res, int *res_n);
void sub_numbers(int *a, int n, int *b, int m, int *res, int *res_n);

int main(void) {
    int a[LEN];
    int b[LEN];
    int a_clean[LEN];
    int b_clean[LEN];
    int sum[LEN + 1];
    int sub[LEN];
    int n;
    int m;
    int n_clean;
    int m_clean;
    int sum_n;
    int sub_n;
    int eof1;
    int eof2;

    if (read_number(a, &n, &eof1) && !eof1 && read_number(b, &m, &eof2)) {
        trim_number(a, n, a_clean, &n_clean);
        trim_number(b, m, b_clean, &m_clean);

        sum_numbers(a_clean, n_clean, b_clean, m_clean, sum, &sum_n);
        output(sum, sum_n);
        printf("\n");

        if (compare(a_clean, n_clean, b_clean, m_clean) >= 0) {
            sub_numbers(a_clean, n_clean, b_clean, m_clean, sub, &sub_n);
            output(sub, sub_n);
        } else {
            printf("n/a");
        }
    } else {
        printf("n/a");
    }

    return 0;
}

int read_number(int *a, int *n, int *is_eof) {
    int result = 1;
    int ch;
    int previous_was_digit = 0;

    *n = 0;
    *is_eof = 0;

    ch = getchar();

    while (ch != '\n' && ch != EOF) {
        if (ch >= '0' && ch <= '9') {
            if (previous_was_digit || *n >= LEN) {
                result = 0;
            } else {
                *(a + *n) = ch - '0';
                (*n)++;
                previous_was_digit = 1;
            }
        } else if (ch == ' ' || ch == '\t') {
            previous_was_digit = 0;
        } else {
            result = 0;
        }

        ch = getchar();
    }

    if (ch == EOF) {
        *is_eof = 1;
    }

    if (*n == 0 || is_zero(a, *n)) {
        result = 0;
    }

    return result;
}

int is_zero(int *a, int n) {
    int result = 1;
    int i = 0;

    while (result && i < n) {
        if (*(a + i) != 0) {
            result = 0;
        }

        i++;
    }

    return result;
}

void trim_number(int *a, int n, int *b, int *m) {
    int start = 0;
    int i = 0;

    while (start < n - 1 && *(a + start) == 0) {
        start++;
    }

    *m = n - start;

    while (i < *m) {
        *(b + i) = *(a + start + i);
        i++;
    }
}

int compare(int *a, int n, int *b, int m) {
    int result = 0;
    int i = 0;

    if (n > m) {
        result = 1;
    } else if (n < m) {
        result = -1;
    } else {
        while (result == 0 && i < n) {
            if (*(a + i) > *(b + i)) {
                result = 1;
            } else if (*(a + i) < *(b + i)) {
                result = -1;
            }

            i++;
        }
    }

    return result;
}

void output(int *a, int n) {
    int i = 0;

    while (i < n) {
        if (i > 0) {
            printf(" ");
        }

        printf("%d", *(a + i));
        i++;
    }
}

void sum_numbers(int *a, int n, int *b, int m, int *res, int *res_n) {
    int temp[LEN + 1];
    int i = n - 1;
    int j = m - 1;
    int carry = 0;
    int count = 0;
    int digit;
    int k = 0;

    while (i >= 0 || j >= 0 || carry > 0) {
        digit = carry;

        if (i >= 0) {
            digit += *(a + i);
            i--;
        }

        if (j >= 0) {
            digit += *(b + j);
            j--;
        }

        *(temp + count) = digit % 10;
        carry = digit / 10;
        count++;
    }

    *res_n = count;

    while (k < count) {
        *(res + k) = *(temp + count - k - 1);
        k++;
    }
}

void sub_numbers(int *a, int n, int *b, int m, int *res, int *res_n) {
    int temp[LEN];
    int i = n - 1;
    int j = m - 1;
    int borrow = 0;
    int count = 0;
    int digit;
    int k = 0;

    while (i >= 0) {
        digit = *(a + i) - borrow;

        if (j >= 0) {
            digit -= *(b + j);
            j--;
        }

        if (digit < 0) {
            digit += 10;
            borrow = 1;
        } else {
            borrow = 0;
        }

        *(temp + count) = digit;
        count++;
        i--;
    }

    while (count > 1 && *(temp + count - 1) == 0) {
        count--;
    }

    *res_n = count;

    while (k < count) {
        *(res + k) = *(temp + count - k - 1);
        k++;
    }
}