|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
howdy folks,
I've been set this assignment to make a calculator (just addition and subtraction thankfully :P) into which you can plug in Roman Numerals and get out a standard Arabic-form answer, along with the numerals tossed in. The thing is, I'm still fairly new to .Net and have very little idea as to where to start. I've made myself a module and put in some public constants, the basics, like so Code:
Module Module1
Public Const I = 1
Public Const V = 5
Public Const X = 10
Public Const L = 50
End Module
But my confusion sets in when I get to numbers like 16 or 14; how do I get the calculator to recognise the general rule that "if the letter before is smaller, subtract it. If the letter before is larger, add it" which pretty much covers Roman Numerals. I'm not sure how to word this into .Net and all my books are sadly lacking. Any help would be very much appreciated as I'm pretty much totally confused here >< |
|
#2
|
|||
|
|||
|
I'm assuming I'm going to have to do the actual calculations "behind the scenes" in the arabic form, so I'd also be really appreciative for any help on converting this arabic number back to roman numerals (In essence flipping the other code around)
again, any help would be greatly appreciated, this has just got me very frustrated >< |
|
#3
|
||||
|
||||
|
There are two ways to do this.
Do the calculation in Roman, which is actually pretty easy. You need two functions, expand() and collapse() Expand() takes a roman numeral string, and unfolds it to only 'I's (roman 1), so XIV becomes "IIIIIIIIIIIIII". Collapse takes a string of 'I's (roman 1) and rewrites it to a proper roman numeral. All you have to do for the calculation then, is expand() both values, concatenate them, and collapse() the resulting string. The alternative is converting the roman numerals to arabic first, doing the calculations, and then converting back. Calculating arabic from roman can also be done in two ways, using the earlier expand() function, and reading the string length, or writing a specific function for it. The same applies to the other way around.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 278
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > Roman Numerals convertor |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|