• Domingo 19 de Mayo de 2024, 18:54

Autor Tema:  Problema con estructuras  (Leído 1527 veces)

Leber

  • Miembro activo
  • **
  • Mensajes: 65
    • Ver Perfil
Problema con estructuras
« en: Jueves 16 de Octubre de 2008, 00:53 »
0
Buenas, tengo un problema:

Ayer hice una pequeña agenda de notas para practicar un poco, un fichero binario con estructuras, todo funciona bien menos al mostrarme los datos, que hay algunas notas que se repiten( esto no se si es problema de al mostrar datos, o al escribirlos en el fichero)

Por ejemplo:

Yo le inserto en Catalan la nota 1 y hago un listado de notas y me muestra:


Código: Text
  1. Notas [Catalan]:
  2. > 1
  3. Notas [Castellano]:
  4. Notas [Matematicas]:
  5. Notas [Ingles]:
  6.  


Luego inserto la nota 5 en matematicas y al hacer el listado, sorpresa:



Código: Text
  1. Notas [Catalan]:
  2. > 1
  3. > 1
  4. Notas [Castellano]:
  5. Notas [Matematicas]:
  6. > 5
  7. Notas [Ingles]:
  8.  


Como ven, salen 2 "1", uno lo puse yo, pero y el otro? He revisado una y otra vez el codigo y no encuentro nada que pueda ser:

Las estructuras son estas:

Código: Text
  1.  
  2. typedef struct
  3. {
  4.     int nota_catalan;
  5.     int nota_castellano;
  6.     int nota_mates;
  7.     int nota_ingles;
  8.  
  9. }MATERIAS;
  10.  
  11. typedef struct __notas
  12. {
  13.    
  14.     int trimestre;
  15.     MATERIAS m;
  16.     struct __notas *sig;
  17.  
  18. }NOTAS;
  19.  


Las funciones de escribir, leer son estas:

Escribe en un fichero la estructura con las notas


 
Código: Text
  1. int
  2. carga_fichero(int nota, int materia, int trimestre)
  3. {
  4.  
  5. int df;
  6.    nodo p;
  7.  
  8.               if(( df = open("notas.dat",O_WRONLY | O_APPEND | O_CREAT,0600)) == -1)
  9.               {
  10.                       perror("notas.dat");
  11.                  return -1;
  12.               }
  13.  
  14.               p=inicializa(p);
  15.  
  16.               if(!p)
  17.                       return -1;
  18.  
  19.               if( materia == 1)
  20.                       p->m.nota_catalan=nota;
  21.                   else if( materia == 2 )
  22.                       p->m.nota_castellano=nota;
  23.                   else if( materia == 3)
  24.                       p->m.nota_mates=nota;
  25.                   else if( materia == 4)
  26.                       p->m.nota_ingles=nota;
  27.                   else
  28.                   {
  29.                       fprintf(stderr,"Materia [%d] desconocidan",materia);
  30.                       return -1;
  31.                   }
  32.                  
  33.                  
  34.                   p->trimestre=trimestre;
  35.                  
  36.                   write(df,p,sizeof(NOTAS));
  37.  
  38.               free(p);
  39.  
  40.               close(df);
  41.  
  42.       return 0;
  43.  
  44. }
  45.  


Lee fichero y devuelve estructura:



Código: Text
  1. lista
  2. carga_notas(void)
  3. {
  4.     nodo p;
  5.     int d_n;
  6.     int r_bytes;
  7.     lista l=NULL;
  8.     int indice=0;
  9.                       if(( d_n = open("notas.dat",O_RDONLY)) == -1)
  10.                             {
  11.                                 perror("2datos.dat");
  12.                                 return NULL;
  13.                             }
  14.                            
  15.                             p=inicializa(p);
  16.                            
  17.                             if(!p)
  18.                                 return NULL;
  19.                            
  20.                             while(( r_bytes=read(d_n,p,sizeof(NOTAS)) ) > 0)
  21.                      {
  22.                                 p->sig=l;
  23.                                 l=p;
  24.                                 p=inicializa(p);
  25.                             }
  26.  
  27.                      free(p);
  28.  
  29.                      close(d_n);
  30.  
  31.            return l;
  32.  
  33. }
  34.  


Muestra notas




