Redpic
Joined: 18 Dec 2005 Posts: 5 Location: Spain
|
Int8 to Roman Numbers (string) function. |
Posted: Wed Oct 11, 2006 7:27 am |
|
|
First: declarations and function:
Code: |
#include <string.h>
...
const int8 Numeros[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
...
//-----------------------------------------------------------------------------
void dec_to_roman(int8 LI, char* ptr){
int i;
char tmp[12];
char* ptmp = (char*) tmp;
strcpy(ptr,"");
// transformo de la tabla Ar�biga a la Rom�nica Latinitatis
// { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
// { 'I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'};
for(i=12;i!=255;i--){
while(LI >= Numeros[i]){
LI = LI - Numeros[i];
Switch(i){
case 0:
strcpy(ptmp,"I\0");
break;
case 1:
strcpy(ptmp,"IV\0");
break;
case 2:
strcpy(ptmp,"V\0");
break;
case 3:
strcpy(ptmp,"IX\0");
break;
case 4:
strcpy(ptmp,"X\0");
break;
case 5:
strcpy(ptmp,"XL\0");
break;
case 6:
strcpy(ptmp,"L\0");
break;
case 7:
strcpy(ptmp,"XC\0");
break;
case 8:
strcpy(ptmp,"C\0");
break;
case 9:
strcpy(ptmp,"CD\0");
break;
case 10:
strcpy(ptmp,"D\0");
break;
case 11:
strcpy(ptmp,"CM\0");
break;
case 12:
strcpy(ptmp,"M\0");
break;
}
ptr=strcat(ptr,ptmp);
}
}
}
//-----------------------------------------------------------------------------
|
Second, a use example:
Code: |
int8 H=128;
char H_in_RomanusNumericum[12];
...
dec_to_roman(H, (char*) H_in_RomanusNumericum);
printf("%s",H_in_RomanusNumericum);
...
|
Now H_in_RomanusNumericum is equal to "CXXVIII\0"
A curiosous example: The Pic Roman Clock
|
|