Thread: Arabic to Roman Numeral

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    12

    Arabic to Roman Numeral

    I have been given the program to do for class to convert standard Arabic to Roman Numeral and back...

    I COULD use a crap load of if statements... but would there be any other way to do it? I thought about enum, but it didn't work out too well...

    I don't want anyone to do this for me!!! I just need to know if I am doing the best code for my problem...

    Thanks...
    JDM

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Here is something that might help.
    Code:
      char *symbols[] = {
        "I",  //1
        "IV", //4
        "V",  //5
        "IX", //9
        "X",  //10
        "XL", //40
        "IL", //49
        "L",  //50
        "XC", //90
        "IC", //99
        "C",  //100
        "CD", //400
        "ID", //499
        "D",  //500
        "CM", //900
        "IM", //999
        "M"}; //1000
      int values[] = {1, 4, 5, 9, 10, 40, 49, 50, 90, 99, 100, 400, 499, 500, 900, 999, 1000};

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I've done this exact program before. What I did is worked out a way of seperating the roman numeral into different parts, called "tokens". This process is called tokenizing. Then you go through each token, work out its value, and add it all up. For example:

    XIV becomes: X IV

    XCLIX becomes: XC L IX

    By looking at a lot of different roman numerals you should be able to work out a number of rules governing the tokenizing process. After this, you need to find ways to work out the value of each token. If I remember correctly, I assigned each relevant letter (ie I V X L etc) an index. If it is found that a token contains an index followed by a larger index, it's a subtraction token, eg, IX. So I would subtract I or 1, from X or 10, leaving 9.

    You also need to add in a bunch of checks for incorrect numerals, such as IVC or IVL.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    Thantos... I know that those are arrays. But I am sort of a newbie... could you give me more info on what that is so I can research it?

    JDM

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    hmm...

    What you could do...

    Is find some sort of a rule...so if you have the number... 113 or something..that would be CXIII. It's CXIII because you can take away 100 from it(C), and then subtract 10(X), and then subtract 3(III). If you could somehow check if a number can be divisible by 100, 10, 3, 2, 1..and act accordingly etc. Lets say that if you have 113 again. If you _can_ take away 100 from it you add an 'C' to a string, then if you can take away 10 from the number you add an 'X' to the string..etc etc until you cannot subtract anything from it anymore..kinda sorta get it?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    im sorry... i really don't... i have never really worked with arrays...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Kinda tired so I'll just give you what I have. You will need to read up on how to use arrays.
    Note: I might be wrong in some of my symbols. I can't remember if IM is valid or not
    Code:
    #include <iostream>
    
    int main (void)
    {
      char *symbols[] = {
        "I",  //1
        "IV", //4
        "V",  //5
        "IX", //9
        "X",  //10
        "XL", //40
        "IL", //49
        "L",  //50
        "XC", //90
        "IC", //99
        "C",  //100
        "CD", //400
        "ID", //499
        "D",  //500
        "CM", //900
        "IM", //999
        "M"}; //1000
      int values[] = {1, 4, 5, 9, 10, 40, 49, 50, 90, 99, 100, 400, 499, 500, 900, 999, 1000};
    
      int count = sizeof values / sizeof values[0];  //This just gets the number of elements
      int num = 3834; // Some number
    
      count--;  //count how the index of the highest value
      while ( num > 0 )
      {
        //if num has a value greater than or equal to the value we are currently looking at
        //subtract that value from num and display the proper symbol for it
        if ( num >= values[count] )
        {
          std::cout<<symbols[count];
          num -= values[count];
        }
        //if num doesn't have a value greater than or equal to the value we are currently looking at
        //decrament count we look at a lower value
        else
          count--;
      }
    
      std::cout<<endl;
      return 0;
    }

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oh I guess my idea was no good hey. Thanks for the acknowledgement of the effort taken to write that post. By the way, I know my way works, because I've made the program already. Also, it's the most logical way to go about the conversion.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  2. roman numeral conversion
    By clarky101 in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 07:13 PM
  3. Code won't compile! O.o
    By Coldcore in forum C Programming
    Replies: 24
    Last Post: 01-07-2006, 09:21 AM
  4. Roman Numeral Calculator
    By The Brain in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 06:23 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM