Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- #define MAXDIGITS 1000 /* maximum length bignum */
- #define PLUS 1 /* positive sign bit */
- #define MINUS -1 /* negative sign bit */
- struct BigInt
- {
- char digits[MAXDIGITS]; /* represent the number */
- int signbit; /* 1 if positive, -1 if negative */
- int lastdigit; /* index of high-order digit */
- BigInt operator+(BigInt& b) {
- BigInt c;
- add_bignum(this, &b, &c);
- return c;
- }
- BigInt operator -(BigInt& b) {
- BigInt c;
- subtract_bignum(this, &b, &c);
- return c;
- }
- BigInt operator << (BigInt& b) {}
- BigInt operator >>(BigInt& b) {}
- bool operator < (BigInt& b)
- {
- return compare_bignum(this, &b) > 0;
- }
- bool operator > (BigInt b)
- {
- return compare_bignum(this, &b) < 0;
- }
- bool operator == (BigInt b)
- {
- return compare_bignum(this, &b) == 0;
- }
- bool operator != (BigInt b) {
- return !(*this == b);
- }
- int max(int a, int b)
- {
- if (a > b) return(a);
- else return(b);
- }
- void add_bignum(BigInt *a, BigInt *b, BigInt *c)
- {
- int carry; /* carry digit */
- int i; /* counter */
- initialize_bignum(c);
- if (a->signbit == b->signbit) c->signbit = a->signbit;
- else {
- if (a->signbit == MINUS) {
- a->signbit = PLUS;
- subtract_bignum(b, a, c);
- a->signbit = MINUS;
- }
- else {
- b->signbit = PLUS;
- subtract_bignum(a, b, c);
- b->signbit = MINUS;
- }
- return;
- }
- c->lastdigit = max(a->lastdigit, b->lastdigit) + 1;
- carry = 0;
- for (i = 0; i <= (c->lastdigit); i++) {
- c->digits[i] = (char)(carry + a->digits[i] + b->digits[i]) % 10;
- carry = (carry + a->digits[i] + b->digits[i]) / 10;
- }
- zero_justify(c);
- }
- void int_to_bignum(int s, BigInt *n)
- {
- int i; /* counter */
- int t; /* int to work with */
- if (s >= 0) n->signbit = PLUS;
- else n->signbit = MINUS;
- for (i = 0; i < MAXDIGITS; i++) n->digits[i] = (char)0;
- n->lastdigit = -1;
- t = abs(s);
- while (t > 0) {
- n->lastdigit++;
- n->digits[n->lastdigit] = (t % 10);
- t = t / 10;
- }
- if (s == 0) n->lastdigit = 0;
- }
- void int_to_bignum(int s)
- {
- int_to_bignum(s, this);
- }
- void str_to_bignum(string num)
- {
- for (int i = 0;i < MAXDIGITS; i++) {
- digits[i] = 0;
- }
- if (num[0] != '-') {
- for (auto it = num.crbegin(); it != num.crend(); it++) {
- digits[it - num.crbegin()] = *it - '0';
- }
- lastdigit = num.size() - 1;
- signbit = PLUS;
- }
- else {
- auto end = num.crend();
- end--;
- for (auto it = num.crbegin(); it != end; it++) {
- digits[it - num.crbegin()] = *it - '0';
- }
- lastdigit = num.size() - 2;
- signbit = MINUS;
- }
- }
- void initialize_bignum(BigInt *n)
- {
- int_to_bignum(0, n);
- }
- void subtract_bignum(BigInt *a, BigInt *b, BigInt *c)
- {
- int borrow; /* has anything been borrowed? */
- int v; /* placeholder digit */
- int i; /* counter */
- initialize_bignum(c);
- if ((a->signbit == MINUS) || (b->signbit == MINUS)) {
- b->signbit = -1 * b->signbit;
- add_bignum(a, b, c);
- b->signbit = -1 * b->signbit;
- return;
- }
- if (compare_bignum(a, b) == PLUS) {
- subtract_bignum(b, a, c);
- c->signbit = MINUS;
- return;
- }
- c->lastdigit = max(a->lastdigit, b->lastdigit);
- borrow = 0;
- for (i = 0; i <= (c->lastdigit); i++) {
- v = (a->digits[i] - borrow - b->digits[i]);
- if (a->digits[i] > 0)
- borrow = 0;
- if (v < 0) {
- v = v + 10;
- borrow = 1;
- }
- c->digits[i] = (char)v % 10;
- }
- zero_justify(c);
- }
- int compare_bignum(BigInt *a, BigInt *b)
- {
- int i;
- if ((a->signbit == MINUS) && (b->signbit == PLUS)) return(PLUS);
- if ((a->signbit == PLUS) && (b->signbit == MINUS)) return(MINUS);
- if (b->lastdigit > a->lastdigit) return (PLUS * a->signbit);
- if (a->lastdigit > b->lastdigit) return (MINUS * a->signbit);
- for (i = a->lastdigit; i >= 0; i--) {
- if (a->digits[i] > b->digits[i]) return(MINUS * a->signbit);
- if (b->digits[i] > a->digits[i]) return(PLUS * a->signbit);
- }
- return 0;
- }
- void zero_justify(BigInt *n)
- {
- while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0))
- n->lastdigit--;
- if ((n->lastdigit == 0) && (n->digits[0] == 0))
- n->signbit = PLUS; /* hack to avoid -0 */
- }
- void print_bignum()
- {
- int i;
- if (signbit == MINUS) printf("- ");
- for (i = lastdigit; i >= 0; i--)
- printf("%c", '0' + digits[i]);
- printf("\n");
- }
- };
- int main()
- {
- BigInt test1;
- test1.str_to_bignum("123456789012345678901234567890");
- test1.print_bignum();
- BigInt test2;
- test2.str_to_bignum("-123456789012345678901234567890");
- test2.print_bignum();
- test2 = test2 + test1;
- test2.print_bignum();
- setlocale(LC_ALL, "Russian");
- int a, b;
- cout << "Enter a:"; cin >> a;
- cout << "Enter b:"; cin >> b;
- BigInt n1, n2, n3, zero;
- n1.int_to_bignum(a);
- n2.int_to_bignum(b);
- n3 = n1 + n2;
- printf("Сочетание= ");
- n3.print_bignum();
- n3 = n1 - n2;
- cout << "Вычитание= ";
- n3.print_bignum();
- cout << "б меньше а? Ответ: " << (n2 < n1) << endl;
- cout << "б больше а? Ответ: " << (n2 > n1) << endl;
- cout << "б равно а? Ответ: " << (n2 == n1) << endl;
- cout << "Не равно ли б переменной а? Ответ: " << (n2 != n1) << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement