-
January 11th, 2010, 04:35 PM
#1
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.
-
January 11th, 2010, 04:56 PM
#2
Re: Question about Roman Number Converter
You could add this inside your for loop.
Code:
for (int i = 0; i < 13; i++)
{
if(i % 2 == 0)
{
// 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.
}
}
}
This will skip every other index in the romein array. % is the modulus operator.
This of course assumes that every other index in your romein array is what you want (starting with index [0]).
Edit: Fixing code.
Last edited by mariocatch; January 11th, 2010 at 04:57 PM.
Reason: fixing code.
-
January 12th, 2010, 10:50 AM
#3
Re: Question about Roman Number Converter
ok, but this gives me an error
-
January 12th, 2010, 11:16 AM
#4
Re: Question about Roman Number Converter
I tested mariocatch 's adjustments and didn't get any error. Could you perhaps post what you have done ¿
-
January 12th, 2010, 01:15 PM
#5
Re: Question about Roman Number Converter
Originally Posted by HanneSThEGreaT
I tested mariocatch 's adjustments and didn't get any error. Could you perhaps post what you have done ¿
When I enter 4 I don't get the Roman Number but that''s because it skips i think... ;/
-
January 12th, 2010, 01:24 PM
#6
Re: Question about Roman Number Converter
Originally Posted by Demoneye
ok, but this gives me an error
wow, what a precision! now it's so easy to tell where the bug is.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
January 12th, 2010, 01:35 PM
#7
Re: Question about Roman Number Converter
Originally Posted by memeloo
wow, what a precision! now it's so easy to tell where the bug is.
I don't know man, I just wanna know the solution to my question..
-
January 12th, 2010, 01:40 PM
#8
Re: Question about Roman Number Converter
I don't know man, I just wanna know the solution to my question..
don't we all
-
January 12th, 2010, 03:39 PM
#9
Re: Question about Roman Number Converter
Originally Posted by mariocatch
don't we all
Seriously, i came here on these forums too get some help and the only thing I get is this one line
if(i % 2 == 0)?!?!?
Come on, I expected more of you guys. I can't do anything with that..
-
January 12th, 2010, 03:43 PM
#10
Re: Question about Roman Number Converter
so? what's the problem? what don't you understand?
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
January 12th, 2010, 05:07 PM
#11
Re: Question about Roman Number Converter
Originally Posted by memeloo
so? what's the problem? what don't you understand?
That line doesn't change anything about the code. I want a Converter can convert numbers to Roman Numbers using only 7 Roman Numbers, no combinations( IV,IX etc..)
So instead of using this:
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" };
It should be like this
Code:
int[] getal = new int[] { 1000, 500, 100, 50, 10, 5, 1 };
string[] romein = new string[] { "M", "D", "C", "L", "X", "V", "I" };
So I thought that by using this code there has to be some mathwork to be done to get IV and IX or any other combination.
When I use this code, if I enter 4 the result will be IIII.
It needs to be IV.
I hope you can help me now, I'm doing my best here :P
-
January 13th, 2010, 01:42 AM
#12
Re: Question about Roman Number Converter
you can also directly get the right item:
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" };
int idx = Array.IndexOf(getal, 4); // the idx will be -1 if not found
string roman = romein[idx];
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
January 15th, 2010, 10:42 AM
#13
Re: Question about Roman Number Converter
I've been trying to solve this and this is the code I have now:
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;
using System.Text.RegularExpressions;
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, 500, 100, 50, 10, 5, 1};
string[] romein = new string[] {"M", "D", "C", "L", "X", "V", "I"};
StringBuilder result = new StringBuilder();
for (int i = 0; i < 7; 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" toegevoeg
}
}
//int teller = 1;
//char romy = 'Z';
//for (int i = 0; i < result.Length; i++)
//{
// if (result[i] == romy)
// {
// teller++;
// if (teller == 4)
// {
// textBox1.Text = "Kan niet!";
// }
// }
// else
// {
// teller = 1;
// }
//}
string tijdelijkeUitkomst = string.Empty;
string tijdelijkeUitkomst2 = string.Empty;
//Voorbeeldje voor alle karakters
for ( int positie = romein.Length-1; positie>=0; positie--)
{
Match match = Regex.Match(result.ToString(), string.Format("[{0}]", romein[positie]));
// MessageBox.Show(string.Format("I kwam {0} maal voor", match.Groups.Count));
int teller = 0;
if (match.Success)
{
for (bool waarde = true; waarde; waarde = match.Success)
{
teller++;
match = match.NextMatch();
}
}
if (teller == 4)
{
tijdelijkeUitkomst += string.Format("{0}", romein[positie], teller) +
string.Format("{0}", romein[positie - 1], teller);
textBox1.Text = tijdelijkeUitkomst;
}
else if (teller == 1)
{
tijdelijkeUitkomst2 = romein[positie] + tijdelijkeUitkomst2;
textBox1.Text = tijdelijkeUitkomst2 ;
}
else if (teller == 2)
{
tijdelijkeUitkomst2 = romein[positie] + romein[positie] + tijdelijkeUitkomst2;
textBox1.Text =tijdelijkeUitkomst2;
}
else if (teller == 3)
{
tijdelijkeUitkomst2 = romein[positie] + romein[positie] + romein[positie] + tijdelijkeUitkomst2;
textBox1.Text = tijdelijkeUitkomst2;
}
}
//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 + "";
}
}
// Rule 4
if (text.Split('V').Length > 2 ||
text.Split('L').Length > 2 ||
text.Split('D').Length > 2)
textBox1.Text = "Kan niet!";
// Rule 1
int count = 1;
char last2 = 'Z';
foreach (char nummer in text)
{
// Valid character?
if ("IVXLCDM".IndexOf(nummer) == -1)
{
textBox1.Text = "NEE";
}
// Duplicate?
if (nummer == last2)
{
count++;
if (count == 4)
{
textBox1.Text = "Kan niet!";
}
}
else
{
count = 1;
last2 = nummer;
}
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
When 4 is entered in the textbox IV will be the result.
But if 9 is entered the result will be just V which isn't right.
And when I enter 3004 you think that the result would be MMMIV or maybe even MMMIIII
but no, It just gives MMM
So I would like to know now how I can get the same sort of code for converting a 4 in IV for the number 9, thats IX. And how to get the 'fours' in other numbers right, for example 24 should be XXIV and 3004 MMMIV etc.
Here's that code about what I'm talking about
Code:
string tijdelijkeUitkomst = string.Empty;
string tijdelijkeUitkomst2 = string.Empty;
//Voorbeeldje voor alle karakters
for ( int positie = romein.Length-1; positie>=0; positie--)
{
Match match = Regex.Match(result.ToString(), string.Format("[{0}]", romein[positie]));
// MessageBox.Show(string.Format("I kwam {0} maal voor", match.Groups.Count));
int teller = 0;
if (match.Success)
{
for (bool waarde = true; waarde; waarde = match.Success)
{
teller++;
match = match.NextMatch();
}
}
if (teller == 4)
{
tijdelijkeUitkomst += string.Format("{0}", romein[positie], teller) +
string.Format("{0}", romein[positie - 1], teller);
textBox1.Text = tijdelijkeUitkomst;
}
else if (teller == 1)
{
tijdelijkeUitkomst2 = romein[positie] + tijdelijkeUitkomst2;
textBox1.Text = tijdelijkeUitkomst2 ;
}
else if (teller == 2)
{
tijdelijkeUitkomst2 = romein[positie] + romein[positie] + tijdelijkeUitkomst2;
textBox1.Text =tijdelijkeUitkomst2;
}
else if (teller == 3)
{
tijdelijkeUitkomst2 = romein[positie] + romein[positie] + romein[positie] + tijdelijkeUitkomst2;
textBox1.Text = tijdelijkeUitkomst2;
}
}
I feel like I'm pretty close now, so I hope someone here knows what to do.
Here's a link to the Project:
Code:
http://rapidshare.com/files/335742029/RomanConv.rar
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|