C# :: Aufgabe #30 :: Lösung #4
8 Lösungen

#30
Zahlen in Römischer Schreibweise
Anfänger - C#
von pocki
- 29.12.2012 um 19:40 Uhr
Schreibe eine Programm welches eine Ganzzahl einliest und diese anschließend in römischer Schreibweise wieder ausgibt und umgekehrt bei einer eingegebenen Zahl in römischer Schreibweise diese als normale Zahl wieder ausgibt:
Die Erkennung der Schreibweise sollte automatisch funktionieren.
Konsolenausgabe:
Geben Sie eine Zahl ein: 1234
entspricht: MCCXXXIV
Geben Sie eine Zahl ein: DXXXVII
entspricht: 537
Die Erkennung der Schreibweise sollte automatisch funktionieren.
#4

von Sebastian89 (80 Punkte)
- 26.02.2015 um 12:32 Uhr
Komplett dynamische Implementierung, die über das Dictionary frei erweitert werden kann.
Hinweis: Die Methode "Dump" stellt eine Ausgabe im LinqPAD dar und kann mit Console.WriteLine() verglichen werden.
C#-Code
Hinweis: Die Methode "Dump" stellt eine Ausgabe im LinqPAD dar und kann mit Console.WriteLine() verglichen werden.

Dictionary<int, string> values = new Dictionary<int, string>() { { 10000, "ↂ" }, { 5000, "ↁ" }, { 1000, "M" }, { 500, "D" }, { 100, "C" }, { 50, "L" }, { 10, "X" }, { 5, "V" }, { 1, "I" } }; void Main() { string input = Console.ReadLine(); int inputValue = 0; if (int.TryParse(input, out inputValue)) { "Berechnung Zahl -> Römische Zahlschrift.".Dump(); string.Format("Eingabe: {0}", inputValue).Dump(); string output = string.Empty; foreach (KeyValuePair<int, string> pair in values) { while (inputValue >= pair.Key) { CheckingSpecialCaseToRomanNumerals(4, ref output, ref inputValue); CheckingSpecialCaseToRomanNumerals(9, ref output, ref inputValue); if (inputValue >= pair.Key) { output += pair.Value; inputValue -= pair.Key; } } } string.Format("Ergebnis: {0}", output).Dump(); } else { "Berechnung Römische Zahlschrift -> Zahl.".Dump(); string.Format("Eingabe: {0}", input).Dump(); int value = 0; int lastValue = 0; for (int i = 0; i < input.Length; i++) { int tmpValue = values.FirstOrDefault(x => x.Value == input[i].ToString()).Key; if (tmpValue > lastValue && lastValue > 0) { if (CheckingSpecialCaseFromRomanNumerals(10, tmpValue, ref value) || CheckingSpecialCaseFromRomanNumerals(5, tmpValue, ref value)) continue; } value += tmpValue; lastValue = tmpValue; } string.Format("Ergebnis: {0}", value).Dump(); } } public void CheckingSpecialCaseToRomanNumerals(int specialCase, ref string output, ref int inputValue) { int val = 0; for (int i = specialCase; i < inputValue; i *= 10) val = i; int reductionValue = 1 * (val / specialCase); if (inputValue > val && inputValue < val + reductionValue) { if (values.ContainsKey(reductionValue) && values.ContainsKey(val + reductionValue)) { output += values[reductionValue] + values[val + reductionValue]; inputValue -= val; } } if (inputValue > 0 && inputValue % specialCase == 0) { int tempValue = inputValue / specialCase; if (values.ContainsKey(tempValue) && values.ContainsKey(tempValue + inputValue)) { output += values[tempValue] + values[tempValue + inputValue]; inputValue = 0; } } } public bool CheckingSpecialCaseFromRomanNumerals(int specialCase, int keyValue, ref int value) { if (keyValue % specialCase == 0) { int shortValue = keyValue / specialCase; value += shortValue * (specialCase - 1) - shortValue; return true; } return false; }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1