Skip to content

Commit 1d4dbdd

Browse files
committed
original files of Lab 12 var 6 ver 2
0 parents  commit 1d4dbdd

3 files changed

Lines changed: 330 additions & 0 deletions

File tree

HugeInt.cpp

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
#include "HugeInt.h"
2+
3+
HugeInt::HugeInt() {
4+
for (int i = 0; i < 41; i++) {
5+
number[i] = 0;
6+
}
7+
depth = 0;
8+
negative = false;
9+
}
10+
11+
HugeInt::HugeInt(int d) {
12+
for (int i = 0; i < 41; i++) {
13+
number[i] = 0;
14+
}
15+
depth = d;
16+
negative = false;
17+
}
18+
19+
HugeInt::HugeInt(int num[41], int dep) {
20+
for (int i = 0; i < 41; i++) {
21+
number[i] = num[i];
22+
}
23+
depth = dep;
24+
negative = false;
25+
}
26+
27+
void HugeInt::set(std::string str) {
28+
if (str[0] == '-') {
29+
depth = str.length() - 1;
30+
negative = true;
31+
int j = 0;
32+
for (int i = str.length() - 1; i > 0; i--) {
33+
number[j++] = str[i] - 48;
34+
}
35+
}
36+
else {
37+
depth = str.length();
38+
negative = false;
39+
int j = 0;
40+
for (int i = str.length() - 1; i >= 0; i--) {
41+
number[j++] = str[i] - 48;
42+
}
43+
}
44+
45+
}
46+
47+
std::string HugeInt::get() {
48+
std::string str;
49+
if (negative) {
50+
str += '-';
51+
for (int i = depth - 1; i >= 0; i--) {
52+
str += number[i] + 48;
53+
}
54+
}
55+
else {
56+
for (int i = depth - 1; i >= 0; i--) {
57+
str += number[i] + 48;
58+
}
59+
}
60+
61+
return str;
62+
}
63+
64+
bool HugeInt::check_zero() {
65+
for (int i = 0; i < depth; i++) {
66+
if (number[i]) {
67+
return false;
68+
}
69+
}
70+
return true;
71+
}
72+
73+
int HugeInt::compare(HugeInt& b) {
74+
if (negative && !b.negative) {
75+
return -1;
76+
}
77+
else if (!negative && b.negative) {
78+
return 1;
79+
}
80+
else if (!negative && !b.negative) {
81+
if (depth > b.depth) {
82+
return 1;
83+
}
84+
else if (b.depth < depth) {
85+
return -1;
86+
}
87+
else {
88+
for (int i = depth - 1; i >= 0; i--) {
89+
if (number[i] > b.number[i]) {
90+
return 1;
91+
}
92+
else if (number[i] < b.number[i]) {
93+
return -1;
94+
}
95+
}
96+
return 0;
97+
}
98+
}
99+
else if (negative && b.negative) {
100+
if (depth > b.depth) {
101+
return -1;
102+
}
103+
else if (b.depth < depth) {
104+
return 1;
105+
}
106+
else {
107+
for (int i = depth - 1; i >= 0; i--) {
108+
if (number[i] > b.number[i]) {
109+
return -1;
110+
}
111+
else if (number[i] < b.number[i]) {
112+
return 1;
113+
}
114+
}
115+
return 0;
116+
}
117+
}
118+
}
119+
120+
HugeInt HugeInt::simple_sum(HugeInt& b) {
121+
int max = 0;
122+
if (depth > b.depth) {
123+
max = depth;
124+
}
125+
else {
126+
max = b.depth;
127+
}
128+
int result[41] = { 0 };
129+
for (int i = 0; i < max; i++) {
130+
int tmp = (result[i] + number[i] + b.number[i]);
131+
result[i] = tmp % 10;
132+
result[i + 1] += tmp / 10;
133+
}
134+
if (result[max]) {
135+
max++;
136+
}
137+
HugeInt res(result, max);
138+
return res;
139+
}
140+
141+
HugeInt HugeInt::simple_dif(HugeInt& b) {
142+
int result[41] = { 0 };
143+
for (int i = 0; i < depth; i++) {
144+
result[i] = number[i] - b.number[i];
145+
}
146+
for (int i = 0; i < depth; i++) {
147+
if (result[i] < 0) {
148+
result[i] += 10;
149+
int j = i + 1;
150+
for (j; result[j] < 0; j++) {
151+
result[j] += 9;
152+
i++;
153+
}
154+
result[i + 1] -= 1;
155+
}
156+
}
157+
HugeInt res(result, depth);
158+
return res;
159+
}
160+
161+
HugeInt HugeInt::sum(HugeInt& b) {
162+
if (!negative && !b.negative) { //+
163+
return this->simple_sum(b);
164+
}
165+
else if (negative && b.negative) { //+
166+
HugeInt result;
167+
result = this->simple_sum(b);
168+
result.negative = true;
169+
return result;
170+
}
171+
else if (negative && !b.negative) { //+
172+
HugeInt tmp(*this);
173+
tmp.negative = false;
174+
if (tmp.compare(b) == -1) {
175+
return b.simple_dif(tmp);
176+
}
177+
else if (tmp.compare(b) == 1) {
178+
HugeInt result;
179+
result = tmp.simple_dif(b);
180+
result.negative = true;
181+
return result;
182+
}
183+
else {
184+
HugeInt result;
185+
result.set("0");
186+
return result;
187+
}
188+
}
189+
else if (!negative && b.negative) { // +
190+
HugeInt tmp(b);
191+
tmp.negative = false;
192+
if (this->compare(tmp) == 1) {
193+
return this->simple_dif(b);
194+
}
195+
else if (this->compare(tmp) == -1) {
196+
HugeInt result;
197+
result = tmp.simple_dif(*this);
198+
result.negative = true;
199+
return result;
200+
}
201+
else {
202+
HugeInt result;
203+
result.set("0");
204+
return result;
205+
}
206+
}
207+
}
208+
209+
HugeInt HugeInt::dif(HugeInt& b) {
210+
if (!negative && !b.negative) { // +
211+
if (this->compare(b) == 1) {
212+
return this->simple_dif(b);
213+
}
214+
else {
215+
HugeInt result;
216+
result = b.simple_dif(*this);
217+
result.negative = true;
218+
return result;
219+
}
220+
}
221+
else if (negative && b.negative) {
222+
HugeInt tmp_this(*this);
223+
HugeInt tmp_b(b);
224+
tmp_this.negative = false;
225+
tmp_b.negative = false;
226+
if (tmp_this.compare(tmp_b) == -1) { //+
227+
return tmp_b.simple_dif(tmp_this);
228+
}
229+
else if (tmp_this.compare(tmp_b) == 1) {//+
230+
HugeInt result;
231+
result = tmp_this.simple_dif(tmp_b);
232+
result.negative = true;
233+
return result;
234+
}
235+
else {
236+
HugeInt result;
237+
result.set("0");
238+
return result;
239+
}
240+
}
241+
else if (negative && !b.negative) { // +
242+
HugeInt result;
243+
result = this->simple_sum(b);
244+
result.negative = true;
245+
return result;
246+
}
247+
else if (!negative && b.negative) { // +
248+
return this->simple_sum(b);
249+
}
250+
}

