Addition of Roman numbers

This is a discussion on Addition of Roman numbers within the C Programming forums, part of the General Programming Boards category; How to write a program , in C ,which inputs two numbers specified in Roman numerals and outputs the sum ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    Unhappy Addition of Roman numbers

    How to write a program ,in C,which inputs two numbers specified in Roman numerals and outputs the sum of these two numbers in Roman numerals,no matter with capitals or not?
    im interested in Using arrays to store the input numbers (character sequences) and utilizing functions to do error checking and array-processing

    example
    input:CCCXi+CxXViIi
    output:CDXXXIX


    input: DCCXiv+DccciV
    output:MDXVIII


    input:MLxvII+Mmiii
    output:MMMLXX

    Code:
    	I =1			i =1
    	V = 5			v =5
    	X = 10		x =10
    	L = 50		l =50
    	C = 100		c = 100
    	D = 500		d = 500
    	M = 1000		m = 1000

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We don't do Homework.

    However, you may want to post a question ON your homework, perhaps?

    Or, you can post some code, along with questions ON that code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    yes,i actually wanted to ask how to do addition ,i.e converting roman number to decimal ,mcmxxxiv

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean, you wanted to ask "How do I convert roman numbers to deciaml?", yes?

    Basicly, you walk through the string and evaluate each letter or letter combination [e.g. IX is 9, not 11].

    You may want to start with a small subset, e.g. only supporting I, V and X as inputs, and just displaying the arabic [our normal 0..9] notation of the number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    So start with a function which converts a roman number (as a string) into an integer.
    Then create one which converts and integer into a roman number (as a string).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    what about 40(XL),60(LX) or 1965(MCMVXXV),2002(MMII)

    romans can be written before and after any other roman,it confuses me

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Yes, basically if a lower number is placed before a higher number, you subtract the smaller part from the bigger. If a higher number is before a lower number, you add them.
    But start small. Try to make a small parser that converts the roman numbers into integers. you could just translate all letters into their integer counterparts and add them together and worry about subtraction later.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > romans can be written before and after any other roman
    It's not ANY, there are rules.

    You have
    M D C L X V I

    1. Only C X I can appear to the left of something which is a higher value.

    2. C can only appear before M or D,
    X can only appear before C or L
    I can only appear before X or V.

    Which is why 1999 is MCMXCIX and not MIM

    3. They're also written most significant first.

    4. The prefix form only appears when you have a '4' or a '9' to encode, so 49 is XLIX (10 before 50 + 1 before 10).

    5. You're not the first person to ever ask this, so try a board search.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    conversion of roman to arabic is done,but now how should i read 2 roman numbers ,how can i get numbers in the left and in the right of +


    and whats wrong with this one its just retypes what i type (conv. of arabic to roman,3digits)


    Code:
    #include<stdio.h>
    
    int main(){
    
    int num;
    
    
    scanf("&#37;d",&num);
    switch(num/100){
    
    case '1' : printf("C");
    	break;
    case '2' : printf("CC");
    	break;
    	case '3': printf("CCC");
    	break;
    	case '4': printf("C D");
    	break;
    	case '5': printf("D");
    	break;
    	case '6': printf("DC");
    	break
    	case '7': printf("DCC");
    	break;
    	case '8': printf("DCCC");
    	break;
    	case '9': printf("CM");
    	break;
    }
    
    switch((num%100)/10){
    
    case '1': printf("X");
    	break;
    case '2': printf("XX");
    	break;
    	case '3': printf("XXX");
    	break;
    	case '4': printf("XL");
    	break;
    	case '5': printf("L");
    	break;
    	case '6': printf("LX");
    	break;
    	case '7': printf("LXX");
    	break;
    	case '8': printf("LXXX");
    	break;
    	case '9': printf("XC");
    	break;
    }
    
    switch(num%100){
    
    case '1': printf("I");
    	break;
    case '2': printf("II");
    	break;
    	case '3': printf("III");
    	break;
    	case '4': printf("IV");
    	break;
    	case '5': printf("V");
    	break;
    	case '6': printf("VI");
    	break;
    	case '7': printf("VII");
    	break;
    	case '8': printf("VIII");
    	break;
    	case '9': printf("IX");
    	break;
    }
    
    printf("%d",num);
    
    return 0;
    
    }
    Last edited by Salem; 12-10-2007 at 02:36 AM. Reason: Use [CODE][/CODE] tags for posting code.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Try case 1: rather than case '1':
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    yes you are right
    but this time i get

    532
    DXXX532

    loop does not enter last switch and output is roman and entered number

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The reason that your solution sort of works is that you can keep up with the bigger deductions like 500 or 100, but the solution fails when it needs to deduct fives or ones.

    Keeping Salems rules in mind basically translates directly into a lengthly, but plausible solution using string concatenation and successive deductions.

    I'm going to assume a ceiling of 999 as you've stated before. Here's an example of what I am talking about in some pseudo-not-really-code:

    Code:
    void to_roman ( unsigned long number )
    {
        if ( number <= 999 && number >= 1 ) {
            if ( number > 899 ) {
                number -= 900;
                printf( "CM" );
            }
            if ( number > 499 ) {
                number -= 500;
                printf( "D" );
            }
            while ( number > 99 ) {
                number -= 100;
                printf( "C" );
            }
            if ( number > 89 ) {
                number -= 90;
                printf( "XC" );
            }
            // and so on ...
        }
        putchar( '\n' );
    }
    It's that simple. Just make sure that you account for all of the possible deductions.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    First you do something like
    y = ( x / 1000 ) &#37; 10

    If y is 4 or 9, then you have one of the prefix forms to deal with.
    If y is 1 to 3, then output that many of the appropriate 'unit' symbol
    if y is 5 to 8, then output the appropriate 'five' symbol, and y-5 of the 'unit' symbol
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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