a. Realizati o implementare in C a unui tip abstract de date pentru numere complexe. Implementarea va
contine urmatoarele operatii: construirea unui numar complex din partea reala si partea imaginara, suma a
doua numere complexe, diferenta a doua numere complexe, conjugatul unui numar complex, produsul a
doua numere complexe, modulul unui numar complex.
Răspunsuri la întrebare
#include <stdio.h>
#include <math.h>
struct NumarComplex {
float re;
float im;
};
struct NumarComplex construire_fun(float Real, float Imaginar){
struct NumarComplex x;
x.re = Real;
x.im = Imaginar;
return x;
}
struct NumarComplex suma_fun(const struct NumarComplex op1, const struct NumarComplex op2){
struct NumarComplex x;
x.re = op1.re + op2.re;
x.im = op1.im + op2.im;
return x;
}
struct NumarComplex diferenta_fun(const struct NumarComplex op1, const struct NumarComplex op2){
struct NumarComplex x;
x.re = op1.re - op2.re;
x.im = op1.im - op2.im;
return x;
}
struct NumarComplex conjugat_fun(const struct NumarComplex op1){
struct NumarComplex x;
x.re = op1.re;
x.im = -op1.im;
return x;
}
struct NumarComplex produs_fun(const struct NumarComplex op1, const struct NumarComplex op2){
struct NumarComplex x;
x.re = op1.re * op2.re - op1.im * op2.im;
x.im = op1.im * op2.re + op1.re * op2.im;
return x;
}
float modul_fun(const struct NumarComplex op){
return (float) sqrt(op.re * op.re + op.im * op.im);
}
struct Op_Numere_Complexe {
struct NumarComplex (*construire)(float Real, float Imaginar);
struct NumarComplex (*suma) (const struct NumarComplex op1, const struct NumarComplex op2);
struct NumarComplex (*diferenta) (const struct NumarComplex op1, const struct NumarComplex op2);
struct NumarComplex (*conjugat)(const struct NumarComplex op);
struct NumarComplex (*produs)(const struct NumarComplex op1, const struct NumarComplex op2);
float (*modul)(const struct NumarComplex op);
} ops;
void init(){//Apelata la inceput
ops.construire = &construire_fun;
ops.suma = &suma_fun;
ops.diferenta = &diferenta_fun;
ops.conjugat = &conjugat_fun;
ops.produs = &produs_fun;
ops.modul = &modul_fun;
}