-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2.8.c
192 lines (166 loc) · 3.66 KB
/
m2.8.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
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef enum {
sum_ok,
sum_base_inv,
sum_bad_alloc,
sum_num_inv,
sum_cnt_inv
} sum_st_code;
bool is_num_valid(const char* str, int base) {
if (*str == '\0') {
return false;
}
while (*str != '\0') {
int ch = isdigit(*str) ? *str - '0' : (isalpha(*str) ? tolower(*str) - 'a' + 10 : -1);
if (ch == -1 || (ch >= base)) {
return false;
}
str++;
}
return true;
}
const char* skip_leading_zeros(const char* num) {
if (*num == '\0') {
return num;
}
while (*num == '0') {
num++;
}
if (*num == '\0') {
return (num - 1);
}
return num;
}
int shift_str_for_i(char* str, unsigned int i) {
if (i == 0) {
return 1;
}
size_t len = strlen(str);
for (int j = len; j >= 0; j--) {
str[j + i] = str[j];
}
for (int j = 0; j < i; j++) {
str[j] = '0';
}
return 0;
}
unsigned char int_to_ascii(int a) {
unsigned char num = a < 10 ? a + '0' : a - 10 + 'A';
return num;
}
int ascii_to_int(char a) {
int num = isdigit(a) ? a - '0' : (isalpha(a) ? tolower(a) - 'a' + 10 : -1);
if (num == -1) {
return -1;
}
return num;
}
sum_st_code sum_up(int base, char** res, size_t* res_size, const char* num) {
if (base < 2 || base > 36) {
return sum_base_inv;
}
if (!is_num_valid(num, base)) {
return sum_num_inv;
}
size_t len1 = strlen(*res);
num = skip_leading_zeros(num);
size_t len2 = strlen(num);
if (len2 > len1) {
while (len2 > *res_size) {
size_t new_sz = len2 * 2;
char* temp_p = realloc(*res, new_sz);
if (temp_p == NULL) {
return sum_bad_alloc;
}
*res = temp_p;
*res_size = new_sz;
}
shift_str_for_i(*res, len2 - len1);
len1 = strlen(*res);
}
bool if_plus_one = false;
long long cntr = 0;
while (cntr < len2) {
int n1 = ascii_to_int((*res)[(len1 - 1) - cntr]);
int n2 = ascii_to_int(num[(len2 - 1) - cntr]);
int sum = n1 + n2;
if (if_plus_one) {
sum += 1;
if_plus_one = false;
}
if (sum >= base) {
if_plus_one = true;
sum -= base;
}
(*res)[(len1 - 1) - cntr] = int_to_ascii(sum);
cntr++;
}
while (if_plus_one) {
if (cntr == len1) {
if (len1 >= *res_size) {
size_t new_sz = *res_size + 1;
char* temp_p = realloc(*res, new_sz);
if (temp_p == NULL) {
return sum_bad_alloc;
}
*res = temp_p;
*res_size = new_sz;
}
shift_str_for_i(*res, 1);
len1 += 1;
}
int sum = ascii_to_int((*res)[(len1 - 1) - cntr]) + 1;
if_plus_one = false;
if (sum >= base) {
if_plus_one = true;
sum -= base;
}
(*res)[(len1 - 1) - cntr] = int_to_ascii(sum);
cntr++;
}
return sum_ok;
}
sum_st_code sum_base_n(size_t cnt, int base, char** res, ...) {
if (cnt == 0) {
return sum_cnt_inv;
}
if (base < 2 || base > 36) {
return sum_base_inv;
}
va_list a;
size_t size = 1024;
char* sum_res = malloc(size * sizeof(char));
if (sum_res == NULL) {
return sum_bad_alloc;
}
strcpy(sum_res, "0");
va_start(a, res);
for (int i = 0; i < cnt; i++) {
char* t_num = va_arg(a, char*);
switch (sum_up(base, &sum_res, &size, t_num)) {
case sum_bad_alloc:
free(sum_res);
return sum_bad_alloc;
case sum_num_inv:
printf("%s is invalid\n", t_num);
break;
default:
break;
}
}
res = &sum_res;
printf("sum result is - %s\n", sum_res);
va_end(a);
return sum_ok;
}
int main() {
char* result;
sum_base_n(3, 16, &result, "00000000000", "FF", "1");
free(result);
return 0;
}