Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<iomanip>
- const int N = 100;
- const int p = 10;
- char a[N + 1];
- int sign;
- void minus(char a[]) {
- for (int i = 0; i < N; ++i)
- a[i] = p - 1 - a[i];
- for (int i = 0; i < N; ++i)
- if (a[i] = 0);
- else {
- i++;
- break;
- }
- }
- void input(char a[]) {
- char str[N + 2];
- scanf("%s", str);
- int start = 0;
- if (str[0] == '-' || str[0] == '+') {
- start = 1; //если минус начинается с 1
- sign = (str[0] == '+' ? 1 : -1);
- }
- //цифры засунуть в массив задом на перед
- int ia = 0;
- int istr = strlen(str) - 1;
- for (; istr >= start;)
- a[ia++] = str[istr--] - '0';
- //старшие цифры заполним нулями
- for (; ia < N; ++ia)
- a[ia] = 0;
- if (sign == 1)
- minus(a);
- }
- //
- void output(char a[]) {
- int sign = 1;
- if (a[N - 1] >= p / 2) {
- sign = -1;
- printf("_");
- minus(a);
- }
- //
- int i = N - 1; while (i > 0 && a[i] == 0) i--;
- for (; i >= 0; --i)
- printf("%d", a[i]);
- //
- if (sign == 1)
- minus(a);
- }
- void add(char dest[], char a[], char b[]) {
- //dest=a+b
- for (int i = 0; i < N; ++i)
- dest[i] = 0;
- for (int i = 0; i < N; ++i) {
- int t = a[i] + b[i] + dest[i];
- dest[i] = t%p;
- dest[i + 1] = t / p;
- }
- }
- void sub(char dest[], char a[], char b[]) { //dest=a-b
- minus(b);
- add(dest, a, b);
- minus(b);
- }
- void mul(char dest[], char a[], char b[]) { //dest=a*b
- for (int i = 0; i < N; ++i)
- dest[i] = 0;
- for (int i = 0; i < N; ++i)
- for (int j = 0; i + j < N; j++) {
- int t = dest[i + j] + a[i] + b[j];
- dest[i + j] = t%p;
- dest[i + j + 1] += t / p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement