-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.c
More file actions
63 lines (55 loc) · 1.29 KB
/
Copy pathfunc.c
File metadata and controls
63 lines (55 loc) · 1.29 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
#include "func.h"
#include <stdio.h>
#include <string.h>
int check_word(char *swd) {
int word_len = strlen(swd);
for (int i = 0; i < word_len; i++) {
if (swd[i] != '0' && swd[i] != '1') {
printf("Word is not valid!\n");
return 0;
}
}
return 1;
}
bool he_exit() {
char c;
printf("Would you like to exit ? (Y/n) : ");
while (scanf("%c", &c)) {
getchar(); // remove ebter key from buffer
if (c == 'Y' || c == 'y')
return true;
if (c == 'N' || c == 'n')
break;
}
return false;
}
uint *construct_result(char *swd, int tot_len) {
uint *result = (uint *)malloc(sizeof(uint) * tot_len);
int cnt = 0; // index for input word array
for (int i = tot_len - 1; i >= 0; i--) {
int j = i + 1;
if (!(j & (j - 1))) // power of 2
result[i] = 0;
else
result[i] = swd[cnt++] - '0';
}
return result;
}
void handle_input(char *buffer, int mx_size) {
if (strchr(buffer, '\n') == NULL) {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
}
return;
}
buffer[strlen(buffer) - 1] = '\0';
}
void assign_parities(uint *result, int tot_len) {
for (int i = tot_len - 1; i >= 0; i--) {
for (int j = 1; j <= tot_len; j *= 2) {
if (i + 1 != j && (i + 1) & j) {
result[j - 1] ^= result[i];
}
}
}
}