E19 Ex 1 - 5
This commit is contained in:
parent
9021010b8d
commit
4706d2abba
@ -13,9 +13,13 @@ namespace Exercises_C_Sharp.E19_Überladung
|
||||
Output("Hallo");
|
||||
}
|
||||
//Code START
|
||||
static void Output(dynamic d)
|
||||
static void Output(int number)
|
||||
{
|
||||
|
||||
Console.WriteLine("Es handelt sich um einen Integer: " + number);
|
||||
}
|
||||
static void Output(string d)
|
||||
{
|
||||
Console.WriteLine("Es handelt sich um einen String: " + d);
|
||||
}
|
||||
//Code ENDE
|
||||
}
|
||||
|
@ -9,23 +9,22 @@ 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(13);
|
||||
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)
|
||||
{
|
||||
if(DoubleNumber != 0.0)
|
||||
Console.WriteLine("Der Double {0} wurde gesetzt.", DoubleNumber);
|
||||
}
|
||||
//Code ENDE
|
||||
|
@ -17,16 +17,25 @@ namespace Exercises_C_Sharp.E19_Überladung
|
||||
}
|
||||
|
||||
//Code START
|
||||
static void Output(dynamic d)
|
||||
static void Output(int d)
|
||||
{
|
||||
|
||||
Console.WriteLine("Wert: " + d);
|
||||
}
|
||||
static void Output(dynamic d, dynamic d2)
|
||||
static void Output(string d, double d2)
|
||||
{
|
||||
|
||||
Console.WriteLine("Wert 1: " + d);
|
||||
Console.WriteLine("Wert 2: " + d2);
|
||||
}
|
||||
static void Output(dynamic d, dynamic d2, dynamic d3)
|
||||
static void Output(double d2, string d)
|
||||
{
|
||||
Console.WriteLine("Wert 1: " + d);
|
||||
Console.WriteLine("Wert 2: " + d2);
|
||||
}
|
||||
static void Output(int d, string d2, char d3)
|
||||
{
|
||||
Console.WriteLine("Wert 1: " + d);
|
||||
Console.WriteLine("Wert 2: " + d2);
|
||||
Console.WriteLine("Wert 3: " + d3);
|
||||
|
||||
}
|
||||
//Code ENDE
|
||||
|
@ -16,9 +16,31 @@ namespace Exercises_C_Sharp.E19_Überladung
|
||||
}
|
||||
|
||||
//Code START
|
||||
static int ConvertToInt(dynamic d)
|
||||
static int ConvertToInt(char d)
|
||||
{
|
||||
return -1;
|
||||
//Lösung 1:
|
||||
return (int)char.GetNumericValue(d);
|
||||
|
||||
//Lösung 2:
|
||||
return Convert.ToInt32(d.ToString());
|
||||
|
||||
//Lösung 3:
|
||||
return d;
|
||||
}
|
||||
|
||||
static int ConvertToInt(string s)
|
||||
{
|
||||
//Lösung 1:
|
||||
int i = 0;
|
||||
foreach(var element in s)
|
||||
i += element;
|
||||
return i;
|
||||
|
||||
//Lösung 2:
|
||||
if(s == "Eins") return 1;
|
||||
else if(s == "Zwei") return 2;
|
||||
else if(s == "Drei") return 3;
|
||||
//...
|
||||
}
|
||||
//Code ENDE
|
||||
}
|
||||
|
@ -20,15 +20,44 @@ namespace Exercises_C_Sharp.E19_Überladung
|
||||
Analyze('S');
|
||||
Analyze("Teststring");
|
||||
Analyze(390);
|
||||
Analyze(33.0);
|
||||
Analyze(33.00000);
|
||||
Analyze(1209);
|
||||
Analyze('c');
|
||||
}
|
||||
|
||||
//Code START
|
||||
static void Analyze(dynamic d)
|
||||
static void Analyze(double d)
|
||||
{
|
||||
int i = 0;
|
||||
if(d.ToString().IndexOf(",") != -1)
|
||||
i = d.ToString().Length - d.ToString().IndexOf(",") + 1;
|
||||
Console.WriteLine("{0} ist ein Double mit {1} Nachkommastellen.", d, i);
|
||||
|
||||
//Lösung 2:
|
||||
int k = (d - (int)d).ToString().Length - 2;
|
||||
|
||||
//Lösung 3:
|
||||
double dd = (d - (int)d);
|
||||
int counter = 0;
|
||||
while(dd != 0)
|
||||
{
|
||||
dd = (d - (int)d);
|
||||
counter++;
|
||||
dd *= 10;
|
||||
}
|
||||
|
||||
}
|
||||
static void Analyze(char d)
|
||||
{
|
||||
Console.WriteLine("{0} ist ein Char und hat den numerischen Wert {1}", d, (int)d);
|
||||
}
|
||||
static void Analyze(string d)
|
||||
{
|
||||
Console.WriteLine("{0} ist ein String und hat die Länge {1}", d, d.Length);
|
||||
}
|
||||
static void Analyze(int d)
|
||||
{
|
||||
Console.WriteLine("{0} ist ein Integer und hat {1} Ziffern", d, d.ToString().Length);
|
||||
}
|
||||
//Code ENDE
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user