C# :: Aufgabe #30
8 Lösungen

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.
Lösungen:
Hier meine Lösung:
C#-Code

void main() { Console.Write("Geben Sie eine Zahl ein: "); string input = Console.ReadLine(); string output = string.Empty; int zahl = 0; if (int.TryParse(input, out zahl)) { output = zahl.IntToRome(); } else { output = input.RomeToInt().ToString(); } Console.WriteLine("entspricht: {0}", output); } public static string IntToRome(this int value) { if ((value < 1) || (value >= Int32.MaxValue)) { return ""; } string res = ""; while (value >= 1000) { value -= 1000; res += "M"; } if (value >= 900) { value -= 900; res += "CM"; } while (value >= 500) { value -= 500; res += "D"; } if (value >= 400) { value -= 400; res += "CD"; } while (value >= 100) { value -= 100; res += "C"; } if (value >= 90) { value -= 90; res += "XC"; } while (value >= 50) { value -= 50; res += "L"; } if (value >= 40) { value -= 40; res += "XL"; } while (value >= 10) { value -= 10; res += "X"; } if (value >= 9) { value -= 9; res += "IX"; } while (value >= 5) { value -= 5; res += "V"; } if (value >= 4) { value -= 4; res += "IV"; } while (value >= 1) { value -= 1; res += "I"; } return res; } public static int RomeToInt(this string value) { if (String.IsNullOrEmpty(value)) { return 0; } int res = 0; while (value.StartsWith("M")) { res += 1000; value = value.Substring(1); } if (value.StartsWith("CM")) { res += 900; value = value.Substring(2); } while (value.StartsWith("D")) { res += 500; value = value.Substring(1); } if (value.StartsWith("CD")) { res += 400; value = value.Substring(2); } while (value.StartsWith("C")) { res += 100; value = value.Substring(1); } if (value.StartsWith("XC")) { res += 90; value = value.Substring(2); } while (value.StartsWith("L")) { res += 50; value = value.Substring(1); } if (value.StartsWith("XL")) { res += 40; value = value.Substring(2); } while (value.StartsWith("X")) { res += 10; value = value.Substring(1); } if (value.StartsWith("IX")) { res += 9; value = value.Substring(2); } while (value.StartsWith("V")) { res += 5; value = value.Substring(1); } if (value.StartsWith("IV")) { res += 4; value = value.Substring(2); } while (value.StartsWith("I")) { res += 1; value = value.Substring(1); } return res; }

using System; namespace RoemischeZahlen { class Program { static void Main(string[] args) { int temp = 0; string rZahl = ""; string ergebnis = ""; System.Console.Write("Geben Sie eine Zahl ein(Maximal: 3999): "); rZahl = System.Console.ReadLine(); if (Int32.TryParse(rZahl,out temp)) { switch (rZahl.Length) { case 4: switch (rZahl[0]) { case '3': ergebnis += "M"; goto case '2'; case '2': ergebnis += "M"; goto case '1'; case '1': ergebnis += "M"; goto default; default: rZahl = rZahl.Remove(0, 1); break; } goto case 3; case 3: temp = int.Parse(rZahl[0].ToString()); if (temp == 9) { ergebnis += "CM"; } else if (temp >= 5) { ergebnis += "D"; temp -= 5; } switch (temp) { case 4: ergebnis += "CD"; break; case 3: ergebnis += "C"; goto case 2; case 2: ergebnis += "C"; goto case 1; case 1: ergebnis += "C"; goto default; default: rZahl = rZahl.Remove(0, 1); break; } goto case 2; case 2: temp = int.Parse(rZahl[0].ToString()); if (temp == 9) { ergebnis += "XC"; } else if (temp >= 5) { ergebnis += "L"; temp -= 5; } switch (temp) { case 4: ergebnis += "XL"; break; case 3: ergebnis += "X"; goto case 2; case 2: ergebnis += "X"; goto case 1; case 1: ergebnis += "X"; goto default; default: rZahl = rZahl.Remove(0, 1); break; } goto case 1; case 1: temp = int.Parse(rZahl[0].ToString()); if (temp == 9) { ergebnis += "IX"; } else if (temp >= 5) { ergebnis += "V"; temp -= 5; } switch (temp) { case 4: ergebnis += "IV"; break; case 3: ergebnis += "I"; goto case 2; case 2: ergebnis += "I"; goto case 1; case 1: ergebnis += "I"; goto default; default: rZahl = rZahl.Remove(0, 1); break; } break; } } else { if (rZahl.Contains("CD")) { temp += 400; rZahl = rZahl.Remove(rZahl.IndexOf("CD", 2)); } if (rZahl.Contains("CM")) { temp += 900; rZahl = rZahl.Remove(rZahl.IndexOf("CM", 2)); } if (rZahl.Contains("XC")) { temp += 90; rZahl = rZahl.Remove(rZahl.IndexOf("XC", 2)); } if (rZahl.Contains("XL")) { temp += 40; rZahl = rZahl.Remove(rZahl.IndexOf("XL", 2)); } if (rZahl.Contains("IX")) { temp += 9; rZahl = rZahl.Remove(rZahl.IndexOf("IX", 2)); } if (rZahl.Contains("IV")) { temp += 4; rZahl = rZahl.Remove(rZahl.IndexOf("IV", 2)); } foreach(char i in rZahl) { if(i.Equals('I')) temp++; if(i.Equals('V')) temp += 5; if(i.Equals('X')) temp += 10; if(i.Equals('L')) temp += 50; if(i.Equals('C')) temp += 100; if(i.Equals('D')) temp += 500; if(i.Equals('M')) temp += 1000; } ergebnis = temp.ToString(); } Console.WriteLine("entspricht: {0}", ergebnis); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RömischeZahlen { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("Geben Sie eine Zahl ein"); string input = Console.ReadLine().Trim(); int dec; if (int.TryParse(input, out dec)) Console.WriteLine("\nentspricht: " + UmrechnenRömisch(dec)); else { dec = UmrechnenDezimal(input.ToUpper()); if (dec > 0) Console.WriteLine("\nentspricht: " + dec); else Console.WriteLine("\nFehler: Haben Sie eine korrekte römische Zahl kleiner 4000 eingegeben?"); } Console.WriteLine(); } } 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; } private static int UmrechnenDezimal(string röm) { Regex rex = new Regex("^[CDILMVX]+$"); if (!rex.IsMatch(röm)) return 0; int zahl = 0; Zahlen intZahl = new Zahlen(); int temp, temp2; try { for (int i = 0; i < röm.Length; i++) { intZahl.zahl.TryGetValue(röm[i], out temp); if (i < röm.Length - 1) intZahl.zahl.TryGetValue(röm[i + 1], out temp2); else temp2 = 0; if (temp < temp2) { zahl += temp2 - temp; i++; } else zahl += temp; } } catch { return 0; } 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"; } } }
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; }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace RoemischConverter { public class RomanNumber { public string Numeral { get; set; } public int Value { get; set; } public int Hierarchy { get; set; } } class Program { static void Main(string[] args) { anfang: Console.Clear(); Console.WriteLine("Geben Sie die Zahl ein, die konvertiert werden soll."); string eingabe = Console.ReadLine(); int zahl; if (int.TryParse(eingabe, out zahl)) { int[] zahlen = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; string[] roemisch = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; int temp1 = zahl; string ausgabe = ""; while (temp1 > 0) { int zahlenzaehler = 0, temp2 = 0; for (int i = 0; i < zahlen.Length; i++) { if (zahlen[i] > temp1) { zahlenzaehler = zahlen[i - 1]; temp2 = i - 1; break; } zahlenzaehler = zahlen[i]; temp2 = i; } ausgabe += roemisch[temp2]; temp1 -= zahlenzaehler; } Console.WriteLine("Die Zahl {0} auf römisch: {1}", zahl, ausgabe); Console.ReadLine(); } else { Regex roman = new Regex("^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$"); if (roman.IsMatch(eingabe)) { List<RomanNumber> RomanNumbers = new List<RomanNumber> { new RomanNumber {Numeral = "M", Value = 1000, Hierarchy = 4}, //{"CM", 900}, new RomanNumber {Numeral = "D", Value = 500, Hierarchy = 4}, //{"CD", 400}, new RomanNumber {Numeral = "C", Value = 100, Hierarchy = 3}, //{"XC", 90}, new RomanNumber {Numeral = "L", Value = 50, Hierarchy = 3}, //{"XL", 40}, new RomanNumber {Numeral = "X", Value = 10, Hierarchy = 2}, //{"IX", 9}, new RomanNumber {Numeral = "V", Value = 5, Hierarchy = 2}, //{"IV", 4}, new RomanNumber {Numeral = "I", Value = 1, Hierarchy = 1} }; var total = 0; for (var i = 0; i < eingabe.Length; i++) { // get current value var current = eingabe[i].ToString(); var curRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == current.ToUpper()); // last number just add the value and exit if (i + 1 == eingabe.Length) { total += curRomanNum.Value; break; } // check for exceptions IV, IX, XL, XC etc var next = eingabe[i + 1].ToString(); var nextRomanNum = RomanNumbers.First(rn => rn.Numeral.ToUpper() == next.ToUpper()); // exception found if (curRomanNum.Hierarchy == (nextRomanNum.Hierarchy - 1)) { total += nextRomanNum.Value - curRomanNum.Value; i++; } else { total += curRomanNum.Value; } } Console.WriteLine("In der Römisch Konvertierung"); Console.WriteLine("Die Zahl {0} als Arabische Zahl: {1}", eingabe, total); Console.ReadLine(); } else { Console.WriteLine("Fehlerhafte Eingabe."); Console.ReadLine(); goto anfang; } } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RömischeZahlenConverter { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("Römische(1) oder Arabische(2) Zahl? Für alle Zahlen (3)"); string Wert = Console.ReadLine(); Console.WriteLine("Wie lautet der Wert?"); switch (Wert) { case "1": { Wert = Console.ReadLine(); Console.WriteLine("Das Ergebnis lautet: {0}\n", RomToArab.Ausgabe(Wert)); Console.ReadLine(); Console.Clear(); break; } case "2": { Wert = Console.ReadLine(); Console.WriteLine("Das Ergebnis lautet: {0}\n", ArabToRom.Ausgabe(Wert)); Console.ReadLine(); Console.Clear(); break; } case "3": { Action.Go(Wert); Console.ReadLine(); Console.Clear(); break; } default: { Console.Clear(); break; } } } } class RomToArab { //Berechnungsalgorithmus public static int Ausgabe(string GivenValue) { GivenValue = GivenValue.ToUpper(); int GivenValueLength = GivenValue.Length; if (CheckIfEquals(GivenValue)) { string LetterNow; string LetterNext; int ergebnis = 0, loop = 0; while (loop != GivenValueLength) { LetterNow = Convert.ToString(GivenValue[loop]); LetterNext = Convert.ToString(GivenValue[loop]); if (loop + 1 != GivenValueLength) { LetterNext = Convert.ToString(GivenValue[loop + 1]); } if (CheckIfSubstract(LetterNow, LetterNext) && loop + 1 != GivenValueLength) { ergebnis += Substract(GivenValue, GivenValueLength, LetterNow, LetterNext); LetterNow = Convert.ToString(GivenValue[loop]); LetterNext = Convert.ToString(GivenValue[loop + 1]); loop += 2; } else { ergebnis += Addition(GivenValue, GivenValueLength, LetterNow); LetterNow = Convert.ToString(GivenValue[loop]); loop += 1; } } if (ergebnis >= 4000) { Console.WriteLine("Fehler!"); ergebnis = 0; Convert.ToBoolean(ergebnis); return ergebnis; } return ergebnis; } Console.WriteLine("Fehler!"); return 0; } //Überprüfen ob Subtraktion public static bool CheckIfSubstract(string LetterNow, string LetterNext) { return "IV,IX,XL,XC,CD,CM".Contains(LetterNow + LetterNext); } //Subtraktion public static int Substract(string GivenValue, int GivenValueLength, string LetterNow, string LetterNext) { int ergebnis = 0; ergebnis += (RoemZahlenGiver(LetterNext) - (RoemZahlenGiver(LetterNow))); return ergebnis; } //Addition public static int Addition(string GivenValue, int GivenValueLength, string LetterNow) { int ergebnis = 0; ergebnis += (RoemZahlenGiver(LetterNow)); return ergebnis; } //Überprüfen ob Grundzahl public static bool IsGrundzahl(string LetterNow) { return "IXCM".Contains(LetterNow); } //Überprüfen ob Nebenzahl public static bool IsNebenZahl(string LetterNow) { return !IsGrundzahl(LetterNow); } //Zahlen"Tabelle" public static int RoemZahlenGiver(string Buchstabe) { switch (Buchstabe) { case "I": return 1; case "V": return 5; case "X": return 10; case "L": return 50; case "C": return 100; case "D": return 500; case "M": return 1000; default: return 0; } } //Überprüfungsalgorithmus public static bool CheckIfEquals(string GivenValue) { GivenValue = GivenValue.ToUpper(); int GivenValueLength = GivenValue.Length; for (int x = 0; x < GivenValueLength; x++) { string LetterNow = Convert.ToString(GivenValue[x]); if (!CheckIfEqualsLetters(LetterNow)) return false; } if (!CheckIfNotMutliple(GivenValue, GivenValueLength)) { return false; } return IsNoDouble(GivenValue); } //Überprüfen ob 4x aufeinanderfolgend public static bool CheckIfNotMutliple(string GivenValue, int GivenValueLength) { if (GivenValueLength >= 4) { int ValueValue = 0; while (ValueValue != GivenValueLength - 3) { if (GivenValue[ValueValue] == GivenValue[ValueValue + 1] && GivenValue[ValueValue + 1] == GivenValue[ValueValue + 2] && GivenValue[ValueValue + 2] == GivenValue[ValueValue + 3]) { return false; } ValueValue++; } } return true; } //Überprüfen ob nur römische Zahlenzeichen public static bool CheckIfEqualsLetters(string LetterNow) { { if (!char.IsLetter(LetterNow, 0)) return false; } string[] ArrayRoemische = new string[7] { "I", "V", "X", "L", "C", "D", "M" }; for (int x = 0; x < 7; x++) { if (LetterNow == ArrayRoemische[x]) //! = operand error ?? return true; } return false; } //Überprüfen ob 2 oder mehr zwischenzahlen nebeneinander public static bool IsNoDouble(string GivenValue) { if (GivenValue.Contains("VV")) return false; if (GivenValue.Contains("LL")) return false; if (GivenValue.Contains("DD")) return false; return true; } } class ArabToRom { public static string Ausgabe(string Eingabe) { int Zwischenergebnis = Convert.ToInt32(Eingabe); string Buchstaben = ""; while (Zwischenergebnis != 0) { Buchstaben += BuchstabeEinzeln(Convert.ToInt32(Zwischenergebnis)); Zwischenergebnis -= Ausrechnen(Convert.ToInt32(Zwischenergebnis)); } Buchstaben = Umwandlung(Buchstaben); return Buchstaben; } public static string Umwandlung(string s) { s = s.Replace("CCCC", "CD").Replace("XXXX", "XL").Replace("IIII", "IV"); s = s.Replace("VIV", "IX").Replace("LXL", "XC").Replace("DCD", "CM"); return s; } public static string BuchstabeEinzeln(int Eingabe) { if ((Eingabe / 1000) >= 1) return "M"; else if ((Eingabe / 500) >= 1) return "D"; else if ((Eingabe / 100) >= 1) return "C"; else if ((Eingabe / 50) >= 1) return "L"; else if ((Eingabe / 10) >= 1) return "X"; else if ((Eingabe / 5) >= 1) return "V"; else if ((Eingabe / 1) >= 1) return "I"; else return ""; } public static int Ausrechnen(int Eingabe) { if ((Eingabe / 1000) >= 1) return 1000; else if ((Eingabe / 500) >= 1) return 500; else if ((Eingabe / 100) >= 1) return 100; else if ((Eingabe / 50) >= 1) return 50; else if ((Eingabe / 10) >= 1) return 10; else if ((Eingabe / 5) >= 1) return 5; else if ((Eingabe / 1) >= 1) return 1; else return 0; } } class Action { public static void Go(string Wert) { Console.Clear(); int int1 = 0; for (int i = 0; i < 4000; i++) { int1 = Convert.ToInt32(Wert); int1++; Wert = Convert.ToString(int1); Console.WriteLine("{0}".PadRight(5) + "{1}".PadLeft(5), i, ArabToRom.Ausgabe(Wert)); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer30 { class Program { static void Main(string[] args) { Console.Write("Geben Sie eine Zahl ein: "); string zahl = Console.ReadLine(); int wert = 0, zahlInt, ausgabe = 0; if (Int32.TryParse(zahl, out zahlInt)) { for (int i = 0; i <= zahl.Length-1; i++) { switch (zahl[i]) { case '1': wert = 1; break; case '2': wert = 2; break; case '3': wert = 3; break; case '4': wert = 4; break; case '5': wert = 5; break; case '6': wert = 6; break; case '7': wert = 7; break; case '8': wert = 8; break; case '9': wert = 9; break; default: break; } switch (zahl.Length - i) { case 1: if (wert <= 3) { for (int l = 1; l <= wert; l++) { Console.Write("I"); } } else if (wert == 4) { Console.WriteLine("IV"); } else if (wert == 5) { Console.Write("V"); } else if (wert <= 8) { Console.Write("X"); for (int l = 1; l <= wert-5; l++) { Console.Write("I"); } } else { Console.Write("IX"); } break; case 2: if (wert <= 4) { for (int l = 1; l <= wert; l++) { Console.Write("X"); } } else if (wert == 5) { Console.Write("L"); } else { Console.Write("L"); for (int l = 1; l <= wert-5; l++) { Console.Write("X"); } } break; case 3: if (wert <= 4) { for (int l = 1; l <= wert; l++) { Console.Write("C"); } } else if (wert == 5) { Console.Write("D"); } else { Console.Write("D"); for (int l = 1; l <= wert - 5; l++) { Console.Write("C"); } } break; case 4: for (int l = 1; l <= wert; l++) { Console.Write("M"); } break; default: break; } } } else { for (int i = 0; i <= zahl.Length-1; i++) { switch (zahl[i]) { case 'I': ausgabe++; break; case 'V': ausgabe += 5; break; case 'X': ausgabe += 10; break; case 'L': ausgabe += 50; break; case 'C': ausgabe += 100; break; case 'D': ausgabe += 500; break; case 'M': ausgabe += 1000; break; } } Console.WriteLine(ausgabe); } Console.ReadKey(); } } }

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; } } }