Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- const int n = 100;
- char a[n + 1], v1[n + 1], v2[n + 1];
- char dest[n + 1];
- const int p = 10;
- int cnt, sgn;
- bool rev = false;
- void copyValueFromAtoV(char a[],char v[])
- {
- for(int i = 0;i < n;i++)
- v[i] = a[i];
- }
- void minus1(char a[], bool rvrs){// перевод в доп код отрицательных чисел
- int test = 0;
- for (int i = 0; i < cnt; ++i)
- {
- test = a[i] - '0';
- test = p - 1 - test;
- a[i] = test + '0';
- }
- if(!rvrs) for (int i = cnt; i < n; ++i){ if (a[i] == '0') a[i] = '9';}
- if(rvrs) for (int i = cnt; i < n; ++i){ if (a[i] == '9') a[i] = '0';}
- int j = 0;
- bool cor = false;
- while(char(int(a[j]) + 1) < '0' || char(int(a[j]) + 1) > '9')
- {
- cor = true;
- a[j] = char((int(a[j] + 1) % 56) + 46);
- a[j + 1] = char(int(a[j + 1] + 1));
- }
- if(j == 0 && cor == false) a[0] = char(int(a[0]) + 1);
- rev = false;
- }
- void input(char a[]){
- char str[n + 2];
- cin >> str;
- cnt = strlen(str);
- cnt--;
- int start = 0;
- if(str[0] == '-')
- {
- // int start = 1;
- sgn = -1;
- for(;start < cnt;)
- {
- a[start] = str[cnt - start];
- // cout << a[start] - '0' << endl;
- start++;
- }
- }
- else
- {
- sgn = 1;
- for(;start <= cnt;)
- {
- a[start] = str[cnt - start];
- // cout << a[start] - '0' << endl;
- start++;
- }
- }
- for (; start < n; ++start) a[start] = '0';
- cnt = strlen(str);
- if (str[0] == '-')
- {
- cnt = strlen(str) - 1;
- //cout << a[4] << endl;
- //for(int i = 0;i < n;i++)
- // cout << a[i] << ' ' << i << '+';
- //cout << endl;
- // rev = true;
- minus1(a, rev);
- }
- }
- void output(char a[]){
- int sign = 1;
- if (a[n - 1] - '0' >= p / 2){
- sign = -1;
- printf("-");
- rev = true;
- minus1(a, rev);
- }
- int i = n - 1;
- while (i > 0 && a[i] == '0')
- i--;
- for (; i >= 0; --i){
- int k = a[i] - '0';
- cout << k;
- }
- }
- void add(char dest[], char a[], char b[]){
- int k[n + 1];
- for (int i = 0; i < n + 1; i++)
- k[i] = a[i] - '0';
- int r[n + 1];
- for (int i = 0; i < n + 1; i++)
- r[i] = b[i] - '0';
- for (int i = 0; i < n + 1; ++i)
- dest[i] = '0';
- int t = 0, x = 0, y = 0;
- for (int i = 0; i < n; ++i){
- t = dest[i] - '0';
- t = k[i] + r[i] + t;
- x = t % p;
- y = t /= p;
- dest[i] = x + '0';
- dest[i + 1] = y + '0';
- //cout << dest[i];
- }
- cout << dest;
- }
- void sub(char a[], char b[]){
- rev = true;
- minus1(b, rev);
- add(dest, a, b);
- rev = true;
- minus1(dest, rev);
- //if(sign == -1) minus1(b);
- }
- void mul(char dest[], char a[], char b[]){// умножение
- for (int i = 0; i < n; ++i)
- dest[i] = 0;
- int t = 0;
- for (int i = 0; i < n; ++i)
- for (int j = 0; i + j < n; j++){
- t = dest[i + j] - '0' + (a[i] - '0') * (b[j] - '0');
- t = t % p;
- dest[i + j] = t;
- t /= p;
- dest[i + j + 1] += t;
- }
- }
- void del(char dest[], char a[], char b[]) {
- int i = 0;
- /*функция делит число a на короткое число b и возвращает остаток от деления*/
- /*r - обозначает текущий остаток, к которому будет приписана очередная цифра*/
- dest[i] = 0;
- //изначально остаток 0
- for (int i = a[0]; i >= 1; --i) {
- /*цикл от старших цифр к младшим*/
- dest[i] = dest[i] * p + a[i];
- //приписывание очередной цифры
- a[i] = dest[i] / b[i];
- //запись частного в результат
- dest[i] %= b[i];
- //пересчет остатка
- }
- /*Частное может содержать меньше цифр,
- поэтому нужно при необходимости уменьшить количество цифр*/
- while (a[0] > 1 && a[a[0]] == 0) {
- --a[0];
- }
- }
- int main(){
- char b[n + 1];
- input(a);
- cout << a << endl;
- input(b);
- cout << b << endl;
- copyValueFromAtoV(a, v1);
- copyValueFromAtoV(b, v2);
- /* add(dest, v1, v2);
- cout << "Otvet: " << endl;
- output(dest); */
- cout << endl << endl;
- copyValueFromAtoV(a, v1);
- copyValueFromAtoV(b, v2);
- sub(v1, v2);
- cout << "Otvet: " << endl;
- output(dest);
- cout << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement