Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib>
- #include "mapa.h"
- typedef struct ramo{
- char* codigo;
- char* nombre;
- int semestre;
- int creditos;
- char* pre;
- }ramo;
- typedef struct nodo{
- void* data;
- char** key;
- struct node* left;
- struct node* right;
- struct node* parent;
- }tree_node;
- typedef struct treemap{
- tree_node* root;
- tree_node* current;
- }treemap;
- TreeMap* createTreeMap(){
- TreeMap* map = (TreeMap*) malloc(sizeof(TreeMap));
- map->root=NULL;
- map->current=NULL;
- return map;
- }
- tree_node* _crear_nodo(char** key, void* dato){
- tree_node* newNode = (tree_node*) malloc(sizeof(tree_node));
- newNode->key = key;
- newNode->data = dato;
- newNode->left = NULL;
- newNode->right = NULL;
- newNode->parent = NULL;
- return newNode;
- }
- void* first(TreeMap* tree){
- tree->current=tree->root;
- while(tree->current->left){
- tree->current=tree->current->left;
- }
- return tree->current->data;
- }
- void* next(TreeMap* tree){
- if(tree->current->right){
- tree->current=tree->current->right;
- while(tree->current->left){
- tree->current=tree->current->left;
- }
- return tree->current->data;
- }
- else{
- tree_node* hijo = tree->current;
- tree->current=tree->current->parent;
- while(tree->current->right==hijo){
- hijo=tree->current;
- tree->current=tree->current->parent;
- }
- return tree->current->data;
- }
- }
- void* upper_bound(TreeMap* tree, char** key){
- tree_node* aux = tree->root;
- while(aux){
- if(strcmp(aux->key, key) == 0){
- return aux->data; //raiz
- }
- if(strcmp(aux->key, key) > 0){
- if(aux->right){ //se mueve hacia la derecha
- aux = aux->right;
- }
- else{
- if(strcmp(aux->parent->key, key) > 0){ //se retorna el upperbound
- return aux->parent->data;
- }
- else{
- return NULL; //el rango es mayor al mayor elemento en el arbol
- }
- }
- }
- else{
- if(aux->left){ //se mueve hacia la izquierda
- aux = aux->left;
- }
- else{ //se retorna el upperbound
- return aux->data;
- }
- }
- }
- return NULL;
- }
- void insert(TreeMap* tree, char** key, void* dato){
- tree_node* new_node = _crearNodo(key, dato);
- if (tree->root == NULL){
- tree->root=new_node; //si esta vacio el arbol
- }
- else{
- tree_node* pos_Anterior = NULL;
- tree_node* pos_Actual = tree->root; //estos 2 son para moverse y tener una manera de volver al ultimo puesto en caso de llegar a NULL
- while(pos_Actual != NULL){
- pos_Anterior = pos_Actual; //si no hemos llegado al fin de una rama, pos_anterior toma la posicion actual
- if(strcmp(new_node->key, pos_Actual->key) < 0){ //y movemos la posicion actual hacia izquierda o derecha mientras buscamos la posicion del nodo nuevo
- pos_Actual = pos_Actual->left;
- }
- else{
- pos_Actual = pos_Actual->right;
- }
- }
- new_node->parent = pos_Anterior; //cuando la posicion actual es NULL, usaremos la anterior para asignarla como padre del nuevo nodo
- if(strcmp(new_node->key, pos_Anterior->key) < 0){
- pos_Anterior->left = new_node; //y aqui se decide si el padre sera padre por la izquierda o derecha
- }
- else{
- pos_Anterior->right = new_node;
- }
- }
- }
- void* search(TreeMap* tree, char** key){
- void* aux = first(tree);
- while(aux){
- if(strcmp(tree->current->key, key)){
- return tree->current->data;
- }
- aux = next(tree);
- }
- return NULL;
- }
- void transplantar(TreeMap* t, tree_node* u, tree_node* v){
- if(!u->parent){
- t->root = v;
- }else if(u == u->parent->left){
- u->parent->left = v;
- }else u->parent->right = v;
- if(v) v->parent = u->parent;
- }
- void erase(TreeMap* tree, char** key){
- search(tree, key);
- if(!tree->current) return;
- tree_node* z = tree->current;
- if(!z->left)
- transplantar(tree,z,z->right);
- else if(!z->right)
- transplantar(tree,z,z->left);
- else {
- tree_node *y=z->right;
- while(y->left)
- y=y->left;
- if(y->parent!=z){
- transplantar(tree,y,y->right);
- y->right=z->right;
- y->right->parent=y;
- }
- transplantar(tree,z,y);
- y->left=z->left;
- y->left->parent=y;
- }
- free(z);
- tree->current=NULL;
- }
- void deleteTree(struct node* node)
- {
- if (node == NULL) return;
- /* first delete both subtrees */
- deleteTree(node->left);
- deleteTree(node->right);
- /* then delete the node */
- printf("\n Deleting node: %d", node->data);
- free(node);
- }
- /*=================================================================/
- / /
- / /
- / END OF TREE_MAP /
- / /
- / /
- /=================================================================*/
- void leeram (ramo* ram, char* str){
- const char s[2] = ",";
- const char corte[2]= "\n"
- char *token;
- //Lee el primer token
- token = strtok(str, s);
- ram->codigo=token;
- token=strtok(NULL,s);
- ram->nombre=token;
- token=strtok(NULL,s);
- ram->semestre=token;
- token=strtok(NULL,s);
- ram->creditos=token;
- //se mueve por los token faltantes
- while(token != NULL) {
- token = strtok(NULL, corte);
- pushfront(ram->pre, token);
- }
- return(0);
- }
- void agregarAsignatura(char* info){
- ramo* ram = (ramo*)calloc(sizeof(ramo));
- leeram(ram, info);
- }
- void leeprofe (profe* profe_actual, char* str){
- const char s[2] = ",";
- const char corte[2]= "\n"
- char *token;
- //Lee el primer token
- token = strtok(str, s);
- profe->rut=token;
- token=strtok(NULL,s);
- profe->nombre=token;
- token=strtok(NULL,s);
- profe->apellido=token;
- // Se mueve por los token flatantes
- token=strtok(NULL,s);
- while(token != NULL) {
- pushfront(profe_actual->ramos, token);
- token = strtok(NULL, corte);
- }
- return(0);
- }
- void agregarProfe(char* info){
- profe* profe_actual = (profe*)calloc(sizeof(profe));
- leeprofe(profe, info);
- }
- profe CrearProfe(int rut, char* nombre){
- char* new_ramo;
- list* ramos = createList();
- int switchmenu = 1;
- profe profe_actual;
- while(switchmenu){
- printf("Ingrese la sigla de un ramo\n"); //se ingresan ramos a la lista auxiliar
- scanf("%s", &new_ramo); //que serĂ¡ agregada al struct de profe
- pushfront(ramos, new_ramo);
- printf("Desea ingresar otro ramo?\n1 - Si\n0 - No\n");
- scanf("%d", &switchmenu);
- }
- const char cut[2] = " ";
- char* token;
- token = strtok(nombre, cut); //se extrae el nombre y se guarda
- profe_actual->nombre = token;
- token = strtok(NULL, cut); //se extrae el apellido y se guarda
- profe_actual->apellido = token;
- profe_actual->rut = rut; //se guarda el nombre
- profe_actual->ramos = ramos; //se guarda el ramo
- return profe_actual;
- }
- void agregarProfesor(treemap profes_rut, treemap_ape){
- int rut;
- char* nombre;
- int switchmenu = 1;
- char **ramo_actual;
- printf("Ingrese el rut sin puntos ni guion\n");
- scanf("%d", &rut);
- if(search(profes, rut)){
- while(switchmenu){
- printf("Ingrese codigo del ramo\n");
- scanf("%s", &ramo_actual);
- if(profes->current->data->max_ramos == profes->current->data->last-1){
- profes->current->data->ramos = realloc(sizeof(profes->current->data->ramos)*2); //ver si manda error luego y arreglar los *s
- }
- profes->current->data->ramos[profes->current->data->last+1]=ramo_actual;
- profes->current->data->last++;
- printf("1 - Ingresar otra asignatura \n0 - Salir \nIngrese una opcion\n");
- scanf("%d", &switchmenu);
- }
- }
- else{
- printf("Ingrese nombre\n");
- scanf("%s", &nombre);
- profe profe_actual = CrearProfe(rut, nombre);
- insert(profes_rut, profe_actual->rut, profe_actual);
- insert(profes_ape, profe_actual->apellido, profe_actual);
- }
- }
- void MostrarAsignaturas(treemap ramos_sigla, treemap ramos_sem, treemap ramos_nombre){ //check
- int menu=0;
- do{
- printf("Como quiere ver sus asignaturas?\n");
- printf("1.- Ordenar por nombre\n");
- printf("2.- Ordenar por codigo\n");
- printf("3.- Ordenar por semestre\n");
- scanf("%d",&menu);
- switch(menu){
- case 1:{
- ramo* aux=first(ramo_nombre);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- while(next(ramo_nombre)){
- aux=next(ramo_nombre);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- printf("%s\n", );
- }
- break;
- }
- case 2:{
- ramo* aux=first(ramo_sigla);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- while(next(mapa)){
- aux=next(mapa);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- printf("%s\n", );
- }
- break;
- }
- case 3:{
- ramo* aux=first(ramo_semestre);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- while(next(ramo_semestre)){
- aux=next(semestre);
- printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- printf("%s\n", );
- }
- break;
- }
- }
- }while(menu);
- }
- void MostrarProfesor(treemap profes_rut, treemap profes_ape){ //NO TERMINADO
- int menu=0;
- do{
- printf("1.- Mostrar profesores por apellido\n");
- printf("2.- Mostrar profesores por rut\n");
- scanf("%d",&menu);
- switch(menu){
- case 1:{
- }
- case 2:{
- }
- }
- }while(menu);
- }
- void MostrarInfo(treemap ramos_sigla){ //NO TERMINADO
- char show_ramo[8];
- int i;
- printf("Ingrese codigo del ramo a mostrar\n");
- scanf("%s", &show_ramo);
- search(show_ramo);
- printf("%s", ramos_sigla->current->nombre);
- first(profes);
- while(profes->current){
- if(profes->current->)
- }
- }
- void guradar(treemap* asign, treemap* profes){ //check
- int menu=0;
- do{
- printf("1.- Guardar datos asignatura\n");
- printf("2.- Guardar datos profesores\n");
- scanf("%d",&menu);
- switch(menu){
- case 1:{
- FILE *fp; //se crea puntero a archivo
- char* input;
- char fileName= (char*)calloc(25,sizeof(char)); //se inicializa input de usuario
- ramo* aux=first(asign); //se crea variable auxiliar para data de asignatura
- printf("Ingresar nombre de archivo\n");
- scanf("%s",&fileName);
- fp = fopen(fileName".csv","a"); //se abre archivo
- do{ // se recorre arbol binario para guardar asignaturas en el archivo
- fprintf(fp,"%s,%s,%d,%d",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
- aux->pre->current=aux->pre->first;
- do{ //se recorre la lista de prerequisitos
- fprintf(fp, ",%s",aux->pre->current);
- aux->pre->current=->aux->pre->next;
- }while(aux->pre->current!=aux->pre->first);
- fprintf(pf, "\n",); //termina la linea
- }while(next(asign));
- fclose(fp);
- break;
- }
- case 2:{
- FILE *fp; //se crea puntero a archivo
- char* input;
- char fileName= (char*)calloc(25,sizeof(char)); //se inicializa input de usuario
- ramo* aux=first(profes); //se crea variable auxiliar para data de asignatura
- printf("Ingresar nombre de archivo\n");
- scanf("%s",&fileName);
- fp = fopen(fileName".csv","a"); //se abre archivo
- do{ //se recorre el arbol de profesores hasta que no se pueda
- fprintf(fp, "%s,%s,%s",aux->rut,aux->nombre, aux->apellido);
- aux->ramos->current=aux->ramos->first;
- do{ //recorre el
- fprintf(fp, ",%s",aux->ramos->current);
- aux->ramos->current=aux->ramos->next;
- }while(aux->ramos->current!=aux->ramos->first)
- aux=next(profes);
- }while(next(prefes));
- break;
- }
- }
- }while(menu);
- }
- void cargarDatos(){
- do{
- printf("1.- Cargar datos asignatura\n");
- printf("2.- Cargar datos profesores\n");
- printf("0.- Salir\n");
- scanf("%d",&menu);
- switch(menu){
- case 1:{
- FILE *fp;
- char* input;
- char fileName= (char*)calloc(25,sizeof(char));
- printf("Ingresar nombre de archivo\n");
- scanf("%s",&fileName);
- fp = fopen(fileName".csv","r");
- if (fp == NULL){
- return 0;
- }
- while(!feof(fp)){
- input=(char*)calloc(150,sizeof(char));
- fscanf(fp,150,input);
- agregarAsignatura(input);
- }
- fclose(fp);
- break;
- }
- case 2:{
- FILE *fp;
- char fileName= (char*)calloc(25,sizeof(char));
- printf("Ingresar nombre de archivo\n");
- scanf("%s",&fileName);
- fp = fopen(fileName".csv","r");
- if (fp == NULL){
- return 0;
- }
- while(!feof(fp)){
- input=(char*)calloc(150,sizeof(char));
- fscanf(fp,150,input);
- agregarProfe(input);
- }
- fclose(fp);
- break;
- }
- }
- }while(menu);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement