-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHugeInteger.cpp
More file actions
140 lines (125 loc) · 3.1 KB
/
HugeInteger.cpp
File metadata and controls
140 lines (125 loc) · 3.1 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
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
// 12191706 ±èÁ¤Áø
// Chapter 9 - 9.14
// Member-functions definitions for class HugeInteger.
#include "HugeInteger.h" // include definition of class HugeInteger from HugeInteger.h
#include <iostream>
using namespace std;
//HugeInteger Class Functions
// provide a constructor that enables objects of this class to be initialized when they are created.
HugeInteger::HugeInteger() {
for (int i{ 0 }; i < 40; i++) {
number[i] = 0;
}
}
HugeInteger::~HugeInteger() {}
// input 40 one-digit integers for each digit of a 40-digit integer.
void HugeInteger::input(int num[40]) {
for (int i{ 0 }; i < 40; i++) {
number[i] = num[i];
}
}
// output 40-digit integer.
void const HugeInteger::output() {
for (int i{ 0 }; i < 40; i++) {
cout << number[i];
}
cout << "\n";
}
// distinguish hugeinteger is zero or not.
bool HugeInteger::isZero() {
bool result = true;
for (int i{ 0 }; i < 40; i++) {
if (number[i] != 0) {
result = false;
break;
}
}
return result;
}
// distinguish two hugeintegers are equal.
bool HugeInteger::isEqualTo(const HugeInteger& other) {
for (int i{ 0 }; i < 40; i++) {
if (number[i] != other.number[i]) {
return false;
}
}
return true;
}
// distinguish two hugeintegers are not equal.
bool HugeInteger::isNotEqualTo(const HugeInteger& other) {
bool result = false;
if (isEqualTo(other) == false)
result = true;
return result;
}
// distinguish hugeinteger is greater than other.
bool HugeInteger::isGreaterThan(const HugeInteger& other) {
bool result = false;
for (int i{ 0 }; i < 40; i++) {
if (number[i] > other.number[i]) {
result = true;
break;
}
else if (number[i] < other.number[i])
break;
}
return result;
}
// distinguish hugeinteger is less than other.
bool HugeInteger::isLessThan(const HugeInteger& other) {
bool result = false;
for (int i{ 0 }; i < 40; i++) {
if (number[i] < other.number[i]) {
result = true;
break;
}
else if (number[i] > other.number[i])
break;
}
return result;
}
// distinguish hugeinteger is greater than or equal to other.
bool HugeInteger::isGreaterThanOrEqualTo(const HugeInteger& other) {
bool result = false;
if (isGreaterThan(other) == true || isEqualTo(other) == true)
result = true;
return result;
}
// distinguish hugeinteger is less than or equal to other.
bool HugeInteger::isLessThanOrEqualTo(const HugeInteger& other) {
bool result = false;
if (isLessThan(other) == true || isEqualTo(other) == true)
result = true;
return result;
}
// add two huge integer
HugeInteger HugeInteger::add(const HugeInteger& other) {
HugeInteger result;
int save = 0;
for (int i{ 39 }; i >= 0; i--) {
result.number[i] = number[i] + other.number[i] + save;
if (i > 0) {
if (result.number[i] >= 10) {
result.number[i] -= 10;
save = 1;
}
}
}
return result;
}
// subtract two huge integer
HugeInteger HugeInteger::subtract(const HugeInteger& other) {
HugeInteger result;
int save = 0;
for (int i{ 39 }; i >= 0; i--) {
result.number[i] = number[i] - other.number[i] - save;
save = 0;
if (i > 0) {
if (result.number[i] < 0) {
result.number[i] += 10;
save = 1;
}
}
}
return result;
}