C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2010, 05:04 AM
xaykogeki xaykogeki is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 18 xaykogeki Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 26 m 5 sec
Reputation Power: 0
(C Linux) Convert 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 I write CD - 400
MD - 1500
IX - 9

I just need a rough guide on how to do this. any example would help me greatly. thanks.

Reply With Quote
  #2  
Old April 13th, 2010, 06:00 AM
LittleGrin LittleGrin is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2007
Posts: 921 LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 11 h 50 m 18 sec
Reputation Power: 535
Look at the order of things in the roman numerals. If small things (I, V) are on the left, they get subtracted from bigger things to the right. So IX is 10 - 1 = 9. If small things are on the right of big things, they get added. So XI = 10 + 1 = 11

If things are repeated, they get added repeatedly. So VIII = 5 + 1 + 1 + 1 = 8. If things get repeated, they should not be on the left (i.e. they don't get subtracted).

That's my rough memory of how roman numerals work. Check - I may have some of the details wrong.
Comments on this post
h4rdc0ded agrees: You got everything right
__________________
Right 98% of the time, and don't care about the other 3%.

“It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets.” (Alan Cooper)

Reply With Quote
  #3  
Old April 13th, 2010, 06:16 AM
Adak Adak is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 194 Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 39 m 32 sec
Reputation Power: 70
Walk the Roman numeral, left to right, char by char, adding as you go. If the "now" char is greater than the previous char, then subtract the previous char's value.

Now subtract the previous char from the "now" char (array[i] probably), and then add that amount onto the running sum you have going.

It may help to have a char array, where 'I' is at roman[1], and 'V' is roman[5], and 'X' is the value at roman[10] and like that, up to a thousand.

Sure, it's a bit of wasted memory, but on the other hand, you can include every single char roman numeral that way, in a simple look up.

Reply With Quote
  #4  
Old April 13th, 2010, 06:23 AM
h4rdc0ded h4rdc0ded is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 64 h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level)h4rdc0ded User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 13 h 7 m 34 sec
Reputation Power: 93
Ok. Use a loop to iterate over all the letters forming your Roman numeral, starting at the END of the numeral.
Inside the for-loop, you have to convert the current letter into a number. Keep track of two things: The decimal value of the previous letter you converted, and the total sum (which will become your final output).
Depending on which number is bigger (the decimal value of the current letter, or the value of the last letter converted), you just add or subtract the decimal value of the current letter to/from your sum.

That's it.

Note: Don't forget to initialize your 'previous value'- variable (with a suitable constant), and to intitialize your 'sum'- variable with 0.

Reply With Quote
  #5  
Old April 13th, 2010, 06:23 AM
xaykogeki xaykogeki is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 18 xaykogeki Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 26 m 5 sec
Reputation Power: 0
how can I read each and every line of those and at the same time, compare?

Reply With Quote
  #6  
Old April 13th, 2010, 07:49 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
What is your level of programming expertise?
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

Reply With Quote
  #7  
Old April 13th, 2010, 11:20 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,906 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 31 m 41 sec
Reputation Power: 1774
Is deja vue Latin for posting all over the interweb?
__________________
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

Reply With Quote
  #8  
Old April 13th, 2010, 11:28 AM
Adak Adak is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 194 Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level)Adak User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 39 m 32 sec
Reputation Power: 70
Quote:
Originally Posted by xaykogeki
so....
char x[10]'s value is really 10?

but, what if I want the user to input a whole string of those letters.
MXIII

how can I read each and every line of those and at the same time, compare?


Code:
if(roman[i] == yourChar)
  romanSum += i;


You want the user to input all the roman numerals char's, all at once. Then you "walk" the resulting char string, char by char, with a for or while loop:

"Here char, here little char, you can't hide little char"

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Convert Symbols to Integers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap