roman numeral to integer

V

Victor Bazarov

level9 said:
How can I convert roman numerals to intergers in c++ VS 6?

You would need to write a program, I recon. Or did you have
something else in mind, like a wizard or by using their debug
windows somehow? Then you'd need to post to a VS 6 newsgroup
and not here...
 
H

Heinz Ozwirk

: How can I convert roman numerals to intergers in c++ VS 6?

Converting well formed roman numerals to integer is quite simple. Examine the 'digits' from left to right and compare each digit with the one to its right. If the value of the left digit is less than the value of the right digit, subtract the value of the left digit from the total value, otherwise add it. Then proceed with the next digit. The value of the last digit must always be added to the total value:

int DigitValue(char ch)
{
if (ch == 'm') return 1000;
...
if (ch == 'i') return 1;
return 0;
}
int RomanToInt(char const* roman)
{
int value = 0;
while (*roman)
{
if (DigitValue(roman[0]) < DigitValue(roman[1]))
value -= DigitValue(roman[0]);
else
value += DigitValue(roman[0]);
++roman;
}
return value;
}

This should work for well formed numerals, but returns (more or less) random results for invalid strings. If you want to validate such strings, there is more work to do, and you have to decide which 'dialect' your code should accept. (Is 'iiii' a valid representation of 4, can 99 be written as 'ic' or must it be written as 'lxxxix', ...?)

HTH
Heinz
 
U

Unforgiven

level9 said:
How can I convert roman numerals to intergers in c++ VS 6?

I had to do that as homework once, so yes. It's not all that hard to figure
out yourself. If you have specific issues, you can always come back and ask
them here. But as a general rule, we don't solve such 'general' issues here.
Especially since it's not really a C++ language problem; regardless of
whether you'd want to do that in C++, C, Java or Visual Basic, the algorithm
would be somewhat similar.
 
S

Stewart Gordon

While it was 12/9/03 7:38 am throughout the UK, Heinz Ozwirk sprinkled
little black dots on a white screen, and they fell thus:

(Is 'iiii' a valid representation of 4, can 99 be written as 'ic' or
must it be written as 'lxxxix', ...?)

In that dialect of Roman numerals, how is 89 written?

Stewart.
 
A

Agent Mulder

How can I convert roman numerals to intergers in c++ VS 6?
</level9>

Here you have a converter from integers to roman numerals (level4)

#include<iostream.h>
#include<string>
static int i=1;
static int x=10;
static int c=100;
static int m=1000;
static string I[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
static string X[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
static string C[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
static string M[]={"","M","MM","MMM"};
string roman(int a){return M[(a/m)%10]+C[(a/c)%10]+X[(a/x)%10]+I[(a/i)%10];}
int main(int argc,char**argv)
{
argc<2
?
cout<<"\nAugust Number Converter Version 1.0\nUsage: August int"
:
atoi(argv[1])<0
?
cout<<"\nNo negative numbers allowed"
:
atoi(argv[1])>3999
?
cout<<"\nNo numbers greater than 3999 allowed"
:
cout<<roman(atoi(argv[1]));
return 0;
}
 
T

Tom

Agent Mulder said:
</level9>

Here you have a converter from integers to roman numerals (level4)

<Sigh> I guess you acknowledge that this is the reverse of what the OP
wanted (roman numerals to integers. But leaving that aside, your
solution, while cute, will not compile on most C++ compilers that are
more-or-less compliant with the C++ standard, and also includes a
number of bad practices from a C++ standpoint. (One problem with
using Open Watcom and Digital Mars, as your posts indicate that you
do, is that both, although excellent compilers, do not comply the C++
standard, and therefore the code you write on them (i) will often not
be portable to other compilers and (ii) will often raise the ire of
some folks in this NG.
#include<iostream.h>

Not a standard header. Make that:

#include said:
#include<string>

Since in C++, string (and all other parts of the standard library)
appear in namespace std, you'll need to qualify your use of string.
static int i=1;
static int x=10;
static int c=100;
static int m=1000;
static string I[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
static string X[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
static string C[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
static string M[]={"","M","MM","MMM"};
string roman(int a){return M[(a/m)%10]+C[(a/c)%10]+X[(a/x)%10]+I[(a/i)%10];}

The 8 variables declared above are never modified, so you should make
then const. Furthermore, why give them global scope when you can make
them local to your roman(int) function without an extra keystroke? I
would therefore rewrite the function like this:

std::string roman(int a)
{
using std::string;
static const int i = 1;
static const int x = 10;
static const int c = 100;
static const int m = 1000;
static const string I[] = { "", "I", "II", "III",
"IV", "V", "VI", "VII",
"VIII", "IX" };
static const string X[]= { "", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC" };
static const string C[] = { "", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM" };
static const string M[]= { "", "M", "MM", "MMM" };

return M[(a / m) % 10] + C [(a / c) % 10]
+ X[(a / x ) % 10] + I [( a / i) % 10];
}
int main(int argc,char**argv)
{
argc<2
?
cout<<"\nAugust Number Converter Version 1.0\nUsage: August int"
:
atoi(argv[1])<0
?

Goodness, your use of the ? operator is confusing at best -
intentionally so I take it. Better practice would be to use regular
if-then statements, which will compile on most systems into the same
code but it will not be so confusing.
cout<<"\nNo negative numbers allowed"

What about zero? Oh wait, I see, there is no roman number for zero -
or is there?
:
atoi(argv[1])>3999
?
cout<<"\nNo numbers greater than 3999 allowed"
:
cout<<roman(atoi(argv[1]));

atoi is defined in <cstdlib>. If you want to use it, you need to
include that header. It may run just fine on your system without it,
apparently because (by blind luck) one of the other headers you
included already includes <cstdlib>, but you can't count on that being
the case on other compilers. Also, for a better C++ way to convert
from text to a number, see sections 38.2 and 38.3 of the FAQ:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.2
return 0;
}

Best regards,

Tom
 
K

Kevin Handy

level9 said:
How can I convert roman numerals to intergers in c++ VS 6?

VS, stands for "verses", as in "kansas vs. the school board",
right? So how did you do it in "6", whatever language that is.


You can do it in the following way in C++. Create an array
of strings like so:

char *roman[] = {
"",
"I",
"II",
"III",
"IV",
"V",
...


Thus, you can input a string from the user, locate it in the
array, and the location where it is found in the array tells
you its integer value. i.e. "MCMXXIV" would be stored in
"roman[1924]"

Now, if you give us your teachers email address we'll send
the program directly to him, so you don't have to bother about
doing that part of your homework either.
 
S

Stewart Gordon

While it was 16/9/03 7:13 pm throughout the UK, Kevin Handy sprinkled
little black dots on a white screen, and they fell thus:
VS, stands for "verses", as in "kansas vs. the school board",
right?
<snip>

No. Versus. Verses are divisions of a song or poem, or the
subdivisions of chapters in the Bible.

Stewart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top