-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
83 lines (69 loc) · 1.74 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int indexOf (char* string, char key);
int convert_to_dec (int from, char* data, char* ref);
int main () {
char ref[] = "0123456789ABCDEF";
char input[256], output[256];
int inbase10, fbase, tbase;
printf("Enter input number: ");
scanf("%s", input);
printf("Enter input base: ");
scanf("%d", &fbase);
printf("Enter target base: ");
scanf("%d", &tbase);
if (fbase != 10) {
inbase10 = convert_to_dec(fbase, input, ref);
} else {
inbase10 = atoi(input);
}
if (tbase == 10) {
printf("Result: %d\nDONE!\n", inbase10);
return 0;
}
int divi = inbase10;
int k = 0;
while (divi > 0) {
int odivi = divi;
int rem = divi % tbase;
output[k] = ref[rem];
divi = (divi - rem) / tbase;
printf("%d / %d = %d \t\t R: %d\n", odivi, tbase, divi, rem);
k++;
}
output[k] = '\0';
printf("\nReminders in base%d: %s\n", tbase, output);
k--;
printf("Result: ");
for(; k >= 0; k--) printf("%c", output[k]);
printf("\nDONE!\n");
return 0;
}
int convert_to_dec (int from, char* data, char* ref) {
int decs[strlen(data)];
int inplen = strlen(data) - 1;
int decsum = 0;
int i = inplen;
for (; i >= 0; i--) {
int r = inplen - i;
int d = indexOf(ref, data[r]);
int dec = d * pow(from, i);
decs[r] = dec;
decsum += dec;
printf("%d x %d^%d = %d\n", d, from, i, dec);
}
printf("%d ", decs[0]);
for (unsigned int j = 1; j < strlen(data); j++) printf("+ %d ", decs[j]);
printf("= %d\n\n", decsum);
return decsum;
}
int indexOf (char* string, char key) {
char* x = strchr(string, key);
if (x == NULL) {
printf("INVALID CHARACTER %c\n", key);
exit(EXIT_FAILURE);
}
return x - string;
}