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+ }
0 commit comments