Advertisement
TecaFondo

Untitled

Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.96 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. ==================================================================
  187. /
  188. /
  189. /
  190. /
  191. ==================================================================
  192.  
  193. void leeram (ramo* ram, char* str){
  194.    const char s[2] = ",";
  195.    const char corte[2]= "\n"
  196.    char *token;
  197.    /* get the first token */
  198.    token = strtok(str, s);
  199.    ram->codigo=token;
  200.    token=strtok(NULL,s);
  201.    ram->nombre=token;
  202.    token=strtok(NULL,s);
  203.    ram->semestre=token;
  204.    token=strtok(NULL,s);
  205.    ram->creditos=token;
  206.    /* walk through other tokens */
  207.    while(token != NULL) {
  208.       token = strtok(NULL, corte);
  209.       ram->pre=token;
  210.    }
  211.    return(0);
  212. }
  213.  
  214. void agregarAsignatura(char* info){
  215.     ramo* ram = (ramo*)calloc(sizeof(ramo));
  216.     leeram(ram, info);
  217. }
  218. void agregarProfesor(treemap profes){
  219.     int rut;
  220.     int switchmenu = 1;
  221.     char **ramo_actual;
  222.  
  223.     printf("Ingrese el rut sin puntos ni guion\n");
  224.     scanf("%d", &rut);
  225.  
  226.     if(search(profes, rut)){
  227.         while(switchmenu){
  228.             printf("Ingrese codigo del ramo\n");
  229.             scanf("%s", &ramo_actual);
  230.  
  231.             if(profes->current->data->max_ramos == profes->current->data->last-1){
  232.                 profes->current->data->ramos = realloc(sizeof(profes->current->data->ramos)*2);      //ver si manda error luego y arreglar los *s
  233.             }
  234.             profes->current->data->ramos[profes->current->data->last+1]=ramo_actual;
  235.             profes->current->data->last++;
  236.  
  237.             printf("1 - Ingresar otra asignatura \n0 - Salir \nIngrese una opcion\n");
  238.             scanf("%d", &switchmenu);
  239.         }
  240.     }
  241.     else{
  242.         profe profe_actual;
  243.         profe_actual->rut = rut;
  244.         profe_actual->max_ramos = 1;
  245.         profe_actual->last = -1;
  246.         profe_actual->ramos = (void**) malloc(sizeof(void*));
  247.  
  248.         printf("Ingrese nombre\n");
  249.         scanf("%s", &profe_actual->nombre);
  250.  
  251.         while(switchmenu){
  252.             printf("Ingrese codigo del ramo\n");
  253.             scanf("%s", &ramo_actual);
  254.  
  255.             if(profes->current->data->max_ramos == profes->current->data->last){
  256.                 profes->current->data->ramos = realloc(sizeof(profes->current->data->ramos));      //ver si manda error luego y arreglar los *s
  257.             }
  258.             profes->current->data->ramos[profes->current->data->last+1]=ramo_actual;
  259.             profes->current->data->last++;
  260.  
  261.             printf("1 - Ingresar otra asignatura \n0 - Salir \nIngrese una opcion\n");
  262.             scanf("%d", &switchmenu);
  263.         }
  264.  
  265.         insert(profes, profe_actual->rut ,profe_actual);
  266. }
  267.    
  268. void MostrarAsignaturas(treemap* mapa){
  269.     int menu=0;
  270.     do{
  271.         printf("Como quiere ver sus asignaturas?\n");
  272.         printf("1.- Ordenar por nombre\n");
  273.         printf("2.- Ordenar por codigo\n");
  274.         printf("3.- Ordenar por semestre\n");
  275.         scanf("%d",&menu);
  276.         switch(menu){
  277.             case 1:{
  278.                 ramo* aux=first(ramo_nombre);
  279.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  280.                 while(next(ramo_nombre)){
  281.                     aux=next(ramo_nombre);
  282.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  283.                     printf("%s\n", );
  284.                 }
  285.                 break;
  286.             }
  287.             case 2:{
  288.                 ramo* aux=first(mapa);
  289.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  290.                 while(next(mapa)){
  291.                     aux=next(mapa);
  292.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  293.                     printf("%s\n", );
  294.                 }
  295.                 break;
  296.             }
  297.             case 3:{
  298.                 ramo* aux=first(ramo_semestre);
  299.                 printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  300.                 while(next(ramo_semestre)){
  301.                     aux=next(semestre);
  302.                     printf("%s,%s,%s,%s,%s\n",aux->codigo,aux->nombre,aux->semestre,aux->creditos);
  303.                     printf("%s\n", );
  304.                 }
  305.                 break;
  306.             }
  307.         }
  308.     }while(menu);
  309.    
  310. }
  311. void MostrarProfesor(){
  312.     int menu=0;
  313.     do{
  314.         printf("1.- Mostrar profesores por apellido\n");
  315.         printf("2.- Mostrar profesores por rut\n");
  316.         scanf("%d",&menu);
  317.         switch(menu){
  318.             case 1:{
  319.                
  320.             }
  321.             case 2:{
  322.                
  323.             }
  324.         }
  325.     }while(menu);
  326.  
  327. }
  328. void MostrarInfo(){
  329.  
  330. }
  331. void guradar(){
  332.  
  333. }
  334. void cargarDatos(){
  335.     do{
  336.         printf("1.- Cargar datos asignatura\n");
  337.         printf("2.- Cargar datos profesores\n");
  338.         printf("0.- Salir\n");
  339.         scanf("%d",&menu);
  340.         switch(menu){
  341.             case 1:{
  342.                 FILE *fp;
  343.                 char* input;
  344.                 char fileName= (char*)calloc(25,sizeof(char));
  345.                 printf("Ingresar nombre de archivo\n");
  346.                 scanf("%s",&fileName);
  347.                 fp = fopen(fileName".cvs","r");
  348.                 if (fp == NULL){
  349.                     return 0;
  350.                 }
  351.                 while(!feof(fp)){
  352.                     input=(char*)calloc(150,sizeof(char));
  353.                     fscanf(fp,150,input);
  354.                     agregarAsignatura(input);
  355.                 }
  356.             }
  357.             case 2:{
  358.                 FILE *fp;
  359.                 char fileName= (char*)calloc(25,sizeof(char));
  360.                 printf("Ingresar nombre de archivo\n");
  361.                 scanf("%s",&fileName);
  362.                 fp = fopen(fileName".cvs","r");
  363.                 if (fp == NULL){
  364.                     return 0;
  365.                 }
  366.             }
  367.         }
  368.     }while(menu);
  369.    
  370.     char nombre[100];
  371.     long key=0;
  372.     while(fgets(nombre,100,fp)){
  373.         strtok(nombre,"\n");
  374.         key = StringToInt(nombre);
  375.         char* dato = (char*)malloc(100*sizeof(char));
  376.         strcpy(dato,nombre);
  377.         insert(Peliculas,key,dato);
  378.     }
  379.     return 1;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement