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 leeram (ramo* ram, char* str){
- const char s[2] = ",";
- const char corte[2]= "\n"
- char *token;
- /* get the first 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;
- /* walk through other tokens */
- while(token != NULL) {
- token = strtok(NULL, corte);
- ram->pre=token;
- }
- return(0);
- }
- void agregarAsignatura(char* info){
- ramo* ram = (ramo*)calloc(sizeof(ramo));
- leeram(ram, info);
- }
- void agregarProfesor(treemap profes){
- int rut;
- 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{
- profe profe_actual;
- profe_actual->rut = rut;
- profe_actual->max_ramos = 1;
- profe_actual->last = -1;
- profe_actual->ramos = (void**) malloc(sizeof(void*));
- printf("Ingrese nombre\n");
- scanf("%s", &profe_actual->nombre);
- while(switchmenu){
- printf("Ingrese codigo del ramo\n");
- scanf("%s", &ramo_actual);
- if(profes->current->data->max_ramos == profes->current->data->last){
- profes->current->data->ramos = realloc(sizeof(profes->current->data->ramos)); //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);
- }
- insert(profes, profe_actual->rut ,profe_actual);
- }
- void MostrarAsignaturas(treemap* mapa){
- 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(mapa);
- 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(){
- 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(){
- }
- void guradar(){
- }
- 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".cvs","r");
- if (fp == NULL){
- return 0;
- }
- while(!feof(fp)){
- input=(char*)calloc(150,sizeof(char));
- fscanf(fp,150,input);
- agregarAsignatura(input);
- }
- }
- case 2:{
- FILE *fp;
- char fileName= (char*)calloc(25,sizeof(char));
- printf("Ingresar nombre de archivo\n");
- scanf("%s",&fileName);
- fp = fopen(fileName".cvs","r");
- if (fp == NULL){
- return 0;
- }
- }
- }
- }while(menu);
- char nombre[100];
- long key=0;
- while(fgets(nombre,100,fp)){
- strtok(nombre,"\n");
- key = StringToInt(nombre);
- char* dato = (char*)malloc(100*sizeof(char));
- strcpy(dato,nombre);
- insert(Peliculas,key,dato);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement