-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
194 lines (177 loc) · 5.92 KB
/
demo.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "bigint.hpp"
#include <fstream>
using namespace std;
int main()
{
// Default constructor creates a bigint object with value of 0,
bigint a;
cout << "a = " << a << '\n';
// Integer constructor: takes a signed 64-bit integer and converts it to a bigint object.
bigint b(267481);
cout << "b = " << b << '\n';
bigint c(-31642);
cout << "c = " << c << '\n';
// String constructor: takes a string and converts it to a bigint object.
bigint d("295712491461964816498164981");
cout << "d = " << d << '\n';
bigint e("-343284521048104795104781");
cout << "e = " << e << '\n';
bigint f("+572907418046716498164891");
cout << "f = " << f << '\n';
// The string constructor will throw exceptions upon receiving an invalid input:
try
{
bigint g("13816361.3131");
}
catch (const invalid_argument &error)
{
cout << "Error: " << error.what() << '\n';
}
try
{
bigint g("00000313131");
}
catch (const invalid_argument &error)
{
cout << "Error: " << error.what() << '\n';
}
// If any error occurs, no object will be created:
try
{
bigint g("gk%45#^$#!");
cout << "g = " << a << '\n';
}
catch (const invalid_argument &error)
{
cout << "Error: " << error.what() << '\n';
}
// We can change the value of a bigint number with `set` function, whether with a string or int:
a.set(123321);
cout << "a = " << a << '\n';
a.set("-9888898888");
cout << "a = " << a << '\n';
// The setter function also ensures that the class invariant is satisfied:
try
{
a.set("AB131351");
cout << "a = " << a << '\n';
}
catch (const invalid_argument &error)
{
cout << "Error: " << error.what() << '\n';
}
// Writing to a file
try
{
bigint z("173917386716391371739");
ofstream output_file("output.txt");
if (output_file.is_open())
{
output_file << z;
output_file.close();
}
}
catch (const exception &error)
{
cout << "Error: " << error.what() << '\n';
}
// We can negate a bigint number with unary negate operator:
bigint h = -a;
cout << "h = " << -a << '\n'; // 9888898888
a.set(0);
cout << "Comparing two bigint numbers:" << '\n';
// We can check if two bigint numbers are equal or not:
bigint i(267481);
cout << "267481 == 267481? " << (b == i) << '\n'; // 1
cout << "267481 == -31642? " << (b == c) << '\n'; // 0
cout << "267481 != 267481? " << (b != i) << '\n'; // 0
cout << "267481 != -31642? " << (b != c) << '\n'; // 1
// Or whether one is less than the other:
cout << "267481 < 267481? " << (b < i) << '\n'; // 0
cout << "267481 < -31642? " << (b < c) << '\n'; // 0
cout << "-31642 < 267481? " << (c < b) << '\n'; // 1
// Or greater than the other:
cout << "267481 > -31642? " << (b > c) << '\n'; // 1
cout << "-31642 > 267481? " << (c > b) << '\n'; // 0
// Less than or equal:
cout << "267481 <= 267481? " << (b <= i) << '\n'; // 1
cout << "267481 <= -31642? " << (b <= c) << '\n'; // 0
// Greater than or equal:
cout << "267481 >= 267481? " << (b >= i) << '\n'; // 1
cout << "267481 >= -31642? " << (b >= c) << '\n'; // 1
// Assign an existing bigint object to another.
bigint j(123);
bigint k("-456");
cout << "j = " << j << '\n';
cout << "k = " << k << '\n';
j = k;
cout << "After j = k Assignment: j = " << j << '\n'; //-456
cout << "\nAdding two bigint numbers:\n";
b.set("186418");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
a += b;
cout << "a += b : a = " << a << '\n'; // 186418
b.set("-186418");
cout << "b = " << b << '\n';
a += b;
cout << "a += b : a = " << a << '\n'; // 0
a.set("12345678910111213141516");
b.set("-161718192021222324252627");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
a += b;
cout << "a += b : a = " << a << '\n'; // -149372513111111111111111
a.set("9999");
b.set(-9999);
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
cout << "a + b = " << a + b << '\n'; // 0
cout << "\nSubtracting two bigint numbers: \n";
a.set("10000000000000000000000");
b.set(1);
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
a -= b;
cout << "a -= b : a = " << a << '\n'; // 9999999999999999999999
b.set("9999999999999999999999");
cout << "b = " << b << '\n';
a -= b;
cout << "a -= b : a = " << a << '\n'; // 0
a.set("10000000000000000000000");
b.set("-10000000000000000000000");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
cout << "a - b = " << a - b << '\n'; // 20000000000000000000000
a.set("-10000000000000000000000");
b.set("10000000000000000000000");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
cout << "a - b = " << a - b << '\n'; // -20000000000000000000000
b.set(0);
cout << "b = " << b;
cout << "a - b = " << a - b << '\n'; // -10000000000000000000000
cout << "b - a = " << b - a << '\n'; // -10000000000000000000000
cout << "\nMultiplying two bigint numbers:\n";
a.set(0);
b.set("500");
a *= b;
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
a *= b;
cout << "a *= b : a = " << a << '\n'; // 0
a.set("-32674816684");
cout << "a = " << a << '\n';
a *= b;
cout << "a *= b : a = " << a << '\n'; // -16337408342000
a.set("592491734917987491");
b.set("999999988888888888");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
cout << "a * b = " << a * b << '\n'; // 592491728334745991384590780072900008
a.set("592491734917987491");
b.set("-999999988888888888");
cout << "a = " << a << '\n';
cout << "b = " << b << '\n';
cout << "a * b = " << a * b << '\n'; // -592491728334745991384590780072900008
}