GatoBooster

Ayuda Tarea 1 3°SEM

Mar 24th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.62 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. Considere la siguiente estructura para el tipo de dato Ramo:
  3.  
  4. typedef struct{
  5.    char Nombre[100];
  6.    char Codigo[12];
  7.    char prereq1[12];
  8.    char prereq2[12];
  9. } Ramo;
  10.  
  11.  
  12. Escriba la función Ramo* creaRamo(char line[]), la cual a partir de una linea de texto (line) con el siguiente formato:
  13.  
  14. "Codigo","Nombre","Pre-requisito1","Pre-requisito2",...
  15.  
  16. crea un dato de tipo Ramo y retorna un puntero a éste.
  17.  
  18. Se recomienda usar la función strtok.
  19. _______________________________________________________________________________________________________________________________________
  20. Ramo* creaRamo(char line[]){
  21.  Ramo* linea =(Ramo*) malloc(sizeof(Ramo));
  22. const char s[2] = ",";
  23. char *token;
  24.  
  25.  token=strtok(line, s);
  26.  token[strlen(token)-1]='\0';
  27.  strcpy(linea->Codigo,token+1);
  28.  token=strtok(NULL, s);
  29.  token[strlen(token)-1]='\0';
  30.  strcpy(linea->Nombre,token+1);
  31.  token=strtok(NULL, s);
  32.  token[strlen(token)-1]='\0';
  33.  strcpy(linea->prereq1,token+1);
  34.  strcpy(linea->prereq2,token+strlen(linea->prereq1));
  35.  
  36.  return linea;
  37.  }
  38. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. Considere la siguiente estructura para el tipo de dato Ramo:
  40.  
  41. typedef struct{
  42.    char Nombre[30];
  43.    char Codigo[10];
  44.    char prereq1[10];
  45.    char prereq2[10];
  46. } Ramo;
  47.  
  48. Escriba un código que genere el arreglo Ramo r[] con los ramos del archivo "mallainf.csv".
  49.  
  50. Puede usar (¡no la implemente!) la función Ramo* creaRamo(char[]) de la pregunta anterior.
  51. _______________________________________________________________________________________________________________________________________
  52.     Ramo *r = (Ramo*) malloc (sizeof(Ramo));
  53.     char cadena[10000];
  54.     int i = 1;
  55.     Ramo *ptr = (Ramo*) malloc (sizeof(Ramo));
  56.    
  57.     FILE *entrada = fopen("mallainf.csv","r");
  58.    
  59.     if(entrada == NULL){
  60.         printf("Archivo de entrada no se pudo abrir\n");
  61.         exit(0);
  62.     }
  63.     fgets(cadena,10000,entrada);//NO recibo los datos, porque es una cadena de ejemplo
  64.     while(fgets(cadena,10000,entrada) != NULL){
  65.    
  66.     ptr = creaRamo(cadena);
  67.        r[i-1] = *ptr;
  68.        i++;
  69.        r = (Ramo*) realloc (r,sizeof(Ramo)*i);
  70.     }
  71.     fclose(entrada);
  72. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. Considere las estructuras:
  74.  
  75. typedef struct stack{
  76.     void** data; //arreglo de datos
  77.     int size; // tamaño maximo del arreglo
  78.     int top; // posicion actual de el top de la pila
  79. }stack;
  80.  
  81. typedef struct queue{
  82.     void** data; // arreglo de datos
  83.     int size; //tamaño maximo del arreglo
  84.     int top; //posicion del ultimo dato ingresado en la cola
  85.     int bottom; //posicion anterior al primer dato ingresado en la cola
  86. }queue;
  87.  
  88. Implemente las funciones stack* createStack(int size) y queue* createQueue(int size) que crean la pila y la cola con un arreglo de tamaño size, inicializan sus valores y retornan las variables. Considere que al crear la cola y la pila, sus posiciones iniciales son -1.
  89. _______________________________________________________________________________________________________________________________________
  90. stack* createStack(int size) {
  91.    stack *ptr = (stack*) malloc(sizeof(stack));
  92.    ptr -> data = (void*) malloc(size * sizeof(void*));
  93.    ptr -> size = size;
  94.    ptr -> top = -1;
  95.    return ptr;
  96. }
  97.  
  98. queue* createQueue(int size) {
  99.     queue* ptr = (queue*) malloc(sizeof(queue));
  100.     ptr -> data = (void*) malloc(size * sizeof(void*));
  101.     ptr -> size = size;
  102.     ptr -> top = -1;
  103.     ptr -> bottom = ptr ->top;
  104.     return ptr;
  105. }
  106. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. Considere las siguientes estructuras:
  108.  
  109. typedef struct stack{
  110.     void** data; //arreglo de datos
  111.     int size; // tamaño del arreglo
  112.     int top; // posicion actual de el top de la pila
  113. }stack;
  114.  
  115. typedef struct queue{
  116.     void** data; // arreglo de datos
  117.     int size; //tamaño del arreglo
  118.     int top; //posicion del ultimo dato ingresado en la cola
  119.     int bottom; //posicion anterior al primer dato ingresado en la cola
  120. }queue;
  121.  
  122. Escriba el código para la función void PushStack(stack* P, void* data) y void PushQueue(queue* Q, void* data). Estas funciones agregan el dato a las estructuras según su definición. Puede hacer uso de la funcion realloc en el caso que la pila o la cola ya estén llenas.
  123. _______________________________________________________________________________________________________________________________________
  124. void PushStack (stack* P,void* data) {
  125.     if(P -> size   == P -> top + 1 ) {
  126.         P -> data = (void*) realloc (P->data, 2*P->size*sizeof(void));
  127.         P -> size = 2*P->size;
  128.         P -> top++;
  129.         P -> data[P->top] = data;
  130.     }
  131.     else {
  132.         P -> top++;
  133.         P -> data[P->top] = data;
  134.     }
  135. }
  136. void PushQueue(queue*Q,void* data) {
  137.      if(Q -> size == Q -> top+1) {
  138.         Q -> data = (void*) realloc (Q->data, 2*Q->size*sizeof(void));
  139.         Q -> size= 2*Q->size;  
  140.         Q -> top++;
  141.         Q -> data[Q->top] = data;
  142.     }
  143.     else {
  144.         Q -> top++;    
  145.         Q -> data[Q->top] = data;
  146.     }
  147. }
  148. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  149. Implemente las funciones void* top(stack* S) y void* front(queue* Q). Que retornan el dato del top de la pila y el front de la cola respectivamente. Recuerde que, si las estructuras están vacías, las funciones deben retornar NULL.
  150.  
  151. typedef struct stack{
  152.     void** data; //arreglo de datos
  153.     int size; // tamaño maximo del arreglo
  154.     int top; // posicion actual de el top de la pila
  155. }stack;
  156.  
  157. typedef struct queue{
  158.     void** data; // arreglo de datos
  159.     int size; //tamaño maximo del arreglo
  160.     int top; //posicion del ultimo dato ingresado en la cola
  161.     int bottom; //posicion anterior al primer dato ingresado en la cola
  162. }queue;
  163.  
  164. Nota: considere una pila vacía con top=-1, y una cola vacía con top=bottom
  165. _______________________________________________________________________________________________________________________________________
  166. void* top(stack* S) {
  167.     if (S->top==-1) {
  168.         return NULL;
  169.     }
  170.     else {
  171.        
  172.         return S->data[S->top];
  173.     }
  174. }
  175.  
  176. void* front(queue* S) {
  177.     if (S->bottom == S->top) {
  178.         return NULL;
  179.     }
  180.     else {
  181.         return S->data[S->bottom+1];
  182.     }
  183. }
  184. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  185. Escriba el código para las funciones void PopStack(stack* S) y void PopQueue(queue* Q). Estas funciónes  eliminan un elemento de las estructuras según su definición.
  186.  
  187.  
  188. ArrayStack:
  189.  
  190. typedef struct{
  191.  void** data; //arreglo de punteros (void*)
  192.  int size; //tamaño del arreglo
  193.  int top; //tamaño actual del arreglo
  194. }stack;
  195.  
  196. ArrayQueue:
  197.  
  198. typedef struct{
  199.     void**  data; //arreglo de punteros (void*)
  200.     int size; //tamaño total del arreglo
  201.     int top; //indice de la cima de la cola
  202.     int bottom; //indice del fondo de la cola
  203. }queue;
  204. _______________________________________________________________________________________________________________________________________
  205. void PopStack(stack* S) {
  206.     S-> top--;    
  207. }
  208. void PopQueue(queue* Q) {
  209.     Q-> bottom++ ;
  210. }
  211. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment