Some new Python/C# code..

Page 1 of 2 12 LastLast

  1. Posts : 22,740
    Windows 10 Home x64
       #1

    Some new Python/C# code..


    Python: Convert a decimal value to a roman numeral value:
    This is a version to test the code with random number 1 to 1,200 and for ten tests:
      My Computer


  2. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #2

    Python Code:
    PHP Code:
    import random
    roman_value 
    str("")

    for 
    x in range (0,5):
        
    dec_to_convert random.randint(1,1200)
        
    hold dec_to_convert
        
    while (dec_to_convert 0):
            if (
    dec_to_convert >= 1000):
                
    dec_to_convert -= 1000
                roman_value 
    += "M"
            
    elif (dec_to_convert >= 900):
                
    dec_to_convert -= 900
                roman_value 
    += "CM"
            
    elif (dec_to_convert >= 500):
                
    dec_to_convert -= 500
                roman_value 
    += "D"
            
    elif (dec_to_convert >= 400):
                
    dec_to_convert -= 400
                roman_value 
    += "CD"
            
    elif (dec_to_convert >= 100):
                
    dec_to_convert -= 100
                roman_value 
    += "C"
            
    elif (dec_to_convert >= 90):
                
    dec_to_convert -= 90
                roman_value 
    += "XC"
            
    elif (dec_to_convert >= 50):
                
    dec_to_convert -= 50
                roman_value 
    += "L"
            
    elif (dec_to_convert >= 40):
                
    dec_to_convert -= 40
                roman_value 
    += "XL"
            
    elif (dec_to_convert >= 10):
                
    dec_to_convert -= 10
                roman_value 
    += "X"
            
    elif (dec_to_convert >= 5):
                
    dec_to_convert -= 5
                roman_value 
    += "V"
            
    elif (dec_to_convert >= 4):
                
    dec_to_convert -= 4
                roman_value 
    += "IV"
            
    elif dec_to_convert >= 1:
                
    dec_to_convert -= 1
                roman_value 
    += "I"
        
    print "Decimal:",hold"Is now Roman Numeral:"roman_value
        roman_value 
    "" 
    Results:
    Decimal: 1088 Is now Roman Numeral: MLXXXVIII
    Decimal: 694 Is now Roman Numeral: DCXCIV
    Decimal: 274 Is now Roman Numeral: CCLXXIV
    Decimal: 646 Is now Roman Numeral: DCXLVI
    Decimal: 302 Is now Roman Numeral: CCCII
    Decimal: 361 Is now Roman Numeral: CCCLXI
    Decimal: 770 Is now Roman Numeral: DCCLXX
    Decimal: 1170 Is now Roman Numeral: MCLXX
    Decimal: 1069 Is now Roman Numeral: MLXVIV
    Decimal: 712 Is now Roman Numeral: DCCXII
      My Computer


  3. Posts : 16,636
    Windows 11 Pro X64
       #3

    Pretty neat Jeff
      My Computers


  4. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #4

    Dude said:
    Pretty neat Jeff
    Thanks.. the first post had code that had bugs in it.. sound familiar. But now it's working just fine and I want to code that up in C# to have a nice GUI for that.
      My Computer


  5. Posts : 16,636
    Windows 11 Pro X64
       #5

    Nice, wish MS would roll out builds like you
      My Computers


  6. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #6

    Dude said:
    Nice, wish MS would roll out builds like you
    This is rather easy code.. I hate to think of how may lines of code are in Win10..
      My Computer


  7. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #7

    Prototype of the C# GUI: 1.0
    Some new Python/C# code..-capture.png
      My Computer


  8. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #8

    The C# version is starting to work.. needs more code but so far it can convert 1000.

    Some new Python/C# code..-capture.png
      My Computer


  9. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #9

    Now the C# version of the conversion program is working just fine:
    Some new Python/C# code..-capture.png
      My Computer


  10. Posts : 22,740
    Windows 10 Home x64
    Thread Starter
       #10

    Now the C# Code:
    PHP Code:
    //Define variables and retreive value to conver to a roman numeral..
    string romanValue "";
    try
    {
    string stuff dectext.Text;
    double decToConvert double.Parse(stuff);
    while (
    decToConvert 0)
    {
        if (
    decToConvert >= 1000)
        {
            
    decToConvert -= 1000;
            
    romanValue += "M";
        }
        else if (
    decToConvert >= 900)
        {
            
    decToConvert -= 900;
            
    romanValue += "CM";
        }
        else if (
    decToConvert >= 500)
        {
            
    decToConvert -= 500;
            
    romanValue += "D";
        }
        else if (
    decToConvert >= 400)
        {
            
    decToConvert -= 400;
            
    romanValue += "CD";
        }
        else if (
    decToConvert >= 100)
        {
            
    decToConvert -= 100;
            
    romanValue += "C";
        }
        else if (
    decToConvert >= 90)
        {
            
    decToConvert -= 90;
            
    romanValue += "XC";
        }
        else if (
    decToConvert >= 50)
        {
            
    decToConvert -= 50;
            
    romanValue += "L";
        }
        else if (
    decToConvert >= 40)
        {
            
    decToConvert -= 40;
            
    romanValue += "XL";
        }
        else if (
    decToConvert >= 10)
        {
            
    decToConvert -= 10;
            
    romanValue += "X";
        }
        else if (
    decToConvert >= 5)
        {
            
    decToConvert -= 5;
            
    romanValue += "V";
        }
        else if (
    decToConvert >= 4)
        {
            
    decToConvert -= 4;
            
    romanValue += "IV";
        }
        else
        {
            
    decToConvert -= 1;
            
    romanValue += "I";
        }
    }
    }
    catch
    {
    finalresults.Text "Non numeric value entered.";
    }

    finalresults.Text "Roman Value: " romanValue;

      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:02.
Find Us




Windows 10 Forums