Advertisement
TecaFondo

Untitled

Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib>
  3. #include "mapa.h"
  4.  
  5. typedef struct ramo{
  6.     char* codigo;
  7.     char* nombre;
  8.     int semestre;
  9.     int creditos;
  10.     char* pre;
  11. }ramo;
  12.  
  13. typedef struct nodo{
  14.    void* data;
  15.    char** key;
  16.    struct node* left;
  17.    struct node* right;
  18.    struct node* parent;
  19. }tree_node;
  20.  
  21.  
  22. typedef struct treemap{
  23.    tree_node* root;
  24.    tree_node* current;
  25. }treemap;
  26.  
  27. TreeMap* createTreeMap(){
  28.     TreeMap* map = (TreeMap*) malloc(sizeof(TreeMap));
  29.     map->root=NULL;
  30.     map->current=NULL;
  31.     return map;
  32. }
  33.  
  34. tree_node* _crear_nodo(char** key, void* dato){
  35.     tree_node* newNode = (tree_node*) malloc(sizeof(tree_node));
  36.     newNode->key = key;
  37.     newNode->data = dato;
  38.     newNode->left = NULL;
  39.     newNode->right = NULL;
  40.     newNode->parent = NULL;
  41.     return newNode;
  42. }
  43.  
  44. void* first(TreeMap* tree){
  45.     tree->current=tree->root;
  46.     while(tree->current->left){
  47.         tree->current=tree->current->left;
  48.     }
  49.     return tree->current->data;
  50. }
  51.  
  52. void* next(TreeMap* tree){
  53.     if(tree->current->right){
  54.         tree->current=tree->current->right;
  55.         while(tree->current->left){
  56.             tree->current=tree->current->left;
  57.         }
  58.         return tree->current->data;
  59.     }
  60.     else{
  61.         tree_node* hijo = tree->current;
  62.         tree->current=tree->current->parent;
  63.         while(tree->current->right==hijo){
  64.             hijo=tree->current;
  65.             tree->current=tree->current->parent;
  66.         }
  67.         return tree->current->data;
  68.     }
  69. }
  70.  
  71. void* upper_bound(TreeMap* tree, char** key){
  72.     tree_node* aux = tree->root;
  73.     while(aux){
  74.         if(strcmp(aux->key, key) == 0){
  75.             return aux->data;       //raiz
  76.         }
  77.    
  78.         if(strcmp(aux->key, key) > 0){
  79.             if(aux->right){         //se mueve hacia la derecha
  80.                 aux = aux->right;
  81.             }
  82.  
  83.             else{
  84.                 if(strcmp(aux->parent->key, key) > 0){     //se retorna el upperbound
  85.                     return aux->parent->data;
  86.                 }
  87.  
  88.                 else{
  89.                     return NULL;    //el rango es mayor al mayor elemento en el arbol
  90.                 }
  91.             }
  92.         }
  93.  
  94.         else{                      
  95.             if(aux->left){          //se mueve hacia la izquierda
  96.                 aux = aux->left;
  97.             }
  98.  
  99.             else{                   //se retorna el upperbound
  100.                 return aux->data;
  101.             }
  102.         }
  103.     }
  104.     return NULL;                  
  105. }
  106.  
  107. void insert(TreeMap* tree, char** key, void* dato){
  108.     tree_node* new_node = _crearNodo(key, dato);
  109.     if (tree->root == NULL){
  110.         tree->root=new_node;    //si esta vacio el arbol
  111.     }
  112.     else{
  113.         tree_node* pos_Anterior = NULL;
  114.         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
  115.         while(pos_Actual != NULL){              
  116.             pos_Anterior = pos_Actual;          //si no hemos llegado al fin de una rama, pos_anterior toma la posicion actual
  117.            
  118.             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    
  119.                 pos_Actual = pos_Actual->left;
  120.             }
  121.             else{
  122.                 pos_Actual = pos_Actual->right;
  123.             }
  124.         }
  125.         new_node->parent = pos_Anterior;        //cuando la posicion actual es NULL, usaremos la anterior para asignarla como padre del nuevo nodo
  126.  
  127.         if(strcmp(new_node->key, pos_Anterior->key) < 0){
  128.             pos_Anterior->left = new_node;      //y aqui se decide si el padre sera padre por la izquierda o derecha
  129.         }
  130.         else{
  131.             pos_Anterior->right = new_node;
  132.         }
  133.  
  134.     }
  135. }
  136.  
  137. void* search(TreeMap* tree, char** key){
  138.     void* aux = first(tree);
  139.     while(aux){
  140.         if(strcmp(tree->current->key, key)){
  141.             return tree->current->data;
  142.         }
  143.         aux = next(tree);
  144.     }
  145.     return NULL;
  146. }
  147.  
  148. void transplantar(TreeMap* t, tree_node* u, tree_node* v){
  149.    
  150.     if(!u->parent){
  151.         t->root = v;
  152.     }else if(u == u->parent->left){
  153.         u->parent->left = v;
  154.     }else u->parent->right = v;
  155.    
  156.     if(v) v->parent = u->parent;
  157. }
  158.  
  159. void erase(TreeMap* tree, char** key){
  160.     search(tree, key);
  161.     if(!tree->current) return;
  162.  
  163.     tree_node* z = tree->current;
  164.     if(!z->left)
  165.         transplantar(tree,z,z->right);
  166.     else if(!z->right)
  167.         transplantar(tree,z,z->left);
  168.     else {
  169.         tree_node *y=z->right;
  170.         while(y->left)
  171.            y=y->left;
  172.            
  173.         if(y->parent!=z){
  174.             transplantar(tree,y,y->right);
  175.             y->right=z->right;
  176.             y->right->parent=y;
  177.         }
  178.         transplantar(tree,z,y);
  179.         y->left=z->left;
  180.         y->left->parent=y;
  181.     }
  182.     free(z);
  183.     tree->current=NULL;
  184. }
  185.  
  186. void deleteTree(struct node* node)
  187. {
  188.     if (node == NULL) return;
  189.  
  190.     /* first delete both subtrees */
  191.     deleteTree(node->left);
  192.     deleteTree(node->right);
  193.    
  194.     /* then delete the node */
  195.     printf("\n Deleting node: %d", node->data);
  196.     free(node);
  197. }
  198.  
  199. /*=================================================================/
  200. /                                                                  /
  201. /                                                                  /    
  202. /                         END OF TREE_MAP                          /
  203. /                                                                  /
  204. /                                                                  /
  205. /=================================================================*/
  206.  
  207. void leeram (ramo* ram, char* str){
  208.    const char s[2] = ",";
  209.    const char corte[2]= "\n"
  210.    char *token;
  211.    //Lee el primer token
  212.    token = strtok(str, s);
  213.    ram->codigo=token;
  214.    token=strtok(NULL,s);
  215.    ram->nombre=token;
  216.    token=strtok(NULL,s);
  217.    ram->semestre=token;
  218.    token=strtok(NULL,s);
  219.    ram->creditos=token;
  220.    //se mueve por los token faltantes
  221.    while(token != NULL) {
  222.       token = strtok(NULL, corte);
  223.       pushfront(ram->pre, token);
  224.    }
  225.    return(0);
  226. }
  227.  
  228. void agregarAsignatura(char* info){
  229.     ramo* ram = (ramo*)calloc(sizeof(ramo));
  230.     leeram(ram, info);
  231. }
  232.  
  233. void leeprofe (profe* profe_actual, char* str){
  234.    const char s[2] = ",";
  235.    const char corte[2]= "\n"
  236.    char *token;
  237.    //Lee el primer token
  238.    token = strtok(str, s);
  239.    profe->rut=token;
  240.    token=strtok(NULL,s);
  241.    profe->nombre=token;
  242.    token=strtok(NULL,s);
  243.    profe->apellido=token;
  244.    // Se mueve por los token flatantes
  245.    token=strtok(NULL,s);
  246.    while(token != NULL) {
  247.       pushfront(profe_actual->ramos, token);
  248.       token = strtok(NULL, corte);
  249.    }
  250.    return(0);
  251. }
  252.  
  253. void agregarProfe(char* info){
  254.     profe* profe_actual = (profe*)calloc(sizeof(profe));
  255.     leeprofe(profe, info);
  256. }
  257.  
  258. profe CrearProfe(int rut, char* nombre){
  259.     char* new_ramo;
  260.     list* ramos = createList();
  261.     int switchmenu = 1;
  262.     profe profe_actual;
  263.  
  264.     while(switchmenu){
  265.         printf("Ingrese la sigla de un ramo\n");        //se ingresan ramos a la lista auxiliar
  266.         scanf("%s", &new_ramo);                         //que serĂ¡ agregada al struct de profe
  267.         pushfront(ramos, new_ramo);
  268.  
  269.         printf("Desea ingresar otro ramo?\n1 - Si\n0 - No\n");
  270.         scanf("%d", &switchmenu);
  271.     }
  272.  
  273.     const char cut[2] = " ";
  274.     char* token;
  275.  
  276.     token = strtok(nombre, cut);        //se extrae el nombre y se guarda
  277.     profe_actual->nombre = token;
  278.     token = strtok(NULL, cut);          //se extrae el apellido y se guarda
  279.     profe_actual->apellido = token;
  280.    
  281.     profe_actual->rut = rut;            //se guarda el nombre
  282.  
  283.     profe_actual->ramos = ramos;        //se guarda el ramo
  284.  
  285.     return profe_actual;
  286. }
  287.  
  288. void agregarProfesor(treemap profes_rut, treemap_ape){
  289.     int rut;
  290.     char* nombre;
  291.     int switchmenu = 1;
  292.     char **ramo_actual;
  293.  
  294.     printf("Ingrese el rut sin puntos ni guion\n");
  295.     scanf("%d", &rut);
  296.  
  297.     if(search(profes, rut)){
  298.         while(switchmenu){
  299.             printf("Ingrese codigo del ramo\n");
  300.             scanf("%s", &ramo_actual);
  301.  
  302.             if(profes->current->data->max_ramos == profes->current->data->last-1){
  303.                 profes->current->data->ramos = realloc(sizeof(profes->current->data->ramos)*2);      //ver si manda error luego y arreglar los *s
  304.             }
  305.             profes->current->data->ramos[profes->current->data->last+1]=ramo_actual;
  306.             profes->current->data->last++;
  307.  
  308.             printf("1 - Ingresar otra asignatura \n0 - Salir \nIngrese una opcion\n");
  309.             scanf("%d", &switchmenu);
  310.         }
  311.     }
  312.     else{
  313.  
  314.         printf("Ingrese nombre\n");
  315.         scanf("%s", &nombre);
  316.  
  317.         profe profe_actual = CrearProfe(rut, nombre);
  318.        
  319.         insert(profes_rut, profe_actual->rut, profe_actual);
  320.         insert(profes_ape, profe_actual->apellido, profe_actual);
  321.     }
  322. }
  323.    
  324. void MostrarAsignaturas(treemap ramos_sigla, treemap ramos_sem, treemap ramos_nombre){ //check
  325.     int menu=0;
  326.     do{
  327.         printf("Como quiere ver sus asignaturas?\n");
  328.         printf("1.- Ordenar por nombre\n");
  329.         printf("2.- Ordenar por codigo\n");
  330.         printf("3.- Ordenar por semestre\n");
  331.         scanf("%d",&menu);
  332.         switch(menu){
  333.             case 1:{
  334.                 ramo* aux=first(ramo_nombre);
  335.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  336.                 while(next(ramo_nombre)){
  337.                     aux=next(ramo_nombre);
  338.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  339.                     printf("%s\n", );
  340.                 }
  341.                 break;
  342.             }
  343.             case 2:{
  344.                 ramo* aux=first(ramo_sigla);
  345.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  346.                 while(next(mapa)){
  347.                     aux=next(mapa);
  348.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  349.                     printf("%s\n", );
  350.                 }
  351.                 break;
  352.             }
  353.             case 3:{
  354.                 ramo* aux=first(ramo_semestre);
  355.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  356.                 while(next(ramo_semestre)){
  357.                     aux=next(semestre);
  358.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  359.                     printf("%s\n", );
  360.                 }
  361.                 break;
  362.             }
  363.         }
  364.     }while(menu);
  365.    
  366. }
  367. void MostrarProfesor(treemap profes_rut, treemap profes_ape){ //NO TERMINADO
  368.     int menu=0;
  369.     do{
  370.         printf("1.- Mostrar profesores por apellido\n");
  371.         printf("2.- Mostrar profesores por rut\n");
  372.         scanf("%d",&menu);
  373.         switch(menu){
  374.             case 1:{
  375.  
  376.             }
  377.             case 2:{
  378.  
  379.             }
  380.         }
  381.     }while(menu);
  382.  
  383. }
  384. void MostrarInfo(treemap ramos_sigla){ //NO TERMINADO
  385.     char show_ramo[8];
  386.     int i;
  387.     printf("Ingrese codigo del ramo a mostrar\n");
  388.     scanf("%s", &show_ramo);
  389.     search(show_ramo);
  390.     printf("%s", ramos_sigla->current->nombre);
  391.     first(profes);
  392.     while(profes->current){
  393.         if(profes->current->)
  394.     }
  395. }
  396. void guradar(treemap* asign, treemap* profes){ //check
  397.     int menu=0;
  398.     do{
  399.         printf("1.- Guardar datos asignatura\n");
  400.         printf("2.- Guardar datos profesores\n");
  401.         scanf("%d",&menu);
  402.         switch(menu){
  403.             case 1:{
  404.                 FILE *fp; //se crea puntero a archivo
  405.                 char* input;
  406.                 char fileName= (char*)calloc(25,sizeof(char)); //se inicializa input de usuario
  407.                 ramo* aux=first(asign); //se crea variable auxiliar para data de asignatura
  408.                 printf("Ingresar nombre de archivo\n");
  409.                 scanf("%s",&fileName);
  410.                 fp = fopen(fileName".csv","a"); //se abre archivo
  411.                 do{ // se recorre arbol binario para guardar asignaturas en el archivo
  412.                     fprintf(fp,"%s,%s,%d,%d",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  413.                     aux->pre->current=aux->pre->first;
  414.                     do{ //se recorre la lista de prerequisitos
  415.                         fprintf(fp, ",%s",aux->pre->current);
  416.                         aux->pre->current=->aux->pre->next;
  417.                     }while(aux->pre->current!=aux->pre->first);
  418.                     fprintf(pf, "\n",); //termina la linea
  419.                 }while(next(asign));
  420.                 fclose(fp);
  421.                 break;
  422.             }
  423.             case 2:{
  424.                 FILE *fp; //se crea puntero a archivo
  425.                 char* input;
  426.                 char fileName= (char*)calloc(25,sizeof(char)); //se inicializa input de usuario
  427.                 ramo* aux=first(profes); //se crea variable auxiliar para data de asignatura
  428.                 printf("Ingresar nombre de archivo\n");
  429.                 scanf("%s",&fileName);
  430.                 fp = fopen(fileName".csv","a"); //se abre archivo
  431.                 do{ //se recorre el arbol de profesores hasta que no se pueda
  432.                     fprintf(fp, "%s,%s,%s",aux->rut,aux->nombre, aux->apellido);
  433.                     aux->ramos->current=aux->ramos->first;
  434.                     do{ //recorre el
  435.                         fprintf(fp, ",%s",aux->ramos->current);
  436.                         aux->ramos->current=aux->ramos->next;
  437.                     }while(aux->ramos->current!=aux->ramos->first)
  438.                     aux=next(profes);
  439.                 }while(next(prefes));
  440.                 break;
  441.             }
  442.         }
  443.     }while(menu);
  444. }
  445. void cargarDatos(){
  446.     do{
  447.         printf("1.- Cargar datos asignatura\n");
  448.         printf("2.- Cargar datos profesores\n");
  449.         printf("0.- Salir\n");
  450.         scanf("%d",&menu);
  451.         switch(menu){
  452.             case 1:{
  453.                 FILE *fp;
  454.                 char* input;
  455.                 char fileName= (char*)calloc(25,sizeof(char));
  456.                 printf("Ingresar nombre de archivo\n");
  457.                 scanf("%s",&fileName);
  458.                 fp = fopen(fileName".csv","r");
  459.                 if (fp == NULL){
  460.                     return 0;
  461.                 }
  462.                 while(!feof(fp)){
  463.                     input=(char*)calloc(150,sizeof(char));
  464.                     fscanf(fp,150,input);
  465.                     agregarAsignatura(input);
  466.                 }
  467.                 fclose(fp);
  468.                 break;
  469.             }
  470.             case 2:{
  471.                 FILE *fp;
  472.                 char fileName= (char*)calloc(25,sizeof(char));
  473.                 printf("Ingresar nombre de archivo\n");
  474.                 scanf("%s",&fileName);
  475.                 fp = fopen(fileName".csv","r");
  476.                 if (fp == NULL){
  477.                     return 0;
  478.                 }
  479.                 while(!feof(fp)){
  480.                     input=(char*)calloc(150,sizeof(char));
  481.                     fscanf(fp,150,input);
  482.                     agregarProfe(input);
  483.                 }
  484.                 fclose(fp);
  485.                 break;
  486.             }
  487.         }
  488.     }while(menu);
  489.    
  490. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement