roman numeral to integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • level9

    roman numeral to integer

    How can I convert roman numerals to intergers in c++ VS 6?
  • Victor Bazarov

    #2
    Re: roman numeral to integer

    "level9" <rvalentine@kno logy.net> wrote...[color=blue]
    > How can I convert roman numerals to intergers in c++ VS 6?[/color]

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


    Comment

    • Heinz Ozwirk

      #3
      Re: roman numeral to integer

      "level9" <rvalentine@kno logy.net> schrieb im Newsbeitrag news:8f72e854.0 309111639.72360 47d@posting.goo gle.com...
      : 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(rom an[0]) < DigitValue(roma n[1]))
      value -= DigitValue(roma n[0]);
      else
      value += DigitValue(roma n[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

      Comment

      • Unforgiven

        #4
        Re: roman numeral to integer

        level9 wrote:[color=blue]
        > How can I convert roman numerals to intergers in c++ VS 6?[/color]

        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

        Comment

        • J. Campbell

          #5
          Re: roman numeral to integer

          rvalentine@knol ogy.net (level9) wrote in message news:<8f72e854. 0309111639.7236 047d@posting.go ogle.com>...[color=blue]
          > How can I convert roman numerals to intergers in c++ VS 6?[/color]

          See this thread:



          and read the posts by:
          Agamemnus

          for some ideas on converting in both directions.

          Comment

          • Stewart Gordon

            #6
            Re: roman numeral to integer

            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>[color=blue]
            > (Is 'iiii' a valid representation of 4, can 99 be written as 'ic' or
            > must it be written as 'lxxxix', ...?)[/color]

            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.

            Comment

            • Agent Mulder

              #7
              Re: roman numeral to integer

              <level9>[color=blue]
              > How can I convert roman numerals to intergers in c++ VS 6?[/color]
              </level9>

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

              #include<iostre am.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(ato i(argv[1]));
              return 0;
              }





              Comment

              • Tom

                #8
                Re: roman numeral to integer

                "Agent Mulder" wrote:[color=blue]
                > <level9>[color=green]
                > > How can I convert roman numerals to intergers in c++ VS 6?[/color]
                > </level9>
                >
                > Here you have a converter from integers to roman numerals (level4)[/color]

                <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.
                [color=blue]
                > #include<iostre am.h>[/color]

                Not a standard header. Make that:

                #include <iostream>
                [color=blue]
                > #include<string >[/color]

                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.
                [color=blue]
                > 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"};[/color]
                [color=blue]
                > string roman(int a){return M[(a/m)%10]+C[(a/c)%10]+X[(a/x)%10]+I[(a/i)%10];}[/color]

                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];
                }
                [color=blue]
                > int main(int argc,char**argv )
                > {
                > argc<2
                > ?
                > cout<<"\nAugust Number Converter Version 1.0\nUsage: August int"
                > :
                > atoi(argv[1])<0
                > ?[/color]

                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.
                [color=blue]
                > cout<<"\nNo negative numbers allowed"[/color]

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

                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:


                [color=blue]
                > return 0;
                > }[/color]

                Best regards,

                Tom

                Comment

                • Kevin Handy

                  #9
                  Re: roman numeral to integer

                  level9 wrote:[color=blue]
                  > How can I convert roman numerals to intergers in c++ VS 6?[/color]

                  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.


                  Comment

                  • Stewart Gordon

                    #10
                    Re: roman numeral to integer

                    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:
                    [color=blue]
                    > level9 wrote:
                    >[color=green]
                    >> How can I convert roman numerals to intergers in c++ VS 6?[/color]
                    >
                    >
                    > VS, stands for "verses", as in "kansas vs. the school board",
                    > right?[/color]
                    <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.

                    Comment

                    Working...