The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
roman.h File Reference
#include <defs.h>
+ Include dependency graph for roman.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int fromRoman (const char *s)
 
SWORD_NAMESPACE_START char isRoman (const char *s, int maxchars=0)
 

Function Documentation

int fromRoman ( const char *  s)

Converts a roman numeral to a value.

Parameters
sRoman numeral to convert.

Definition at line 39 of file roman.cpp.

39  {
40  int i, n = (int)strlen(str);
41  short * num= (short *) calloc(n, sizeof(short));
42  for (i = 0; str[i]; i++) {
43  switch(str[i]) {
44  case 'i':
45  case 'I':
46  num[i] = 1;
47  break;
48  case 'v':
49  case 'V':
50  num[i] = 5;
51  break;
52  case 'x':
53  case 'X':
54  num[i] = 10;
55  break;
56  case 'l':
57  case 'L':
58  num[i] = 50;
59  break;
60  case 'c':
61  case 'C':
62  num[i] = 100;
63  break;
64  case 'd':
65  case 'D':
66  num[i] = 500;
67  break;
68  case 'm':
69  case 'M':
70  num[i] = 1000;
71  break;
72  default:
73  num[i] = 0;
74  }
75  }
76  for (i = 1; str[i]; i++) {
77  if (num[i] > num[i-1]) {
78  num[i] -= num[i-1];
79  num[i-1] = 0;
80  }
81  }
82  n = 0;
83  for (i = 0; str[i]; i++) {
84  n += num[i];
85  }
86 
87  free(num);
88 
89  return n;
90 }
free(preg->fastmap)
SWORD_NAMESPACE_START char isRoman ( const char *  s,
int  maxchars = 0 
)

Checks if a string is a roman numeral.

Definition at line 31 of file roman.cpp.

31  {
32  char *ch = (char*)str;
33  for (; *ch && (!maxchars || (ch-str) <= maxchars); ch++)
34  if (!strchr("IVXLCDMivxlcdm ", *ch))
35  return 0;
36  return 1;
37 }