This code is supossed to do some operations over polynomials (add, sub, mul,div) and everything is working beside division.
Looks like when I'm calling the add function inside of div function it crash for some reason, but I dont get why.
Here's the whole code, Im using a list to store the polynomials
The function called "dividir_polinomios" crash in this line:
p_aaux = sumar_polinomios(p_aaux,p_sumaaux);
I dont get why it crash, if I call the same function in Main, with the same polynomials, it works, I'm really having a bad time here because Im not used to program in C.
Here is the struct i use:
struct poli{
float cons;
unsigned char exp;
struct poli *sig;
};
Here is the add function:
struct poli *sumar_polinomios(struct poli* p_a, struct poli *p_b){
struct poli *res=NULL;
struct poli *aux=NULL;
struct poli *aux_a,*aux_b;
aux_a=p_a;
aux_b=p_b;
while(aux_a || aux_b){
aux=(struct poli*)malloc(sizeof(struct poli));
if(aux_a->exp == aux_b->exp){
aux->cons=aux_a->cons+aux_b->cons;
aux->exp=aux_a->exp;
aux->sig=NULL;
res=crear_lista(res,aux);
aux_a=aux_a->sig;
aux_b=aux_b->sig;
}
else if(aux_a->exp > aux_b->exp){
aux->cons=aux_a->cons;
aux->exp=aux_a->exp;
aux->sig=NULL;
res=crear_lista(res,aux);
aux_a=aux_a->sig;
}
else if(aux_a->exp < aux_b->exp){
aux->cons=aux_b->cons;
aux->exp=aux_b->exp;
aux->sig=NULL;
res=crear_lista(res,aux);
aux_b=aux_b->sig;
}
}
return res;
};
Here is the div function:
void dividir_polinomios(struct poli *p_a, struct poli *p_b){
struct poli *p_aaux=p_a;
struct poli *p_baux=p_b;
struct poli *aux;
struct poli *cuociente=NULL;
struct poli *p_sumaaux=NULL;
struct poli *p_ressuma;
while(1){
//CONDICION PARA ROMPER WHILE AQUI//
//Termino del cuociente
aux = (struct poli*)malloc(sizeof(struct poli));
aux->cons = p_aaux->cons/p_baux->cons;
aux->exp = p_aaux->exp - p_baux->exp;
aux->sig=NULL;
cuociente=crear_lista(cuociente,aux);
//printf("TEST: constante: %.2f\nexponente: %d",aux->cons,aux->exp);
//Multiplicar termino por p_baux
p_sumaaux=multiplicar_polinomios(p_baux,aux);
//Invertir signo para realizar la suma
p_sumaaux=cambiar_signo(p_sumaaux);
printf("polinomio a:\n");
mostrar_lista(p_aaux);
printf("\npolinomio p_summaux:\n");
mostrar_lista(p_sumaaux);
//Sumar p_aaux + p_summaux
printf("\n\nAhora exploto");
p_aaux = sumar_polinomios(p_aaux,p_sumaaux); //<--THIS CRASH
//Se quita el primer elemento
p_aaux=p_aaux->sig;
mostrar_lista(p_aaux);
}
};
And here's the function i use to create the lists
struct poli *crear_lista(struct poli *lista,struct poli *nodo){
struct poli *aux;
if(!lista){
lista=nodo;
}
else{
aux=lista;
while(aux->sig!=NULL){
aux=aux->sig;
}
aux->sig=nodo;
}
return lista;
}
Basically, if I used this input: (I actually dont store the "x", just the constants and the exps).
Poly_a = -20x^5 + 37x^3 -8x^2 - 15x
Poly_b = 4x^2-5
it should store in "cuociente" the following result:
-5x^3+3x-2
But instead of that, the code crash when "sumar_polinomnios" is called inside of "dividir_polinomios".
EDIT:
I managed to fix the problem, the problem was in "sumar_polinomios" and that was because aux_b list reach NULL value faster than aux_a list, so when aux_b list is null and I did some comparation like
aux_a->exp == aux_b->exp
it crashed because aux_b was null, so I did this.
struct poli *sumar_polinomios(struct poli** p_a, struct poli **p_b){
struct poli *res=NULL;
struct poli *aux=NULL;
struct poli *aux_a,*aux_b;
aux_a=(*p_a);
aux_b=(*p_b);
while(aux_a || aux_b){
aux=(struct poli*)malloc(sizeof(struct poli));
//printf("\nestoy aqui\n");
//printf("\nexp_a:%d\texp_b:%d\n",aux_a->exp,aux_b->exp);
if(aux_a && aux_b){
if(aux_a->exp == aux_b->exp){
//printf("exponentes iguales\n");
aux->cons=aux_a->cons+aux_b->cons;
aux->exp=aux_a->exp;
aux->sig=NULL;
crear_lista(&res,aux);
aux_a=aux_a->sig;
aux_b=aux_b->sig;
//printf("fin exponentes iguales\n");
}
else if(aux_a->exp > aux_b->exp){
//printf("exponenten a > exponente b");
aux->cons=aux_a->cons;
aux->exp=aux_a->exp;
aux->sig=NULL;
crear_lista(&res,aux);
aux_a=aux_a->sig;
//printf("fin exponenten a > exponente b");
}
else if(aux_a->exp < aux_b->exp){
//printf("exponente a < exponente b");
aux->cons=aux_b->cons;
aux->exp=aux_b->exp;
aux->sig=NULL;
crear_lista(&res,aux);
aux_b=aux_b->sig;
//printf("fin exponente a < exponente b");
}
}
else if(aux_a!=NULL){
aux->cons=aux_a->cons;
aux->exp=aux_a->exp;
aux->sig=NULL;
crear_lista(&res,aux);
aux_a=aux_a->sig;
}
else{
aux->cons=aux_b->cons;
aux->exp=aux_b->exp;
aux->sig=NULL;
crear_lista(&res,aux);
aux_b=aux_b->sig;
}
}
return res;
}
Now I check if both list aren't NULL, and if one of them is null i just append the not-null list to the result list.
};— that has an empty declaration after the function. Some compilers complain; others don't.gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -c x.c), but if you add-pedantic, it 'fesses up:x.c:1:1: error: ISO C does not allow extra ‘;’ outside of a function [-Werror=pedantic]when the filex.ccontains a semicolon and a newline. So no, under pedantic rules, a semicolon after the close brace is not allowed, but GCC is a kind, tolerant compiler and you have to tell it to reject such code before it does so.