From 07897d244fa6476be3c58fe6ff81ceea65ca822e Mon Sep 17 00:00:00 2001 From: Yash Soni Date: Wed, 7 Oct 2020 18:19:24 +0200 Subject: [PATCH 1/6] Added >, <, <=, >= operators --- big-int/src/bigint.hpp | 8 +-- big-int/src/operators/greater_equal.cpp | 9 ++- big-int/src/operators/greater_than.cpp | 35 ++++++++++ big-int/src/operators/less_equal.cpp | 9 ++- big-int/src/operators/less_than.cpp | 35 ++++++++++ tests/operators/greater_equal_input.txt | 88 +++++++++++++++++++++++++ tests/operators/greater_equal_test.cpp | 22 +++++++ tests/operators/greater_than_input.txt | 88 +++++++++++++++++++++++++ tests/operators/greater_than_test.cpp | 22 +++++++ tests/operators/less_equal_input.txt | 87 ++++++++++++++++++++++++ tests/operators/less_equal_test.cpp | 22 +++++++ tests/operators/less_than_input.txt | 88 +++++++++++++++++++++++++ tests/operators/less_than_test.cpp | 22 +++++++ 13 files changed, 529 insertions(+), 6 deletions(-) create mode 100644 tests/operators/greater_equal_input.txt create mode 100644 tests/operators/greater_equal_test.cpp create mode 100644 tests/operators/greater_than_input.txt create mode 100644 tests/operators/greater_than_test.cpp create mode 100644 tests/operators/less_equal_input.txt create mode 100644 tests/operators/less_equal_test.cpp create mode 100644 tests/operators/less_than_input.txt create mode 100644 tests/operators/less_than_test.cpp diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index a9cb938..33f9562 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -106,19 +106,19 @@ class largeInt bool operator!=(int); bool operator!=(int64_t); - bool operator<(largeInt); + bool operator<(const largeInt &); bool operator<(int); bool operator<(int64_t); - bool operator>(largeInt); + bool operator>(const largeInt &); bool operator>(int); bool operator>(int64_t); - bool operator<=(largeInt); + bool operator<=(const largeInt &); bool operator<=(int); bool operator<=(int64_t); - bool operator>=(largeInt); + bool operator>=(const largeInt &); bool operator>=(int); bool operator>=(int64_t); diff --git a/big-int/src/operators/greater_equal.cpp b/big-int/src/operators/greater_equal.cpp index f5c6018..020c48b 100644 --- a/big-int/src/operators/greater_equal.cpp +++ b/big-int/src/operators/greater_equal.cpp @@ -32,5 +32,12 @@ namespace libbig { - +bool largeInt::operator>=(const largeInt &a) +{ + if(*this>a || *this==a) + { + return 1; + } + return 0; +} } // namespace libbig \ No newline at end of file diff --git a/big-int/src/operators/greater_than.cpp b/big-int/src/operators/greater_than.cpp index f5c6018..98a56b0 100644 --- a/big-int/src/operators/greater_than.cpp +++ b/big-int/src/operators/greater_than.cpp @@ -32,5 +32,40 @@ namespace libbig { +bool largeInt::operator>(const largeInt &a) +{ + if(this->sign == NEGATIVE && a.sign == POSITIVE) + { + return 0; + } + if(a.sign == NEGATIVE && this->sign == POSITIVE) + { + return 1; + } + if(this->number.length() > a.number.length()) + { + return 0^this->sign; + } + if(this->number.length() < a.number.length()) + { + return 1^this->sign; + } + for(int i=0;inumber[i]==a.number[i]) + { + continue; + } + else if(this->number[i]>a.number[i]) + { + return 1; + } + else + { + return 0; + } + } + return 0; +} } // namespace libbig \ No newline at end of file diff --git a/big-int/src/operators/less_equal.cpp b/big-int/src/operators/less_equal.cpp index f5c6018..3a89e9c 100644 --- a/big-int/src/operators/less_equal.cpp +++ b/big-int/src/operators/less_equal.cpp @@ -32,5 +32,12 @@ namespace libbig { - +bool largeInt::operator<=(const largeInt &a) +{ + if(*thissign == NEGATIVE && a.sign == POSITIVE) + { + return 1; + } + if(a.sign == NEGATIVE && this->sign == POSITIVE) + { + return 0; + } + if(this->number.length() > a.number.length()) + { + return 1^this->sign; + } + if(this->number.length() < a.number.length()) + { + return 0^this->sign; + } + for(int i=0;inumber[i]==a.number[i]) + { + continue; + } + else if(this->number[i] +#include +#include +#include + +int main() +{ + std::fstream fin; + fin.open("./greater_equal_input.txt", std::fstream::in); + + libbig::largeInt a,b; + bool chk; + + while(fin>>a) + { + fin>>b; + fin>>chk; + + assert((a>=b) == chk); + } + fin.close(); +} \ No newline at end of file diff --git a/tests/operators/greater_than_input.txt b/tests/operators/greater_than_input.txt new file mode 100644 index 0000000..fdc1e48 --- /dev/null +++ b/tests/operators/greater_than_input.txt @@ -0,0 +1,88 @@ +937864077970184465681 +3381792131859029443889 +0 + +8071817347467627280023 +5276731293166549118617 +1 + +-732196172899489923100 +671461191637821381001 +0 + +8396424147443744035145 +-33234537844131749187 +1 + +9402052509307741740147 +6934277148464033600444 +1 + +9416571267865071113256 +1634892503984664158695 +1 + +8959439666801519218732 +-259428379065791799487 +1 + +9630523981200582930465 +-393363994092533469964 +1 + +5326159542154270202831 +2100344403479688168284 +1 + +-114059027155018715539 +7130556088400012655055 +0 + +7537568365134285951562 +784091538030158075963 +1 + +1698548716417344871097 +2340498473019062693303 +0 + +6818447841672913184398 +-989041599642583902328 +1 + +2892128124480500635013 +9018083887687338463980 +0 + +-813122146720403328416 +4990158085224629715148 +0 + +1000 +1000 +0 + +0 +0 +0 + +-54345 +-54345 +0 + +-2345 +-1 +0 + +-456543 +-3245678976 +1 + +94538745 +-332 +1 + +10000000000 +9999999999 +1 + diff --git a/tests/operators/greater_than_test.cpp b/tests/operators/greater_than_test.cpp new file mode 100644 index 0000000..d289fc8 --- /dev/null +++ b/tests/operators/greater_than_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + std::fstream fin; + fin.open("./greater_than_input.txt", std::fstream::in); + + libbig::largeInt a,b; + bool chk; + + while(fin>>a) + { + fin>>b; + fin>>chk; + + assert((a>b) == chk); + } + fin.close(); +} \ No newline at end of file diff --git a/tests/operators/less_equal_input.txt b/tests/operators/less_equal_input.txt new file mode 100644 index 0000000..85b156d --- /dev/null +++ b/tests/operators/less_equal_input.txt @@ -0,0 +1,87 @@ +5116670116452377351150 +3515114515634820044954 +0 + +2685498127967040710311 +8099979434864989736373 +1 + +9190169370165075794621 +4246028417480670351393 +0 + +6625028558695083192502 +6281552326488791111208 +0 + +2184439939888661046865 +6776806241726010273426 +1 + +4882155297335127340593 +7104724314242213375400 +1 + +9914541764560594695056 +3739645675499112255635 +0 + +7938769821729383590015 +5968305963912112647075 +0 + +317963590907591345848 +4995566673197201111068 +1 + +4693750288902861331917 +5406107993945793952501 +1 + +4507907071655352922573 +-212881566280704837218 +0 + +1687531364819939435550 +-639889082490554430789 +0 + +8926523313228474211881 +5335306439027933966892 +0 + +1387960488807741079098 +7003258902415025764228 +1 + +2067994599107446884874 +2613410403378282128372 +1 + +1000 +1000 +1 + +0 +0 +1 + +-54345 +-54345 +1 + +-2345 +-1 +1 + +-456543 +-3245678976 +0 + +94538745 +-332 +0 + +10000000000 +9999999999 +0 diff --git a/tests/operators/less_equal_test.cpp b/tests/operators/less_equal_test.cpp new file mode 100644 index 0000000..4c5c8b4 --- /dev/null +++ b/tests/operators/less_equal_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + std::fstream fin; + fin.open("./less_equal_input.txt", std::fstream::in); + + libbig::largeInt a,b; + bool chk; + + while(fin>>a) + { + fin>>b; + fin>>chk; + + assert((a<=b) == chk); + } + fin.close(); +} \ No newline at end of file diff --git a/tests/operators/less_than_input.txt b/tests/operators/less_than_input.txt new file mode 100644 index 0000000..cd55558 --- /dev/null +++ b/tests/operators/less_than_input.txt @@ -0,0 +1,88 @@ +7712455858855223093492 +93176207232753996727 +0 + +9662725895665793045000 +7298295721470341822624 +0 + +3766398404572547866168 +6742067873751969152280 +1 + +-778517944521860412702 +4565313151343238246550 +1 + +6097832772697245156374 +3930318101023766633886 +0 + +5592023043999271537108 +-652522135989857501471 +0 + +5984332100992931749278 +-697594591398820484085 +0 + +7105014984844065230176 +5799543243685778387773 +0 + +8015457162671388899825 +4450939043662393079203 +0 + +6554585487132402066273 +4075439520403745665974 +0 + +1575538778831396761438 +877179813002687051732 +0 + +8467096354506196155294 +6165676294605086731595 +0 + +248938298773043158359 +-688903820046814866008 +0 + +-321918931702584808864 +2902015286859164044445 +1 + +-970418834927008528866 +7042168198474811106108 +1 + +1000 +1000 +0 + +0 +0 +0 + +-54345 +-54345 +0 + +-2345 +-1 +1 + +-456543 +-3245678976 +0 + +94538745 +-332 +0 + +10000000000 +9999999999 +0 + diff --git a/tests/operators/less_than_test.cpp b/tests/operators/less_than_test.cpp new file mode 100644 index 0000000..56b0bb3 --- /dev/null +++ b/tests/operators/less_than_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + std::fstream fin; + fin.open("./less_than_input.txt", std::fstream::in); + + libbig::largeInt a,b; + bool chk; + + while(fin>>a) + { + fin>>b; + fin>>chk; + + assert((a Date: Wed, 7 Oct 2020 23:36:16 +0200 Subject: [PATCH 2/6] Added automated tester with slight modification in the code --- big-int/src/operators/greater_than.cpp | 4 +- big-int/src/operators/less_than.cpp | 4 +- tests/operators/greater_equal_input.txt | 88 ------------------------- tests/operators/greater_equal_test.cpp | 24 +++---- tests/operators/greater_than_input.txt | 88 ------------------------- tests/operators/greater_than_test.cpp | 24 +++---- tests/operators/less_equal_input.txt | 87 ------------------------ tests/operators/less_equal_test.cpp | 24 +++---- tests/operators/less_than_input.txt | 88 ------------------------- tests/operators/less_than_test.cpp | 24 +++---- 10 files changed, 56 insertions(+), 399 deletions(-) delete mode 100644 tests/operators/greater_equal_input.txt delete mode 100644 tests/operators/greater_than_input.txt delete mode 100644 tests/operators/less_equal_input.txt delete mode 100644 tests/operators/less_than_input.txt diff --git a/big-int/src/operators/greater_than.cpp b/big-int/src/operators/greater_than.cpp index 98a56b0..df19ed6 100644 --- a/big-int/src/operators/greater_than.cpp +++ b/big-int/src/operators/greater_than.cpp @@ -59,11 +59,11 @@ bool largeInt::operator>(const largeInt &a) } else if(this->number[i]>a.number[i]) { - return 1; + return !(1^this->sign); } else { - return 0; + return !(0^this->sign); } } return 0; diff --git a/big-int/src/operators/less_than.cpp b/big-int/src/operators/less_than.cpp index 49e78b7..6ec4bd3 100644 --- a/big-int/src/operators/less_than.cpp +++ b/big-int/src/operators/less_than.cpp @@ -59,11 +59,11 @@ bool largeInt::operator<(const largeInt &a) } else if(this->number[i]sign); } else { - return 0; + return !(0^this->sign); } } return 0; diff --git a/tests/operators/greater_equal_input.txt b/tests/operators/greater_equal_input.txt deleted file mode 100644 index ec1232c..0000000 --- a/tests/operators/greater_equal_input.txt +++ /dev/null @@ -1,88 +0,0 @@ -6156014625931118376552 --552434957822643406179 -1 - -6059877517968410982025 -8108838432690647595961 -0 - --666561177384430055566 -9987992394646247947935 -0 - -6378181689988931566094 -4821519687004366290127 -1 - -2650854528147120574758 -2476692132729281663023 -1 - -6350553499231035819769 -3304594744938625830424 -1 - -5263118277382338902226 -2917303770357477590355 -1 - --761672436025853579326 -9773736479645918637498 -0 - -8793647235465969994876 -5760754033722912178316 -1 - -9108493856206169723265 -3845437802939735584395 -1 - -1684915109230952556352 -8575333323812432206420 -0 - -9602539553280201145678 -2028777896603415437861 -1 - -3116081557653529262750 --172451814506290008143 -1 - -897503069736410932870 -2655230010303684220158 -0 - -9517796208047309236548 -351929505192131001807 -1 - -1000 -1000 -1 - -0 -0 -1 - --54345 --54345 -1 - --2345 --1 -0 - --456543 --3245678976 -1 - -94538745 --332 -1 - -10000000000 -9999999999 -1 - diff --git a/tests/operators/greater_equal_test.cpp b/tests/operators/greater_equal_test.cpp index 4a42361..ad46d1e 100644 --- a/tests/operators/greater_equal_test.cpp +++ b/tests/operators/greater_equal_test.cpp @@ -1,22 +1,24 @@ #include #include -#include +#include #include int main() { - std::fstream fin; - fin.open("./greater_equal_input.txt", std::fstream::in); - libbig::largeInt a,b; - bool chk; - - while(fin>>a) + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + + for(int i=0;i<100;i++) { - fin>>b; - fin>>chk; + long long i1 = distr(gen); + long long i2 = distr(gen); - assert((a>=b) == chk); + a = i1; + b = i2; + + assert((a>=b) == (i1>=i2)); } - fin.close(); } \ No newline at end of file diff --git a/tests/operators/greater_than_input.txt b/tests/operators/greater_than_input.txt deleted file mode 100644 index fdc1e48..0000000 --- a/tests/operators/greater_than_input.txt +++ /dev/null @@ -1,88 +0,0 @@ -937864077970184465681 -3381792131859029443889 -0 - -8071817347467627280023 -5276731293166549118617 -1 - --732196172899489923100 -671461191637821381001 -0 - -8396424147443744035145 --33234537844131749187 -1 - -9402052509307741740147 -6934277148464033600444 -1 - -9416571267865071113256 -1634892503984664158695 -1 - -8959439666801519218732 --259428379065791799487 -1 - -9630523981200582930465 --393363994092533469964 -1 - -5326159542154270202831 -2100344403479688168284 -1 - --114059027155018715539 -7130556088400012655055 -0 - -7537568365134285951562 -784091538030158075963 -1 - -1698548716417344871097 -2340498473019062693303 -0 - -6818447841672913184398 --989041599642583902328 -1 - -2892128124480500635013 -9018083887687338463980 -0 - --813122146720403328416 -4990158085224629715148 -0 - -1000 -1000 -0 - -0 -0 -0 - --54345 --54345 -0 - --2345 --1 -0 - --456543 --3245678976 -1 - -94538745 --332 -1 - -10000000000 -9999999999 -1 - diff --git a/tests/operators/greater_than_test.cpp b/tests/operators/greater_than_test.cpp index d289fc8..ada1135 100644 --- a/tests/operators/greater_than_test.cpp +++ b/tests/operators/greater_than_test.cpp @@ -1,22 +1,24 @@ #include #include -#include +#include #include int main() { - std::fstream fin; - fin.open("./greater_than_input.txt", std::fstream::in); - libbig::largeInt a,b; - bool chk; - - while(fin>>a) + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + + for(int i=0;i<100;i++) { - fin>>b; - fin>>chk; + long long i1 = distr(gen); + long long i2 = distr(gen); - assert((a>b) == chk); + a = i1; + b = i2; + + assert((a>b) == (i1>i2)); } - fin.close(); } \ No newline at end of file diff --git a/tests/operators/less_equal_input.txt b/tests/operators/less_equal_input.txt deleted file mode 100644 index 85b156d..0000000 --- a/tests/operators/less_equal_input.txt +++ /dev/null @@ -1,87 +0,0 @@ -5116670116452377351150 -3515114515634820044954 -0 - -2685498127967040710311 -8099979434864989736373 -1 - -9190169370165075794621 -4246028417480670351393 -0 - -6625028558695083192502 -6281552326488791111208 -0 - -2184439939888661046865 -6776806241726010273426 -1 - -4882155297335127340593 -7104724314242213375400 -1 - -9914541764560594695056 -3739645675499112255635 -0 - -7938769821729383590015 -5968305963912112647075 -0 - -317963590907591345848 -4995566673197201111068 -1 - -4693750288902861331917 -5406107993945793952501 -1 - -4507907071655352922573 --212881566280704837218 -0 - -1687531364819939435550 --639889082490554430789 -0 - -8926523313228474211881 -5335306439027933966892 -0 - -1387960488807741079098 -7003258902415025764228 -1 - -2067994599107446884874 -2613410403378282128372 -1 - -1000 -1000 -1 - -0 -0 -1 - --54345 --54345 -1 - --2345 --1 -1 - --456543 --3245678976 -0 - -94538745 --332 -0 - -10000000000 -9999999999 -0 diff --git a/tests/operators/less_equal_test.cpp b/tests/operators/less_equal_test.cpp index 4c5c8b4..1c9c72c 100644 --- a/tests/operators/less_equal_test.cpp +++ b/tests/operators/less_equal_test.cpp @@ -1,22 +1,24 @@ #include #include -#include +#include #include int main() { - std::fstream fin; - fin.open("./less_equal_input.txt", std::fstream::in); - libbig::largeInt a,b; - bool chk; - - while(fin>>a) + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + + for(int i=0;i<100;i++) { - fin>>b; - fin>>chk; + long long i1 = distr(gen); + long long i2 = distr(gen); - assert((a<=b) == chk); + a = i1; + b = i2; + + assert((a<=b) == (i1<=i2)); } - fin.close(); } \ No newline at end of file diff --git a/tests/operators/less_than_input.txt b/tests/operators/less_than_input.txt deleted file mode 100644 index cd55558..0000000 --- a/tests/operators/less_than_input.txt +++ /dev/null @@ -1,88 +0,0 @@ -7712455858855223093492 -93176207232753996727 -0 - -9662725895665793045000 -7298295721470341822624 -0 - -3766398404572547866168 -6742067873751969152280 -1 - --778517944521860412702 -4565313151343238246550 -1 - -6097832772697245156374 -3930318101023766633886 -0 - -5592023043999271537108 --652522135989857501471 -0 - -5984332100992931749278 --697594591398820484085 -0 - -7105014984844065230176 -5799543243685778387773 -0 - -8015457162671388899825 -4450939043662393079203 -0 - -6554585487132402066273 -4075439520403745665974 -0 - -1575538778831396761438 -877179813002687051732 -0 - -8467096354506196155294 -6165676294605086731595 -0 - -248938298773043158359 --688903820046814866008 -0 - --321918931702584808864 -2902015286859164044445 -1 - --970418834927008528866 -7042168198474811106108 -1 - -1000 -1000 -0 - -0 -0 -0 - --54345 --54345 -0 - --2345 --1 -1 - --456543 --3245678976 -0 - -94538745 --332 -0 - -10000000000 -9999999999 -0 - diff --git a/tests/operators/less_than_test.cpp b/tests/operators/less_than_test.cpp index 56b0bb3..5418243 100644 --- a/tests/operators/less_than_test.cpp +++ b/tests/operators/less_than_test.cpp @@ -1,22 +1,24 @@ #include #include -#include +#include #include int main() { - std::fstream fin; - fin.open("./less_than_input.txt", std::fstream::in); - libbig::largeInt a,b; - bool chk; - - while(fin>>a) + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + + for(int i=0;i<100;i++) { - fin>>b; - fin>>chk; + long long i1 = distr(gen); + long long i2 = distr(gen); - assert((a Date: Wed, 7 Oct 2020 23:41:51 +0200 Subject: [PATCH 3/6] Corrected typos in tester --- tests/operators/greater_equal_test.cpp | 8 +++----- tests/operators/greater_than_test.cpp | 8 +++----- tests/operators/less_equal_test.cpp | 8 +++----- tests/operators/less_than_test.cpp | 8 +++----- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/tests/operators/greater_equal_test.cpp b/tests/operators/greater_equal_test.cpp index ad46d1e..2a18cfd 100644 --- a/tests/operators/greater_equal_test.cpp +++ b/tests/operators/greater_equal_test.cpp @@ -9,16 +9,14 @@ int main() std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator - std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range for(int i=0;i<100;i++) { - long long i1 = distr(gen); - long long i2 = distr(gen); - + int i1 = distr(gen); + int i2 = distr(gen); a = i1; b = i2; - assert((a>=b) == (i1>=i2)); } } \ No newline at end of file diff --git a/tests/operators/greater_than_test.cpp b/tests/operators/greater_than_test.cpp index ada1135..4aead69 100644 --- a/tests/operators/greater_than_test.cpp +++ b/tests/operators/greater_than_test.cpp @@ -9,16 +9,14 @@ int main() std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator - std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range for(int i=0;i<100;i++) { - long long i1 = distr(gen); - long long i2 = distr(gen); - + int i1 = distr(gen); + int i2 = distr(gen); a = i1; b = i2; - assert((a>b) == (i1>i2)); } } \ No newline at end of file diff --git a/tests/operators/less_equal_test.cpp b/tests/operators/less_equal_test.cpp index 1c9c72c..89cff4e 100644 --- a/tests/operators/less_equal_test.cpp +++ b/tests/operators/less_equal_test.cpp @@ -9,16 +9,14 @@ int main() std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator - std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range for(int i=0;i<100;i++) { - long long i1 = distr(gen); - long long i2 = distr(gen); - + int i1 = distr(gen); + int i2 = distr(gen); a = i1; b = i2; - assert((a<=b) == (i1<=i2)); } } \ No newline at end of file diff --git a/tests/operators/less_than_test.cpp b/tests/operators/less_than_test.cpp index 5418243..41b2e97 100644 --- a/tests/operators/less_than_test.cpp +++ b/tests/operators/less_than_test.cpp @@ -9,16 +9,14 @@ int main() std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator - std::uniform_int_distribution<> distr(-1e18, 1e18); // define the range + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range for(int i=0;i<100;i++) { - long long i1 = distr(gen); - long long i2 = distr(gen); - + int i1 = distr(gen); + int i2 = distr(gen); a = i1; b = i2; - assert((a Date: Wed, 7 Oct 2020 23:45:24 +0200 Subject: [PATCH 4/6] Removed an unnecessary cout statement --- big-int/src/operators/equal_to.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/big-int/src/operators/equal_to.cpp b/big-int/src/operators/equal_to.cpp index 3f7f493..24e2200 100644 --- a/big-int/src/operators/equal_to.cpp +++ b/big-int/src/operators/equal_to.cpp @@ -71,7 +71,7 @@ bool largeInt::operator==(int z) { return false; } - std:: cout << z << std::endl; + z /= 10; n--; } From 42b8366c1621b8a8fce61cae91654780e158a40d Mon Sep 17 00:00:00 2001 From: Yash Soni Date: Thu, 8 Oct 2020 09:33:34 +0200 Subject: [PATCH 5/6] One condition removed and comments added for <,>,<=,>= operators --- big-int/src/operators/greater_equal.cpp | 8 +++++- big-int/src/operators/greater_than.cpp | 35 ++++++++++++++++--------- big-int/src/operators/less_equal.cpp | 8 +++++- big-int/src/operators/less_than.cpp | 33 +++++++++++++++-------- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/big-int/src/operators/greater_equal.cpp b/big-int/src/operators/greater_equal.cpp index 020c48b..1df0c16 100644 --- a/big-int/src/operators/greater_equal.cpp +++ b/big-int/src/operators/greater_equal.cpp @@ -32,9 +32,15 @@ namespace libbig { +//! Operator overload for >= for largeInt class +/*! + This operator returns a bool value which is + 1 when the >= condition is satisfied between the two largeInt objects + 0 otherwise +*/ bool largeInt::operator>=(const largeInt &a) { - if(*this>a || *this==a) + if(*this>a || *this==a) //! if both > and == conditions are satisfied then >= condition is satified too { return 1; } diff --git a/big-int/src/operators/greater_than.cpp b/big-int/src/operators/greater_than.cpp index df19ed6..3e8fc58 100644 --- a/big-int/src/operators/greater_than.cpp +++ b/big-int/src/operators/greater_than.cpp @@ -32,40 +32,51 @@ namespace libbig { +//! Operator overload for > for largeInt class +/*! + This operator returns a bool value which is + 1 when the > condition is satisfied between the two largeInt objects + 0 otherwise +*/ bool largeInt::operator>(const largeInt &a) { + /** If first number is negative and second number is positive + * then > condition is not satisfied. + */ if(this->sign == NEGATIVE && a.sign == POSITIVE) { return 0; } + /** If first number is positive and second number is negative + * then > condition is satisfied. + */ if(a.sign == NEGATIVE && this->sign == POSITIVE) { return 1; } + /** If both the numbers have same sign + * then comparing their lengths + */ if(this->number.length() > a.number.length()) { - return 0^this->sign; - } + return 0^this->sign; //! if first number is longer and both are negative then the condition is satisfied. + } //! but if first number is longer and both are positive then the condition is not satisfied. if(this->number.length() < a.number.length()) { - return 1^this->sign; - } + return 1^this->sign; //! if first number is shorter and both are negative then the condition is not satisfied. + } //! but if first number is shorter and both are positive then the condition is satisfied. - for(int i=0;inumber[i]==a.number[i]) - { - continue; - } - else if(this->number[i]>a.number[i]) + if(this->number[i]>a.number[i]) { return !(1^this->sign); } - else + if(this->number[i]sign); } } return 0; } -} // namespace libbig \ No newline at end of file +} // namespace libbig diff --git a/big-int/src/operators/less_equal.cpp b/big-int/src/operators/less_equal.cpp index 3a89e9c..3c5ed86 100644 --- a/big-int/src/operators/less_equal.cpp +++ b/big-int/src/operators/less_equal.cpp @@ -32,9 +32,15 @@ namespace libbig { +//! Operator overload for <= for largeInt class +/*! + This operator returns a bool value which is + 1 when the <= condition is satisfied between the two largeInt objects + 0 otherwise +*/ bool largeInt::operator<=(const largeInt &a) { - if(*thissign == NEGATIVE && a.sign == POSITIVE) { return 1; } + /** If first number is positive and second number is negative + * then < condition is not satisfied. + */ if(a.sign == NEGATIVE && this->sign == POSITIVE) { return 0; } + /** If both the numbers have same sign + * then comparing their lengths + */ if(this->number.length() > a.number.length()) { - return 1^this->sign; - } + return 1^this->sign; //! if first number is longer and both are negative then the condition is not satisfied. + } //! but if first number is longer and both are positive then the condition is satisfied. if(this->number.length() < a.number.length()) { - return 0^this->sign; - } + return 0^this->sign; //! if first number is shorter and both are negative then the condition is satisfied. + } //! but if first number is shorter and both are positive then the condition is not satisfied. - for(int i=0;inumber[i]==a.number[i]) - { - continue; - } - else if(this->number[i]number[i]sign); } - else + if(this->number[i]>a.number[i]) { return !(0^this->sign); } From f25e4d9a084d6d777bb03f54b02bc59a7ee148aa Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Fri, 9 Oct 2020 23:04:42 +0530 Subject: [PATCH 6/6] minor fixes --- big-int/src/operators/greater_equal.cpp | 33 ++++++---- big-int/src/operators/greater_than.cpp | 88 ++++++++++++++++--------- big-int/src/operators/less_equal.cpp | 31 +++++---- big-int/src/operators/less_than.cpp | 88 ++++++++++++++++--------- 4 files changed, 152 insertions(+), 88 deletions(-) diff --git a/big-int/src/operators/greater_equal.cpp b/big-int/src/operators/greater_equal.cpp index 1df0c16..3aa3111 100644 --- a/big-int/src/operators/greater_equal.cpp +++ b/big-int/src/operators/greater_equal.cpp @@ -32,18 +32,25 @@ namespace libbig { -//! Operator overload for >= for largeInt class -/*! - This operator returns a bool value which is - 1 when the >= condition is satisfied between the two largeInt objects - 0 otherwise -*/ -bool largeInt::operator>=(const largeInt &a) -{ - if(*this>a || *this==a) //! if both > and == conditions are satisfied then >= condition is satified too + /** + * @brief greater equal operator overload for largeInt class. + * + * @param right hand parameter of >= operator + * @return true if *this (object on which this function is + * called upon) is greater or equal else + * false + * + */ + bool largeInt::operator>=(const largeInt &a) + { + /** + * if both > and == conditions are satisfied then >= condition + * is satified too + */ + if(*this>a || *this==a) { - return 1; + return true; } - return 0; -} -} // namespace libbig \ No newline at end of file + return false; + } +} // namespace libbig diff --git a/big-int/src/operators/greater_than.cpp b/big-int/src/operators/greater_than.cpp index 3e8fc58..b1f6ea3 100644 --- a/big-int/src/operators/greater_than.cpp +++ b/big-int/src/operators/greater_than.cpp @@ -32,51 +32,77 @@ namespace libbig { -//! Operator overload for > for largeInt class -/*! - This operator returns a bool value which is - 1 when the > condition is satisfied between the two largeInt objects - 0 otherwise -*/ -bool largeInt::operator>(const largeInt &a) -{ - /** If first number is negative and second number is positive - * then > condition is not satisfied. + /** + * This operator returns a bool value which is + * 1 when the > condition is satisfied between the two largeInt object + * 0 otherwise + */ + bool largeInt::operator>(const largeInt &a) + { + /** + * If first number is negative and second number is positive + * then > condition is not satisfied. + * */ if(this->sign == NEGATIVE && a.sign == POSITIVE) { - return 0; + return false; } - /** If first number is positive and second number is negative - * then > condition is satisfied. + + /** + * If first number is positive and second number is negative + * then > condition is satisfied. + * */ if(a.sign == NEGATIVE && this->sign == POSITIVE) { - return 1; + return true; } - /** If both the numbers have same sign - * then comparing their lengths + + /** + * If both the numbers have same sign + * then comparing their lengths. + * */ if(this->number.length() > a.number.length()) { - return 0^this->sign; //! if first number is longer and both are negative then the condition is satisfied. - } //! but if first number is longer and both are positive then the condition is not satisfied. + /** + * if first number is longer and both are negative then + * the condition is satisfied, but if first number is + * longer and both are positive then the condition is not + * satisfied. + */ + return 0^this->sign; + } if(this->number.length() < a.number.length()) { - return 1^this->sign; //! if first number is shorter and both are negative then the condition is not satisfied. - } //! but if first number is shorter and both are positive then the condition is satisfied. + /** + * if first number is shorter and both are negative then + * the condition is not satisfied, but if first number is + * shorter and both are positive then the condition is + * satisfied. + * + */ + return 1^this->sign; + } - for(int i=0;inumber[i]>a.number[i]) - { - return !(1^this->sign); - } - if(this->number[i]sign); - } + if(this->number[i]>a.number[i]) + { + return !(1^this->sign); + } + if(this->number[i]sign); + } } - return 0; -} + return false; + } } // namespace libbig diff --git a/big-int/src/operators/less_equal.cpp b/big-int/src/operators/less_equal.cpp index 3c5ed86..9099b52 100644 --- a/big-int/src/operators/less_equal.cpp +++ b/big-int/src/operators/less_equal.cpp @@ -32,18 +32,23 @@ namespace libbig { -//! Operator overload for <= for largeInt class -/*! - This operator returns a bool value which is - 1 when the <= condition is satisfied between the two largeInt objects - 0 otherwise -*/ -bool largeInt::operator<=(const largeInt &a) -{ - if(*thissign == NEGATIVE && a.sign == POSITIVE) { - return 1; + return true; } - /** If first number is positive and second number is negative - * then < condition is not satisfied. + + /** + * If first number is positive and second number is negative + * then < condition is not satisfied. + * */ if(a.sign == NEGATIVE && this->sign == POSITIVE) { - return 0; + return false; } - /** If both the numbers have same sign - * then comparing their lengths + + /** + * If both the numbers have same sign + * then comparing their lengths + * */ if(this->number.length() > a.number.length()) { - return 1^this->sign; //! if first number is longer and both are negative then the condition is not satisfied. - } //! but if first number is longer and both are positive then the condition is satisfied. + /** + * if first number is longer and both are negative then the + * condition is not satisfied, but if first number is longer + * and both are positive then the condition is satisfied. + * + */ + return 1^this->sign; + } if(this->number.length() < a.number.length()) { - return 0^this->sign; //! if first number is shorter and both are negative then the condition is satisfied. - } //! but if first number is shorter and both are positive then the condition is not satisfied. + /** + * if first number is shorter and both are negative then the + * condition is satisfied, but if first number is shorter and + * both are positive then the condition is not satisfied. + * + */ + return 0^this->sign; + } - for(int i=0;inumber[i]sign); - } - if(this->number[i]>a.number[i]) - { - return !(0^this->sign); - } + if(this->number[i]sign); + } + if(this->number[i]>a.number[i]) + { + return !(0^this->sign); + } } - return 0; -} + return false; + } } // namespace libbig