Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- class BigInt
- {
- private:
- int *digits = new int[10];
- int amount;
- string zn;
- public:
- void input(){
- string num, zn;
- cout << "Введите знак числа" << endl;
- cin >> zn;
- cout << "Введите число" << endl;
- cin >> num;
- input(num, zn);;
- }
- string getZn(){
- return zn;
- }
- void input(string s, string zn1)
- {
- string str = s;
- zn = zn1;
- int pos = 0;
- for (int i = str.size() - 1; i >= 0; i--)
- digits[pos++] = str[i] - '0';
- amount = str.size();
- }
- BigInt(int num1) {
- string num = "";
- if (num1 < 0) zn = "-";
- else zn = "+";
- num = num1;
- input(num, zn);
- }
- BigInt() {
- string num = "57";
- input(num, "+");
- }
- BigInt(string s, string zn1) {
- input(s, zn1);
- }
- void output()
- {
- cout << zn;
- for (int i = amount - 1; i >= 0; i--)
- cout << digits[i];
- }
- friend bool operator > (BigInt &a, BigInt &b) {
- if(a.zn=="-" && b.zn=="-"){
- if (a.amount != b.amount)
- return b.amount>b.amount;
- for (int i = a.amount - 1; i >= 0; i--)
- {
- if (a.digits[i] != b.digits[i])
- return b.digits[i]>a.digits[i];
- }
- return false;
- }
- else{
- if (a.amount != b.amount)
- return a.amount>b.amount;
- for (int i = a.amount - 1; i >= 0; i--)
- {
- if (a.digits[i] != b.digits[i])
- return a.digits[i]>b.digits[i];
- }
- return false;
- }
- }
- friend bool operator < (BigInt &a, BigInt &b) {
- if(a.zn=="-" && b.zn=="-"){
- if (a.amount != b.amount) return b.amount<a.amount;
- for (int i = a.amount - 1; i >= 0; i--)
- {
- if (a.digits[i] != b.digits[i])
- return b.digits[i]<a.digits[i];
- }
- return false;
- }
- else{
- if (a.amount != b.amount) return a.amount<b.amount;
- for (int i = a.amount - 1; i >= 0; i--)
- {
- if (a.digits[i] != b.digits[i])
- return a.digits[i]<b.digits[i];
- }
- return false;
- }
- }
- BigInt plus(BigInt &a, BigInt &b)
- {
- BigInt res;
- res.amount = max(a.amount, b.amount);
- int r = 0;
- for (int i = 0; i < res.amount; i++)
- {
- if (i >= min(a.amount, b.amount)) {
- if (res.amount == a.amount) b.digits[i] = 0;
- else a.digits[i] = 0;
- }
- res.digits[i] = a.digits[i] + b.digits[i] + r;
- if (res.digits[i] >= 10)
- {
- res.digits[i] -= 10;
- r = 1;
- }
- else
- r = 0;
- if (i == res.amount - 1)
- {
- i++;
- res.digits[i] = r;
- }
- }
- if (res.digits[res.amount])
- res.amount++;
- if (a.zn == b.zn && (a.zn == "+")) res.zn = "+";
- else if (a.zn == b.zn && (a.zn == "-")) res.zn = "-";
- else res.zn = "+";
- return res;
- }
- BigInt minus(BigInt &a, BigInt &b)
- {
- BigInt res;
- bool isMinus = false;
- if (a < b) {
- isMinus = true;
- }
- if (max(a.amount, b.amount) == a.amount && b < a) {
- res = a;
- }
- else {
- res = b;
- swap(a, b);
- }
- if (a.zn == "-" && b.zn == "-" && b < a) isMinus = false;
- if (isMinus) res.zn = "-";
- else res.zn = "+";
- int r = 0;
- for (int i = 0; i < res.amount; i++)
- {
- if (i >= min(a.amount, b.amount)) {
- if (res.amount == a.amount) b.digits[i] = 0;
- else a.digits[i] = 0;
- }
- res.digits[i] -= b.digits[i] + r;
- if (res.digits[i] < 0)
- {
- res.digits[i] += 10;
- res.digits[i + 1]--;
- }
- }
- int pos = res.amount;
- while (pos && !res.digits[pos])
- pos--;
- res.amount = pos + 1;
- res.amount--;
- return res;
- }
- friend void operator++(BigInt &a){
- if (a.zn == "+") a.zn = "-";
- else a.zn = "+";
- }
- friend BigInt operator+(BigInt &a, BigInt &b){
- if (a.zn == b.zn && (a.zn == "+" || a.zn == "-")) {
- return a.plus(a, b);
- }
- else if (a.zn == "+" && b.zn == "-") {
- b.zn = "+";
- return a.minus(a, b);
- }
- else if (a.zn == "-" && b.zn == "+") {
- a.zn="+";
- return a.minus(b, a);
- }
- }
- friend BigInt operator-(BigInt &a, BigInt &b)
- {
- if (a.zn == b.zn && a.zn == "+") {
- return a.minus(a, b);
- }
- else if (a.zn == "+" && b.zn == "-") {
- return a.plus(a, b);
- }
- else if (a.zn == "-" && b.zn == "+") {
- return a.minus(a, b);
- }
- else if (a.zn == "-" && b.zn == "-") {
- b.zn = "+";
- return a.minus(b, a);
- }
- }
- friend BigInt operator*(BigInt &a, BigInt &b)
- {
- BigInt res;
- int temp1 = 10;
- if (a < b) swap(a, b);
- BigInt temp = a;
- for (int j = 0; j < b.amount; j++){
- for (int i = 0; i < b.digits[j] - 1; i++){
- res = a.plus(a, temp);
- temp = res;
- }
- b.digits[j + 1] *= temp1;
- temp1 *= 10;
- }
- if (a.zn == b.zn) res.zn = "+";
- else res.zn = "-";
- return res;
- }
- friend BigInt operator/(BigInt &a, BigInt &b)
- {
- string zn1 = "+";
- if (a.zn != b.zn) zn1 = "-";
- BigInt res("25", "+"), res1("1", "+");
- int temp1 = 0;
- int k = 0;
- if (a < b) swap(a, b);
- BigInt temp = a;
- while (res > res1){
- int f = 0;
- res = a.minus(temp, b);
- temp = res;
- temp1++;
- for (int i = 0; i < res.amount; i++){
- if (res.digits[i] == 0) f++;
- }
- if (f == res.amount) break;
- }
- if (a.zn == b.zn) cout << zn1 << temp1 << endl;
- else cout << zn1 << temp1 << endl;
- return res;
- }
- };
- int main()
- {
- setlocale(LC_ALL, "Russian");
- BigInt bg("50", "+"), bg1("5", "+"), bg2;
- cout << "Введите 1 для суммы, 2-разности, 3-умножения, 4-деления, 5-сравнения, 6-смены знака, 0-выход" << endl;
- int i;
- cin >> i;
- while (i != 0){
- if (i == 1){
- BigInt bg;
- BigInt bg1;
- BigInt bg2;
- bg.input();
- bg1.input();
- cout << "Сумма: ";
- bg2 = bg + bg1;
- bg2.output();
- cout << endl;
- }
- if (i == 2){
- BigInt bg;
- BigInt bg1;
- BigInt bg2;
- bg.input();
- bg1.input();
- cout << "Разность: ";
- bg2 = bg - bg1;
- bg2.output();
- cout << endl;
- }
- if (i == 3){
- BigInt bg;
- BigInt bg1;
- BigInt bg2;
- bg.input();
- bg1.input();
- cout << "Умножение: ";
- bg2 = bg*bg1;
- bg2.output();
- cout << endl;
- }
- if (i == 4){
- BigInt bg;
- BigInt bg1;
- BigInt bg2;
- bg.input();
- bg1.input();
- cout << "Деление: ";
- bg2 = bg / bg1;
- }
- if (i == 5){
- BigInt bg;
- BigInt bg1;
- bg.input();
- bg1.input();
- cout << "Сравнение: ";
- ++bg;
- if ((bg < bg1) == true) cout << "Первое число больше";
- else cout << "Второе число больше";
- }
- if (i == 6){
- BigInt bg;
- bg.input();
- cout << "Смена знака: ";
- ++bg;
- bg.output();
- }
- cin >> i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement