C# :: Aufgabe #100 :: Lösung #3
9 Lösungen

#100
Jahreszahlenkonverter für römische Schreibweise
Anfänger - C#
von BlackBird321
- 04.06.2015 um 22:36 Uhr
Bitte schreibe ein Programm, welches eine einzugebende Jahreszahl in eine römische Schreibweise umwandelt.
Beispiel:
1995 = MCMXCV
2015 = MMXV
Beispiel:
1995 = MCMXCV
2015 = MMXV
#3

von Mexx (2370 Punkte)
- 08.06.2015 um 14:40 Uhr

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JahreszahlNachRoemisch { class Program { static void Main(string[] args) { Console.WriteLine("Geben Sie die umzurechnende Jahreszahl ein"); int zahl = 0; do { if (int.TryParse(Console.ReadLine(), out zahl)) break; else { zahl = 0; Console.WriteLine("Sie müssen eine Ganzzahl zwischen 1 und 3999 angeben"); } } while (zahl == 0); Console.WriteLine("{0} = {1}", zahl, UmrechnenRömisch(zahl)); Console.ReadKey(); } private static string UmrechnenRömisch(int dec) { if (dec > 3999) return "Die übergebene Zahl war zu groß \nMaximale Zahlengröße ist 3999 \n"; Zahlen römZahl = new Zahlen(); string zahl = string.Empty; try { while (dec > 0) { if (dec >= 1000) { zahl += römZahl.tausend; dec -= 1000; continue; } if (dec >= 900) { zahl += römZahl.hundert; zahl += römZahl.tausend; dec -= 900; continue; } if (dec >= 500) { zahl += römZahl.fünfhundert; dec -= 500; continue; } if (dec >= 400) { zahl += römZahl.hundert; zahl += römZahl.fünfhundert; dec -= 400; continue; } if (dec >= 100) { zahl += römZahl.hundert; dec -= 100; continue; } if (dec >= 90) { zahl += römZahl.zehn; zahl += römZahl.hundert; dec -= 90; continue; } if (dec >= 50) { zahl += römZahl.fünfzig; dec -= 50; continue; } if (dec >= 40) { zahl += römZahl.zehn; zahl += römZahl.fünfzig; dec -= 40; continue; } if (dec >= 10) { zahl += römZahl.zehn; dec -= 10; continue; } if (dec >= 9) { zahl += römZahl.eins; zahl += römZahl.zehn; dec -= 9; continue; } if (dec >= 5) { zahl += römZahl.fünf; dec -= 5; continue; } if (dec >= 4) { zahl += römZahl.eins; zahl += römZahl.fünf; dec -= 4; continue; } if (dec >= 1) { zahl += römZahl.eins; dec -= 1; continue; } } } catch { return "Fehler: Haben Sie eine Korrekte Dezimalzahl übergeben?"; } return zahl; } class Zahlen { public Dictionary<char, int> zahl; public Zahlen() { zahl = new Dictionary<char, int>(); zahl.Add('I', 1); zahl.Add('V', 5); zahl.Add('X', 10); zahl.Add('L', 50); zahl.Add('C', 100); zahl.Add('D', 500); zahl.Add('M', 1000); } public string eins = "I"; public string fünf = "V"; public string zehn = "X"; public string fünfzig = "L"; public string hundert = "C"; public string fünfhundert = "D"; public string tausend = "M"; } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1