From 1573bf6b8c6c53647e06e64d39d4722987838e88 Mon Sep 17 00:00:00 2001 From: Dark Date: Wed, 15 Jul 2020 16:47:52 -0500 Subject: [PATCH 1/3] created BigInt64 class with a string constructor and an print --- include/BigInt64/BigInt64.hpp | 32 +++++++++++++++ .../BigInt64/constructors/constructors.cpp | 41 +++++++++++++++++++ include/BigInt64/operators/io_stream.cpp | 8 ++++ include/BigInt64/test.cpp | 16 ++++++++ 4 files changed, 97 insertions(+) create mode 100644 include/BigInt64/BigInt64.hpp create mode 100644 include/BigInt64/constructors/constructors.cpp create mode 100644 include/BigInt64/operators/io_stream.cpp create mode 100644 include/BigInt64/test.cpp diff --git a/include/BigInt64/BigInt64.hpp b/include/BigInt64/BigInt64.hpp new file mode 100644 index 0000000..eee62d5 --- /dev/null +++ b/include/BigInt64/BigInt64.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +#include "../../release/BigInt.hpp" + +/* + +Some notes about big int 64: +sign cannot be stored conventionally so all intergers +will be stored with their positive values. +sign wil be dictated by using the positive and negative boolean values. +I have used 2 values here for user comfort but these can be easily changed to a +single sign boolean. + +*/ +class BigInt64 { +private: + std::vector* numArr; +public: + bool positive; + bool negative; + //bool zero; + BigInt64(); + BigInt64(const std::string&); + ~BigInt64(); + //ops + std::string toString() const; + friend std::ostream& operator<<(std::ostream&, const BigInt64&); +}; \ No newline at end of file diff --git a/include/BigInt64/constructors/constructors.cpp b/include/BigInt64/constructors/constructors.cpp new file mode 100644 index 0000000..9e8a287 --- /dev/null +++ b/include/BigInt64/constructors/constructors.cpp @@ -0,0 +1,41 @@ +#pragma once + +#include "../BigInt64.hpp" + +BigInt64::BigInt64(){ + this->numArr = new std::vector(); + this->numArr->push_back(0); + this->positive = true; + this->negative = false; //in the 2s complement system zero is repersented as 0x0 with the sign bit being zero +} + +BigInt64::BigInt64(const std::string& s){ + this->numArr = new std::vector(); + std::string newS; + if(s[0] == '-'){ + this->negative = true; + this->positive = false; + newS = s.substr(1,s.length()); + }else if ( s[0] == '+'){ + this->negative = false; + this->positive = true; + newS = s.substr(1,s.length()); + }else{ + this->negative = false; + this->positive = true; + newS = s; + } + + BigInt num = BigInt(newS); + BigInt base = BigInt("18446744073709551616"); // 2**64 + + while(num > 0){ + uint64_t remainder = (uint64_t)(num%base).to_long_long(); + this->numArr->push_back(remainder); + num /= base; + } +} + +BigInt64::~BigInt64(){ + delete numArr; +} \ No newline at end of file diff --git a/include/BigInt64/operators/io_stream.cpp b/include/BigInt64/operators/io_stream.cpp new file mode 100644 index 0000000..cd6bb43 --- /dev/null +++ b/include/BigInt64/operators/io_stream.cpp @@ -0,0 +1,8 @@ +#pragma once + +#include "../BigInt64.hpp" + +std::ostream& operator<<(std::ostream& out, const BigInt64& num) { + out << num.numArr; + return out; +} \ No newline at end of file diff --git a/include/BigInt64/test.cpp b/include/BigInt64/test.cpp new file mode 100644 index 0000000..4cb5d67 --- /dev/null +++ b/include/BigInt64/test.cpp @@ -0,0 +1,16 @@ +#include "BigInt64.hpp" + +#include + +int main(){ + + BigInt64 t = BigInt64("1"); + std::cout<< t << std::endl; + + t = BigInt64("18446744073709551616"); + std::cout<< t << std::endl; + + return 0; + + +} \ No newline at end of file From 7dd1d0608a3f4f592babef80cb407125d497abac Mon Sep 17 00:00:00 2001 From: Dark Date: Wed, 15 Jul 2020 17:01:17 -0500 Subject: [PATCH 2/3] small bug fix --- include/BigInt64/constructors/constructors.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/BigInt64/constructors/constructors.cpp b/include/BigInt64/constructors/constructors.cpp index 9e8a287..009ef60 100644 --- a/include/BigInt64/constructors/constructors.cpp +++ b/include/BigInt64/constructors/constructors.cpp @@ -15,11 +15,11 @@ BigInt64::BigInt64(const std::string& s){ if(s[0] == '-'){ this->negative = true; this->positive = false; - newS = s.substr(1,s.length()); + newS = s.substr(1,s.length()-1); }else if ( s[0] == '+'){ this->negative = false; this->positive = true; - newS = s.substr(1,s.length()); + newS = s.substr(1,s.length()-1); }else{ this->negative = false; this->positive = true; From eb80684bf8a1942cdd04dfcc679cd8fd46f1be9c Mon Sep 17 00:00:00 2001 From: Dark Date: Wed, 15 Jul 2020 17:06:41 -0500 Subject: [PATCH 3/3] formatting --- .../BigInt64/constructors/constructors.cpp | 33 ++++++++++--------- include/BigInt64/operators/io_stream.cpp | 2 +- include/BigInt64/test.cpp | 5 --- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/include/BigInt64/constructors/constructors.cpp b/include/BigInt64/constructors/constructors.cpp index 009ef60..45a9a98 100644 --- a/include/BigInt64/constructors/constructors.cpp +++ b/include/BigInt64/constructors/constructors.cpp @@ -2,40 +2,41 @@ #include "../BigInt64.hpp" -BigInt64::BigInt64(){ +BigInt64::BigInt64() { this->numArr = new std::vector(); - this->numArr->push_back(0); + this->numArr->push_back( 0 ); this->positive = true; - this->negative = false; //in the 2s complement system zero is repersented as 0x0 with the sign bit being zero + this->negative = false; // in the 2s complement system zero is repersented + // as 0x0 with the sign bit being zero } -BigInt64::BigInt64(const std::string& s){ +BigInt64::BigInt64( const std::string &s ) { this->numArr = new std::vector(); std::string newS; - if(s[0] == '-'){ + if ( s[ 0 ] == '-' ) { this->negative = true; this->positive = false; - newS = s.substr(1,s.length()-1); - }else if ( s[0] == '+'){ + newS = s.substr( 1, s.length() - 1 ); + } else if ( s[ 0 ] == '+' ) { this->negative = false; this->positive = true; - newS = s.substr(1,s.length()-1); - }else{ + newS = s.substr( 1, s.length() - 1 ); + } else { this->negative = false; this->positive = true; - newS = s; + newS = s; } - BigInt num = BigInt(newS); - BigInt base = BigInt("18446744073709551616"); // 2**64 + BigInt num = BigInt( newS ); + BigInt base = BigInt( "18446744073709551616" ); // 2**64 - while(num > 0){ - uint64_t remainder = (uint64_t)(num%base).to_long_long(); - this->numArr->push_back(remainder); + while ( num > 0 ) { + uint64_t remainder = ( uint64_t )( num % base ).to_long_long(); + this->numArr->push_back( remainder ); num /= base; } } -BigInt64::~BigInt64(){ +BigInt64::~BigInt64() { delete numArr; } \ No newline at end of file diff --git a/include/BigInt64/operators/io_stream.cpp b/include/BigInt64/operators/io_stream.cpp index cd6bb43..35d0159 100644 --- a/include/BigInt64/operators/io_stream.cpp +++ b/include/BigInt64/operators/io_stream.cpp @@ -2,7 +2,7 @@ #include "../BigInt64.hpp" -std::ostream& operator<<(std::ostream& out, const BigInt64& num) { +std::ostream &operator<<( std::ostream &out, const BigInt64 &num ) { out << num.numArr; return out; } \ No newline at end of file diff --git a/include/BigInt64/test.cpp b/include/BigInt64/test.cpp index 4cb5d67..a88f145 100644 --- a/include/BigInt64/test.cpp +++ b/include/BigInt64/test.cpp @@ -3,14 +3,9 @@ #include int main(){ - BigInt64 t = BigInt64("1"); std::cout<< t << std::endl; - t = BigInt64("18446744073709551616"); std::cout<< t << std::endl; - return 0; - - } \ No newline at end of file