-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInteger.h
157 lines (113 loc) · 4.82 KB
/
BigInteger.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
==========================================================
Name: Anurag Prashant Umale =
Student Id: 1887255 =
E-mail: aumale@ucsc.edu =
File: BigInteger.h (Header file for of BigInteger ADT) =
Class: CSE 101 Spring 2023 =
==========================================================
*/
#include<iostream>
#include<string>
#include"List.h"
#ifndef BIG_INTEGER_H_INCLUDE_
#define BIG_INTEGER_H_INCLUDE_
// Exported type -------------------------------------------------------------
class BigInteger{
private:
// BigInteger Fields
int signum; // +1 (positive), -1 (negative), 0 (zero)
List digits; // List of digits in this BigInteger
public:
// Class Constructors & Destructors ----------------------------------------
// BigInteger()
// Constructor that creates a new BigInteger in the zero state:
// signum=0, digits=().
BigInteger();
// BigInteger()
// Constructor that creates a new BigInteger from the long value x.
BigInteger(long x);
// BigInteger()
// Constructor that creates a new BigInteger from the string s.
// Pre: s is a non-empty string consisting of (at least one) base 10 digit
// {0,1,2,3,4,5,6,7,8,9}, and an optional sign {+,-} prefix.
BigInteger(std::string s);
// BigInteger()
// Constructor that creates a copy of N.
BigInteger(const BigInteger& N);
// Optional Destuctor
// ~BigInteger()
// ~BigInteger();
// Access functions --------------------------------------------------------
// sign()
// Returns -1, 1 or 0 according to whether this BigInteger is positive,
// negative or 0, respectively.
int sign() const;
// compare()
// Returns -1, 1 or 0 according to whether this BigInteger is less than N,
// greater than N or equal to N, respectively.
int compare(const BigInteger& N) const;
// Manipulation procedures -------------------------------------------------
// makeZero()
// Re-sets this BigInteger to the zero state.
void makeZero();
// negate()
// If this BigInteger is zero, does nothing, otherwise reverses the sign of
// this BigInteger positive <--> negative.
void negate();
// BigInteger Arithmetic operations ----------------------------------------
// add()
// Returns a BigInteger representing the sum of this and N.
BigInteger add(const BigInteger& N) const;
// sub()
// Returns a BigInteger representing the difference of this and N.
BigInteger sub(const BigInteger& N) const;
// mult()
// Returns a BigInteger representing the product of this and N.
BigInteger mult(const BigInteger& N) const;
// Other Functions ---------------------------------------------------------
// to_string()
// Returns a string representation of this BigInteger consisting of its
// base 10 digits. If this BigInteger is negative, the returned string
// will begin with a negative sign '-'. If this BigInteger is zero, the
// returned string will consist of the character '0' only.
std::string to_string();
// Overriden Operators -----------------------------------------------------
// operator<<()
// Inserts string representation of N into stream.
friend std::ostream& operator<<( std::ostream& stream, BigInteger N );
// operator==()
// Returns true if and only if A equals B.
friend bool operator==( const BigInteger& A, const BigInteger& B );
// operator<()
// Returns true if and only if A is less than B.
friend bool operator<( const BigInteger& A, const BigInteger& B );
// operator<=()
// Returns true if and only if A is less than or equal to B.
friend bool operator<=( const BigInteger& A, const BigInteger& B );
// operator>()
// Returns true if and only if A is greater than B.
friend bool operator>( const BigInteger& A, const BigInteger& B );
// operator>=()
// Returns true if and only if A is greater than or equal to B.
friend bool operator>=( const BigInteger& A, const BigInteger& B );
// operator+()
// Returns the sum A+B.
friend BigInteger operator+( const BigInteger& A, const BigInteger& B );
// operator+=()
// Overwrites A with the sum A+B.
friend BigInteger operator+=( BigInteger& A, const BigInteger& B );
// operator-()
// Returns the difference A-B.
friend BigInteger operator-( const BigInteger& A, const BigInteger& B );
// operator-=()
// Overwrites A with the difference A-B.
friend BigInteger operator-=( BigInteger& A, const BigInteger& B );
// operator*()
// Returns the product A*B.
friend BigInteger operator*( const BigInteger& A, const BigInteger& B );
// operator*=()
// Overwrites A with the product A*B.
friend BigInteger operator*=( BigInteger& A, const BigInteger& B );
};
#endif