Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,443 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi friends,
I'm making a program that converts decimal numbers into Roman numerals and vice versa. I'm having trouble converting roman to number. I found a method on the internet in C#, but it uses a Dictionary and I've never used it. I would like to convert the method below to C++ Winforms.
Tks
I found it too. A possible conversion:
static ref class Roman
{
static initonly Dictionary<wchar_t, int>^ RomanNumberDictionary;
static Roman( )
{
RomanNumberDictionary = gcnew Dictionary<wchar_t, int>;
RomanNumberDictionary['I'] = 1;
RomanNumberDictionary['V'] = 5;
RomanNumberDictionary['X'] = 10;
RomanNumberDictionary['L'] = 50;
RomanNumberDictionary['C'] = 100;
RomanNumberDictionary['D'] = 500;
RomanNumberDictionary['M'] = 1000;
}
public:
static int From( String^ roman )
{
int total = 0;
int current, previous = 0;
wchar_t currentRoman, previousRoman = L'\0';
for( int i = 0; i < roman->Length; i++ )
{
currentRoman = roman[i];
previous = previousRoman != L'\0' ? RomanNumberDictionary[previousRoman] : L'\0';
current = RomanNumberDictionary[currentRoman];
if( previous != 0 && current > previous )
{
total = total - ( 2 * previous ) + current;
}
else
{
total += current;
}
previousRoman = currentRoman;
}
return total;
}
};
// Example:
// Console::WriteLine( Roman::From( "MMXXIII" ) );
Hi Viorel.
It worked perfect.
Thanks a lot for the help.
The program turned out really nice.
José Carlos - Brazil