-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInt.h
75 lines (60 loc) · 2.57 KB
/
BigInt.h
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
#ifndef BIGINT_H
#define BIGINT_H
// A class to represent arbitrary-precision integers.
// by Andrew Kerr <[email protected]>, January 2022
#include <iostream>
#include <vector>
#include <string>
class BigInt {
public:
static const int BASE = 10;
BigInt(); // default ctor
BigInt(const std::string& val); // ctor
BigInt(const char* val); // ctor from c-style string
BigInt(const int val); // ctor from int (is this a good idea?)
BigInt& operator=(const std::string& val); // assignment from string
BigInt& operator=(const char* val); // assignment from c-style string
bool is_negative() const;
int length() const;
// arithmetic-assignment operators
BigInt& operator+=(const BigInt& rhs);
BigInt& operator-=(const BigInt& rhs);
BigInt& operator*=(const BigInt& rhs);
BigInt& operator/=(const BigInt& rhs);
// arithmetic operators
BigInt operator+(const BigInt& rhs) const;
BigInt operator-(const BigInt& rhs) const;
BigInt operator*(const BigInt& rhs) const;
BigInt operator/(const BigInt& rhs) const;
// unary operators
BigInt operator+() const;
BigInt operator-() const;
BigInt& operator++();
BigInt& operator--();
BigInt operator++(int);
BigInt operator--(int);
// comparison operators
// https://stackoverflow.com/questions/4421706 for reference
bool operator==(const BigInt& rhs) const;
bool operator!=(const BigInt& rhs) const;
bool operator< (const BigInt& rhs) const;
bool operator> (const BigInt& rhs) const;
bool operator<=(const BigInt& rhs) const;
bool operator>=(const BigInt& rhs) const;
std::string to_string() const;
friend std::ostream& operator<<(std::ostream& os,
const BigInt& val);
private:
// BigInts are stored as an underlying vector of
// integers whose values are between 0 and 9 inclusive.
// The digits are stored in least-significant digit order,
// i.e. the first element in the digits vector represents
// the ones place, in hopes that this will provide good
// performance when adding more digits with calls to
// push_back(). I'm not sure if there is a better way to
// do this, but have considered storing digits in a deque.
std::vector<int> digits;
BigInt(const std::vector<int>& digits_in, const bool negative_in);
bool negative;
};
#endif // BIGINT_H