File: to_roman_numerals.c

package info (click to toggle)
hodie 1.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 144 kB
  • ctags: 104
  • sloc: ansic: 1,132; makefile: 75
file content (34 lines) | stat: -rw-r--r-- 709 bytes parent folder | download | duplicates (7)
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
#include "includes.h"

static const char* ones[10]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
static const char* tens[10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
static const char* cents[10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
static const char* mils[10]={"","M","MM","MMM","MMMM","MMMMM","MMMMMM","MMMMMMMM","MMMMMMMMM"};
static const char* naught="-";
		     
void to_roman_numerals(int n, char* str)
{
    int o,t,c,m;
    
    if(n==0)
    {
	sprintf(str,"%s",naught);
	return;
    }
    
    o=n%10;
    n=n/10;

    t=n%10;
    n=n/10;

    c=n%10;
    n=n/10;

    m=n%10;

    if(m>9)
	m=0;
    
    sprintf(str, "%s%s%s%s",mils[m],cents[c],tens[t],ones[o]);
}