Длинная арифметика в С++
Вот, написал!!! Длинная арифметика в С++.
Прикольно получилось:
Прикольно получилось:
| #include <iostream> #include <string.h> using namespace std; const int NumDigits = 4; const int MaxLen = 100; char buf[NumDigits*MaxLen + 1]; class BigInt { int len; short a[MaxLen]; short znak; public: BigInt(long t = 0); void read(); void readln(); void write() const; void writeln() const; BigInt operator+(BigInt r); BigInt operator-(BigInt r); BigInt operator*(BigInt r); BigInt operator/(BigInt r); BigInt operator+(long r); BigInt operator-(long r); BigInt operator*(long r); BigInt operator/(long r); BigInt operator++(); BigInt operator--(); BigInt operator+=(BigInt r); BigInt operator-=(BigInt r); BigInt operator*=(BigInt r); BigInt operator/=(BigInt r); BigInt operator+=(long r); BigInt operator-=(long r); BigInt operator*=(long r); BigInt operator/=(long r); bool operator==(BigInt r); bool operator<(long r); bool operator>(long r); bool operator==(long r); bool operator<=(BigInt r); bool operator>=(BigInt r); bool operator<=(long r); bool operator>=(long r); bool operator<(BigInt r); bool operator>(BigInt r); bool operator!=(BigInt r); bool operator!=(long r); private: void Clear(); }; //Ввод/Вывод: void BigInt::write() const { if (znak<0) printf("-"); if (len == 0) printf(0); for (int i = 0; i < len; i++) { printf("%hd",a[len-i-1]); } } void BigInt::writeln() const { if (znak<0) printf("-"); if (len == 0) printf(0); for (int i = 0; i < len; i++) { printf("%hd",a[len-i-1]); } cout<<endl; } void BigInt::readln() { scanf("%s", buf); len = strlen(buf); int i; znak = 1; for (i = 0; i < len; i++) { if (buf[i]==' ') break; if (*(buf+len-i-1) == '-') znak = -1; else a[i] = (*(buf+len-i-1)-'0'); } if (znak == -1) len--; for (;i < MaxLen; i++) { a[i] = 0; } } void BigInt::read() { if (p >= strlen(buf)) { scanf("%s", buf); p = 0; } len = strlen(buf); int i; znak = 1; for (i = p; i < len; i++) { if (buf[i]==' ') break; if (*(buf+len-i-1) == '-') znak = -1; else a[i] = (*(buf+len-i-1)-'0'); } p = i + 1; if (znak == -1) len--; for (;i < MaxLen; i++) { a[i] = 0; } } BigInt::BigInt(long t) { if (t < 0) { znak = -1; t = - t; } else znak = 1; len = 0; for (int i = 0; i < MaxLen; i++) { a[i] = 0; } while(t > 0) { a[len++]=t%10; t/=10; } } //************************************** //*********** Логические операции************** bool BigInt::operator<(long r) { BigInt a(r); if (*this < a) return 1; else return 0; } bool BigInt::operator>(long r) { BigInt a(r); if (*this > a) return 1; else return 0; } bool BigInt::operator==(long r) { BigInt a(r); if (*this == a) return 1; else return 0; } bool BigInt::operator==(BigInt r) { bool ret = 1; if (len == r.len) { for (int i = 0; i < len; i++) { if (a[i] != r.a[i]) ret = 0; } } else ret = 0; return ret; } bool BigInt::operator!=(BigInt r) { if (*this == r) return 0; else return 1; } bool BigInt::operator!=(long r) { BigInt a(r); return a != *this; } bool BigInt::operator<(BigInt r) { bool ret = 1; if (len > r.len) ret = 0; else if (len == r.len) for (int i=len-1; i>=0; i--) if (a[i]!=r.a[i]) {if (a[i]<r.a[i]) ret = 1; else ret = 0; break;} return ret; } bool BigInt::operator>(BigInt r) { if ((*this != r) && (*this < r == false)) return 1; else return 0; } bool BigInt::operator>=(BigInt r) { if ((*this > r) || (*this == r)) return 1; else return 0; } bool BigInt::operator<=(BigInt r) { if ((*this < r) || (*this == r)) return 1; else return 0; } bool BigInt::operator>=(long r) { BigInt a(r); return(*this >= a); } bool BigInt::operator<=(long r) { BigInt a(r); return(*this <= a); } //************************************** //******************* Арифметические операции *********************** BigInt BigInt::operator-(BigInt r) { BigInt ret,vs; if ((znak < 0) && (r.znak < 0)) { BigInt va = ret - *this,vb = ret - r; ret = ret - va + vb; } else { if ((znak < 0) && (r.znak > 0)) ret = r + ret - *this; else { if (r.znak < 0) { BigInt s = r; s.znak = 1; ret = *this + s; } else { if (*this < r) { ret.znak = -1; BigInt a = *this; *this = r; r = a; } int i; short perenos = 0; for (i = 0; i < max(len, r.len); i++) { ret.a[i] = a[i] - r.a[i] - perenos; if (ret.a[i] < 0) { ret.a[i] += 10; perenos = 1; } else perenos = 0; } ret.len=max(len,r.len); for (int z=0;z<9;) if (ret.a[ret.len] == 0) ret.len--; else z=10; ret.len++; if (ret.len <= 0) ret.len=1; if (ret.a[ret.len-1]==0) ret.znak = 1; if (perenos == 1) { ret.a[ret.len] = 1; ret.len++; } if (ret.znak == -1) { BigInt a; a = r; r = *this; *this = a; } } } } if (znak == -1) ret.znak = -ret.znak; return ret; } BigInt BigInt::operator*(BigInt r) { BigInt ret; int i,j; short perenos = 0, perenos2 = 0; for (i = 1; i <= len+1; i++) { for (j = 1; j <= r.len+1; j++) { perenos2 = perenos; perenos = (ret.a[i+j-2] + a[i-1]*r.a[j-1]+perenos2) / 10; ret.a[i+j-2] = (ret.a[i+j-2] + a[i-1]*r.a[j-1]+perenos2) % 10; } } ret.len = i+j-3; int k; for (i=0; i<ret.len;i++) { if (ret.a[ret.len-i] == 0) k=i; else i=ret.len; } ret.len-=k; ret.znak = znak*r.znak; ret.znak = znak * r.znak; return ret; } BigInt BigInt::operator/(BigInt r) { BigInt ret, b; int zn2 = r.znak, zn1 = znak; r.znak = 1; znak = 1; if (a[len-1]!=0) { int i,j; for (j = len-1; j >= 0; j--) { b = b * 10 + a[j]; for (i = 0; i <= 9; i++) { if (((r * i < b) && (r * (i + 1) > b)) || (i == 9)) { if ((ret.len > 0) || ((ret == 0) && (i != 0))) ret =ret * 10 + i; b = b - (r * i); break; } } if (b == 0) break; } } else ret.len=1; r.znak = zn2; znak = zn1; ret.znak = znak * r.znak; return ret; } BigInt BigInt::operator+(BigInt r) { BigInt ret; if ((r.znak < 0) && (znak < 0)) { BigInt va = *this, vb = r; va.znak = 1; vb.znak = 1; ret = ret - (va + vb); } else { if ((r.znak < 0) && (znak > 0)) { BigInt s = r; s.znak = 1; ret = *this - s; } else { if ((r.znak > 0) && (znak < 0)) { BigInt s = *this; s.znak = 1; ret = r - s; } else { short perenos = 0; int i; for (i = 0; i < max(len, r.len); i++) { ret.a[i] = (a[i] + r.a[i] + perenos) % 10; perenos = (a[i] + r.a[i]) / 10; } ret.len = i; if (perenos == 1) { ret.a[i]=perenos; ret.len++; } } } } return ret; } BigInt BigInt::operator+(long r) { BigInt a(r); a=*this+a; return a; } BigInt BigInt::operator-(long r) { BigInt a(r); a=*this-a; return a; } BigInt BigInt::operator*(long r) { BigInt a(r); a=*this*a; return a; } BigInt BigInt::operator/(long r) { BigInt a(r); return *this/a; } //************************************** BigInt BigInt::operator++() { *this = *this + 1; } BigInt BigInt::operator--() { *this = *this - 1; } //************************************** BigInt BigInt::operator+=(BigInt r) { return *this+r; } BigInt BigInt::operator-=(BigInt r) { return *this-r; } BigInt BigInt::operator*=(BigInt r) { return *this*r; } BigInt BigInt::operator/=(BigInt r) { return *this/r; } BigInt BigInt::operator+=(long r) { return *this+r; } BigInt BigInt::operator-=(long r) { return *this-r; } BigInt BigInt::operator*=(long r) { return *this*r; } BigInt BigInt::operator/=(long r) { return *this/r; } //********************************** int main() { BigInt a,b; BigInt c(32); cout<<"Реализация длинной арифметики:"<<endl; cout<<"c = "; c.writeln(); cout<<"Введите какие-нибудь страшные и длинные числа:"<<endl; cout<<"a = "; a.read(); cout<<"b = "; b.read(); c = a; cout<<"c = a => c = "; c.writeln(); c = a + b; cout<<"c = a + b => c = "; c.writeln(); c = a - b; cout<<"c = a - b => c = "; c.writeln(); c = a * b; cout<<"c = a * b => c = "; c.writeln(); c = a / b; cout<<"c = a / b => c = "; c.writeln(); c += a; cout<<"c += a => c = "; c.writeln(); c -= a; cout<<"c -= a => c = "; c.writeln(); c *= a; cout<<"c *= a => c = "; c.writeln(); c /= a; cout<<"c /= a => c = "; c.writeln(); c = ((a + b) / 2) * (a + b - c / a * b - 9)+ 15; cout<<"c = ((a + b) / 2) * (a + b - c / a * b - 9)+ 15 => c = "; c.writeln(); ++c; --a; cout<<"++c => c = "; c.writeln(); cout<<"--a => a = "; a.writeln(); if (a >= b) cout<<"a >= b"<<endl; if (a <= b) cout<<"a <= b"<<endl; if (a > b) cout<<"a > b"<<endl; if (a < b) cout<<"a < b"<<endl; if (a == b) cout<<"a == b"<<endl; system("pause >nul"); } |
happy




working
