470,641 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Roman numeral to Decimal Conversion

Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
(dec2rom).

int rom2dec(char rom[])
{
//Local Variables
int a = 0; //counter
int dec = 0;//decimal equivalent

//Statements
while(rom[a] != '\0')
{
if(rom[a] >= rom[a+1])
{
dec += rom[a];
}
else
{
dec += (rom[a+1] - rom[a]);
a++;
}

a++;
}//end of while loop

return dec;
}//end of rom2dec

this function basically adds up the values of each roman numeral in
the array and it also checks for subractive numbers (i.e. IV = 4).
The program does not need to worry about numbers that dont make sense
like IIIIX. I must be accessing the character array wrongly because
when I enter a roman numeral of 'I' (one), it calculates the decimal
as 73.
The second part of the program is a huge amount of if's and for's.

void dec2rom(int dec, char rom[])
{
//Local Variables
int count = 0; //counter
int loop = 0; //loop counter
int pos = 0; //position on array

//Statements
if( (dec/M) >= 1 )
{
count = dec/M;
dec -= (count * M);
pos += count;

for(loop = 0; loop < count; loop++)
{
rom[loop] = 'M';
}//end of for loop
}
else if( (dec/C) == 9)
{
rom[pos] = 'C';
rom[pos + 1] = 'M';

pos += 2;
}
else if( (dec/D) >= 1 )
{
count = dec/D;
dec -= (count * D);

rom[pos] = 'D';

pos += count;
}
else if( (dec/C) >= 1 )
{
count = dec/C;
dec -= (count * C);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'C';
}//end of for loop

pos += count;
}
else if( (dec/X) == 9)
{
rom[pos] = 'X';
rom[pos + 1] = 'C';

pos += 2;
}
else if( (dec/L) >= 1 )
{
count = dec/L;
dec -= (count * L);

rom[pos] = 'L';

pos += count;
}
else if( (dec/X) >= 1 )
{
count = dec/X;
dec -= (count * X);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'X';
}//end of for loop

pos += count;
}
else if( (dec/I) == 9)
{
rom[pos] = 'I';
rom[pos + 1] = 'X';

pos += 2;
}
else if( (dec/I) == 4)
{
rom[pos] = 'I';
rom[pos + 1] = 'V';

pos += 2;
}
else if( (dec/V) >= 1 )
{
count = dec/V;
dec -= (count * V);

rom[pos] = 'V';

pos += count;
}
else if( (dec/I) >= 1 )
{
count = dec/I;
dec -= (count * I);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'I';
}//end of for loop

pos += count;
}

rom[pos] = '\0';

return;
}//end of dec2rom

This function should work it's way through a number from the thousands
and end with the smallest numbers. however, if an input of 5329 is
put in, it comes back with 'MMMMM' and doesnt have anything lower than
1000.

I appreciate any suggestions and anyone telling me that I'm an idiot
because I'm accessing character arrays wrongly (if that's my problem)
=)

thx.

Apr 9 '07 #1
7 6285
On 8 Apr 2007 17:30:38 -0700, bi********@gmail.com wrote:
>Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
(dec2rom).

int rom2dec(char rom[])
{
//Local Variables
int a = 0; //counter
int dec = 0;//decimal equivalent

//Statements
while(rom[a] != '\0')
{
if(rom[a] >= rom[a+1])
What will this do with "CL" which represents 150?
{
dec += rom[a];
Your program does not take into account that the value a roman
character represents is completely unrelated to the representation of
that character. 'C' is 0x43 in ASCII but represents 100.
}
else
{
dec += (rom[a+1] - rom[a]);
a++;
}

a++;
}//end of while loop

return dec;
}//end of rom2dec

this function basically adds up the values of each roman numeral in
the array and it also checks for subractive numbers (i.e. IV = 4).
The program does not need to worry about numbers that dont make sense
like IIIIX. I must be accessing the character array wrongly because
when I enter a roman numeral of 'I' (one), it calculates the decimal
as 73.
What is the representation of 'I' on your system?
>

The second part of the program is a huge amount of if's and for's.

