C# :: Aufgabe #30 :: Lösung #8
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.
#8

von feuerfee (20 Punkte)
- 05.04.2018 um 15:05 Uhr

namespace _030 { class Program { static void Main(string[] args) { int arab; string roem; while (true) { Console.Write("Geben Sie eine Zahl ein: "); var temp = Console.ReadLine(); if (int.TryParse(temp, out arab)) { roem = arabToRoem(arab); Console.WriteLine($"entspricht: {roem}"); } else { arab = roemToArab(temp); Console.WriteLine($"entsricht: {arab}"); } } } private static string arabToRoem(int arab) { string convert = ""; while (arab > 1000) { arab -= 1000; convert += "M"; } while (arab > 500) { arab -= 500; convert += "D"; } while (arab > 100) { arab -= 100; convert += "C"; } while (arab > 50) { arab -= 50; convert += "L"; } while (arab > 10) { arab -= 10; convert += "X"; } while (arab > 5) { arab -= 5; convert += "V"; } if (arab == 5) convert += "V"; else if (arab == 4) convert += "IV"; else if (arab == 3) convert += "III"; else if (arab == 2) convert += "II"; else if (arab == 1) convert += "I"; return convert; } private static int roemToArab(string roem) { char[] chars = roem.ToCharArray(0, roem.Length); int convert = 0; for (int i = 0; i < roem.Length; i++) { if (chars[i].Equals('M') || chars[i].Equals('m')) { convert += 1000; } else if (chars[i].Equals('D') || chars[i].Equals('d')) { convert += 500; } else if (chars[i].Equals('C') || chars[i].Equals('c')) { convert += 100; } else if (chars[i].Equals('L') || chars[i].Equals('l')) { convert += 50; } else if (chars[i].Equals('X') || chars[i].Equals('x')) { convert += 10; } else if (chars[i].Equals('V') || chars[i].Equals('v')) { if (i+1 < roem.Length && (chars[i + 1].Equals('I') || chars[i + 1].Equals('i'))) { if (i+2 < roem.Length && (chars[i + 2].Equals('I') || chars[i + 2].Equals('i'))) { if (i+3 < roem.Length && (chars[i + 3].Equals('I') || chars[i + 3].Equals('i'))) { convert += 8; i += 3; } else { convert += 7; i += 2; } } else { convert += 6; i += 1; } } else { convert += 5; } } else if (chars[i].Equals('I') || chars[i].Equals('i')) { if (i+1 < roem.Length && (chars[i + 1].Equals('V') || chars[i + 1].Equals('v'))) { convert += 4; i++; } else if (i+1 < roem.Length && (chars[i + 1].Equals('I') || chars[i + 1].Equals('i'))) { if (i+2 < roem.Length && (chars[i + 2].Equals('I') || chars[i + 2].Equals('i'))) { convert += 3; i += 2; } else { convert += 2; i += 2; } } } } return convert; } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1