summaryrefslogtreecommitdiffstats
path: root/snc.c
blob: 58aea389fffc9ff3ec2538af8400dcf8eea25978 (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
/***
	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 "snc.h"

void usage()
{
	printf("Simple numbers converter\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\t****\n"
			"\t-A, --list-alphabets\n"
			"\t\t****\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
	}
	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;
		else if(last_value == cur_value || last_value == 0) {
			answer += cur_value;
			amount_char++;
		}
		else if(last_value > cur_value) {
			answer += cur_value;
			amount_char = 1;
		}
		else if(last_value < cur_value) {
			if(last_value * 10 < cur_value)
				return -1;
			else if(last_value == SNC_ROMAN_V ||
					last_value == SNC_ROMAN_L || last_value == SNC_ROMAN_D)
				return -1;
			
			answer += cur_value - 2 * last_value;
			if(amount_char > 1)
				return -1;
			amount_char = 1;
		}
		last_value = cur_value;

		if(amount_char > 3)
			return -1;
		else if(cur_value == SNC_ROMAN_V ||
				cur_value == SNC_ROMAN_L || cur_value == SNC_ROMAN_D) {
			if(amount_char > 1) return -1;
		}

		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 main(int argc, char *argv[])
{
	if(argc == 1) {
		usage();
		return 0;
	}

	int result;
	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", required_argument, NULL, 'A'},
		{NULL, 0, NULL, 0}
	};

	while((result = getopt_long(argc, argv, short_options,
		long_options, NULL)) != -1) {
		switch(result) {
		case 'h': {
			usage();
			return 0;
		}
		case 'i': {
			break;
		}
		case 'o': {
			break;
		}
		case 'a': {
			break;
		}
		case 'A': {
			break;
		}
		default: break;
		}
	}

	return 0;
}