summaryrefslogtreecommitdiffstats
path: root/snc.c
blob: e321dceed30e953884ccd7824fe583e3872de7be (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/***
	This file is part of Simple numbers converter.

	Copyright (C) 2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>

	Simple numbers converter is free software; you can redistribute it
	and/or modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	Simple numbers converter is distributed in the hope that it will be
	useful, but WITHOUT ANY WARRANTY; without even the implied warranty
	of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, see <http://www.gnu.org/licenses/>.
***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <math.h>

#include "snc.h"

void push(struct stack **head, char ch)
{
	struct stack *temp = malloc(sizeof(struct stack));
	temp->next = *head;
	temp->ch = ch;
	*head = temp;
}

char pop(struct stack **head)
{
	struct stack *temp = *head;
	char ch = (*head)->ch;
	*head = (*head)->next;
	free(temp);
	return ch;
}

void usage()
{
	printf("SNC (Simple numbers converter)\n"
			"Copyright (C) 2021 Aleksandr D. Goncharov\n"
			"SNC is free software; you can redistribute it and/or modify\n"
			"it under the terms of the GNU General Public License as\n"
			"published by the Free SoftwareFoundation; either version\n"
			"2 of the License, or (at your option) any later version.\n"
			"\n"
			"Synopsis: snc [OPTION] ... [NUMBER]\n"
			"Option:\n"
			"\t-h, --help\n"
			"\t\tPrint this text.\n"
			"\t-i, --input\n"
			"\t\t****\n"
			"\t-o, --output\n"
			"\t\t****\n"
			"\t-a, --alphabet\n"
			"\t\tOutput numeral system. Arabic is used by default\n"
			"\t-A, --list-alphabets\n"
			"\t\tPrint valid alphabets\n");
}

int getRomanValue(char symbol)
{
	switch(symbol)
	{
		case 'I': return SNC_ROMAN_I; // have triple notation
		case 'V': return SNC_ROMAN_V;
		case 'X': return SNC_ROMAN_X; // have triple notation
		case 'L': return SNC_ROMAN_L;
		case 'C': return SNC_ROMAN_C; // have triple notation
		case 'D': return SNC_ROMAN_D;
		case 'M': return SNC_ROMAN_M; // have triple notation
		default: return 0;
	}
}

int romanToArabic(char *str)
{
	int answer = 0;
	int last_value = 0;
	int amount_char = 0;
	while(*str)
	{
		int cur_value = getRomanValue(*str);
		if(cur_value == 0)
			return -1;

		if(last_value == cur_value || last_value == 0) {
			if(amount_char >= 3)
				return -1;
			answer += cur_value;
			if(cur_value == SNC_ROMAN_V || cur_value == SNC_ROMAN_L
					|| cur_value == SNC_ROMAN_D) {
				if(amount_char > 1) return -1;
			}

			amount_char++;
		}
		else if(last_value > cur_value) {
			answer += cur_value;
			amount_char = 1;
		}
		else if(last_value < cur_value) {
			if(amount_char > 1)
				return -1;
			if(last_value * 10 < cur_value)
				return -1;
			if(last_value == SNC_ROMAN_V || last_value == SNC_ROMAN_L
					|| last_value == SNC_ROMAN_D)
				return -1;

			answer += cur_value - 2 * last_value;
			amount_char = 1;
		}
		last_value = cur_value;

		str++;
	}

	return answer;
}

char *arabicToRoman(char *str)
{
	int number = atoi(str);
	if(!number || number > SNC_ROMAN_MAX_NUMBER)
		return NULL;

	const char *units[] = {"","I","II","III","IV",
							"V","VI","VII","VIII","IV"};
	const char *tens[] = {"","X","XX","XXX","XL",
							"L","LX","LXX","LXXX","XC"};
	const char *hundreds[] = {"","C","CC","CCC","CD",
							"D","DC","DCC","DCCC","CM"};
	const char *thousands[] = {"","M","MM","MMM"};
	char *answer = malloc(sizeof(char) * (SNC_ROMAN_MAXLEN + 1));

	strcpy(answer, thousands[number / 1000]);
	strcat(answer, hundreds[number / 100 % 10]);
	strcat(answer, tens[number / 10 % 10]);
	strcat(answer, units[number % 10]);

	return answer;
}

int getValueFromChar(char ch)
{
	switch(ch)
	{
		case '0'...'9':
			return ch - 48;
		case 'A'...'Z':
			return ch - 55; // ch - 65 + 10
		case 'a'...'z':
			return ch - 87; // ch - 97 + 10
		default: return -1;
	}
}

char getCharFromValue(int value)
{
	if(value >= 0 && value <= 9)
		return '0' + value;
	else if(value >= 10 && value <= 36)
		return 'A' - 10 - value;
	return '}';
}

int toTenBase(int base, char *str)
{
	int length = strlen(str);
	int answer = 0;

	while(*str)
	{
		int value = getValueFromChar(*str);
		if(value == -1 || value >= base)
			return -1;
		answer += value * pow(base, --length);
		str++;
	}
	return answer;
}

char *fromTenBaseTo(int base, int number)
{
	int length = 1, i;
	char *answer;
	struct stack *s = NULL;
	while(number >= base)
	{
		push(&s, getCharFromValue(number % base));
		number /= base;
		length++;
	}

	answer = malloc(sizeof(char) * (length+1));
	answer[0] = getCharFromValue(number);
	for(i = 1; s; i++)
		answer[i] = pop(&s);
	answer[length] = '\0';
	return answer;
}

int main(int argc, char *argv[])
{
	if(argc == 1) {
		usage();
		return 0;
	}

	int opt;
	const char short_options[] = "hi:o:a:A";
	const struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"input", required_argument, NULL, 'i'},
		{"output", required_argument, NULL, 'o'},
		{"alphabet", required_argument, NULL, 'a'},
		{"list-alphabets", no_argument, NULL, 'A'},
		{NULL, 0, NULL, 0}
	};

	int convert_from;
	int convert_to = SNC_ARABIC;
	int input_base = 10;
	int output_base = 10;

	while((opt = getopt_long(argc, argv, short_options,
		long_options, NULL)) != -1) {
		switch(opt) {
		case 'h': {
			usage();
			return 0;
		}
		case 'i': {
			input_base = atoi(optarg);
			if(input_base == 0) {
				printf("Uncorrect input base\n");
				return 1;
			}
			break;
		}
		case 'o': {
			output_base = atoi(optarg);
			if(output_base == 0) {
				printf("Uncorrect output base\n");
				return 1;
			}
			break;
		}
		case 'a': {
			if(strcmp(optarg, "roman") == 0) {
				convert_to = SNC_ROMAN;
				break;
			}
			else if(strcmp(optarg, "arabic") == 0) {
				convert_to = SNC_ARABIC;
				break;
			}
			// else print all available alphabets
		}
		case 'A': {
			printf("Valid alphabets are: roman, arabic\n");
			return 0;
		}
		default: break;
		}
	}

	char *str_number = argv[optind];
	if(!str_number) {
		usage();
		return 1;
	}

	if(convert_to == SNC_ROMAN && output_base != 10) {
		printf("Roman alphabet support only ten base\n");
		return 1;
	}

	if(atoi(str_number))
		convert_from = SNC_ARABIC;
	else {
		convert_from = SNC_ROMAN;
		if(input_base != 10) {
			printf("Roman alphabet support only ten base\n");
			return 1;
		}
	}

	char *str_arabic = str_number;
	char *str_tenbase;
	char *str_roman;
	int tb_number;

	if(convert_to != convert_from && convert_from == SNC_ROMAN) {
		int temp = romanToArabic(str_number);
		if(temp == -1) {
			printf("%s is not correct roman number.\n", str_number);
			return 1;
		}
		str_arabic = malloc(sizeof(char) * (4+1)); // how define 4?
		sprintf(str_arabic, "%d", temp);
	}

	tb_number = toTenBase(input_base, str_arabic);
	if(str_arabic != str_number)
			free(str_arabic);
	if(tb_number == -1) {
		printf("%s contains not valid chars for %d base system.\n",
			str_number, input_base);
		return 1;
	}

	str_tenbase = fromTenBaseTo(output_base, tb_number);
	if(convert_to != convert_from && convert_from == SNC_ARABIC) {
		str_roman = arabicToRoman(str_tenbase);
		free(str_tenbase);
		if(!str_roman) {
			printf("%s is not correct arabic number.\n", str_roman);
			return 1;
		}
		printf("%s\n", str_roman);
		free(str_roman);
	}
	else {
		printf("%s\n", str_tenbase);
		free(str_tenbase);
	}

	return 0;
}