ich habe folgendes Programm geschrieben um mit Komplexen Zahlen zu rechnen. Das Programm soll allerdings jetzt so umgeschrieben werden das die Berechnung eine neue Komplexe Zahl zurückgibt mit der im Hauptprogramm weiter gearbeitet werden kann. Habt ihr da evtl. gute Vorschläge zur Umsetzung?
#include <stdio.h> #include <string.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> class complex{ public: complex(double r, double i) {re=r; im=i;} void print(){ printf("Die komplexe Zahl lautet: %.1lf + %.1lfi\n",re,im); } void add(complex z2){ re2 = z2.re + re; im2 = z2.im + im; if (im2 < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die addierte komplexe Zahl lautet: %.1lf %c %.1lfi\n",re2,op,im2); } void sub(complex z2){ re2 = re - z2.re; im2 = im - z2.im; if (im2 < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die subtrahierte komplexe Zahl lautet: %.1lf %c %.1lfi\n",re2,op,im2); } void mult(complex z2){ re2 = re * z2.re - im * z2.im; im2 = (re * z2.im + im * z2.re); if (im2 < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die multiplizierte komplexe Zahl lautet: %.1lf %c %.1lfi\n",re2,op,im2); } void div(complex z2){ re2z = ((re * z2.re) + (im * z2.im)); re2n = (z2.re * z2.re) + (z2.im * z2.im); im2z = (re*z2.im)+(z2.re*im); im2n = (z2.re * z2.re) + (z2.im * z2.im); if (im2z < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die dividierte komplexe Zahl lautet:\n"); printf("%.1lf %c %.1lfi\n",re2z,op,im2z); printf("____________\n"); printf("%.1lf %c %.1lfi\n",re2n,op,im2n); } void quad(){ re2 = re * re - im * im; im2 = 2 * re * im; if (im2 < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die quadrierte komplexe Zahl lautet: %.1lf %c %.1lfi\n",re2,op,im2); } void konj(){ re2 = re; im2 = im * (-1); if (im2 < 0){ im2 *= -1; op = '-'; } else op = '+'; printf("Die konjugiert komplexe Zahl lautet: %.1lf %c %.1lfi\n",re2,op,im2); } private: double re, im; double re2, im2; double re2z, re2n, im2z, im2n; char op; }; main(){ complex z1(3.0,7.0); z1.print(); complex z2(5.0,-2.0); z2.print(); z1.add(z2); z1.sub(z2); z1.mult(z2); z1.div(z2); z1.quad(); z2.quad(); z1.konj(); }
Kommentar