real- und imaginärteil einer komplexen Zahl ermitteln?

BeachBoy

Mitglied
Hallo,

Wie kann ich aus einer komplexen Zahl den realteil und den imaginärteil auslesen?
Kann ich einfach #define z1 3-2i schreiben, um die komplexe Zahl zu definieren?
Wie muss die Funktion aussehen, um an die 3 für real bzw. die -2 für imaginär zu kommen?

Danke,
Sebastian
 
Mhm da wüsste ich jetzt nix.. Du koenntest es aber selber basteln:

Code:
#include <stdio.h>
#include <stdlib.h>

struct complex{
    double real;
    double imag;
};

static complex* create_complex(double real, double imag){
    complex* obj = (complex*)malloc(sizeof(complex));
    if(obj){
        obj->real = real;
        obj->imag = imag;
    }
    return obj;
}

static void destroy_complex(complex* toDestroy){
    if(toDestroy)
        free(toDestroy);
}

void add(complex* res, const complex* a, const complex* b){
    res->real = (a->real + b->real);
    res->imag = (a->imag + b->imag); 

}
     
int main(){
    complex* a = create_complex(5,4);
    complex* b = create_complex(2,3);
    complex* sum = create_complex(2,3);
    add(sum, a, b);
    printf("Die summe aus a und b beträgt: Real: %f, Imag: %f\n", sum->real, sum->imag );
    destroy_complex(a);
    destroy_complex(b);
    destroy_complex(sum);
}

Wenn du noch funktionen wie mult oder div brauchst musst du sie dir
nat. wie die Funktion add auf dem Datentyp selbst definieren...

Gruß

RedWing
 
Hi.

Der neue C Standard unterstützt auch einen Typ für komplexe Zahlen.

Code:
#include <complex.h>
#include <stdio.h>

int main (void) {
  double complex c = 2.1 + .3*I;

  printf ("%f + %f*i\n", creal(c), cimag(c));

  return 0;
}
Gruß
 
wie sieht das aus, wenn ich jetzt als Zahlen für real und imag irgendwas mit (wurzel 7)/3 dort hätte? wie müsste das aussehen?

Sebastian
 
Zurück