-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinToRoman_v2.c
277 lines (226 loc) · 4.22 KB
/
inToRoman_v2.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
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
/*
intToRoman_v2.c - Esta versão tem alocação dinâmica de memória
CodeWars exercise:
Create a function taking a positive integer as its parameter and
returning a string containing the Roman Numeral representation
of that integer.
Modern Roman numerals are written by expressing each digit separately
starting with the left most digit and skipping any digit with a value
of zero.
In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting
in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses
each Roman symbol in descending order: MDCLXVI.
Example:
solution(1000); // => "M"
Help:
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
Remember that there can't be more than 3 identical symbols in a row.
Referência: https://escolakids.uol.com.br/matematica/numeros-romanos-2.htm
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *solution(int n);
char *milhar(int n);
char *centena(int n);
char *dezena(int n);
char *unidade(int n);
void error();
void test(int i, char *r);
void tests();
int main(){
int nr_int;
char *nr_roman;
//printf("Entre com um inteiro entre 0 e 3999: ");
//scanf("%d",&nr_int);
//if(nr_int < 0 || nr_int>3999) error();
tests();
nr_roman = solution(nr_int);
return 0;
}
char *solution(int n) {
char buff[50] = "";
int p1,p2,p3,p4;
int aux,q;
char *r1,*r2,*r3,*r4;
char *roman_nr;
q = n/1000;
p1 = q*1000;
aux = n-p1;
r1 = milhar(p1);
q = aux/100;
p2 = q*100;
aux = aux-p2;
r2 = centena(p2);
q = aux/10;
p3 = q*10;
r3 = dezena(p3);
p4 = aux-p3;
r4 = unidade(p4);
strcat(buff,r1);
strcat(buff,r2);
strcat(buff,r3);
strcat(buff,r4);
roman_nr = malloc(strlen(buff)+1);
if(!roman_nr){
printf("Memory allocation error.\n");
exit(1);
}
for(int i=0;i<strlen(buff);i++){
*roman_nr++ = buff[i];
}
*roman_nr = '\0';
roman_nr -= strlen(buff);
return roman_nr;
}
char *milhar(int n){
switch(n){
case 0:
return "";
case 1000:
return "M";
case 2000:
return "MM";
case 3000:
return "MMM";
default:
error();
}
}
char *centena(int n){
switch(n){
case 0:
return "";
case 100:
return "C";
case 200:
return "CC";
case 300:
return "CCC";
case 400:
return "CD";
case 500:
return "D";
case 600:
return "DC";
case 700:
return "DCC";
case 800:
return "DCCC";
case 900:
return "CM";
}
}
char *dezena(int n){
switch(n){
case 0:
return "";
case 10:
return "X";
case 20:
return "XX";
case 30:
return "XXX";
case 40:
return "XL";
case 50:
return "L";
case 60:
return "LX";
case 70:
return "LXX";
case 80:
return "LXXX";
case 90:
return "XC";
}
}
char *unidade(int n){
switch(n){
case 0:
return "";
case 1:
return "I";
case 2:
return "II";
case 3:
return "III";
case 4:
return "IV";
case 5:
return "V";
case 6:
return "VI";
case 7:
return "VII";
case 8:
return "VIII";
case 9:
return "IX";
}
}
void error(){
printf("ERROR. Number out of interval. Must be: 0 <= number <= 3999.\n");
exit(1);
}
/* TESTES */
void test(int i, char *r){
char *p = "";
char *s = solution(i);
//printf("s: %s\n",s);
//printf("r: %s\n",r);
//printf("strcmp: %d\n",strcmp(s,r));
//printf("strlen s: %d\n",strlen(s));
//printf("strlen r: %d\n",strlen(r));
if( !strcmp(s,r)){
printf("OK\n");
}
else{
printf("ERRO\n");
}
*s = *p;
}
void tests(){
printf("testes - inicio\n");
test(1,"I");
test(4,"IV");
test(1000,"M");
test(1990,"MCMXC");
test(2007,"MMVII");
test(2,"II");
test(3,"III");
test(5,"V");
test(9,"IX");
test(10,"X");
test(11,"XI");
test(15,"XV");
test(19,"XIX");
test(22,"XXII");
test(1001,"MI");
test(2008,"MMVIII");
test(1004,"MIV");
test(2004,"MMIV");
test(3003,"MMMIII");
test(1991,"MCMXCI");
test(1992,"MCMXCII");
test(2091,"MMXCI");
test(1996,"MCMXCVI");
test(2843,"MMDCCCXLIII");
test(964,"CMLXIV");
test(345,"CCCXLV");
test(979,"CMLXXIX");
test(746,"DCCXLVI");
test(390,"CCCXC");
test(376,"CCCLXXVI");
test(189,"CLXXXIX");
printf("testes - fim\n");
}
/* end of intToRoman_v2.c */