HugeInt.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef HUGEINT_H
2+
#define HUGEINT_H
3+
4+
#include <string>
5+
#include <iostream>
6+
7+
class HugeInt {
8+
private:
9+
int number[40];
10+
int depth;
11+
bool negative;
12+
public:
13+
HugeInt();
14+
HugeInt(int depth);
15+
HugeInt(int numbers[40], int depth);
16+
17+
void set(std::string str);
18+
std::string get();
19+
20+
bool check_zero();
21+
int compare(HugeInt& b);
22+
HugeInt simple_sum(HugeInt& b);
23+
HugeInt simple_dif(HugeInt& b);
24+
HugeInt sum(HugeInt& b);
25+
HugeInt dif(HugeInt& b);
26+
27+
};
28+
29+
#endif

L12_6.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "HugeInt.h"
2+
3+
/*Создайте класс HugeInt, который хранит в 40-элементном массиве цифр целые числа разрядностью до 40 знаков.
4+
Разрядность указать в конструкторе. Предусмотреть функции сложения, вычитания, сравнения и проверки на 0.
5+
*/
6+
/*
7+
1)НЕ ДОП:
8+
расставить const
9+
сет и гет для всех полей
10+
узнавать знак числа
11+
менять знак числа
12+
узнавать позицию на разряде
13+
менять позицию на разряде
14+
15+
урезать размер числа, чтобы не было 0
16+
2)ДОП:
17+
перегрузка операторов
18+
умножение
19+
нахождение остатка от деления
20+
*/
21+
22+
using namespace std;
23+
int main() {
24+
25+
HugeInt a;
26+
HugeInt b;
27+
HugeInt s;
28+
29+
a.set("1000");
30+
b.set("7");
31+
32+
if (a.check_zero() == true) { cout << a.get() << " - its zero " << endl; }
33+
else { cout << a.get() << " - not zero " << endl; }
34+
if (b.check_zero() == true) { cout << b.get() << " - its zero " << endl; }
35+
else { cout << b.get() << " - not zero " << endl; }
36+
37+
if (a.compare(b) == 1) { cout << a.get() << " > " << b.get() << endl; }
38+
else if (a.compare(b) == -1 ) { cout << a.get() << " < " << b.get() << endl; }
39+
else if (a.compare(b) == 0) { cout << a.get() << " = " << b.get() << endl; }
40+
41+
s = a.dif(b);
42+
cout << "Difference = ";
43+
cout << s.get() << endl;
44+
s = a.sum(b);
45+
cout << "SUM = ";
46+
cout << s.get() << endl;
47+
48+
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)