diff --git a/BigInt.hpp b/BigInt.hpp new file mode 100644 index 0000000..3bece2c --- /dev/null +++ b/BigInt.hpp @@ -0,0 +1,141 @@ +//myClasses/BigInt.hpp +//Matthew Ellison +// Created: 11-4-18 +//Modified: 11-4-18 +//This class uses a vector of ints to make a larger int + + +#include +#include +#include +#include + + +class BigInt{ +private: + std::vector num; + void overflow(){ + //This function handles any overflows that occured during the operations + //Limit each number to < 1B + for(int cnt = 0;cnt < num.size();++cnt){ + while(num.at(cnt) >= 1000000000){ + if(num.size() > cnt + 1){ + num.at(cnt + 1) += 1; + } + else{ + num.push_back(1); + } + num.at(cnt) -= 1000000000; + } + } + ///How do you handle negatives? + } +public: + BigInt(){ num.push_back(0); } + BigInt(int64_t n) : num(n) { } + BigInt(BigInt& n){ num = n.num; } + //std::vector getNums(){ return num; } + BigInt operator+(const BigInt& rightSide){ + BigInt temp; + while(rightSide.num.size() > temp.num.size()){ + temp.num.push_back(0); + } + while(num.size() > temp.num.size()){ + temp.num.push_back(0); + } + int largerSize; + if(rightSide.num.size() > num.size()){ + largerSize = rightSide.num.size(); + } + else{ + largerSize = num.size(); + } + for(int cnt = 0;cnt < largerSize;++cnt){ + int64_t ls; + int64_t rs; + if(cnt < num.size()){ + ls = num.at(cnt); + } + else{ + ls = 0; + } + if(cnt < rightSide.num.size()){ + rs = rightSide.num.at(cnt); + } + else{ + rs = 0; + } + temp.num.at(cnt) = ls + rs; + } + temp.overflow(); + return temp; + } + BigInt operator+(const int rightSide){ + BigInt temp; + temp.num.at(0) = num.at(0) + rightSide; + temp.overflow(); + return temp; + } + /* These are not ready. need to account for borrowing + BigInt operator-(const BigInt& rightSide){ + BigInt temp; + while(rightSide.num.size() > temp.num.size()){ + temp.num.push_back(0); + } + while(num.size() > temp.num.size()){ + temp.num.push_back(0); + } + for(int cnt = 0;cnt < rightSide.num.size();++cnt){ + int64_t ls; + int64_t rs; + temp.num.at(cnt) = ls - rs; + } + overflow(); + return tempInt; + } + BigInt operator-(const int rightSide){ + num.at(0) -= rightSide; + overflow(); + return tempInt; + } + */ + BigInt operator*(const BigInt& rightSide){ + + } + BigInt operator*(const int rightSide){ + BigInt tempInt = *this; + for(int cnt = 1;cnt < rightSide;++cnt){ + tempInt += *this; + } + tempInt.overflow(); + return tempInt; + } + BigInt& operator=(const BigInt& rightSide){ + num = rightSide.num; + return *this; + } + BigInt& operator=(const int rightSide){ + num.clear(); + num.push_back(rightSide); + return *this; + } + void operator+=(const BigInt& rightSide){ + *this = *this + rightSide; + } + void operator+=(const int rightSide){ + *this = *this + rightSide; + } + void operator*=(const BigInt& rightSide){ + *this = *this * rightSide; + } + void operator*=(const int rightSide){ + *this = *this * rightSide; + } + std::string printNum() const{ + std::string number; + for(int cnt = num.size() - 1;cnt >= 0;--cnt){ + number += std::to_string(num.at(cnt)); + } + return number; + } +};