Código: Text
  1. void mostrar_total_notas(lista l, unsigned int assig)
  2. {
  3.     nodo p;
  4.  
  5.        p=l;
  6.  
  7.             while(p)
  8.               {
  9.                   if(assig == 0)
  10.                   {
  11.                      
  12.                       if ( p->m.nota_catalan != 0)
  13.                       {
  14.                           printf("NOTAS CATALANn");
  15.                          printf("> %dn",p->m.nota_catalan);
  16.                       }
  17.                   }
  18.               else if(assig == 1)
  19.                   {
  20.                       if(p->m.nota_castellano != 0)
  21.                       printf("> %dn",p->m.nota_castellano);
  22.                   }
  23.                   else if(assig == 2)
  24.                   {
  25.                       if(p->m.nota_mates != 0)
  26.                       printf("> %dn",p->m.nota_mates);
  27.                   }
  28.                   else
  29.                   {
  30.                       if(p->m.nota_ingles != 0)
  31.                       printf("> %dn",p->m.nota_ingles);
  32.                   }
  33.  
  34.                   p=p->sig;
  35.               }
  36. }
  37.  


Yo no le veo nada extraño, y de hecho no se porque me lo hace..., a ver si alguien puede arrojar un poco de luz, porque estoy algo preucupado.

Muchas gracias

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: Problema con estructuras
« Respuesta #1 en: Jueves 16 de Octubre de 2008, 09:54 »
0
No has especificado el tipo de dato lista. Parece un puntero, por favor postea su especificación, un saludo.

Leber

  • Miembro activo
  • **
  • Mensajes: 65
    • Ver Perfil
Re: Problema con estructuras
« Respuesta #2 en: Jueves 16 de Octubre de 2008, 14:52 »
0
Primero de todo gracias por responder, y gastar un poco de tiempo, aqui van las declaraciones y luego la aplicacion:

Decir que, solo se repite la primera asignatura que escribo una nota, es decir, si arranco el programa y le inserto un 4 a matematicas, me mostrara un 4, como si el programa escribiese un 4 siempre en la nota, las demas asignaturas me las muestro bien.

notas.h
Código: Text
  1. #if !defined(_NOTAS_H)
  2. #define _NOTAS_H
  3.  
  4.  
  5. typedef struct
  6. {
  7.     int nota_catalan;
  8.         int nota_castellano;
  9.     int nota_mates;
  10.     int nota_ingles;
  11.  
  12. }MATERIAS;
  13.  
  14. typedef struct __notas
  15. {
  16.     int trimestre;
  17.     MATERIAS m;
  18.     struct __notas *sig;
  19.  
  20. }NOTAS;
  21.  
  22.  
  23. typedef NOTAS *nodo;
  24. typedef NOTAS *lista;
  25.  
  26. int menu();
  27. int leer_notas(lista l);
  28. int escribir_nota(lista l);
  29. int escribir_trabajo(lista l);
  30. int total_tp(lista l);
  31. int fin(lista l);
  32.  
  33.  
  34. void mostrar_notas(lista l, int tipox);
  35. int getint();
  36. int mostrar_tipos();
  37. int carga_fichero(int nota, int materia, int trimestre);
  38. void carga_notas(lista l);
  39. void _liberar_mem_struct(lista l);
  40.  
  41. int(*pfunc[])(lista l)=
  42. {
  43.     leer_notas,
  44.    escribir_nota,
  45.     escribir_trabajo,
  46.     total_tp,
  47.     fin,
  48.    
  49. };
  50.  
  51. #endif
  52.  
  53.  

Apli:

Código: Text
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "notas.h"
  6. #include <fcntl.h>
  7.  
  8. #define BUFSIZE 1024
  9.  
  10. void mostrar_notas(lista l, int tipox);
  11.  
  12. int getint(void)
  13. {
  14.     char c;
  15.     int nbytes;
  16.     char BUFFER[BUFSIZE];
  17.  
  18.  
  19.            nbytes=read(0,&c,1);
  20.  
  21.              if(nbytes != 1)
  22.              {
  23.                  perror("READ ");
  24.                  return -1;
  25.              }
  26.  
  27.              nbytes=read(0,BUFFER,BUFSIZE-1);
  28.              memset(BUFFER,'',BUFSIZE);  
  29.            
  30.              return (c-'0');
  31. }
  32.  
  33. int
  34. main()
  35. {
  36.  
  37.     lista l=NULL;
  38.     int cod_error;
  39.  
  40.             while(!0)
  41.                if(( cod_error=pfunc[menu()](l) ) == -1 )
  42.                     pfunc[4](l);
  43.            
  44.     return 0;
  45. }    
  46. int
  47. menu(void)
  48. {
  49.  
  50.     int elec=0;
  51.  
  52.                printf("Agenda de notas-n");
  53.                 printf("ntOpciones:
  54.                           nt1- Leer notas
  55.                           nt2- Escribir nota
  56.                         nt3- Escribir trabajo
  57.                           nt4- Total tp
  58.                           nt5 -Fn");
  59.  
  60.                   puts("--");
  61.  
  62.                  elec=(getint())-1;
  63.          
  64.                      
  65.  
  66.     return (elec < 0 || elec > 4 ) ? 4 : elec;
  67. }
  68.  
  69. int
  70. escribir_nota(lista l)
  71. {
  72.    
  73.     int (*ptr_wr)();
  74.     unsigned int m=0;
  75.    unsigned int t=0;
  76.     unsigned int n=0;
  77.    int cod_error;
  78.  
  79.                ptr_wr=carga_fichero;
  80.        
  81.                 printf("Materia:n");
  82.                     printf("nt1- Catalan
  83.                               nt2- Castellano
  84.                               nt3- Matematicas
  85.                               nt4- Inglesn");
  86.  
  87.  
  88.                     printf("Escoga->n");
  89.                m=getint();
  90.  
  91.                     printf("Nota:n");              
  92.  
  93.                     n=getint();
  94.  
  95.                printf("Trimestre:n");
  96.                    
  97.                     t=getint();
  98.              
  99.  
  100.                     if( (cod_error=((*ptr_wr)(n,m,t)) ) == -1)
  101.                     {
  102.                         fprintf(stderr,"Error al escribir en el ficheron");
  103.                         return -1;
  104.                     }
  105.  
  106.                
  107.     return 0;
  108. }
  109.  
  110. int
  111. mostrar_tipos(void)
  112. {
  113.     int opcion=0;
  114.  
  115.           printf("nt1- Visualizar por asignatura
  116.                     nt2- Visualizar media de una asignatura
  117.                     nt3- Visualizar media de todas las asignaturas
  118.                     n");
  119.  
  120.             opcion=getint();
  121.  
  122.             return opcion;
  123.  
  124. }
  125.  
  126. void mostrar_total_notas(lista l, unsigned int assig)
  127. {
  128.     nodo p;
  129.  
  130.        p=l;
  131.  
  132.             while(p)
  133.               {
  134.                   if(assig == 0)
  135.                   {
  136.                      
  137.                       if ( p->m.nota_catalan != 0)
  138.                       {
  139.                           printf("NOTAS CATALANn");
  140.                          printf("> %dn",p->m.nota_catalan);
  141.                       }
  142.                   }
  143.               else if(assig == 1)
  144.                   {
  145.                       if(p->m.nota_castellano != 0)
  146.                       printf("> %dn",p->m.nota_castellano);
  147.                   }
  148.                   else if(assig == 2)
  149.                   {
  150.                       if(p->m.nota_mates != 0)
  151.                       printf("> %dn",p->m.nota_mates);
  152.                   }
  153.                   else
  154.                   {
  155.                       if(p->m.nota_ingles != 0)
  156.                       printf("> %dn",p->m.nota_ingles);
  157.                   }
  158.  
  159.                   p=p->sig;
  160.               }
  161.  
  162.            
  163.  
  164. }
  165.  
  166. float media_assignaturas(lista l, int materia_t)
  167. {
  168. nodo p;
  169. int n_notas=0;
  170. float n_media=0;
  171.  
  172.  p=l;
  173.  
  174.  printf("Mat->%dn",materia_t);
  175.  
  176.                 if(materia_t != 0)
  177.                      {
  178.                          while(p)
  179.                          {
  180.                     if(materia_t == 1)
  181.                           {
  182.                              if(p->m.nota_catalan != 0)
  183.                               {
  184.                                   printf("NOTAS CATALANan");
  185.                                 n_media+=p->m.nota_catalan;
  186.                                 n_notas++;
  187.                               }
  188.                           }
  189.                           else if(materia_t == 2)
  190.                           {
  191.                               if(p->m.nota_castellano != 0)
  192.                               {
  193.                                   n_media+=p->m.nota_castellano;
  194.                                   n_notas++;
  195.                               }
  196.                           }
  197.                           else if(materia_t == 3)
  198.                           {
  199.                               if(p->m.nota_mates != 0)
  200.                               {
  201.                                   n_media+=p->m.nota_mates;
  202.                                   n_notas++;
  203.                               }
  204.                           }
  205.                           else
  206.                           {
  207.                               if(p->m.nota_ingles != 0)
  208.                                                          n_notas++;
  209.                                                   }
  210.                           }
  211.                            p=p->sig;
  212.                          }
  213.  
  214.                 }
  215.  
  216.                      
  217.  
  218.     return (n_media/n_notas);
  219.  
  220. }
  221.  
  222. void
  223. mostrar_notas(lista l, int tipox)
  224. {
  225. const char *const asignaturas[]={"Catalan","Castellano",
  226.                                  "Matematicas","Ingles"};
  227. unsigned int _numero_assig=0;
  228. unsigned int assig=0;  
  229. float media_t;
  230.                         switch(tipox)
  231.                              {  
  232.                                 default: case 1:
  233.                                                    
  234.                                     while( _numero_assig < 4 )
  235.                                                {
  236.                                                   printf("Notas [%s]:n",asignaturas[_numero_assig]);
  237.                                                   mostrar_total_notas(l,_numero_assig);
  238.                                                   _numero_assig++;
  239.                                                 }  
  240.                                       break;
  241.                  
  242.                                                    
  243.                                 case 2:      
  244.                                       printf("Assignatura:n");
  245.                                                  assig=getint();
  246.  
  247.                                                   if(assig < 1 || assig > 4 )
  248.                                                   {
  249.                                                       fprintf(stderr,"Asignatura fuera de rangon");
  250.                                                       return;
  251.                                                   }
  252.  
  253.                                       media_t=media_assignaturas(l,assig);
  254.  
  255.                 printf(">Media [%s] %4.2fn",asignaturas[assig-1],media_t);
  256.  
  257.                                              break;
  258.  
  259.                                   case 3:
  260.                                                   while(assig++ < 4)
  261.                                                   {
  262.                                                       media_t=media_assignaturas(l,assig);
  263.                                                       printf("> Media [%s] -> %4.2fn",asignaturas[assig-1],media_t);
  264.                                       }
  265.                                                   break;
  266.  
  267.                              }
  268.  
  269.                                 _liberar_mem_struct(l);
  270.                                    
  271.                  
  272. }
  273.  
  274. int
  275. leer_notas(lista l)
  276. {
  277.  
  278.   nodo p;
  279.  
  280.            l=NULL;
  281.            
  282.              
  283.          
  284.            if(( l=carga_notas() ) != NULL)
  285.             {
  286.                     /*p=l;
  287.  
  288.                     while(p)
  289.                     {
  290.                        
  291.                        
  292.                        
  293.                     }*/
  294.  
  295.                   mostrar_notas(l, mostrar_tipos());
  296.                 return 0;
  297.              }
  298.  
  299.   return -1;
  300. }
  301.  
  302. int fin(lista l)
  303. {
  304.     _liberar_mem_struct(l);
  305.  
  306.     exit(0);
  307. }
  308.  
  309. int carga_fichero(int nota, int materia, int trimestre)
  310. {
  311.     int df;
  312.    nodo p;  
  313.    static int veces=0;
  314.     printf("CARGA FICHEROn");
  315.  
  316.            
  317.                   if((df=open("notas.dat",O_WRONLY | O_APPEND)) == -1)
  318.                   {
  319.                       perror("2notas.dat");
  320.                       return -1;
  321.                   }
  322.              
  323.        
  324.             p=inicializa(p);
  325.  
  326.             if(!p)
  327.                 return -1;
  328.  
  329.                 if( materia == 1)
  330.                          p->m.nota_catalan=nota;
  331.                      else if( materia == 2 )
  332.                          p->m.nota_castellano=nota;
  333.                      else if( materia == 3)
  334.                          p->m.nota_mates=nota;
  335.                      else if( materia == 4)
  336.                          p->m.nota_ingles=nota;
  337.                      else
  338.                      {
  339.                          fprintf(stderr,"Materia [%d] desconocidan",materia);
  340.                          return -1;
  341.                      }
  342.  
  343.                      p->trimestre=trimestre;
  344.  
  345.                
  346.  
  347.                     write(df,p,sizeof(NOTAS));
  348.  
  349.                     free(p);
  350.                 num_free++;
  351.             close(df);
  352.  
  353.         return 0;
  354.            
  355. }
  356.  
  357. lista carga_notas(void)
  358. {
  359.         nodo p;
  360.         int d_n;
  361.         int r_bytes=0;
  362.         lista l=NULL;
  363.         int indice=0;
  364.  
  365.                       if(( d_n = open("notas.dat",O_RDONLY)) == -1)
  366.                             {
  367.                                   perror("2datos.dat");
  368.                                   return NULL;
  369.                            }
  370.  
  371.              
  372.                          p=inicializa(p);
  373.                
  374.                          if(!p)
  375.                            return NULL;
  376.                      
  377.                          while(( r_bytes=read(d_n,p,sizeof(NOTAS)) ) > 0)
  378.                          {    
  379.                             p->sig=l;
  380.                             l=p;
  381.                             p=inicializa(p);
  382.                          }
  383.                      
  384.                          free(p);
  385.                          num_free++;
  386.                          close(d_n);
  387.                      
  388.               return l;
  389.  
  390. }
  391. nodo inicializa( nodo p)
  392. {
  393.        num_malloc++;
  394.        p=(nodo)malloc(sizeof(NOTAS));
  395.  
  396.        return p;      
  397.  
  398. }  
  399.  
  400.  

Gracias antemano

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: Problema con estructuras
« Respuesta #3 en: Jueves 16 de Octubre de 2008, 16:01 »
0
Mi opinión es que lo haces algo sencillo de una manera excesivamente complicada...

¿Puedes explicarme qué pretendes con lo siguiente?

Código: C
  1.  
  2. int(*pfunc[])(lista l)=
  3. {
  4.     leer_notas,
  5.     escribir_nota,
  6.     escribir_trabajo,
  7.     total_tp,
  8.     fin,  
  9. };
  10.  
  11.  

Leber

  • Miembro activo
  • **
  • Mensajes: 65
    • Ver Perfil
Re: Problema con estructuras
« Respuesta #4 en: Jueves 16 de Octubre de 2008, 16:21 »
0
Si, perdona esque pretendia que las funciones principales tuvieran los mismos parametros y devolvieran lo mismo, asi hacer un while y el valor que retorne la funcion menu, pues ir a sea funcion, pero he simplificado el codigo eliminando tonterias para intentar localizar el problema, pero sigo sin dar con ello.

Código: Text
  1. #if !defined(_NOTAS_H)
  2. #define _NOTAS_H
  3.  
  4. int menu(void);
  5.  
  6. typedef struct
  7. {
  8.     int nota_catalan;
  9.     int nota_castellano;
  10.     int nota_mates;
  11.     int nota_ingles;
  12.  
  13. }MATERIAS;
  14.  
  15. typedef struct __notas
  16. {
  17.    
  18.     int trimestre;
  19.     MATERIAS m;
  20.     struct __notas *sig;
  21.  
  22. }NOTAS;
  23. typedef NOTAS *nodo;
  24. typedef NOTAS *lista;
  25.  
  26. int leer_notas(void);
  27. int escribir_nota(void);
  28. int fin(void);
  29.  
  30. lista carga_notas(lista l);
  31. nodo inicializa(nodo p);
  32. int carga_fichero(int nota, int materia, int trimestre);
  33.  
  34.  int(*pfunc[])(void)=
  35. {
  36.     leer_notas,
  37.    escribir_nota,
  38.     fin,
  39.    
  40. };
  41.  
  42. #endif
  43.  
  44.  
  45.  

Código: Text
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "notas.h"
  6. #include <fcntl.h>
  7.  
  8. #define BUFSIZE 1024
  9.  
  10. void mostrar_notas(lista l, int tipox);
  11.  
  12. static int num_malloc=0;
  13. static int num_free=0;
  14.  
  15.  
  16.  
  17. int getint(void)
  18. {
  19.     char c;
  20.     int nbytes;
  21.     char BUFFER[BUFSIZE];
  22.  
  23.  
  24.            nbytes=read(0,&c,1);
  25.  
  26.              if(nbytes != 1)
  27.              {
  28.                  perror("READ ");
  29.                  return -1;
  30.              }
  31.  
  32.              while(strlen(fgets(BUFFER,BUFSIZE,stdin)) > 1)
  33.                  memset(BUFFER,'',sizeof(BUFFER));
  34.              return (c-'0');
  35. }
  36.  
  37. int
  38. main()
  39. {
  40.  
  41.    
  42.     int cod_error;
  43.  
  44.             while(!0)
  45.                if(( cod_error=pfunc[menu()]() ) == -1 )
  46.                     pfunc[2]();
  47.            
  48.     return 0;
  49. }  
  50.  
  51. int
  52. menu(void)
  53. {
  54.  
  55.     int elec=0;
  56.    
  57.  
  58.                  
  59.  
  60.                printf("Agenda de notas-n");
  61.                 printf("ntOpciones:
  62.                           nt1- Leer notas
  63.                           nt2- Escribir nota
  64.                           nt3 -Fn");
  65.  
  66.                   puts("--");
  67.  
  68.                  elec=(getint())-1;
  69.  
  70.  
  71.                          
  72.          
  73.                      
  74.  
  75.     return (elec < 0 || elec > 4 ) ? 4 : elec;
  76. }
  77.  
  78. int
  79. escribir_nota(void)
  80. {
  81.    
  82.    
  83.     unsigned int m=0;
  84.    unsigned int t=0;
  85.     unsigned int n=0;
  86.    int cod_error;
  87.  
  88.                ptr_wr=carga_fichero;
  89.        
  90.                 printf("Materia:n");
  91.                     printf("nt1- Catalan
  92.                               nt2- Castellano
  93.                               nt3- Matematicas
  94.                               nt4- Inglesn");
  95.  
  96.  
  97.                     printf("Escoga->n");
  98.                m=getint();
  99.  
  100.                     printf("Nota:n");              
  101.  
  102.                     n=getint();
  103.  
  104.                printf("Trimestre:n");
  105.                    
  106.                     t=getint();
  107.              
  108.  
  109.                     if( (cod_error=(carga_fichero(n,m,t))  == -1)
  110.                     {
  111.                         fprintf(stderr,"Error al escribir en el ficheron");
  112.                         return -1;
  113.                     }
  114.  
  115.                
  116.     return 0;
  117. }
  118.  
  119.  
  120. int
  121. mostrar_tipos(void)
  122. {
  123.     int opcion=0;
  124.  
  125.           printf("nt1- Visualizar por asignatura
  126.                     nt2- Visualizar media de una asignatura
  127.                     nt3- Visualizar media de todas las asignaturas
  128.                     n");
  129.  
  130.             opcion=getint();
  131.  
  132.             return opcion;
  133.  
  134. }
  135.  
  136.  
  137.  
  138. void mostrar_total_notas(lista l, unsigned int assig)
  139. {
  140.     nodo p;
  141.  
  142.        p=l;
  143.  
  144.             while(p)
  145.               {
  146.                   if(assig == 0)
  147.                   {
  148.                      
  149.                      // if ( p->m.nota_catalan != 0)
  150.                           printf("> %dn",p->m.nota_catalan);
  151.                      
  152.                    }
  153.                                   else if(assig == 1)
  154.                   {
  155.                       //if(p->m.nota_castellano != 0)
  156.                       printf("> %dn",p->m.nota_castellano);
  157.                   }
  158.                   else if(assig == 2)
  159.                   {
  160.                       //if(p->m.nota_mates != 0)
  161.                       printf("> %dn",p->m.nota_mates);
  162.                   }
  163.                   else
  164.                   {
  165.                       //if(p->m.nota_ingles != 0)
  166.                       printf("> %dn",p->m.nota_ingles);
  167.                   }
  168.  
  169.                   p=p->sig;
  170.               }
  171.  
  172.              
  173.  
  174. }
  175.  
  176.  
  177.  
  178. float media_assignaturas(lista l, int materia_t)
  179. {
  180.  
  181.     nodo p;
  182.     int n_notas=0;
  183.     float n_media=0;
  184.  
  185.  p=l;
  186.  
  187.  
  188.  
  189.                                                if(materia_t != 0)
  190.                            {
  191.                          while(p)
  192.                          {
  193.                                                     if(materia_t == 1)
  194.                           {
  195.                              if(p->m.nota_catalan != 0)
  196.                               {
  197.                                  
  198.                                 n_media+=p->m.nota_catalan;
  199.                                 n_notas++;
  200.                               }
  201.                           }
  202.                           else if(materia_t == 2)
  203.                           {
  204.                               if(p->m.nota_castellano != 0)
  205.                               {
  206.                                   n_media+=p->m.nota_castellano;
  207.                                   n_notas++;
  208.                               }
  209.                           }
  210.                           else if(materia_t == 3)
  211.                           {
  212.                               if(p->m.nota_mates != 0)
  213.                               {
  214.                                   n_media+=p->m.nota_mates;
  215.                                   n_notas++;
  216.                               }
  217.                           }
  218.                           else
  219.                           {
  220.                               if(p->m.nota_ingles != 0)
  221.                               {
  222.                                   n_media+=p->m.nota_ingles;
  223.                                                                  n_notas++;
  224.                                                           }
  225.                           }
  226.                            p=p->sig;
  227.                          }
  228.  
  229.                 }
  230.  
  231.                      
  232.  
  233.     return (n_media/n_notas);
  234.  
  235. }
  236.  
  237.  
  238.  
  239. void _liberar_mem_struct(lista l)
  240. {
  241.     nodo p;
  242.  
  243.  
  244.          while(l)
  245.          {
  246.               p=l;
  247.               l=p->sig;
  248.               free(p);
  249.               num_free++;
  250.            
  251.        }
  252.           printf("Free-> %dtMalloc-> %dn",num_free,num_malloc);
  253. }
  254.  
  255.  
  256. void
  257. mostrar_notas(lista l, int tipox)
  258. {
  259.  char *[]={"Catalan","Castellano",
  260.                                  "Matematicas","Ingles"};
  261. unsigned int _numero_assig=0;
  262. unsigned int assig=0;  
  263. float media_t;
  264.                                                             switch(tipox)
  265.                                         {  
  266.                                        default: case 1:
  267.                                                    
  268.                                                                                           while( _numero_assig < 4 )
  269.                                                {
  270.                                                   printf("Notas [%s]:n",asignaturas[_numero_assig]);
  271.                                                   mostrar_total_notas(l,_numero_assig);
  272.                                                   _numero_assig++;
  273.                                               }  
  274.                                                                                  break;
  275.                  
  276.                                                    
  277.                                 case 2:      
  278.                                                                                          printf("Assignatura:n");
  279.                                                  assig=getint();
  280.  
  281.                                                   if(assig < 1 || assig > 4 )
  282.                                                   {
  283.                                                       fprintf(stderr,"Asignatura fuera de rangon");
  284.                                                       return;
  285.                                                   }
  286.  
  287.                                                                                   media_t=media_assignaturas(l,assig);
  288.  
  289.                                          printf(">Media [%s] ->%4.2fn",asignaturas[assig-1],media_t);
  290.  
  291.                                              break;
  292.  
  293.                                   case 3:
  294.                                          while(assig++ < 4)
  295.                                           {
  296.                                           media_t=media_assignaturas(l,assig);
  297.                                           printf("> Media [%s] ->  %4.2fn",asignaturas[assig-1],media_t);
  298.                                                                                   }
  299.                                             break;
  300.  
  301.                                                    }
  302.  
  303.                                
  304.                                    
  305.                  
  306. }
  307.  
  308.  
  309. int
  310. leer_notas(void)
  311. {
  312.  
  313.   lista l=NULL;
  314.  
  315.            if(( l=carga_notas(l) ) != NULL)
  316.             {
  317.               mostrar_notas(l, mostrar_tipos());
  318.            _liberar_mem_struct(l);
  319.                 return 0;
  320.              }
  321.  
  322.              
  323.  
  324.   return -1;
  325. }
  326.  
  327.  
  328.  
  329. int fin(void)
  330. {
  331.    
  332.     exit(0);
  333. }
  334.  
  335.  
  336.  
  337. int carga_fichero(int nota, int materia, int trimestre)
  338. {
  339.   int df;
  340.    nodo p;  
  341.  
  342.    
  343.  
  344.            
  345.                   if((df=open("notas.dat",O_WRONLY | O_APPEND | O_CREAT,0600)) == -1)
  346.                   {
  347.                       perror("2notas.dat");
  348.                       return -1;
  349.                   }
  350.              
  351.        
  352.             p=inicializa(p);
  353.  
  354.             if(!p)
  355.             return -1;
  356.  
  357.                                          if( materia == 1)
  358.                          p->m.nota_catalan=nota;
  359.                      else if( materia == 2 )
  360.                          p->m.nota_castellano=nota;
  361.                      else if( materia == 3)
  362.                          p->m.nota_mates=nota;
  363.                      else if( materia == 4)
  364.                          p->m.nota_ingles=nota;
  365.                      else
  366.                      {
  367.                          fprintf(stderr,"Materia [%d] desconocidan",materia);
  368.                          return -1;
  369.                      }
  370.  
  371.                      p->trimestre=trimestre;
  372.  
  373.                
  374.  
  375.                     write(df,p,sizeof(NOTAS));
  376.  
  377.                     free(p);
  378.                                     num_free++;
  379.                     close(df);
  380.  
  381.         return 0;
  382.            
  383. }
  384.  
  385.  
  386. lista carga_notas(lista l)
  387. {
  388.         nodo p;
  389.         int d_n;
  390.         int r_bytes=0;
  391.        
  392.  
  393.                       if(( d_n = open("notas.dat",O_RDONLY)) == -1)
  394.                 {
  395.                                    perror("datos.dat");
  396.                    return NULL;
  397.                 }
  398.  
  399.              
  400.                          p=inicializa(p);
  401.                
  402.                          if(!p)
  403.                            return NULL;
  404.                      
  405.                          while(( r_bytes=read(d_n,p,sizeof(NOTAS)) ) > 0)
  406.                          {    
  407.                             p->sig=l;
  408.                             l=p;
  409.                             p=inicializa(p);
  410.                          }
  411.  
  412.                          
  413.                          free(p);
  414.                          num_free++;
  415.                          close(d_n);
  416.                      
  417.               return l;
  418.  
  419. }
  420.  
  421.  
  422. nodo inicializa( nodo p)
  423. {
  424.        num_malloc++;
  425.        p=(nodo)malloc(sizeof(NOTAS));
  426.  
  427.        return p;        
  428.  
  429. }
  430.  
  431.  
  432.  

Aun asi, he arreglado un par de cosillas y tampoco, me repite la nota de la primera asignatura que entro, no se donde más mirar.

Gracias

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: Problema con estructuras
« Respuesta #5 en: Jueves 16 de Octubre de 2008, 16:35 »
0
Hmm, no sé si estoy ciego  :bad: , pero sigo sin encontrar la definición de lista...

Eternal Idol

  • Moderador
  • ******
  • Mensajes: 4696
  • Nacionalidad: ar
    • Ver Perfil
Re: Problema con estructuras
« Respuesta #6 en: Jueves 16 de Octubre de 2008, 17:39 »
0
Cita de: "m0skit0"
Hmm, no sé si estoy ciego  :bad: , pero sigo sin encontrar la definición de lista...

Código: Text
  1. typedef NOTAS *lista;
  2.  

Nacional y Popular En mi país la bandera de Eva es inmortal.


Queremos una Argentina socialmente justa, económicamente libre y  políticamente soberana.
¡Perón cumple, Evita dignifica!


La mano invisible del mercado me robo la billetera.

Leber

  • Miembro activo
  • **
  • Mensajes: 65
    • Ver Perfil
Re: Problema con estructuras
« Respuesta #7 en: Jueves 16 de Octubre de 2008, 21:34 »
0
Si, ahi tienes la deficion.  Creo que se donde esta el posible fallo:

En la funcion cargar_notas, hago esto para leer las notas del fichero y pasarlas a la estructura:

Código: Text
  1. # while(( r_bytes=read(d_n,p,sizeof(NOTAS)) ) > 0)
  2. #                          {    
  3. #                             p->sig=l;
  4. #                             l=p;
  5. #                             p=inicializa(p);   //si es la ultima vez, l apunta a que? porque p pierde su valor
  6. #                          }
  7. #  
  8. #                          
  9. #                          free(p);
  10.  

Creo que es ahi, ya que en ser la ultima iteracion, inicializamos p, pero ya no quedan mas datos, entonces se pierde ese puntero, no?

Yo diria que si, pero estoy tan desquiciado con esto que quiza veo errores donde no los hay.

Saludos