Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- const string alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int E = 5;
- string replace(string in)
- {
- int k;
- if (in.length() % 2 == 0)
- k = in.length() / 2;
- else
- k = in.length() / 2 + 1;
- for (int i = 0; i < k; i++)
- {
- swap(in[i], in[in.length() - i - 1]);
- }
- return in;
- }
- char find_symbol(int a)
- {
- return alph[a];
- }
- int find_number(char a)
- {
- int i = 0;
- while (a != alph[i])
- {
- i++;
- //cout << i << " ";
- }
- return i;
- }
- int to_ten_base(string first, int osn)
- {
- int second = 0;
- int k = 1;
- while (k <= first.length())
- {
- second += find_number(first[k - 1]) * pow(osn, (first.length() - k));
- k++;
- }
- return second;
- }
- string to_x_base(int ten, int out)
- {
- int x;
- string res;
- while ((ten / out) > 0)
- {
- x = ten % out;
- res = find_symbol(x) + res;
- ten = ten / out;
- }
- res = find_symbol(ten) + res;
- return res;
- }
- char sravn(string a, string b)
- {
- if (a.length() > b.length())
- return '>';
- else if (a.length() < b.length())
- return '<';
- else
- {
- for (int i = 0; i < a.length(); i++)
- {
- if (a[i] > b[i])
- {
- return '>';
- }
- else if (a[i] < b[i])
- {
- return '<';
- }
- }
- return '=';
- }
- }
- string sum(string a, string b, int osn)
- {
- string summed;
- int buf = 0;
- int x = 0, i;
- a = replace(a);
- b = replace(b);
- if (a.length() > b.length())
- {
- for (i = b.length(); i < a.length(); i++)
- {
- b = b + '0';
- }
- }
- else if (b.length() > a.length())
- {
- for (i = a.length(); i < b.length(); i++)
- {
- a = a + '0';
- }
- }
- a += '0';
- b += '0';
- for (i = 0; i < a.length(); i++)
- {
- x = find_number(a[i]) + find_number(b[i]) + buf;
- if (x >= osn)
- {
- buf = 1;
- x = x - osn;
- }
- else
- {
- buf = 0;
- }
- summed = summed + find_symbol(x);
- }
- i = summed.length() - 1;
- if (to_ten_base(summed, osn) != 0)
- {
- while (summed[i] == '0')
- {
- summed.erase(i, 1);
- i--;
- }
- }
- else
- summed = '0';
- summed = replace(summed);
- return summed;
- }
- string proizv(string a, string b, int n)
- {
- string x;
- int kolvo = to_ten_base(b, n);
- if (to_ten_base(a, n) == 0 || to_ten_base(b, n) == 0)
- {
- x = '0';
- return x;
- }
- for (int i = 0; i < kolvo; i++)
- {
- x = sum(x, a, n);
- }
- return x;
- }
- string difference(string a, string b, int osn)
- {
- string diff;
- bool flag = 0;
- int x = 0, i, buf = 0, k = 0;
- a = replace(a);
- b = replace(b);
- if (a.length() > b.length())
- {
- for (i = b.length(); i < a.length(); i++)
- {
- b = b + '0';
- }
- }
- else if (b.length() > a.length())
- {
- for (i = a.length(); i < b.length(); i++)
- {
- a = a + '0';
- }
- }
- for (i = 0; i < a.length(); i++)
- {
- x = find_number(a[i]) - find_number(b[i]) - buf;
- if (x < 0)
- {
- x = x + osn;
- buf = 1;
- }
- else
- {
- buf = 0;
- }
- diff = diff + find_symbol(x);
- }
- i = diff.length() - 1;
- if (to_ten_base(diff, osn) != 0)
- {
- while (diff[i] == '0')
- {
- diff.erase(i, 1);
- i--;
- }
- }
- else
- diff = '0';
- diff = replace(diff);
- return diff;
- }
- string del(string a, string b, int n)
- {
- string x;
- int answer = 0, k = 0;
- bool flag = 0;
- while (to_ten_base(a, n) >= to_ten_base(b, n))
- {
- a = difference(a, b, n);
- answer++;
- }
- x = to_x_base(answer, n);
- return x;
- }
- string del_s_ost(string a, string b, int n)
- {
- string x, na = to_x_base(n, n), chastn, kolvo;
- int answer = 0, k = 0;
- bool flag = 0;
- while (to_ten_base(a, n) >= to_ten_base(b, n))
- {
- a = difference(a, b, n);
- answer++;
- }
- x = to_x_base(answer, n);
- if (to_ten_base(a, n) != 0)
- {
- if (E > 0)
- x = x + ".";
- while (to_ten_base(a, n) != 0 && k < E)
- {
- a = a + '0';
- chastn = del(a, b, n);
- x = x + chastn;
- if (to_ten_base(a, n) >= to_ten_base(b, n))
- {
- kolvo = del(a, b, n);
- a = difference(a, proizv(kolvo, b, n), n);
- }
- if (to_ten_base(proizv(chastn, b, n), n) == n)
- break;
- k++;
- }
- }
- return x;
- }
- int main()
- {
- string one, two, one10, two10, in1, in2;
- int dex, first, second;
- char otvet, task;
- bool sign_first = 0, sign_second = 0;
- setlocale(LC_ALL, "Russian");
- cout << "Введите число a: ";
- cin >> in1;
- cout << "Введите число b: ";
- cin >> in2;
- cout << "Введите операцию: ";
- cin >> task;
- cout << "Введите основание —— , в которой будет проводитьс¤ операци¤: ";
- cin >> dex;
- if (in1[0] == '-')
- {
- in1.erase(0, 1);
- sign_first = 1;
- }
- first = to_ten_base(in1, 10);
- if (in2[0] == '-')
- {
- in2.erase(0, 1);
- sign_second = 1;
- }
- second = to_ten_base(in2, 10);
- one10 = to_x_base(first, 10);
- two10 = to_x_base(second, 10);
- one = to_x_base(first, dex);
- two = to_x_base(second, dex);
- switch (task)
- {
- case '+':
- {
- if (one10 == two10)
- {
- if (sign_first == 1 && sign_second == 0)
- cout << "-" << one << " + " << two << " = 0 = 0" << endl;
- else if (sign_first == 0 && sign_second == 1)
- cout << one << " + (-" << two << ") = 0 = 0" << endl;
- else if (sign_first == 1 && sign_second == 1)
- cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- }
- else if (one10 > two10)
- {
- if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << "-" << one << " + " << two << " = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " + (-" << two << ") = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
- else
- cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- }
- }
- else
- {
- if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << "-" << one << " + " << two << " = " << difference(two, one, dex) << " = " << to_ten_base(difference(two, one, dex), dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " + (-" << two << ") = -" << difference(two, one, dex) << " = -" << to_ten_base(difference(two, one, dex), dex) << endl;
- else
- cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- }
- }
- break;
- }
- case '-':
- {
- if (one10 == two10)
- {
- if (sign_first == 0 && sign_second == 0)
- cout << one << " - " << two << " = 0 = 0" << endl;
- else if (sign_first == 1 && sign_second == 0)
- cout << "-" << one << " - " << two << " = " << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- else if (sign_first == 0 && sign_second == 1)
- cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << "-" << one << " - (-" << two << ") = 0 = 0" << endl;
- }
- else if (one10 > two10)
- {
- if (sign_first == 1)
- {
- if (sign_second == 1)
- {
- cout << "-" << one << " - (-" << two << ") = " << difference(two, one, dex) << " = " << to_ten_base(difference(two, one, dex), dex) << endl;
- }
- else
- cout << "-" << one << " - " << two << " = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << one << " - " << two << " = " << difference(one, two, dex) << " = " << to_ten_base(difference(one, two, dex), dex) << endl;
- }
- }
- else
- {
- if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " - (-" << two << ") = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
- else
- cout << "-" << one << " - " << two << " = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
- else
- cout << one << " - " << two << " = -" << difference(two, one, dex) << " = -" << to_ten_base(difference(two, one, dex), dex) << endl;
- }
- }
- break;
- }
- case '*':
- {
- if (to_ten_base(one, 10) == 0 || to_ten_base(two, 10) == 0)
- {
- if (sign_first) cout << "-";
- cout << one << " * ";
- if (sign_second) cout << "-";
- cout << two << " = 0 = 0" << endl;
- }
- else if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " * (-" << two << ") = " << proizv(one, two, dex) << " = " << to_ten_base(proizv(one, two, dex), dex) << endl;
- else
- cout << "-" << one << " * " << two << " = -" << proizv(one, two, dex) << " = -" << to_ten_base(proizv(one, two, dex), dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " * (-" << two << ") = -" << proizv(one, two, dex) << " = -" << to_ten_base(proizv(one, two, dex), dex) << endl;
- else
- cout << one << " * " << two << " = " << proizv(one, two, dex) << " = " << to_ten_base(proizv(one, two, dex), dex) << endl;
- }
- break;
- }
- case '/':
- {
- cout << "Введите количество знаков после зап¤той: ";
- cin >> E;
- if (to_ten_base(one, 10) == 0 && sign_second == 1)
- cout << " 0 / (-" << two << ") = 0 = 0" << endl;
- else if (to_ten_base(two, dex) == 0)
- {
- cout << "Деление на ноль, звершение работы программы" << endl;
- break;
- }
- else if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " / (-" << two << ") = " << del_s_ost(one, two, dex) << " = " << del_s_ost(one10, two10, 10) << endl;
- else
- cout << "-" << one << " / " << two << " = -" << del_s_ost(one, two, dex) << " = -" << del_s_ost(one10, two10, 10) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " / (-" << two << ") = -" << del_s_ost(one, two, dex) << " = -" << del_s_ost(one10, two10, 10) << endl;
- else
- cout << one << " / " << two << " = " << del_s_ost(one, two, dex) << " = " << del_s_ost(one10, two10, 10) << endl;
- }
- break;
- }
- case '?':
- {
- if (sign_first == 1)
- {
- if (sign_second == 1)
- cout << "-" << one << " " << sravn(one, two) << " -" << two << " => -" << to_ten_base(one, dex) << " " << sravn(one, two) << " -" << to_ten_base(two, dex) << endl;
- else
- cout << "-" << one << " < " << two << " => -" << to_ten_base(one, dex) << " < " << to_ten_base(two, dex) << endl;
- }
- else
- {
- if (sign_second == 1)
- cout << one << " > -" << two << " => " << to_ten_base(one, dex) << " > -" << to_ten_base(two, dex) << endl;
- else
- cout << one << " " << sravn(one, two) << " " << two << " => " << to_ten_base(one, dex) << " " << sravn(one, two) << " " << to_ten_base(two, dex) << endl;
- }
- break;
- }
- default:
- {
- cout << "¬веденной операции не существует" << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement