-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuge.cpp
More file actions
100 lines (86 loc) · 2.52 KB
/
Copy pathhuge.cpp
File metadata and controls
100 lines (86 loc) · 2.52 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
#include "huge.h"
#include <string>
/* Конструктор по-умолчанию */
Huge::Huge() {
list.push_back(0);
}
/* Формирование большого числа из строки */
Huge::Huge(const std::string& string) {
int length = 0;
num_type number = 0;
/* разделение на подсписки по 9 цифр */
List<std::string> stringList;
std::string temp = "";
for (char c : reverseString(string)) {
if (isDigit(c)) {
if (length == MAX_DIGITS) {
stringList.push_back(reverseString(temp));
length = 0;
temp = "";
}
++length;
temp += c;
}
}
if (temp != "") {
stringList.push_back(reverseString(temp));
}
/* Формирование большого числа */
auto iter = stringList.cbegin();
do {
for (char c : iter->value) {
number = number * 10 + (c - '0');
}
list.push_back(number);
number = 0;
iter = iter->next;
} while (iter != stringList.cbegin());
}
/* Копирования списка */
Huge::Huge(const List<num_type>& origList) : list(origList) {}
/* Конструктор копирования */
Huge::Huge(const Huge& orig) : list(orig.list) {}
/* Перегрузка оператора присваивания */
Huge& Huge::operator=(const Huge& other) {
if (this != &other) {
this->list = other.list;
}
return *this;
}
/* Проверка, является ли символ цифрой */
bool Huge::isDigit(char c) {
return c >= '0' && c <= '9';
}
/* Возвращает длину числа */
short Huge::getLength(num_type number) {
short length = 0;
do {
number /= 10;
++length;
} while (number != 0);
return length;
}
/* Возвращает строку из count нулей */
std::string Huge::zeros(short count) {
std::string returnString = "";
for (short i = 0; i < count; i++) {
returnString += '0';
}
return returnString;
}
/* Реверсирование строки */
std::string Huge::reverseString(const std::string& src) {
std::string reversedString;
for (char c : src) {
reversedString = c + reversedString;
}
return reversedString;
}
/* Возведение base в стпень p */
num_type Huge::pow(num_type base, short p) {
num_type retValue = 1;
for (int i = 0; i < p; i++) {
retValue *= base;
}
return retValue;
}