Results 1 to 2 of 2

Thread: Roman Numerals --> Integers

  1. #1

    Thread Starter
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641

    Roman Numerals --> Integers

    I am on chat and someone has asked me to solve this for them:


    i have to write a program that converts Roman numerals into decimal equivalents
    for example...if i Input XIV it should give me 14
    could someon help??

    btw: it is for c++..

    Thanks,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    each occurrance of following stands for

    M - 1000

    D - 500

    CM - 900
    CD - 400

    C - 100

    L - 50

    XC - 90
    XL - 40

    X - 10

    IX - 9

    V - 5

    IV - 4
    I - 1



    so by parsing out longest string possible and then integrate their meaning you get the result. Note they have to come in the order above, and each 5*10^n character may not appear more than once as well as 10^n character may not appear more than 3 times.

    You can loop trough the string and sum up both their occurance and sum.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width