CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Jan 2010
    Posts
    8

    Arrow Question about Roman Number Converter

    Hi to all!

    I'm new to this forums and i gotta say, looks nice!

    My question has something to do with a Roman Number Converter made with C#.

    This is the code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Roman
    {
        public partial class RomeinsConverter : Form
        {
            public RomeinsConverter()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                string text = textBox1.Text;
                string textextra = textBox1.Text;
    
                if (text == "") //Als er niks ingevuld wordt, volgt er een waarschuwing.
                {
                    textBox1.Text = "Niks invullen, Niks krijgen. :)";
                }
    
                else if (Char.IsDigit(text[0]) == true)
                {
                    int textnumeriek = (int.Parse(text));
    
                    if (textnumeriek < 0 || textnumeriek > 3999) //Controleer of het getal tussen de 0 en 3999 ligt.
                    {
                        textBox1.Text = "Het getal moet tussen 0 en 3999 liggen. :)";
                    }
    
                    else if ((Convert.ToInt32(text) == 0)) //Als het getal gelijk aan 0 is, komt als uitvoer N tevoorschijn.
                    {
                        textBox1.Text = "N";
                    }
                    else
                    {
                        int[] getal = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
                        string[] romein = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
    
                        StringBuilder result = new StringBuilder();
    
                        for (int i = 0; i < 13; i++) //int i start op 0, zolang i < 13 (lengte van de string) wordt er 1 opgeteld bij i.
                        {
                            // Als het getal wat moet worden geconverteerd kleiner is dan de test waarde voeg dan het bijbehorende getal toe
                            while (textnumeriek >= getal[i])
                            {
                                textnumeriek -= getal[i]; //trek de waarde die op positie [i] staat af van de originele input (textnumeriek)
                                result.Append(romein[i]); //De Romeinse Waarde die hoort bij het zojuist van textnumeriek afgetrokken getal wordt aan de string "result" toegevoegd.
                            }
                        }
    
                        textBox1.Text = result.ToString(); //De waarde van result wordt in de textbox weergegeven.
                    }
                }
                else if (Char.IsDigit(text[0]) == false)
                {
                    if (text == "N")
                    {
                        textBox1.Text = "0";
                    }
                    else
                    {
                        
                        string restant = textBox1.Text;
    
                        
                        int som = 0;
                        int last = 0;
                        int i = 0;
                        int ii = 0;
                        string[] getal2 = new string[7] { "1", "5", "10", "50", "100", "500", "1000" };
                        string[] romein2 = new string[7] { "I", "V", "X", "L", "C", "D", "M" };
    
                                 {
                                    for (i = text.Length - 1; i >= 0; i--)
                                    {
                                        for (ii = 0; ii < romein2.Length; ii++)
                                        {
                                            if (text[i].ToString() == romein2[ii])
                                            {
                                                som += Convert.ToInt32(getal2[ii]);
    
                                                if (i != text.Length)
                                                {
                                                    if (Convert.ToInt32(getal2[ii]) < Convert.ToInt32(getal2[last]))
                                                    {
                                                        som -= 2 * Convert.ToInt32(getal2[ii]);
    
                                                    }
                                                    last = ii;
                                                }
    
                                            }
                                        }
                                    }
                                    textBox1.Text = "" + som + "";
                                }
                            }
                    
                    int count = 1;
                    char last2 = 'Z';
                    foreach (char nummer in text)
                    {
                        // Dit zorgt ervoor dat je niet zomaar wat kan invoeren zoals "HAII" en dat ie dan 2 als uitvoer geeft.
                        if ("IVXLCDM".IndexOf(nummer) == -1)
                        {
                            textBox1.Text = "Alleen Romeinse Cijfers gebruiken he, FOEI :P";
                        }
                        if ("VV".IndexOf(nummer) == 1)
                        {
                            textBox1.Text = "Alleen Romeinse Cijfers gebruiken he, FOEI :P";
                        }
                        // Voorkomt dat als je bijvoorbeeld VVVVVVVVVVVVVVVVVV invoert je dan een uitvoer krijgt, of IIII met als uitvoer 4. Dat kan niet :)
                        if (nummer == last2)
                        {
                            count++;
                            if (count == 4)
                            {
                                textBox1.Text = "Das iets TE VEEl van het goeie :P!";
                            }
                        }
                        else
                        {
                            count = 1;
                            last2 = nummer;
                        }
                        // Voorkomt dat als je bijvoorbeeld LL invoert je als uitvoer 100 krijgt.
                        if (text.Split('V').Length > 2 || text.Split('L').Length > 2 || text.Split('D').Length > 2)
                        {
                            textBox1.Text = "Helaas, dubbel aantal L of V of D gaat niet :)";
                        }
    
                        }
                        
                        }
                    }
    
            
                }
              
    }
    Comments are in Dutch, because I am Dutch.. hehe sry bout that.

    This works, you can convert a Roman Number to a Decimal Number and vice versa.
    But something bothers me.

    Code:
    int[] getal = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
    string[] romein = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
    
                        StringBuilder result = new StringBuilder();
    
                        for (int i = 0; i < 13; i++) //int i start op 0, zolang i < 13 (lengte van de string) wordt er 1 opgeteld bij i.
                        {
                            // Als het getal wat moet worden geconverteerd kleiner is dan de test waarde voeg dan het bijbehorende getal toe
                            while (textnumeriek >= getal[i])
                            {
                                textnumeriek -= getal[i]; //trek de waarde die op positie [i] staat af van de originele input (textnumeriek)
                                result.Append(romein[i]); //De Romeinse Waarde die hoort bij het zojuist van textnumeriek afgetrokken getal wordt aan de string "result" toegevoegd.
                            }
                        }
    
                        textBox1.Text = result.ToString(); //De waarde van result wordt in de textbox weergegeven.
    that part of the code that is. You could say, nothing wrong here right? But what I want now is to only use 7 Roman Numbers. So that means M, D, C, L, X, V and I. So not IV, IX etc.

    I just can't figure this out and it's really bothering me :/

    So if you guys can help me out here it would be great!
    Last edited by Demoneye; January 12th, 2010 at 05:13 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured