Skip to content

Commit

Permalink
WIP: Change BigInt's data members
Browse files Browse the repository at this point in the history
  • Loading branch information
faheel committed Apr 10, 2018
1 parent 4788596 commit 20c8173
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
5 changes: 3 additions & 2 deletions include/BigInt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#define BIG_INT_HPP

#include <iostream>
#include <vector>

class BigInt {
std::string value;
char sign;
std::vector<unsigned long long> magnitude;
bool is_negative;

public:
// Constructors:
Expand Down
40 changes: 22 additions & 18 deletions include/constructors/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/

BigInt::BigInt() {
value = "0";
sign = '+';
magnitude = { 0 };
is_negative = false;
}


Expand All @@ -28,8 +28,8 @@ BigInt::BigInt() {
*/

BigInt::BigInt(const BigInt& num) {
value = num.value;
sign = num.sign;
magnitude = num.magnitude;
is_negative = num.is_negative;
}


Expand All @@ -39,13 +39,8 @@ BigInt::BigInt(const BigInt& num) {
*/

BigInt::BigInt(const long long& num) {
value = std::to_string(num);
if (num < 0) {
sign = '-';
value = value.substr(1); // remove minus sign from value
}
else
sign = '+';
magnitude = { (unsigned long long) llabs(num) };
is_negative = num < 0;
}


Expand All @@ -56,25 +51,34 @@ BigInt::BigInt(const long long& num) {

BigInt::BigInt(const std::string& num) {
if (num[0] == '+' or num[0] == '-') { // check for sign
std::string magnitude = num.substr(1);
if (is_valid_number(magnitude)) {
value = magnitude;
sign = num[0];
std::string num_magnitude = num.substr(1);
if (is_valid_number(num_magnitude)) {
/*
TODO
----
magnitude = convert_to_base_2_to_the_64(num_magnitude);
*/
magnitude = {0};
is_negative = num[0] == '-';
}
else {
throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
}
}
else { // if no sign is specified
if (is_valid_number(num)) {
value = num;
sign = '+'; // positive by default
/*
TODO
----
magnitude = convert_to_base_2_to_the_64(num_magnitude);
*/
magnitude = {0};
is_negative = false; // positive by default
}
else {
throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
}
}
strip_leading_zeroes(value);
}

#endif // BIG_INT_CONSTRUCTORS_HPP

0 comments on commit 20c8173

Please sign in to comment.