void dec2rom(int dec, char rom[])
{
//Local Variables
int count = 0; //counter
int loop = 0; //loop counter
int pos = 0; //position on array

//Statements
if( (dec/M) >= 1 )
What is M?
{
count = dec/M;
dec -= (count * M);
pos += count;

for(loop = 0; loop < count; loop++)
{
rom[loop] = 'M';
}//end of for loop
}
else if( (dec/C) == 9)
Didn't your compiler complain about all the undefined variables?

snip
Remove del for email
Apr 9 '07 #2
<bi********@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
(dec2rom).

int rom2dec(char rom[])
{
//Local Variables
int a = 0; //counter
int dec = 0;//decimal equivalent

//Statements
while(rom[a] != '\0')
{
if(rom[a] >= rom[a+1])
{
dec += rom[a];
}
else
{
dec += (rom[a+1] - rom[a]);
a++;
}

a++;
}//end of while loop

return dec;
}//end of rom2dec

this function basically adds up the values of each roman numeral in
the array and it also checks for subractive numbers (i.e. IV = 4).
The program does not need to worry about numbers that dont make sense
like IIIIX. I must be accessing the character array wrongly because
when I enter a roman numeral of 'I' (one), it calculates the decimal
as 73.
Try this:

#include <ctype.h/* toupper() */
#include <string.h/* strlen() */

enum roman {
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
};

static int getdec(char c)
{
switch (toupper(c)) {
case 'I': return I;
case 'J': return J;
case 'U': return U;
case 'V': return V;
case 'X': return X;
case 'L': return L;
case 'C': return C;
case 'D': return D;
case 'M': return M;
default: return 0;
}
}

int rom2dec(const char *rom)
{
int i, dec;
size_t len;

for (i=0, dec=0, len=strlen(rom); i < len; i++) {
int temp;

if ((temp=getdec(rom[i])) < getdec(rom[i+1]))
dec -= temp;
else
dec += temp;
}
return dec;
}
The second part of the program is a huge amount of if's and for's.

void dec2rom(int dec, char rom[])
{
//Local Variables
int count = 0; //counter
int loop = 0; //loop counter
int pos = 0; //position on array

//Statements
if( (dec/M) >= 1 )
{
count = dec/M;
dec -= (count * M);
pos += count;

for(loop = 0; loop < count; loop++)
{
rom[loop] = 'M';
}//end of for loop
}
else if( (dec/C) == 9)
{
rom[pos] = 'C';
rom[pos + 1] = 'M';

pos += 2;
}
else if( (dec/D) >= 1 )
{
count = dec/D;
dec -= (count * D);

rom[pos] = 'D';

pos += count;
}
else if( (dec/C) >= 1 )
{
count = dec/C;
dec -= (count * C);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'C';
}//end of for loop

pos += count;
}
else if( (dec/X) == 9)
{
rom[pos] = 'X';
rom[pos + 1] = 'C';

pos += 2;
}
else if( (dec/L) >= 1 )
{
count = dec/L;
dec -= (count * L);

rom[pos] = 'L';

pos += count;
}
else if( (dec/X) >= 1 )
{
count = dec/X;
dec -= (count * X);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'X';
}//end of for loop

pos += count;
}
else if( (dec/I) == 9)
{
rom[pos] = 'I';
rom[pos + 1] = 'X';

pos += 2;
}
else if( (dec/I) == 4)
{
rom[pos] = 'I';
rom[pos + 1] = 'V';

pos += 2;
}
else if( (dec/V) >= 1 )
{
count = dec/V;
dec -= (count * V);

rom[pos] = 'V';

pos += count;
}
else if( (dec/I) >= 1 )
{
count = dec/I;
dec -= (count * I);

for(loop = pos; loop < (count + pos); loop++);
{
rom[loop] = 'I';
}//end of for loop

pos += count;
}

rom[pos] = '\0';

return;
}//end of dec2rom

This function should work it's way through a number from the thousands
and end with the smallest numbers. however, if an input of 5329 is
put in, it comes back with 'MMMMM' and doesnt have anything lower than
1000.

and this:

