Thread: Converting Symbols to Integers

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    Converting Symbols to Integers

    Can anyone give me somewhere to start if
    I have to convert from Roman Numeral to decimals?

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

    that if the user inputs CD - 400
    MD - 1500
    IX - 9
    MXIII - 1013

    I just need a rough guide on how to do this.
    All I know is if the digit's value before is smaller, then subtracts and if not, just keep adding. any example would help me greatly. thanks.

    PS: how do I also make the scanf determine the number of letters input?

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    This might give you some ideas. It isn't complete by any means, but is a general approach.

    Code:
    char numerals[80];
    int numeralCount,i;
    int firstValue,secondValue;
    scanf("%s",numerals);
    printf("You entered: %s\n",numerals);
    numeralCount = strlen(numerals);
    for (i = 0; i < numeralCount; i++) {
    	printf("%c\n",numerals[i]);
    	if (i != (numeralCount - 1)) {   // not on last character
    		firstValue = getValue(numerals[i]);    // return 1000 for 'M', etc....
    		secondValue = getValue(numerals[i+1]);
    		if (firstValue < secondValue) {
    			// do your 'less than' magic
    			i++;   // 'consume' the second char, since you want to step over it next time
    		}
    		else {
    			// just add the value
    		}
    	}
    	else {   // on last character
    		// to get here, must need to add final char value
    	}
    }
    note that when you have a 'less than' case, like IV, you want the next time thru the loop to skip to the character after the V. That's what the i++ 'consume' is for.

    You'd have a 'finalValue' variable or some such that got added to each time thru the loop, and at the end would be your final result.

    -------
    Beginner Computer Programming
    teaching programming to beginners using C

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xaykogeki View Post
    PS: how do I also make the scanf determine the number of letters input?
    Use strlen on the string, or some loop like this:
    Code:
    char ch;
    while (scanf("%c",&ch)) {
          [ verfify ch ]
          [ add value to total ] // see note below
    }
    You might find this interesting (skip the first couple paragraphs), I have two different roman numeral parsers in C there:

    Parsing

    NOTE: It's an interesting parsing task because you must use some kind of "lookahead" character to determine what a character combination's value is -- eg, IX does not equal 11, it equals 9. Because of that the loop idea might not be as good as just reading in a string all at once. Have a look at that page.
    Last edited by MK27; 04-13-2010 at 07:37 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    Use strlen on the string, or some loop like this:
    Code:
    char ch;
    while (scanf("%c",&ch)) {
          [ verfify ch ]
          [ add value to total ] // see note below
    }
    Or scanf's %n modifier, like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
      char buf[1024];
      int bufLength;
      if(scanf("%1023s%n", buf, &bufLength)) {
        printf("Your string has a length of %d\n", bufLength);
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Trouble with Windows/DirectX programming
    By bobbelPoP in forum Windows Programming
    Replies: 16
    Last Post: 07-08-2008, 02:27 AM
  3. Converting String to integers
    By Mister C in forum C Programming
    Replies: 21
    Last Post: 06-01-2005, 03:13 PM
  4. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM
  5. Converting Integers to Binary
    By cprog in forum C Programming
    Replies: 19
    Last Post: 10-03-2002, 08:20 PM