-
Notifications
You must be signed in to change notification settings - Fork 0
/
numc.c
176 lines (151 loc) · 4.69 KB
/
numc.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
// https://github.com/jantari
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
unsigned long long int other_to_decimal (char input[], int input_length, int source_num_sys, _Bool verbosemode);
void decimal_to_other (unsigned long long int input, unsigned int target_num_sys, _Bool verbosemode);
_Bool string_is_zeroes(char* string, unsigned int string_length);
void print_help (void);
int main(int argc, char *argv[]) {
if (argc < 4) {
print_help();
exit(0);
}
// Variablendeklaration
int target_num_sys = 0, source_num_sys = 0;
_Bool verbosemode = 0;
unsigned long long int decimalNum = 0;
// Using getopt
int option = 0;
while ((option = getopt(argc, argv, "s:t:hv")) != -1) {
switch (option) {
case 's':
if(!(source_num_sys = atoi(optarg))) {
printf("Wrong command line argument: %s\n", optarg);
exit(-1);
}
break;
case 't':
if(!(target_num_sys = atoi(optarg))) {
printf("Wrong command line argument: %s\n", optarg);
exit(-1);
}
break;
case 'h':
print_help();
exit(0);
break;
case 'v':
verbosemode = 1;
break;
default:
break;
}
}
char *input;
unsigned int input_length = 0;
for (int index = optind; index < argc; index++) {
if (argv[optind]) {
input_length = strlen(argv[index]);
input = malloc(input_length * sizeof(char) + 1);
strcpy(input, argv[index]);
} else {
printf("Please specify a number to convert.");
exit(-1);
}
}
// Checking if input was 0
if (string_is_zeroes(input, input_length)) {
if (verbosemode == 1) {
printf("Nothing to convert.\n");
}
putchar('0');
putchar('\n');
return 0;
}
// Checking if input number is valid in specified number system
for (unsigned int iterate = 0, currentValue = 0; iterate < input_length; iterate++) {
if (input[iterate] > '9') {
currentValue = input[iterate] - '7';
} else {
currentValue = input[iterate] - '0';
}
if (currentValue >= source_num_sys) {
printf("%s is not a valid number in the %i-number system.\n", input, source_num_sys);
exit(-1);
}
}
// Hauptprogramm
if (source_num_sys != 10) {
// Berechnung andere Zahlensysteme zu Dezimal (Routine 1)
decimalNum = other_to_decimal(input, input_length, source_num_sys, verbosemode);
}
if (target_num_sys != 10) {
// Berechnung Dezimal zu anderen Zahlensystemen (Routine 2)
if (source_num_sys != 10) {
decimal_to_other(decimalNum, target_num_sys, verbosemode);
} else {
decimal_to_other(atol(input), target_num_sys, verbosemode);
}
} else {
printf("%llu", decimalNum);
}
// Programmende
free(input);
if (verbosemode == 1) putchar('\n');
putchar('\n');
return 0;
}
unsigned long long int other_to_decimal (char input[], int input_length, int source_num_sys, _Bool verbosemode) {
unsigned long long int valueOfDigit = 1, endzahl2 = 0;
for (int position = 0; position != input_length; position++) {
if (input[position] != '0') {
if (input[position] > '9') valueOfDigit = input[position] - '7';
else { valueOfDigit = input[position] - '0'; }
endzahl2 += valueOfDigit * pow(source_num_sys, input_length - position - 1);
}
if (verbosemode == 1) {
printf("\n%d ^ %d: %i", source_num_sys, input_length - position - 1, (int)pow(source_num_sys, input_length - position - 1));
printf("\nPosition: %i --- Value: %c", position, input[position]);
printf("\n>> Result so far: %llu\n",endzahl2);
}
}
if (verbosemode == 1) printf("\n%s(%d) in decimal is: %llu\n", input, source_num_sys, endzahl2);
return endzahl2;
}
void decimal_to_other (unsigned long long int input, unsigned int target_num_sys, _Bool verbosemode) {
// Allocate memory for a string with enough characters to hold the result of the calculation
char *output = malloc((int)(log(input) / log(target_num_sys) + 2) * sizeof(char));
if (verbosemode == 1) {
printf("Allocated memory for '%f (rounded down) + 2' chars to store result.\n\n", (log(input) / log(target_num_sys) ) * sizeof(char));
}
int i = 0;
static char numberArray[] = "0123456789abcdefghijklmnopqrstuvwxyz";
for (; input != 0; i++) {
output[i] = numberArray[input % target_num_sys];
if (verbosemode == 1) {
printf("i: %i ----------- \tquotient:\t%llu\tremainder:\t%c\n", i, input, output[i]);
}
input /= target_num_sys;
}
output[i] = '\0';
do {
putchar(output[--i]);
} while (i >= 0);
free(output);
}
_Bool string_is_zeroes(char* string, unsigned int string_length) {
if (string_length != 0 && *string != '\0') {
for (int i = 0; i < string_length; i++) {
if (string[i] != '0') {
return 0;
}
}
}
return 1;
}
void print_help (void) {
printf("Usage: numc (input number) -s (input number system) -t (target-number-system) [-v]\n");
}