src/utilfuns/roman.cpp File Reference

#include <stdlib.h>
#include <string.h>
#include <roman.h>
Include dependency graph for roman.cpp:

Go to the source code of this file.

Functions

int from_rom (const char *str)
SWORD_NAMESPACE_START char isroman (const char *str, int maxchars)

Function Documentation

int from_rom ( const char *  s  ) 

Converts a roman numeral to a string.

Parameters:
s Roman numeral to convert.

Definition at line 34 of file roman.cpp.

00034                               {
00035     int i, n = strlen(str);
00036     short * num= (short *) calloc(n, sizeof(short));
00037     for (i = 0; str[i]; i++) {
00038         switch(str[i]) {
00039         case 'i':
00040         case 'I':
00041             num[i] = 1;
00042             break;
00043         case 'v':
00044         case 'V':
00045             num[i] = 5;
00046             break;
00047         case 'x':
00048         case 'X':
00049             num[i] = 10;
00050             break;
00051         case 'l':
00052         case 'L':
00053             num[i] = 50;
00054             break;
00055         case 'c':
00056         case 'C':
00057             num[i] = 100;
00058             break;
00059         case 'd':
00060         case 'D':
00061             num[i] = 500;
00062             break;
00063         case 'm':
00064         case 'M':
00065             num[i] = 1000;
00066             break;
00067         default:
00068             num[i] = 0;
00069         }
00070     }
00071     for (i = 1; str[i]; i++) {
00072         if (num[i] > num[i-1])  {
00073             num[i] -= num[i-1];
00074             num[i-1] = 0;
00075         }
00076     }
00077     n = 0;
00078     for (i = 0; str[i]; i++) {
00079         n += num[i];
00080     }
00081      free(num);
00082     return n;
00083 }

SWORD_NAMESPACE_START char isroman ( const char *  ,
int  maxchars = 0 
)

Checks if a string is a roman numeral.

Definition at line 26 of file roman.cpp.

00026                                              {
00027     char *ch = (char*)str;
00028     for (; *ch && (!maxchars || (ch-str) <= maxchars); ch++)
00029         if (!strchr("IVXLCDMivxlcdm ", *ch))
00030             return 0;
00031     return 1;
00032 }


Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1