473,385 Members | 1,676 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,385 software developers and data experts.

roman numeral to integer

How can I convert roman numerals to intergers in c++ VS 6?
Jul 19 '05 #1
9 20772
"level9" <rv********@knology.net> wrote...
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...
Jul 19 '05 #2
"level9" <rv********@knology.net> schrieb im Newsbeitrag news:8f**************************@posting.google.c om...
: 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
Jul 19 '05 #3
level9 wrote:
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.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #4
rv********@knology.net (level9) wrote in message news:<8f**************************@posting.google. com>...
How can I convert roman numerals to intergers in c++ VS 6?


See this thread:

http://forum.qbasicnews.com/viewtopic.php?t=3874

and read the posts by:
Agamemnus

for some ideas on converting in both directions.
Jul 19 '05 #5
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:

<snip>
(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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #6
<level9>
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;
}

Jul 19 '05 #7
Tom
"Agent Mulder" wrote:
<level9>
How can I convert roman numerals to intergers in c++ VS 6? </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 <iostream>
#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-lit....html#faq-38.2
return 0;
}


Best regards,

Tom
Jul 19 '05 #8
level9 wrote:
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.
Jul 19 '05 #9
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:
level9 wrote:
How can I convert roman numerals to intergers in c++ VS 6?

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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Christopher Benson-Manica | last post by:
Inspired by a thread on clc++, I decided to try it out... #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv ) { int i; int result=0; int this;
4
by: Patrick Blackman | last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
7
by: daming23 | last post by:
Hi, I'm taking an online C++ class and having some difficulty understanding the assignments. Can someone please look at the code I wrote to ensure validity? I am not asking for the answer just some...
31
by: ARMAS | last post by:
Make a program that allows the user to input a number and aoutput it's roman numeral equivalent..... plzzzzzz help.... Ex: 109 output:CIX
7
by: billbaitsg | last post by:
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...
2
by: tonesgirl85 | last post by:
I need some direction to know if I am on the right track. Please contique program below. import java.util.*; public class Roman { static Scanner console = new Scanner(System.in);
3
by: mookbooker | last post by:
I have a code written out with zero errors but i know my main() function is completely wrong and after i build the solution i compile it and nothing happens!! Not even cout statements please help. ...
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!!...
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.