C#-code

hallo an alle!
ich schreibe gerade eine c# code für polynome zweiten Grades(ax^2+bx+c).ich möchte für die klasse poly2 Operatormethoden zur Behandlung der operationen aus dem Tester-programm definieren, aber weiss es nicht wie ich das machen kann.
kann bitte jemand mir helfen? hier sieht mein code aus:

class Poly2
{
//datamember
public double a;
public double b;
public double c;
//konstruktoren
public Poly2(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;

}
public Poly2(Poly2 obj)
{
a = obj.a;
b = obj.b;
c = obj.c;
}
public Poly2()
{
this.a = 0;
this.b = 0;
this.c = 0;
}
//operatoren
//public static Poly2 operator +(dou)
//{

//}

}
//tester-programm
class Program
{
static void Main(string[] args)
{
Poly2 u = new Poly2(1.0,2.0,3.0);
Poly2 v = new Poly2(4.0,5.0,6.0);
Poly2 w = new Poly2(u);
Poly2 z = new Poly2();
Console.WriteLine(u);
z = 3.0 * u + v - w;
Console.WriteLine(z);
Console.WriteLine(z);
Console.WriteLine(z);
Console.WriteLine(z.ToString());
Console.WriteLine(z);
}
}
zu ausgabe, möchte ich gerne mit der methode public virtual string ToString() in form: ax^2+bx+c

Danke im voraus

Hallo

ich möchte für die klasse poly2
Operatormethoden zur Behandlung der operationen aus dem
Tester-programm definieren, aber weiss es nicht wie ich das
machen kann.

public static Poly2 operator +(Poly2 lhs, Poly2 rhs)
{
 return (new Poly2(
 lhs.a + rhs.a,
 lhs.b + rhs.b,
 lhs.c + rhs.c));
}


public static Poly2 operator -(Poly2 lhs, Poly2 rhs)
{
 return (new Poly2(
 lhs.a - rhs.a,
 lhs.b - rhs.b,
 lhs.c - rhs.c));
}

public static Poly2 operator \*(double lhs, Poly2 rhs)
{
 return (new Poly2(
 lhs \* rhs.a,
 lhs \* rhs.b,
 lhs \* rhs.c));
}

Das ganze gehört dann natürlich in das innere deiner Klasse Poly2.
Ich denke für den Minus-Operator bekommst du das dann auch selber hin.

Die Ausgabe könnte so funktionieren:

public override string ToString()
{
 StringBuilder str = new StringBuilder();
 str.Append(a);
 str.Append("x^2 + ");
 str.Append(b);
 str.Append("x + ");
 str.Append(c);
 return (str.ToString());
}

Das wäre aber noch zu perfektionieren, z.B. steht jetzt zwischen den einzelnen Potenzen von x immer ein +, auch wenn der folgende Koeffizient eigentlich negativ ist.

Also bei mir funktionierts! :smile:

MfG IGnow

hallo IGnow!
Danke für deine Antwort, das hab geklapt. aber ich habe noch eine problem mit dem Main() funktion und das sieht so aus:

static void Main(string[] args)
{
Poly2 u = new Poly2(1.0,2.0,3.0);
Poly2 v = new Poly2(4.0,5.0,6.0);
Poly2 w = new Poly2(u);
Poly2 z = new Poly2();
Console.WriteLine(u.Eval(5.0));
z = 3.0 * u + v - w;
Console.WriteLine(z.a);
Console.WriteLine(z.b);
Console.WriteLine(z.c);
Console.WriteLine(z.ToString());
Console.WriteLine(z.Eval(5.0));
}
wie kann ich die methode Eval implementieren.
Danke im voraus

Hallo

Ich vermute mal Eval soll dir das Polynom ausrechnen, wobei du als Argument den Wert für x übergibst:

public double Eval(double x)
{
 return (a \* x \* x + b \* x + c);
}

Danke sehr
hat geklappt