using System; class Roman2Arabic { /// /// Roman2Arabic Bishop and Horspool May 2002 /// ============ /// Converts and prints a number in Roman notation /// that is interactively entered by the user. /// Minimal validity checking of the number is performed. /// Illustrates the use of switch statements. /// int Arabic( string s ) { int units, tens, hundreds, thousands; units = tens = hundreds = thousands = 0; for ( int i=0; i < s.Length; i++ ) { char c = s[i]; switch (c) { case 'I': case 'i': units++; break; case 'V': case 'v': units = 5 - units; break; case 'X': case 'x': tens += 10 - units; units = 0; break; case 'L': case 'l': tens = 50 - tens - units; units = 0; break; case 'C': case 'c': hundreds += 100 - tens - units; tens = units = 0; break; case 'D': case 'd': hundreds = 500 - hundreds - tens - units; tens = units = 0; break; case 'M': case 'm': thousands += 1000 - hundreds - tens - units; hundreds = tens = units = 0; break; default: Console.WriteLine( "Error: {0} is not a Roman digit", c); break; } } return thousands+hundreds+tens+units; } void Go() { for ( ; ; ) { Console.Write( "Enter Roman number (or empty line to exit): "); string s = Console.ReadLine(); s = s.Trim(); if (s.Length == 0) break; Console.WriteLine( "{0} is the Roman notation for {1}",s,Arabic(s)); } } static void Main() { new Roman2Arabic().Go(); } }