CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

Roman no.system to Decimal no. system converting topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 04-13-2010, 02:05 PM
Member
 
Join Date: Feb 2010
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
inspiration is on a distinguished road
| More
Roman no.system to Decimal no. system converting

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?
Reply With Quote
  #2  
Old 04-13-2010, 02:08 PM
Senior Member
 
Join Date: Feb 2010
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
meyup is on a distinguished road
| More
Re: Roman no.system to Decimal no. system converting

This might work. It isn't complete by any means, but is a general approach.

Code:
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

Last edited by shabbir; 04-13-2010 at 07:26 PM. Reason: Code Blocks
Reply With Quote
  #3  
Old 04-13-2010, 02:11 PM
Member
 
Join Date: Feb 2010
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
techme is on a distinguished road
| More
Re: Roman no.system to Decimal no. system converting

Use strlen on the string, or some loop like this:
Code:
Code:
char ch;
while (scanf("%c",&ch)) {
      [ verfify ch ]
      [ add value to total ] // see note below
}
Or scanf's %n modifier, like this:
Code:
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);
  }
}

Last edited by shabbir; 04-13-2010 at 07:26 PM. Reason: Code Blocks
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Abort a System SHutdown (system sleep) evarmint C# Programming 1 05-31-2010 10:36 PM
CFanatic has new Spam prevention system shabbir Announcements 0 10-15-2008 05:46 AM
How to capture the System Cursor Hadah C# Programming 0 04-28-2008 11:10 AM
C# system clock sync xzyfer C# Programming 0 01-14-2008 01:23 AM
Database System missthangythang2003 Looking to hire 0 03-08-2007 11:59 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 05:03 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board