char *dec2rom(int dec, char *rom)
{
int i = 0;

while (dec M) { /* counting thousands */
rom[i++] = 'M';
dec -= M;
}

if (dec >= M-C){ /* 900-999 */
rom[i++] = 'C';
rom[i++] = 'M';
dec -= M-C;
} else if (dec D) { /* 500-899 */
rom[i++] = 'D';
dec -= D;
}

if (dec >= D-C){ /* 400-499 */
rom[i++] = 'C';
rom[i++] = 'D';
dec -= D-C;
} else
while (dec C) { /* counting hundrets */
rom[i++] = 'C';
dec -= C;
}

if (dec >= C-X){ /* 90-99 */
rom[i++] = 'X';
rom[i++] = 'C';
dec -= C-X;
} else if (dec L) { /* 50-89 */
rom[i++] = 'L';
dec -= L;
}

if (dec >= L-X){ /* 40-49 */
rom[i++] = 'X';
rom[i++] = 'L';
dec -= L-X;
} else
while (dec X) { /* counting tens */
rom[i++] = 'X';
dec -= X;
}

if (dec = X-I){ /* 9 */
rom[i++] = 'I';
rom[i++] = 'X';
dec -= X-I;
} else if (dec V) { /* 5-8 */
rom[i++] = 'V';
dec -= V;
}

if (dec = V-I){ /* 4 */
rom[i++] = 'I';
rom[i++] = 'V';
} else
while (dec--) /* counting ones */
rom[i++] = 'I';

rom[i] = '/0';

return rom;
}

I'd be happy to hear where it needs improvement...

Bye, Jojo
I appreciate any suggestions and anyone telling me that I'm an idiot
because I'm accessing character arrays wrongly (if that's my problem)
=)

thx.

Apr 9 '07 #3
"Joachim Schmitz" <no************@hp.comschrieb im Newsbeitrag
news:Yn*****************@news.cpqcorp.net...
<bi********@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
>Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
....
3 stupid Typos of mine corrected...:
and this:

