#include #include //#include "komplexe_zahl.hpp" using namespace std; class KomplexeZahl { private: double r; double i; public: // Konstruktoren KomplexeZahl(); KomplexeZahl(double, double); //Elementfunktionen void ausgabe(); //Operatorfunktionen KomplexeZahl operator + (KomplexeZahl); KomplexeZahl operator - (KomplexeZahl); KomplexeZahl operator * (KomplexeZahl); KomplexeZahl operator / (KomplexeZahl); KomplexeZahl operator = (KomplexeZahl); }; KomplexeZahl::KomplexeZahl() { r = 0; i = 0; }; KomplexeZahl::KomplexeZahl(double a, double b) { r = a; i = b; }; void KomplexeZahl::ausgabe() { cout << r; if (i >= 0) cout << "+"; cout << i << "j"; }; // Rueckgabetyp Klasse Summand KomplexeZahl KomplexeZahl::operator + (KomplexeZahl z) { //Hilfsvariable erg ist eine komplexe Zahl //erg ist hier das Ergebnis der Addition KomplexeZahl erg(r + z.r, i + z.i); return erg; }; KomplexeZahl KomplexeZahl::operator - (KomplexeZahl z) { KomplexeZahl erg(r - z.r, i - z.i); return erg; }; // Rueckgabetyp Klasse Faktor KomplexeZahl KomplexeZahl::operator * (KomplexeZahl z) { //Hilfsvariable erg ist eine komplexe Zahl //erg ist das Ergebnis der Multiplikation KomplexeZahl erg(r * z.r - i * z.i, r * z.i + i * z.r); return erg; }; KomplexeZahl KomplexeZahl::operator / (KomplexeZahl z) { KomplexeZahl erg((r*z.r + z.i*i)/(z.r*z.r + z.i*z.i), (i*z.r - r*z.i)/(z.r*z.r + z.i*z.i)); return erg; }; KomplexeZahl KomplexeZahl::operator = (const KomplexeZahl &z) { r = z.r; i = z.i; }; int main(int argc, char *argv[]) { KomplexeZahl z1(0.0, 1.0), z2(2.0, -3.0), z(0.0, 0.0); cout << "Fuer die komplexen Zahlen"; cout << "\n"; cout << "\n\t z1: "; z1.ausgabe(); cout << "\n\t z2: "; z2.ausgabe(); cout << "\n"; cout << "\n erhalten wir:"; cout << "\n"; cout << "\n\t z1 + z2: "; (z1 + z2).ausgabe(); cout << "\n\t z1 * z2: "; (z1 * z2).ausgabe(); cout << "\n\t z1 - z2: "; (z1 - z2).ausgabe(); cout << "\n\t z1 / z2: "; (z1 / z2).ausgabe(); cout << "\n"; cout << "\n Berechnug ueber den Zuweisungsoperator (=): "; cout << "\n"; cout << "\n\t z = (z1 + z2): "; z = (z1 + z2); z.ausgabe(); cout << "\n\t z = (z1 * z2): "; z = (z1 * z2); z.ausgabe(); cout << "\n\t z = (z1 - z2): "; z = (z1 - z2); z.ausgabe(); cout << "\n\t z = (z1 / z2): "; z = (z1 / z2); z.ausgabe(); cout << "\n"; system("PAUSE"); return EXIT_SUCCESS; return 0; };