Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions src/bigint_library.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Description: BigInt (Big Integer library)
* Usage: See constructors, operators like +, -, *, /, >, >=, <, <=, ==, toString
* Note: Remove references '&' in overloaded operators for chaining operations.
Expand Down Expand Up @@ -49,7 +49,7 @@ class Bigint {

//Put everything back to original state
for (int i = 0; i < maxLen; i++) Y[i]=('9'-Y[i])+'0';
X[lenX]='\0'; Y[lenY]='\0';
X[lenX]='\0'; Y[lenY]='\0';

return len;
}
Expand Down Expand Up @@ -83,22 +83,22 @@ class Bigint {
}

template<class T>
int divideNmodulo(char *X, int lenX, T divisor, char *Z, T &modulo) {
int remainder = 0;
int size = 0;
for(int i = lenX-1; i >= 0; i--){
remainder *= 10;
remainder += X[i]- '0';
Z[size++] = remainder/divisor + '0';
remainder %= divisor;
}
Z[size]='\0';
reverse(Z, Z+size);
modulo = remainder;
return size;
}

//Logical Operations
int divideNmodulo(char *X, int lenX, T divisor, char *Z, T &modulo) {
int remainder = 0;
int size = 0;
for(int i = lenX-1; i >= 0; i--){
remainder *= 10;
remainder += X[i]- '0';
Z[size++] = remainder/divisor + '0';
remainder %= divisor;
}
Z[size]='\0';
reverse(Z, Z+size);
modulo = remainder;
return size;
}

// Relational Operations
bool equals(char *X, int lenX, char *Y, int lenY) {
int maxLen = max(lenX, lenY);
align(X, maxLen); align(Y, maxLen);
Expand All @@ -115,8 +115,9 @@ class Bigint {
bool greater(char *X, int lenX, char *Y, int lenY) {
int maxLen = max(lenX, lenY);
align(X, maxLen); align(Y, maxLen);
for (int i = maxLen; i >= 0; i--) {
for (int i = maxLen - 1; i >= 0; i--) {
if (X[i] > Y[i]) return true;
if (X[i] < Y[i]) return false;
}

//Put everything back to original state
Expand Down Expand Up @@ -181,13 +182,13 @@ class Bigint {
return modulo;
}

//Logical Operators
// Relational Operators
bool operator ==(Bigint &b) {
return equals(this->x, this->length, b.x, b.length);
}

bool operator >(Bigint &b) {
return !greater(b.x, b.length, this->x, this->length);
return greater(this->x, this->length, b.x, b.length);
}

bool operator >=(Bigint &b) {
Expand All @@ -212,7 +213,10 @@ class Bigint {
string toString() {
string s(x, x+length);
reverse(s.begin(), s.end());
return trimZeros(s);
s = trimZeros(s);
if(s.length() == 0)
return "0";
return s;
}

friend std::ostream& operator<<(ostream &o, Bigint v) {
Expand All @@ -225,10 +229,13 @@ int main() {
Bigint A("123456789"); // Construct Bigint using string representation
Bigint B(987654321); // Construct Bigint using integer representation
Bigint C("456789");
cout << A * B << endl; // Overridden ostream
cout << A * B + C << endl; // Chaining operations
cout << (A > B) << endl;
Bigint D("0");
cout << "A * B: " << A * B << endl; // Overridden ostream
cout << "A * B + C: " << A * B + C << endl; // Chaining operations
cout << "(A > B): " << (A > B) << endl;
cout << "D: " << D << endl;
// logical operations
if (A > B) cout << "A is greater than B" << endl;
else cout << "B is greater than A" << endl;
if (B > A) cout << "B is greater than A" << endl;
if (A == B) cout << "A is equal to B" << endl;
}