char *dec2rom(int dec, char *rom)
{
int i = 0;

while (dec M) { /* counting thousands */
rom[i++] = 'M';
dec -= M;
}

if (dec >= M-C){ /* 900-999 */
rom[i++] = 'C';
rom[i++] = 'M';
dec -= M-C;
} else if (dec D) { /* 500-899 */
rom[i++] = 'D';
dec -= D;
}

if (dec >= D-C){ /* 400-499 */
rom[i++] = 'C';
rom[i++] = 'D';
dec -= D-C;
} else
while (dec C) { /* counting hundrets */
rom[i++] = 'C';
dec -= C;
}

if (dec >= C-X){ /* 90-99 */
rom[i++] = 'X';
rom[i++] = 'C';
dec -= C-X;
} else if (dec L) { /* 50-89 */
rom[i++] = 'L';
dec -= L;
}

if (dec >= L-X){ /* 40-49 */
rom[i++] = 'X';
rom[i++] = 'L';
dec -= L-X;
} else
while (dec X) { /* counting tens */
rom[i++] = 'X';
dec -= X;
}

if (dec = X-I){ /* 9 */
if (dec == X-I){ /* 9 */
rom[i++] = 'I';
rom[i++] = 'X';
dec -= X-I;
} else if (dec V) { /* 5-8 */
rom[i++] = 'V';
dec -= V;
}

if (dec = V-I){ /* 4 */
if (dec == V-I){ /* 4 */
rom[i++] = 'I';
rom[i++] = 'V';
} else
while (dec--) /* counting ones */
rom[i++] = 'I';

rom[i] = '/0';
rom[i] = '\0';
>
return rom;
}

I'd be happy to hear where it needs improvement...
Bye, Jojo
Apr 9 '07 #4
On Apr 9, 7:41 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
"Joachim Schmitz" <nospam.schm...@hp.comschrieb im Newsbeitragnews:Yn*****************@news.cpqcorp.n et...<billbai...@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman

...
3 stupid Typos of mine corrected...:
and this:
char *dec2rom(int dec, char *rom)
{
int i = 0;
while (dec M) { /* counting thousands */
rom[i++] = 'M';
dec -= M;
}
if (dec >= M-C){ /* 900-999 */
rom[i++] = 'C';
rom[i++] = 'M';
dec -= M-C;
} else if (dec D) { /* 500-899 */
rom[i++] = 'D';
dec -= D;
}
if (dec >= D-C){ /* 400-499 */
rom[i++] = 'C';
rom[i++] = 'D';
dec -= D-C;
} else
while (dec C) { /* counting hundrets */
rom[i++] = 'C';
dec -= C;
}
if (dec >= C-X){ /* 90-99 */
rom[i++] = 'X';
rom[i++] = 'C';
dec -= C-X;
} else if (dec L) { /* 50-89 */
rom[i++] = 'L';
dec -= L;
}
if (dec >= L-X){ /* 40-49 */
rom[i++] = 'X';
rom[i++] = 'L';
dec -= L-X;
} else
while (dec X) { /* counting tens */
rom[i++] = 'X';
dec -= X;
}
if (dec = X-I){ /* 9 */

if (dec == X-I){ /* 9 */ rom[i++] = 'I';
rom[i++] = 'X';
dec -= X-I;
} else if (dec V) { /* 5-8 */
rom[i++] = 'V';
dec -= V;
}
if (dec = V-I){ /* 4 */

if (dec == V-I){ /* 4 */
rom[i++] = 'I';
rom[i++] = 'V';
} else
while (dec--) /* counting ones */
rom[i++] = 'I';
rom[i] = '/0';
rom[i] = '\0';
return rom;
}
I'd be happy to hear where it needs improvement...

Bye, Jojo
I forgot to mention that I defined all the roman numerals in upper and
lower case at the beginning of the program.

I think the two problems I had were that I was accessing the
characters in the wrong way in the rom2dec function and the thing was
giving me the ascii value instead of my assigned value( I = 73 instead
of I = 1).

for my dec2rom function, I should have made them all if statements
instead of else if statements. (I believe that's what you fixed,
Joachim)

I think changing the else if's to simple if's should fix the dec2rom
(doesnt really do anything for the efficiency, but it should work)

The thing I still need to figure out is how to get the right value
from the character array.

Apr 9 '07 #5

<bi********@gmail.comschrieb im Newsbeitrag
news:11*********************@y66g2000hsf.googlegro ups.com...
On Apr 9, 7:41 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
>"Joachim Schmitz" <nospam.schm...@hp.comschrieb im
Newsbeitragnews:Yn*****************@news.cpqcorp. net...>
<billbai...@gmail.comschrieb im Newsbeitrag
>news:11**********************@d57g2000hsg.googleg roups.com...
Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman

...
3 stupid Typos of mine corrected...:
More stupid typos corrected:
>>
and this:
char *dec2rom(int dec, char *rom)
{
int i = 0;
while (dec M) { /* counting thousands */
while (dec >= M) { /* counting thousands */
rom[i++] = 'M';
dec -= M;
}
if (dec >= M-C){ /* 900-999 */
rom[i++] = 'C';
rom[i++] = 'M';
dec -= M-C;
} else if (dec D) { /* 500-899 */
} else if (dec >= D) { /* 500-899 */
rom[i++] = 'D';
dec -= D;
}
if (dec >= D-C){ /* 400-499 */
rom[i++] = 'C';
rom[i++] = 'D';
dec -= D-C;
} else
while (dec C) { /* counting hundrets */
while (dec >= C) { /* counting hundrets */
rom[i++] = 'C';
dec -= C;
}
if (dec >= C-X){ /* 90-99 */
rom[i++] = 'X';
rom[i++] = 'C';
dec -= C-X;
} else if (dec L) { /* 50-89 */
} else if (dec >= L) { /* 50-89 */
rom[i++] = 'L';
dec -= L;
}
if (dec >= L-X){ /* 40-49 */
rom[i++] = 'X';
rom[i++] = 'L';
dec -= L-X;
} else
while (dec X) { /* counting tens */
while (dec >= X) { /* counting tens */
rom[i++] = 'X';
dec -= X;
}
if (dec = X-I){ /* 9 */

if (dec == X-I){ /* 9 */ rom[i++] = 'I';
rom[i++] = 'X';
dec -= X-I;
} else if (dec V) { /* 5-8 */
} else if (dec >= V) { /* 5-8 */
rom[i++] = 'V';
dec -= V;
}
if (dec = V-I){ /* 4 */

if (dec == V-I){ /* 4 */
rom[i++] = 'I';
rom[i++] = 'V';
} else
while (dec--) /* counting ones */
rom[i++] = 'I';
rom[i] = '/0';
rom[i] = '\0';
return rom;
}
I'd be happy to hear where it needs improvement...

Bye, Jojo

I forgot to mention that I defined all the roman numerals in upper and
lower case at the beginning of the program.
I guessed that. And used an enum rather than #define in my program, the
lower case ones are taken care of in my helper function getdec(), by using
toupper().
I think the two problems I had were that I was accessing the
characters in the wrong way in the rom2dec function and the thing was
giving me the ascii value instead of my assigned value( I = 73 instead
of I = 1).
Indeed.
for my dec2rom function, I should have made them all if statements
instead of else if statements. (I believe that's what you fixed,
Joachim)
May well be, I've just written mine from scratch.
I think changing the else if's to simple if's should fix the dec2rom
(doesnt really do anything for the efficiency, but it should work)
The thing I still need to figure out is how to get the right value
from the character array.
Huh? You mean in the rom2dec() function? Use something like my helper
function getdec().

Bye, Jojo
Apr 9 '07 #6
On Apr 9, 10:05 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
<billbai...@gmail.comschrieb im Newsbeitragnews:11*********************@y66g2000hs f.googlegroups.com...On Apr 9, 7:41 am, "Joachim Schmitz" <nospam.schm...@hp.comwrote:
"Joachim Schmitz" <nospam.schm...@hp.comschrieb im
Newsbeitragnews:Yn*****************@news.cpqcorp.n et...>
<billbai...@gmail.comschrieb im Newsbeitrag
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi, I need some help with figuring out why my program is all messed
up. There are two portions two it, one that converts from roman to
decimal (rom2dec) and another that converts from decimal to roman
...
3 stupid Typos of mine corrected...:

More stupid typos corrected:
and this:
char *dec2rom(int dec, char *rom)
{
int i = 0;
while (dec M) { /* counting thousands */

while (dec >= M) { /* counting thousands */
rom[i++] = 'M';
dec -= M;
}
if (dec >= M-C){ /* 900-999 */
rom[i++] = 'C';
rom[i++] = 'M';
dec -= M-C;
} else if (dec D) { /* 500-899 */
} else if (dec >= D) { /* 500-899 */
rom[i++] = 'D';
dec -= D;
}
if (dec >= D-C){ /* 400-499 */
rom[i++] = 'C';
rom[i++] = 'D';
dec -= D-C;
} else
while (dec C) { /* counting hundrets */

while (dec >= C) { /* counting hundrets */
rom[i++] = 'C';
dec -= C;
}
if (dec >= C-X){ /* 90-99 */
rom[i++] = 'X';
rom[i++] = 'C';
dec -= C-X;
} else if (dec L) { /* 50-89 */
} else if (dec >= L) { /* 50-89 */
rom[i++] = 'L';
dec -= L;
}
if (dec >= L-X){ /* 40-49 */
rom[i++] = 'X';
rom[i++] = 'L';
dec -= L-X;
} else
while (dec X) { /* counting tens */

while (dec >= X) { /* counting tens */
rom[i++] = 'X';
dec -= X;
}
if (dec = X-I){ /* 9 */
if (dec == X-I){ /* 9 */ rom[i++] = 'I';
rom[i++] = 'X';
dec -= X-I;
} else if (dec V) { /* 5-8 */
} else if (dec >= V) { /* 5-8 */
rom[i++] = 'V';
dec -= V;
}
if (dec = V-I){ /* 4 */
if (dec == V-I){ /* 4 */
rom[i++] = 'I';
rom[i++] = 'V';
} else
while (dec--) /* counting ones */
rom[i++] = 'I';
rom[i] = '/0';
rom[i] = '\0';
return rom;
}
I'd be happy to hear where it needs improvement...
Bye, Jojo
I forgot to mention that I defined all the roman numerals in upper and
lower case at the beginning of the program.

I guessed that. And used an enum rather than #define in my program, the
lower case ones are taken care of in my helper function getdec(), by using
toupper().
I think the two problems I had were that I was accessing the
characters in the wrong way in the rom2dec function and the thing was
giving me the ascii value instead of my assigned value( I = 73 instead
of I = 1).

Indeed.
for my dec2rom function, I should have made them all if statements
instead of else if statements. (I believe that's what you fixed,
Joachim)

May well be, I've just written mine from scratch.
I think changing the else if's to simple if's should fix the dec2rom
(doesnt really do anything for the efficiency, but it should work)
The thing I still need to figure out is how to get the right value
from the character array.

Huh? You mean in the rom2dec() function? Use something like my helper
function getdec().

Bye, Jojo
Hey, I've tweaked up my functions with your help and now they work
perfectly! =D

Thanks for all the help!

Apr 9 '07 #7
Slightly modified from snippets:
/*
** ROMAN2L.C Covert roman numerals to long integers.
**
** public domain by Bob Stout
*/

#include <ctype.h>

struct numeral {
long val;
int ch;
};

static struct numeral numerals[] =
{
{1L, 'I'},
{5L, 'V'},
{10L, 'X'},
{50L, 'L'},
{100L, 'C'},
{500L, 'D'},
{1000L, 'M'}
};

/*
** roman2long() - Converts a roman numeral string into a long
integer
**
** Arguments: 1 - Roman numeral string
**
** Returns: Long value if valid, else -1L
*/

long roman2long(const char *str)
{
int i,
j,
k;
long retval = 0L;

if (!str || 0 == *str)
return -1L;
for (i = 0, k = -1; str[i]; ++i) {
for (j = 0; j < 7; ++j) {
if (numerals[j].ch == toupper(str[i]))
break;
}
if (7 == j)
return -1L;
if (k >= 0 && k < j) {
retval -= numerals[k].val * 2;
retval += numerals[j].val;
} else
retval += numerals[j].val;
k = j;
}
return retval;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main(int argc, char *argv[])
{
while (--argc) {
++argv;
printf("roman2long(%s) returned %ld\n", *argv,
roman2long(*argv));
}
}

#endif /* UNIT_TEST */
Here is the other direction:

/*
** ROMAN.C - Converts integers to Roman numerals
** Jim Walsh, Dann Corbit, and Others made this.
**
** This Program Is Released To The Public Domain
**
**
----------------------------------------------------------------------
** Usage:
** This program can operate several ways. If a decimal number is
** supplied on the command line, it is converted into a roman
numeral.
** If a second argument is supplied on the command line, it is
converted
** in a non-standard way (does not use prefix notation so that 4
becomes
** IIII instead of IV). If no arguments are supplied on the
command line,
** the user is prompted for an input value.
**
----------------------------------------------------------------------
** Compiling:
** If the symbol ALLOW_BAR_NOTATION is defined, then bar notation
is
** allowed. If the Romans wanted to record one million, they did
not
** write down 1000 M's. They would right one M with a bar over it.
** Any Roman numeral with a bar over it is multiplied by 1000.
** Unfortunately, this is not a standard character in most
character sets.
** If you choose to #define ALLOW_BAR_NOTATION, then you will have
to
** translate the symbols to final form yourself.
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
long value,
dvalue;

int PrefixesAreOK = 1;

typedef struct tag_RomanToDecimal {
int PostValue;
char *PostLetter;
int PreValue;
char *PreLetter;
} R2D;

R2D RomanConvert[] = {
#ifdef ALLOW_BAR_NOTATION
/*
** A Roman numeral with a bar over it is
** that value multiplied by 1000.
*/
{1000000, "<M-bar>", 900000, "<CM-bar>"},
{500000, "<D-bar>", 400000, "<CD-bar>"},
{100000, "<C-bar>", 90000, "<XC-bar>"},
{50000, "<L-bar>", 40000, "<XL-bar>"},
{10000, "<X-bar>", 9000, "<IX-bar>"},
{5000, "<V-bar>", 4000, "<IV-bar>"},
#endif
{1000, "M", 900, "CM"},
{500, "D", 400, "CD"},
{100, "C", 90, "XC"},
{50, "L", 40, "XL"},
{10, "X", 9, "IX"},
{5, "V", 4, "IV"},
{1, "I", 1, "I"}
};

int place = 0;
if (argc == 2)
value = atol(argv[1]);
else {
if (argc == 3) {
value = atol(argv[1]);
PrefixesAreOK = 0;
} else {
printf("\nEnter an integer value: ");
scanf("%ld", &value);
}
}
dvalue = value;

printf("\n%ld = ", dvalue);
do {
while (value >= RomanConvert[place].PostValue) {
printf("%s", RomanConvert[place].PostLetter);
value -= RomanConvert[place].PostValue;
}
if (PrefixesAreOK)
if (value >= RomanConvert[place].PreValue) {
printf("%s", RomanConvert[place].PreLetter);
value -= RomanConvert[place].PreValue;
}
place++;
} while (value 0);

return (0);
}

Apr 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by level9 | last post: by
2 posts views Thread by Jeffrey A. Voigt | last post: by
reply views Thread by Martyn Fewtrell | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.