-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.c
230 lines (173 loc) · 4.8 KB
/
lab2.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#define BASE (int)1e4
#define SIZE 78
struct uint1024_t {
int digits[SIZE];
};
char dig[312] = "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216";
struct uint1024_t cck;
char comp(struct uint1024_t a, struct uint1024_t b) {
for (int i = SIZE - 1; i >= 0; --i) {
if (a.digits[i] < b.digits[i]) {
return 0;
}
else if (a.digits[i] > b.digits[i]) {
return 1;
}
}
return 2;
}
struct uint1024_t from_uint(unsigned int x) {
struct uint1024_t res;
for (int i = 0; i < SIZE; ++i) {
res.digits[i] = 0;
}
int next = 0;
while (x) {
res.digits[next++] = x % BASE;
x /= BASE;
}
return res;
}
struct uint1024_t add_op(struct uint1024_t* x, struct uint1024_t* y) {
struct uint1024_t res;
for (int i = 0; i < SIZE; i++) {
res.digits[i] = x->digits[i] + y->digits[i];
}
for (int i = 0; i < SIZE - 1; i++) {
if (res.digits[i] >= BASE) {
res.digits[i] -= BASE;
res.digits[i + 1]++;
}
}
if (comp(from_uint(0), res) == 1 || comp(res, cck)) {
printf("Undefined Behavior\n");
return from_uint(0);
}
return res;
}
struct uint1024_t subtr_op(struct uint1024_t x, struct uint1024_t y) {
struct uint1024_t res;
for (int i = 0; i < SIZE; i++) {
res.digits[i] = x.digits[i] - y.digits[i];
}
for (int i = 0; i < SIZE - 1; i++) {
if (res.digits[i] < 0) {
res.digits[i] += BASE;
res.digits[i + 1]--;
}
}
if (comp(from_uint(0), res) == 1 || comp(res, cck)) {
printf("Undefined Behavior\n");
return from_uint(0);
}
return res;
}
struct uint1024_t mult_op(struct uint1024_t x, struct uint1024_t y) {
struct uint1024_t res;
res = from_uint(0);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - i; j++) {
res.digits[i + j] += x.digits[i] * y.digits[j];
}
}
for (int i = 0; i < SIZE - 1; i++) {
if (res.digits[i] >= BASE) {
res.digits[i + 1] += res.digits[i] / BASE;
res.digits[i] = res.digits[i] % BASE;
}
}
if (comp(from_uint(0), res) == 1 || comp(res, cck)) {
printf("Undefined Behavior\n");
return from_uint(0);
}
return res;
}
void printf_value(struct uint1024_t c) {
short fl = 0;
for (int i = SIZE - 1; i >= 0; --i) {
if (c.digits[i] != 0) {
fl++;
}
if (fl) {
if (fl == 1) {
printf("%d", c.digits[i]);
fl++;
} else {
for (int j = 1000; j > 0; j /= 10)
if (c.digits[i] / j == 0) {
printf("0");
}
if (c.digits[i] != 0) {
printf("%d", c.digits[i]);
}
}
}
}
if (comp(from_uint(0), c) == 2) {
printf("0");
}
printf("\n");
}
void scanf_value(struct uint1024_t* x) {
char a[312];
for (int i = 0; i < 312; ++i) {
a[i] = '0';
}
scanf("%s", a);
int n = 0;
for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i) {
if (a[i] >= '0' && a[i] <= '9') {
n++;
} else {
break;
}
}
for (int i = 0; i <= n / 4; ++i) {
int cnt = 4, t = 1, rs = 0;
if (i == n / 4) {
cnt = n % 4;
}
int ind = n - i * 4 - 1;
for (int j = 0; j < cnt; ++j) {
rs += (a[ind - j] - '0') * t;
t *= 10;
}
x->digits[i] = rs;
}
}
struct uint1024_t undef(char a[312]) {
struct uint1024_t x = from_uint(0);
int n = 309;
for (int i = 0; i <= n / 4; ++i) {
int cnt = 4;
int t = 1;
int rs = 0;
if (i == n / 4) {
cnt = n % 4;
}
int ind = n - i * 4 - 1;
for (int j = 0; j < cnt; ++j) {
rs += (a[ind - j] - '0') * t;
t *= 10;
}
x.digits[i] = rs;
}
return x;
}
int main(int argc, char* argv[]) {
cck = undef(dig); // 2 ^ 1024
struct uint1024_t a = from_uint(333), b = from_uint(332);
struct uint1024_t c = add_op(a, b);
//printf_value(c);
//printf("\n");
//printf("%d", comp(from_uint(0), from_uint(0)));
//printf_value(cck);
//printf("\n");
scanf_value(&a);
scanf_value(&b);
printf("\n");
printf_value(add_op(a, b));
printf_value(subtr_op(a, b));
printf_value(mult_op(a, b));
}