Hello, I'm trying to create a math expression tree with the following logic:
My expression is a string, and I have 4 character options:
- (: When finding this character, my code must create a node for
pref
, insert in itsconteudo(pref->conteudo)
the character '#', save the address ofpref
in a vector of pointersendereco
, it adds the variablek
(causing the vector to move to next iteration) and makepref
point topref->esq
. - Numerals (0 through 9): Creates a node in
pref
and fills itsconteudo(pref->conteudo)
with the numeric character. - Operator (+ - * /): When finding an operator, it looks at the last address of the
endereco
vector, causespref = endereco[k] (implicando que pref irá apontar para esse novo endereço)
to thenpref->conteudo
to receive the character for the operator, after that% . - ): Decreases the%% variable by making the referential of the
pref = pref->dir(aponta para a direita)
vector decreases. If so, I thought of the vectork
as if it were a stack itself.
That was my idea, though I'm getting serious segment fault issues even though I can not think of what I'm missing. The code below breaks down into main and arvbin.c
NOTE: My intention was to have a pointer moving through the tree ( endereco
) and something static as a reference for the root, but as I said I'm having serious problems with pointer handling.
PS: I noticed in other answers that typedef defining a pointer to a structure is a bad programming practice, but that was the only way I could learn to define a pointer to a structure x within that structure.
arvbin.h
#include <stdio.h>
#include <stdlib.h>
#include "arvbin.h"
typedef struct no *pontNo;
typedef struct no{
char conteudo;
pontNo esq;
pontNo dir;
}no;
void CRIA_NO(pontNo novoNo,char info){
novoNo = malloc(sizeof(no));
novoNo->conteudo = info;
novoNo->esq = NULL;
novoNo->dir = NULL;
}
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arvbin.h"
int main() {
//Declaracao de variaveis & Inicializacoes:
no raiz; //Raiz da arvore declarada
raiz.conteudo = '#'; //Declarando conteudo da raiz
pontNo pm = &raiz; //(P)onteiro para (M)ovimentacao na arvore,inicialmente apontando para raiz
pontNo endereco[20]; //Vetor de ponteiros para endereco do tipo pontNo, funcionara como uma pilha
char expressao[100]; //Expressao matematica inserida
int resultado;
//Funcionamento:
while(1){
printf("Escreva a expressao matematica(Caso queira parar, escreva 0): ");
scanf("%s",expressao);
if(strcmp(expressao,"0") == 0) break; //Condicao de parada do loop de insercao de expressoes
int k = 0;
int error = 0;
int i;
for(i = 0;expressao[i] != '#include <stdio.h>
#include <stdlib.h>
#include "arvbin.h"
typedef struct no *pontNo;
typedef struct no{
char conteudo;
pontNo esq;
pontNo dir;
}no;
void CRIA_NO(pontNo novoNo,char info){
novoNo = malloc(sizeof(no));
novoNo->conteudo = info;
novoNo->esq = NULL;
novoNo->dir = NULL;
}
';i++){
if(expressao[i] == '('){
if(i == 0){
endereco[k] = pm;
printf("%c\n",endereco[k]->conteudo);
k++;
pm = pm->esq;
}
else{
CRIA_NO(pm,'#');
endereco[k] = pm;
printf("%c\n",endereco[k]->conteudo);
k++;
pm = pm->esq;
}
}
else if(expressao[i] == '0'||expressao[i] == '1'||expressao[i] == '2'||expressao[i] == '3'||expressao[i] == '4'||expressao[i] == '5'||expressao[i] == '6'||expressao[i] == '7'||expressao[i] == '8'||expressao[i] == '9'){
CRIA_NO(pm,expressao[i]);
printf("%c\n",pm->conteudo);
}
else if(expressao[i] == '+'||expressao[i] == '-'||expressao[i] == '*'||expressao[i] == '/'){
}
else if(expressao[i] == ')'){
}
else{
printf("Erro na expressao: char nao reconhecido!\n");
error = 1;
break;
}
}
}
return 0;
}
If you have another way to solve this problem let me know!