如何整数值的罗马数字字符串转换? [英] How to convert integer value to Roman numeral string?
问题描述
整数转换其在C字符串重新presentation在罗马数字?
convert integer to its string representation in Roman Numbers in C ?
推荐答案
最简单的方法可能是建立三个数组对于复杂的案件和使用一个简单的功能,如:
The easiest way is probably to set up three arrays for the complex cases and use a simple function like:
// convertToRoman:
// In: val: value to convert.
// res: buffer to hold result.
// Out: n/a
// Cav: caller responsible for buffer size.
void convertToRoman (unsigned int val, char *res) {
char *huns[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
char *tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
char *ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
int size[] = { 0, 1, 2, 3, 2, 1, 2, 3, 4, 2};
// Add 'M' until we drop below 1000.
while (val >= 1000) {
*res++ = 'M';
val -= 1000;
}
// Add each of the correct elements, adjusting as we go.
strcpy (res, huns[val/100]); res += size[val/100]; val = val % 100;
strcpy (res, tens[val/10]); res += size[val/10]; val = val % 10;
strcpy (res, ones[val]); res += size[val];
// Finish string off.
*res = '\0';
}
这将处理任何无符号整数,尽管大量将有非常多的 M
在前面的字符和调用者必须确保他们的缓冲区足够大。
This will handle any unsigned integer although large numbers will have an awful lot of M
characters at the front and the caller has to ensure their buffer is large enough.
一旦数量已经减少低于1000,这是一个简单的3查表,分别为百,十位和个。例如,拿的情况下 VAL
是 314
。
Once the number has been reduced below 1000, it's a simple 3-table lookup, one each for the hundreds, tens and units. For example, take the case where val
is 314
.
VAL / 100
将 3
在这种情况下,在匈奴
数组查找会给 CCC
,那么 VAL = VAL%100
为您提供 14
为数万
查找。
val/100
will be 3
in that case so the huns
array lookup will give CCC
, then val = val % 100
gives you 14
for the tens
lookup.
然后 VAL / 10
将 1
在这种情况下,在数万
数组查找会给 X
,那么 VAL = VAL%10
为您提供 4
为那些
查找。
Then val/10
will be 1
in that case so the tens
array lookup will give X
, then val = val % 10
gives you 4
for the ones
lookup.
然后 VAL
将 4
在这种情况下,在那些
数组查找会给 IV
。
Then val
will be 4
in that case so the ones
array lookup will give IV
.
这是给你 CCCXIV
为 314
。
一个缓冲区溢出检查的版本是一个简单的一步,从那里:
A buffer-overflow-checking version is a simple step up from there:
// convertToRoman:
// In: val: value to convert.
// res: buffer to hold result.
// Out: returns 0 if not enough space, else 1.
// Cav: n/a
int convertToRoman (unsigned int val, char *res, size_t sz) {
char *huns[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
char *tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
char *ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
int size[] = { 0, 1, 2, 3, 2, 1, 2, 3, 4, 2};
// Add 'M' until we drop below 1000.
while (val >= 1000) {
if (sz-- < 1) return 0;
*res++ = 'M';
val -= 1000;
}
// Add each of the correct elements, adjusting as we go.
if (sz < size[val/100]) return 0;
sz -= size[val/100];
strcpy (res, huns[val/100]);
res += size[val/100];
val = val % 100;
if (sz < size[val/10]) return 0;
sz -= size[val/10];
strcpy (res, tens[val/10]);
res += size[val/10];
val = val % 10;
if (sz < size[val) return 0;
sz -= size[val];
strcpy (res, ones[val]);
res += size[val];
// Finish string off.
if (sz < 1) return 0;
*res = '\0';
return 1;
}
虽然,在这一点上,你可以因为他们是如此的相似认为重构的几百人,几十台加工成一个单独的功能。我会离开,作为一个额外的锻炼。
although, at that point, you could think of refactoring the processing of hundreds, tens and units into a separate function since they're so similar. I'll leave that as an extra exercise.
这篇关于如何整数值的罗马数字字符串转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!