Commit 05030622 authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

2020-04-21

parent 43665896
......@@ -4,7 +4,7 @@ namespace _01OperatorOverloading
{
class Bruch
{
int z, n;
private int z, n;
public Bruch(int Z, int N = 1)
{
z = Z;
......@@ -12,8 +12,39 @@ namespace _01OperatorOverloading
throw new ArgumentNullException("Nenner darf nicht 0 sein!");
n = N;
}
//public static explicit operator int(Bruch b1) => 1;
public static implicit operator int(Bruch b1) => 1;
public Bruch(string t)
{
string[] d = t.Split('/');
z = Convert.ToInt32(d[0]);
n = Convert.ToInt32(d[1]);
}
public static Bruch Mult1(Bruch b1, Bruch b2)
{
int zaehler = b1.z * b2.z;
int nenner = b1.n * b2.n;
//b1.z = zaehler;
//b1.n = nenner;
Bruch erg = new Bruch(zaehler, nenner);
return erg;
}
public static Bruch Mult2(Bruch b1, Bruch b2)
{
return new Bruch(b1.z * b2.z, b1.n * b2.n);
}
public static Bruch Mult(Bruch b1, Bruch b2)
=> new Bruch(b1.z * b2.z, b1.n * b2.n);
public static Bruch operator *(Bruch b1, Bruch b2)
=> new Bruch(b1.z * b2.z, b1.n * b2.n);
public static explicit operator int(Bruch b)
=> b.z / b.n;
public static implicit operator double(Bruch b)
=> (double)b.z / (double)b.n;
public static implicit operator Bruch(int zahl)
=> new Bruch(zahl);
}
......@@ -21,8 +52,25 @@ namespace _01OperatorOverloading
{
static void Main(string[] args)
{
Bruch b1 = new Bruch(1, 3);
int k = b1;
Bruch b1 = new Bruch(2, 3);
Bruch b2 = new Bruch(2);
Bruch b3 = new Bruch("3/4");
// Bruch e1 = 3.Mult(b1);
Bruch e2 = Bruch.Mult(b1, b2);
Bruch e3 = b1 * b2;
Bruch e4 = b1 * 2;
Bruch e5 = 2 * b1;
// 2 --> impl.Konv. in einen Bruch
// op* (Bruch, Bruch)
int k = (int)e3; // explizite Konvertierung
double d = e3; // implizite Konvertierung
Console.WriteLine(k);
Console.WriteLine(d);
double d2 = 34;
int k2 = (int)3.5;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment