Skip to content

Instantly share code, notes, and snippets.

@robsonfaxas
Last active August 30, 2018 12:27
Show Gist options
  • Save robsonfaxas/51c9453f5c91c232495942d06da3a73f to your computer and use it in GitHub Desktop.
Save robsonfaxas/51c9453f5c91c232495942d06da3a73f to your computer and use it in GitHub Desktop.
#algorithm Andy's First Dictionary
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct Nodo{
char node[200];
struct Nodo *r;
struct Nodo *l;
} Nodo;
typedef struct Tree{
Nodo *root;
} Tree;
char* stolower( char* s )
{
char* p = s;
while (*p = tolower( *p )) p++;
return s;
}
void initializeTree(Tree *t){
t->root = NULL;
}
void inorder(Nodo *n)
{
if(n != NULL)
{
inorder(n->l);
printf("%s\n",n->node);
inorder(n->r);
}
}
void ChgW(Nodo *str){
int i;
for (i=0; i<strlen(str->node); i++){
if (str->node[i] == '\0' || str->node[i] == '\n')
break;
if ((str->node[i] >90 || str->node[i] < 65) && (str->node[i] <97 || str->node[i] > 122)){
str->node[i] = ' ';
}
}
}
void insNodo(Nodo n, Tree *t){
Nodo *n1 = (Nodo *)malloc(sizeof(Nodo));
Nodo *aux;
int res = 0;
int insere = 0;
aux = t->root;
strcpy(n.node, stolower(n.node));
*n1 = n;
if (aux == NULL){
t->root = n1;
} else
while(insere != 1){
res = strcmp(stolower(n.node), stolower(aux->node));
if (res == 0){
//aux = n1;
insere = 1;
}
else
if (res > 0){
if (aux->r == NULL){
aux->r = n1;
insere = 1;
}
else
aux = aux->r;
}
else{
if (aux->l == NULL){
aux->l = n1;
insere = 1;
}
else
aux = aux->l;
}
}
}
int TaW(char *str, Nodo *result){
int i, fPos = -1, lPos = -1;
strcpy(result->node,"");
for (i=0; i< strlen(str); i++){
if (fPos == -1 && str[i] != ' ')
fPos = i;
else
if (fPos != -1 && str[i] == ' ' ){
lPos = i;
break;
}
}
if (fPos != -1 && lPos == -1)
lPos =strlen(str);
for (i=fPos; i<lPos; i++){
result->node[i-fPos] = str[i];
}
result->node[i-fPos] = '\0';
if (fPos == -1)
return -1;
else{
ChgW(result);
return 0;
}
}
int DaW(Nodo*str2, Nodo *result){
int i, fPos = -1, start=-1;
Nodo *str = (Nodo *) malloc(sizeof(Nodo));
strcpy(str->node, str2->node);
strcpy(result->node, "");
for (i=0; i< strlen(str->node); i++){
if (fPos == -1 && str->node[i] != ' ')
fPos = i;
else
if (fPos != -1 && str->node[i] == ' '){
fPos = -1;
start = i;
break;
}
}
for (i=start; i<strlen(str->node); i++){
if (fPos == -1 && str->node[i] != ' '){
fPos = i;
break;
}
}
i = 0;
if (fPos != -1 && start != -1)
for (i=fPos; i<strlen(str->node); i++){
result->node[i-fPos] = str->node[i];
}
free(str);
result->node[i-fPos] = '\0';
if (fPos == -1)
return -1;
else{
ChgW(result);
return 0;
}
}
int main(){
Tree t;
t.root = NULL;
Nodo K, n1;
int res;
int sai1 = 0;
K.l = NULL;
K.r = NULL;
n1.l = NULL;
n1.r = NULL;
strcpy(K.node,"");
strcpy(n1.node,"");
char str[200];
while (gets(K.node) != NULL){
ChgW(&K);
while (TaW(K.node, &n1) == 0)
{
insNodo(n1, &t);
DaW(&K, &n1);
strcpy(K.node, n1.node);
strcpy(n1.node, "");
}
}
inorder(t.root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment