C# :: Aufgabe #100

9 Lösungen Lösungen öffentlich

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

Lösungen:

2 Kommentare
vote_ok
von pocki (4190 Punkte) - 06.06.2015 um 16:56 Uhr
Quellcode ausblenden C#-Code
void Main()
{
	Console.Write("Jahreszahl eingeben: ");
	string jahr = Console.ReadLine();
	Console.WriteLine(IntToRome(int.Parse(jahr)));
}

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;
}
vote_ok
von DBqFetti (2480 Punkte) - 07.06.2015 um 12:06 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Römische_Zahlen {
	class Program {
		static void Main(string[] args) {
			Console.Write("Jahr> ");
			Console.Write("Röm.: " + intToRoman(Convert.ToInt32(Console.ReadLine())));
		}

		static string intToRoman(int arabic) {
			if (arabic < 1 || arabic > 4999) return "";
			StringBuilder sb = new StringBuilder();

			Dictionary<char, int> RomanNumerals = new Dictionary<char, int>(){
				{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}
			};
			foreach (KeyValuePair<char, int> Numeral in RomanNumerals)
				while (arabic >= Numeral.Value) {
					arabic -= Numeral.Value;
					sb.Append(Numeral.Key);
				}

			Dictionary<string, string> Replacements = new Dictionary<string, string>(){
				{"DCCCC", "CM"}, {"CCCC", "CD"}, {"LXXXX", "XC"}, {"XXXX", "XL"}, {"VIIII", "IX"}, {"IIII", "IV"}
			};
			foreach (KeyValuePair<string, string> Replacement in Replacements)
				sb.Replace(Replacement.Key, Replacement.Value);

			return sb.ToString();
		}
	}
}

vote_ok
von Mexx (2370 Punkte) - 08.06.2015 um 14:40 Uhr
Quellcode ausblenden C#-Code
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";
        }
    }
}
vote_ok
von niknik (1230 Punkte) - 18.08.2015 um 10:59 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoemischConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            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 zahl;

            do
            {
                Console.WriteLine("Geben Sie die Zahl ein, die konvertiert werden soll.");
            } while (!int.TryParse(Console.ReadLine(), out zahl) || zahl < 1);

            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();

        }
    }
}
vote_ok
von n.rohde (400 Punkte) - 18.08.2015 um 16:35 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JahrAlsRoemischeZahl
{
    class Program
    {
        static void Main(string[] args)
        {
            int jahreszahl;
            string roemischeZahl="";

            Console.Write("Bitte geben Sie eine Jahreszahl ein: ");
            jahreszahl = Convert.ToInt32(Console.ReadLine());
            
            while (jahreszahl != 0)
            {
                if ((jahreszahl-1000) >= 0)
                {
                    roemischeZahl += "M";
                    jahreszahl = jahreszahl - 1000;
                    continue;
                }
                else if ((jahreszahl - 900) >= 0)
                {
                    roemischeZahl += "CM";
                    jahreszahl = jahreszahl - 900;
                    continue;
                }
                else if ((jahreszahl - 500) >= 0)
                {
                    roemischeZahl += "D";
                    jahreszahl = jahreszahl - 500;
                    continue;
                }
                else if ((jahreszahl - 400) >= 0)
                {
                    roemischeZahl += "CD";
                    jahreszahl = jahreszahl - 400;
                    continue;
                }
                else if ((jahreszahl - 100) >= 0)
                {
                    roemischeZahl += "C";
                    jahreszahl = jahreszahl - 100;
                    continue;
                }
                else if ((jahreszahl - 90) >= 0)
                {
                    roemischeZahl += "XC";
                    jahreszahl = jahreszahl - 90;
                    continue;
                }
                else if ((jahreszahl - 50) >= 0)
                {
                    roemischeZahl += "L";
                    jahreszahl = jahreszahl - 50;
                    continue;
                }
                else if ((jahreszahl - 40) >= 0)
                {
                    roemischeZahl += "XL";
                    jahreszahl = jahreszahl - 40;
                    continue;
                }
                else if ((jahreszahl - 10) >= 0)
                {
                    roemischeZahl += "X";
                    jahreszahl = jahreszahl - 10;
                    continue;
                }
                else if ((jahreszahl - 9) >= 0)
                {
                    roemischeZahl += "IX";
                    jahreszahl = jahreszahl - 9;
                    continue;
                }
                else if ((jahreszahl - 5) >= 0)
                {
                    roemischeZahl += "V";
                    jahreszahl = jahreszahl - 5;
                    continue;
                }
                else if ((jahreszahl - 4) >= 0)
                {
                    roemischeZahl += "IV";
                    jahreszahl = jahreszahl - 4;
                    continue;
                }
                else if ((jahreszahl - 1) >= 0)
                {
                    roemischeZahl += "I";
                    jahreszahl = jahreszahl - 1;
                    continue;
                }
            }
            Console.WriteLine(roemischeZahl);
            Console.Read();
        }
    }
}
vote_ok
von Mentalist999 (680 Punkte) - 19.08.2015 um 17:31 Uhr
Erweiterbare Loop. Kürzer und schneller als Pocki's Lösung. *Dance*

