Convert a method from C# to C++ Winforms

José Carlos 886 Reputation points
2023-02-10T18:40:27.5166667+00:00

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



Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,443 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,857 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.9K Reputation points
    2023-02-10T20:20:36.5233333+00:00

    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" ) );
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. José Carlos 886 Reputation points
    2023-02-10T21:51:43.21+00:00

    Hi Viorel.

    It worked perfect.

    Thanks a lot for the help.

    The program turned out really nice.

    José Carlos - Brazil

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.