C++ conversion

This is a discussion on C++ conversion within the C++ Programming forums, part of the General Programming Boards category; In general Roman numberals can be converted mathmatically to their arabic equivalent by simply assiging a numerical value to each ...

  1. #1
    charlie
    Guest

    C++ conversion

    In general Roman numberals can be converted mathmatically
    to their arabic equivalent by simply assiging a numerical value
    to each letter, according to the chart below, and calculating a total.

    M=1000|D=500|C=100|L=50|X=10|V=5|I=1

    In code, could you show me how to along with comments convert
    it.please help...much appreciated...

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What bit are you stuck on?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As zen pointed out you are well on your way here. There are many ways to do this. But what the hell, here are a few:
    Code:
    /* way number one */
    #define M 1000
    #define D 500
    #define C 100
    //you ge the idea
    
    /* way number two */
    short M = 1000, D = 500, C = 100; //not too hard
    There are also enumerations. You could also slap together some macros or functions to do simple opporations such as V + I = VI. But you will have make a function that returns a string for that sort of answer.

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb easyness

    Well... think about it... just set up a loop to go along and pick up letterz:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #define M 1000
    #define D 500
    #define C 100
    
    int RomanToAribic(char *string)
    {
         int ret = 0;
         for( int sloop = 0 ; sloop<strlen(string) ; sloop++ )
         {
              switch(toupper(string[sloop]))
              {
                     case 'M': ret += M; break;
                     case 'D': ret += D; break;
                     case 'C': ret += C; break;
                    default: break;
              }
         }
    }
    
    void main()
    {
            char rom[255];
             printf("Enter a roman number and I will spit back Aribic:");
             int r = RomanToAribic(rom);
             printf("\n%i",r);
    // If I got it wrong and its * instead of + or something please correct me. :)
    }
    SPH

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I dont want to spoil your party but that wont work minime. You see there is a catch with roman numerals and that is CD is not DC yet to your code it is. so its slightly more complicated than that.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Question okay

    I asked to correct me if im wrong... so how does it work then?

    SPH

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    For example...

    LX is 60
    XL is 40

    If the preceding number is smaller, that many is subtracted... get it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21