-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathconstructors.hpp
More file actions
75 lines (60 loc) · 1.61 KB
/
constructors.hpp
File metadata and controls
75 lines (60 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
===========================================================================
Constructors
===========================================================================
*/
#ifndef BIG_INT_CONSTRUCTORS_HPP
#define BIG_INT_CONSTRUCTORS_HPP
#include "BigInt.hpp"
#include "functions/utility.hpp"
/*
Default constructor
-------------------
*/
BigInt::BigInt() {
magnitude = { 0 };
is_negative = false;
}
/*
Copy constructor
----------------
*/
BigInt::BigInt(const BigInt& num) {
magnitude = num.magnitude;
is_negative = num.is_negative;
}
/*
Integer to BigInt
-----------------
*/
BigInt::BigInt(const long long& num) {
magnitude = { (unsigned long long) llabs(num) };
is_negative = num < 0;
}
/*
String to BigInt
----------------
*/
BigInt::BigInt(const std::string& num) {
if (num[0] == '+' or num[0] == '-') { // check for sign
std::string num_magnitude = num.substr(1);
if (is_valid_number(num_magnitude)) {
magnitude = convert_to_base_64(num_magnitude);
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)) {
std::string num_magnitude(num);
magnitude = convert_to_base_64(num_magnitude);
is_negative = false; // positive by default
}
else {
throw std::invalid_argument("Expected an integer, got \'" + num + "\'");
}
}
}
#endif // BIG_INT_CONSTRUCTORS_HPP