E19 Ex 2 - 5

This commit is contained in:
sebi 2025-03-11 17:02:35 +01:00
parent ce0172a438
commit 1fcd56b9f5
4 changed files with 72 additions and 19 deletions

View File

@ -9,23 +9,21 @@ namespace Exercises_C_Sharp.E19_Überladung
//Schreiben Sie den unteren Code so um, dass die Felder Number und DoubleNumber entfernt werden können. Nutzen Sie dafür die Möglichkeit der Überladung.
//Code START
static int Number = 0;
static double DoubleNumber = 0.0;
public static void Start()
{
Number = 13;
Output();
DoubleNumber = 34.23;
Output();
Output(546);
Output(4);
Output(34.23);
}
static void Output()
static void Output(int Number)
{
if(Number != 0)
Console.WriteLine("Der Integer {0} wurde gesetzt.", Number);
else if(DoubleNumber != 0.0)
}
static void Output(double DoubleNumber)
{
Console.WriteLine("Der Double {0} wurde gesetzt.", DoubleNumber);
}
//Code ENDE

View File

@ -17,17 +17,27 @@ namespace Exercises_C_Sharp.E19_Überladung
}
//Code START
static void Output(dynamic d)
static void Output(int d)
{
Console.WriteLine("DER INTEGER: " + d);
}
static void Output(string d, double d2)
{
Console.WriteLine("DER STRING: " + d);
Console.WriteLine("DER DOUBLE: " + d2);
}
static void Output(dynamic d, dynamic d2)
static void Output(double d2, string d)
{
Console.WriteLine("DER STRING: " + d);
Console.WriteLine("DER DOUBLE: " + d2);
}
static void Output(dynamic d, dynamic d2, dynamic d3)
static void Output(int d, string d2, char d3)
{
Console.WriteLine("DER STRING: " + d2);
Console.WriteLine("DER INTEGER: " + d);
Console.WriteLine("DER CHARACTER: " + d3);
}
//Code ENDE
}

View File

@ -16,9 +16,15 @@ namespace Exercises_C_Sharp.E19_Überladung
}
//Code START
static int ConvertToInt(dynamic d)
static int ConvertToInt(char d)
{
return -1;
return d;
}
static int ConvertToInt(string d)
{
int sum = 0;
foreach(var element in d) sum += element;
return sum;
}
//Code ENDE
}

View File

@ -26,9 +26,48 @@ namespace Exercises_C_Sharp.E19_Überladung
}
//Code START
static void Analyze(dynamic d)
static void Analyze(double d)
{
int amount = 0;
int index = d.ToString().IndexOf(",");
if(index == -1) amount = 0;
else amount = d.ToString().Length - index - 1;
Console.WriteLine("Das übergebene Element ist ein Double. Dieser besitzt {0} Nachkommastellen.", amount);
//ODER:
double res = d - (int)d;
int amount2 = 0;
while(res != 0)
{
amount2++;
res *= 10;
res = res - (int)res;
}
Console.WriteLine("Das übergebene Element ist ein Double. Dieser besitzt {0} Nachkommastellen.", amount2);
}
static void Analyze(char d)
{
Console.WriteLine("Das übergebene Element ist ein Character mit der dezimalen Codierung {0}.", (int)d);
}
static void Analyze(string d)
{
Console.WriteLine("Das übergebene Element ist ein String mit der Länge {0}.", d.Length);
}
static void Analyze(int d)
{
Console.WriteLine("Das übergebene Element ist ein Integer {0} Stellen.", d.ToString().Length);
//ODER:
int amount = 0;
do
{
amount++;
d /= 10;
}while(d > 0);
Console.WriteLine("Das übergebene Element ist ein Integer {0} Stellen.", amount);
}
//Code ENDE
}