Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature may not be available in some browsers.
struct Complex {
int real, imm;
};
struct Complex add(struct Complex a, struct Complex b)
{
struct Complex result;
result.real = a.real + b.real;
result.imm = a.imm + b.imm;
return result;
}
int is_real(struct Complex *this)
{
return !(this->imm);
}
La typedef puoi anche metterla, ma aggiungere dei puntatori tanto per aggiungerli non è una buona idea. Nel C non c'è un meccanismo di protezione dei dati, è tutto public. Potresti utilizzare un approccio alla Python: metti un underscore davanti al campo e per convenzione dici che l'utente non dovrebbe toccarlo.
I puntatori a funzione ti possono servire se vuoi dire che i metodi fanno parte dello stato dell'oggetto, altrimenti anche delle funzioni normalissime vanno più che bene. Se vuoi agire sullo stato dell'oggetto puoi fare una cosa di questo tipo:
C:int is_real(struct Complex *this) { return !(this->imm); }
Non scomoderei i puntatori a funzione amenoché non ne hai effettivamente bisogno. Non so precisamente quali siano le tue idee, ma a me non sembrano necessari.
#include <math.h>
typedef struct complex{
int a; //Parte Reale
int b; //Parte immaginaria
int (*c_real)(complex);
int (*c_immaginary)(complex);
float (*c_module)(int,int)
};
int rem (complex); //Ritorna parte reale
int imm (complex); //Ritorna parte immaginaria
float modulo (complex); //Ritorna il modulo
#include <math.h>
int rem (complex){
return complex.a;
}
int imm(complex){
return complex.b;
}
float modulo (complex){
return sqrt(((complex.a *complex.a)+(complex.b * complex.b)));
}
typedef struct complex {
int a; //Parte Reale
int b; //Parte immaginaria
int (*c_real)(struct complex);
int (*c_immaginary)(struct complex);
float (*c_module)(int,int);
} complex;
int rem(complex); //Ritorna parte reale
int imm(complex); //Ritorna parte immaginaria
float modulo(complex); //Ritorna il modulo
int rem (complex comp)
{
return comp.a;
}
int imm(complex comp)
{
return comp.b;
}
float modulo (complex comp)
{
return sqrt(((comp.a *comp.a)+(comp.b * comp.b)));
}
int a = comp1.modulo(comp1);
int b = modulo(comp1);
#include <stdlib.h>
#include <math.h>
#include "complex.h"
#define _free(p) {if(p){free((void *)p); (p) = NULL; }}
typedef struct complex_pr {
complex com;
int r;
int i;
} complex_pr;
static int rem_pr(complex_pr *comp) {
return comp->r;
}
static int imm_pr(complex_pr *comp) {
return comp->i;
}
static float modulo_pr(complex_pr *comp) {
return sqrt(((comp->r *comp->r)+(comp->i * comp->i)));
}
static int rem(complex *comp) {
if(comp == NULL) return 0;
return rem_pr((complex_pr *) comp);
}
static int imm(complex *comp) {
if(comp == NULL) return 0;
return imm_pr((complex_pr *) comp);
}
static float modulo(complex *comp) {
if(comp == NULL) return 0;
return modulo_pr((complex_pr *) comp);
}
complex *new_complex(int r, int i) {
complex_pr *comp_pr = NULL;
if((comp_pr = (complex_pr *) malloc(sizeof(complex_pr))) == NULL)
return NULL;
comp_pr->com.c_real = rem;
comp_pr->com.c_immaginary = imm;
comp_pr->com.c_module = modulo;
comp_pr->r = r;
comp_pr->i = i;
return (complex *) comp_pr;
}
void del_complex(complex *comp) {
_free(comp);
}
#ifndef __INC_COMPLEX_H__
#define __INC_COMPLEX_H__
typedef struct complex {
int (*c_real)(struct complex *);
int (*c_immaginary)(struct complex *);
float (*c_module)(struct complex *);
} complex;
complex *new_complex(int r, int i);
void del_complex(complex *comp);
#endif
#include <stdio.h>
#include "complex.h"
int main(int argc, char *argv[]){
complex *c1 = new_complex(12, 2);
complex *c2 = new_complex(22, 5);
printf("test real c1 %d\n", c1->c_real(c1));
printf("test imm c2 %d\n", c2->c_immaginary(c2));
printf("test mod c1 %f\n", c1->c_module(c1));
del_complex(c1);
del_complex(c2);
return 0;
}