Quellcode ausblenden C#-Code
        enum Ronum
        {
            I = 1,
            V = 5,
            X = 10,
            L = 50,
            C = 100,
            D = 500,
            M = 1000,
            IↃ = 500,
            ↀ = 1000,
            ↁ = 5000,
            ↂ = 10000,
            CCCIↃↃↃ = 50000,
            IↃↃↃↃ = 100000,
            CCCCIↃↃↃↃ = 500000,
            CCCIↃↃↃↂↂↂↀↁIↃLXVII = 1000000
        }
        public static string ToRoman(int Value)
        {
            string Result = "";

            foreach (Ronum Ronum in ((IEnumerable<Ronum>)Enum.GetValues(typeof(Ronum))).Reverse<Ronum>())
            {
                Result += (Value / (int)Ronum < 4) 
                            ? String.Concat(Enumerable.Repeat(Ronum.ToString(), Value / (int)Ronum))
                            : (Result != "" && Result[Result.Length - 1].ToString() == ((Ronum)((int)Ronum * 5)).ToString()) 
                                ? "\b" + Ronum.ToString() + ((Ronum)((int)Ronum * 10)).ToString()
                                : Ronum.ToString() + ((Ronum)((int)Ronum * 5)).ToString();

                Value %= (int)Ronum;
            }

            return Result;
        }
vote_ok
von Snuuug (120 Punkte) - 04.09.2015 um 13:42 Uhr
Quellcode ausblenden C#-Code
    /// ///////////////////////////////////////////

    //Römische Ziffer     I V  X   L   C   D   M
    //Wert                1 5  10  50  100 500 1000

    /// ///////////////////////////////////////////
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Bitte Jahr eingeben: ");
            string Jahr = Console.ReadLine();
            Console.WriteLine(ConvvertToRome(Convert.ToInt16(Jahr)));
            Console.ReadKey();

        }

        public static string ConvvertToRome(int jahr)
        {
            string romeYear = string.Empty;
            while (jahr >= 1000)
            {
                jahr -= 1000;
                romeYear += "M";
            }
            if (jahr >= 900)
            {
                jahr -= 900;
                romeYear += "CM";
            }

            while (jahr >= 500)
            {
                jahr -= 500;
                romeYear += "D";
            }
            if (jahr >= 400)
            {
                jahr -= 400;
                romeYear += "CD";
            }

            while (jahr >= 100)
            {
                jahr -= 100;
                romeYear += "C";
            }
            if (jahr >= 90)
            {
                jahr -= 90;
                romeYear += "XC";
            }

            while (jahr >= 50)
            {
                jahr -= 50;
                romeYear += "L";
            }
            if (jahr >= 40)
            {
                jahr -= 40;
                romeYear += "XL";
            }

            while (jahr >= 10)
            {
                jahr -= 10;
                romeYear += "X";
            }
            if (jahr >= 9)
            {
                jahr -= 9;
                romeYear += "IX";
            }

            while (jahr >= 5)
            {
                jahr -= 5;
                romeYear += "V";
            }
            if (jahr >= 4)
            {
                jahr -= 4;
                romeYear += "IV";
            }

            while (jahr >= 1)
            {
                jahr -= 1;
                romeYear += "I";
            }

            return romeYear;
        }
    }
}

vote_ok
von jingyophuong (140 Punkte) - 01.11.2015 um 20:55 Uhr
Quellcode ausblenden C#-Code
static void Main(string[] args)
        {
            //Aufgabe : Umwandlung der arabischen Jahr in der römischen Jahr
            //Jahr eingeben
            do
            {
                string Ziel = "";
                Console.Write("Geben Sie bitte ein Jahr ein: ");
                int aJahr;
                string Jahrstring = Console.ReadLine();
                if (!int.TryParse(Jahrstring, out aJahr) || (aJahr < 0) || (aJahr > 3999))
                {
                    Console.WriteLine("Error! Ein Jahr muss eine positive , kleiner als 4000 Zahl sein");
                }
                else
                {
                
                    Console.Write("\n {0} wird in römischen Zahl umgewandelt. Das Ergebnis ist ", aJahr.ToString());
                    //arabischeZahle und römische Zahle aufgelistet.
                    int[] arabischZahl = {1000, 900,500,400,100,90,50,40,10,9,5,4,1 };
                    string[] roemischeZahl = {"M","CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV","I" };
                    while (aJahr > 0)
                    {
                        for (int i = 0; i < arabischZahl.Length; i++)
                        {
                            if (aJahr >= arabischZahl[i])
                            {
                                Ziel += roemischeZahl[i];
                                aJahr -= arabischZahl[i];
                                i = arabischZahl.Length;
                            }                     
                        }
                    }
                    Console.WriteLine(Ziel);
                    
                }

            }
            while (true);
vote_ok
von hollst (13980 Punkte) - 30.01.2017 um 16:30 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace aufgabe_100  {
    static class Program  {
        static void Main()  {
            //Aufgabenstellung umgekehrt
            Console.Write("Bitte Jahreszahl eingeben (Römische Zahl z. B. MMXVII für 2017): ");
            string input = Console.ReadLine();
            Console.WriteLine(input.RomeToInt_hollst().ToString());
            Console.ReadKey();
        }
    }

    static class myExtensions   {
        public static int RomeToInt_hollst(this String s)   {
            Dictionary<string, int> d = new Dictionary<string, int>() {
                {"M", 1000 }, {"D", 500 }, {"C", 100 }, {"L", 50 }, {"X", 10 }, {"V", 5 }, {"I", 1 }
            };            
            int result = 0, merker = 0;                       
            for (var i = 0; i < s.Length; i++ )  {
                string ss = s[i].ToString().ToUpper();
                if (d.Keys.Contains(ss))  {
                    int value = d[ss];
                    if (merker < value)
                        result -= merker;
                    //  Steht ein weniger wertes Zeichen vor einem höherwertigen, so hat man den Wert des niedriger wertigen Zeichens abzuziehen.
                    //  Gibt sicherlich noch weitere, uneinheitliche Regeln.
                    else
                        result += merker;
                    merker = value;
                }
                else
                    return -1;               
            }
            result += merker;
            return result;
        }
    }
}