473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 6495
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: level9 | last post by:
How can I convert roman numerals to intergers in c++ VS 6?
2
by: Jeffrey A. Voigt | last post by:
Can someone tell me why this was not provided in .net? Or can someone at least point me in a direction to download a class file to handle this for me? Thanks, - jv
0
by: Martyn Fewtrell | last post by:
Hi there I'm sure there is a straight forward answer to this but I haven't quite got there. If I have a String in Sterling Currency Format and try to convert this to a Decimal (for arithmetic...
3
by: Ven | last post by:
Hi, I want to convert a decimal feild into a packed decimal field in my C++ program. How can I do that ? Is there any functions in C++ that can do that. I am using g++ compiler to compile my...
2
by: legoses | last post by:
I don't know why, but I'm having a tough time thinking of the best way to do this. I need to parse a string of input roman numerals and output if it is a valid roman numeral or not. Any ideas!!...
4
by: DixitSolanki | last post by:
I want to convert ASCII hex number to decimal conversion. For Example : 1: F01A is converted into Decimal: 61466 2: FFFC64F0 is converted into Decimal: 4294730992 What is the best...
1
by: selvakumari | last post by:
I am doing decimal conversion in my code as below if datachNode.InnerText is "147.00" and scaleFactor.InnerText is "1" decimal value = Convert.ToDecimal(datachNode.InnerText.ToString());...
3
by: capoeira26 | last post by:
I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.