elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / ayuda con explicacion de codigo en: 23 Junio 2010, 23:51 pm
hola a todos me podrian dar una pequeña clase y explicar las lineas d codigos de este pequeño fragemento de codigos.
gracias de antemano
saludos cordiales


Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <iostream>
  6. #define MAXSTR 0xFF
  7.  
  8. using namespace std;
  9.  
  10. typedef struct ClaseAlumno{
  11.    int matricula;
  12.    char* nombre;
  13. }Alumno;
  14.  
  15. int comparar_alumno (const void *alumno1, const void *alumno2);
  16. void imprimir_alumno (Alumno *alumno);
  17. Alumno* agregar_alumno (Alumno *alumnos, Alumno *alumno, int *n);
  18. char* leer_linea    (const char *mensaje);
  19. char* leer_cadena   (const char *mensaje, char *cadena);
  20. int   leer_entero   (const char *mensaje, int menor, int mayor);
  21. float leer_real     (const char *mensaje);
  22. char  leer_caracter (const char *mensaje);
  23. int   leer_campo (FILE *archivo, char *cadena);
  24.  
  25. int main(){
  26.    Alumno alumno, *alumnos = NULL, *p;
  27.    int opcion, n=0, i;
  28.    char cadena[MAXSTR], *ruta = "D:\\alumnos.txt";
  29.    FILE *archivo;
  30.    archivo = fopen(ruta, "r");
  31.    if (archivo!=NULL)
  32.    {
  33.        while (leer_campo(archivo, cadena))
  34.        {
  35. alumno.matricula = atoi(cadena);
  36.            leer_campo(archivo, cadena);
  37.            alumno.nombre = strdup (cadena);
  38.            alumnos = agregar_alumno(alumnos, &alumno, &n);
  39.        }
  40.        fclose(archivo);
  41.    }
  42.    do{
  43.        system ("cls");
  44.        cout<<endl
  45. <<"MENU DEL SISTEMA DE MATRICULA DE ALUMNOS"<<endl
  46. <<"1.- Agregar nuevo alumno"<<endl
  47. <<"2.- Consultar un alumno"<<endl
  48. <<"3.- Modificar un alumno"<<endl
  49. <<"4.- Eliminar un alumno"<<endl
  50. <<"5.- Reporte de todos los alumnos"<<endl
  51. <<"6.- Salir del sistema"<<endl
  52. <<endl;
  53.        opcion = leer_entero("Seleccione una opcion: ", 1, 6);
  54.        if(opcion<5)
  55.        {
  56.            alumno.matricula=leer_entero("Ingrese el matricula del alumno: ", 0, INT_MAX);
  57.            p = n==0? NULL:(Alumno*) bsearch(&alumno, alumnos, n, sizeof(Alumno), comparar_alumno);
  58.            if(p!=NULL)
  59.                imprimir_alumno (p);
  60.        }
  61.        if(p!=NULL && opcion==1)
  62.            cout<<"El alumno ya existe"<<endl;
  63.        else if (p==NULL && opcion>=2 && opcion<=4)
  64.            cout<<"Alumno no encontrado"<<endl;
  65.        else switch(opcion)
  66.        {
  67.            case 1:
  68.                alumno.nombre = leer_linea("Ingrese el nombre: ");
  69.                alumnos = agregar_alumno (alumnos, &alumno, &n);
  70.                cout<<"Registro agregado correctamente"<<endl;
  71.                break;
  72.            case 3:
  73.                free(p->nombre);
  74.                p->nombre = leer_linea("Ingrese el nombre: ");        
  75.                cout<<"Alumno actualizado correctamente"<<endl;
  76.                break;
  77.            case 4:
  78.                free(p->nombre);
  79.                if (p!=&alumnos[n-1])
  80.                    memcpy (p, &alumnos[n-1], sizeof(Alumno));
  81.                n--;
  82.                alumnos=(Alumno*) realloc(alumnos, sizeof(Alumno) * n);
  83.                qsort(alumnos, n, sizeof(Alumno), comparar_alumno);
  84.                cout<<"Alumno borrado correctamente"<<endl;
  85.                break;
  86.            case 5:
  87.                for (i=0; i<n; i++)
  88.                    imprimir_alumno (&alumnos[i]);
  89.                break;
  90.        }
  91.        if(opcion<6 && opcion>=1)
  92.        {
  93.            system("pause");
  94.        }
  95.    } while(opcion!=6);
  96.    archivo=fopen(ruta, "w");
  97.    if(archivo!=NULL)
  98.    {
  99.        for (i=0; i<n; i++)
  100.            fprintf (archivo, "%d,%s\n", alumnos[i].matricula, alumnos[i].nombre);
  101.        fclose(archivo);
  102.    }
  103.    return EXIT_SUCCESS;
  104. }
  105.  
  106. int comparar_alumno(const void *alumno1, const void *alumno2)
  107. {
  108.    return((Alumno*)alumno1)->matricula-((Alumno*)alumno2)->matricula;
  109. }
  110.  
  111. void imprimir_alumno(Alumno *alumno)
  112. {
  113.    printf("matricula: %d\n", alumno->matricula);
  114.    printf("nombre   : %s\n", alumno->nombre);
  115. }
  116.  
  117. Alumno* agregar_alumno(Alumno *alumnos, Alumno *alumno, int *n)
  118. {
  119.    *n = *n + 1;
  120.    alumnos=(Alumno*)realloc(alumnos, sizeof(Alumno)*(*n));
  121.    memcpy(&alumnos[(*n)-1], alumno, sizeof(Alumno));
  122.    qsort(alumnos, *n, sizeof(Alumno), comparar_alumno);
  123.    return alumnos;
  124. }
  125.  
  126. char *leer_linea(const char *mensaje)
  127. {
  128.    char linea[MAXSTR];
  129.    leer_cadena(mensaje, linea);
  130.    return strdup(linea);
  131. }
  132.  
  133. char* leer_cadena(const char *mensaje, char *cadena)
  134. {
  135.    char *salto;
  136.    cout<<mensaje;
  137.    fgets(cadena, MAXSTR, stdin);
  138.    salto=strchr(cadena, '\n');
  139.    if(salto!=NULL)
  140.        *salto = '\0';
  141.    return cadena;
  142. }
  143.  
  144. int leer_entero(const char *mensaje, int menor, int mayor)
  145. {
  146.    int valor;
  147.    do {
  148.        valor =(int)leer_real (mensaje);
  149.        if (valor<menor || valor>mayor)
  150.            cout<<"Numero no valido"<<endl;
  151.    } while(valor<menor || valor>mayor);
  152.    return valor;
  153. }
  154.  
  155. float leer_real(const char *mensaje)
  156. {
  157.    char cadena[MAXSTR];
  158.    leer_cadena(mensaje, cadena);
  159.    return atof(cadena);
  160. }
  161.  
  162. char leer_caracter(const char *mensaje)
  163. {
  164.    char linea[MAXSTR];
  165.    leer_cadena(mensaje, linea);
  166.    return linea[0];
  167. }
  168.  
  169. int leer_campo(FILE *archivo, char *cadena)
  170. {
  171.    fscanf(archivo, "%[^,\n\r]", cadena);
  172.    if(feof(archivo))
  173. return 0;
  174.    fgetc(archivo);
  175. return 1;
  176. }
  177.  
  178.  
2  Programación / Programación C/C++ / CODIGO en: 17 Junio 2010, 11:39 am
hola mil disculpas bueno el codigo q tengo es este:
Código
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #define MAXSTR 0xFF
  7.  
  8. using namespace std;
  9.  
  10. typedef struct ClaseAlumno {
  11.    int matricula;
  12.    char* nombre;
  13. } Alumno;
  14.  
  15. int comparar_alumno (const void *alumno1, const void *alumno2);
  16. void imprimir_alumno (Alumno *alumno);
  17. Alumno* agregar_alumno (Alumno *alumnos, Alumno *alumno, int *n);
  18. char* leer_linea    (const char *mensaje);
  19. char* leer_cadena   (const char *mensaje, char *cadena);
  20. int   leer_entero   (const char *mensaje, int menor, int mayor);
  21. float leer_real     (const char *mensaje);
  22. char  leer_caracter (const char *mensaje);
  23. int   leer_campo (FILE *archivo, char *cadena);
  24.  
  25. int main ()
  26. {
  27.    Alumno alumno, *alumnos = NULL, *p;
  28.    int opcion, n=0, i;
  29.    char cadena[MAXSTR], *ruta = "D:\\alumnos.txt";
  30.    FILE *archivo;
  31.    archivo = fopen (ruta, "r");
  32.    if (archivo!=NULL)
  33.    {
  34.        while (leer_campo (archivo, cadena))
  35.        {
  36.            alumno.matricula = atoi (cadena);
  37.            leer_campo (archivo, cadena);
  38.            alumno.nombre = strdup (cadena);
  39.            alumnos = agregar_alumno (alumnos, &alumno, &n);
  40.        }
  41.        fclose (archivo);
  42.    }
  43.    do {
  44.        system ("cls");
  45.        cout<<endl
  46. <<"MENU"<<endl
  47. <<"1.- Nuevo"<<endl
  48. <<"2.- Salir"<<endl
  49. <<endl;
  50.  
  51.        opcion = leer_entero ("Seleccione una opcion: ", 1, 2);
  52.  
  53.        if (opcion < 2)
  54.        {
  55.            alumno.matricula = leer_entero ("Ingrese el matricula del alumno: ", 0, INT_MAX);
  56.            p = n==0? NULL: (Alumno*) bsearch (&alumno, alumnos, n, sizeof (Alumno), comparar_alumno);
  57.            if (p!=NULL)
  58.                imprimir_alumno (p);
  59.        }
  60.        if (p!=NULL && opcion==1)
  61.            cout<<"El alumno ya existe"<<endl;
  62.        else if (p==NULL && opcion>=2 && opcion<=1)
  63.            cout<<"Alumno no encontrado"<<endl;
  64.        else switch (opcion)
  65.        {
  66.            case 1:
  67.                alumno.nombre = leer_linea ("Ingrese el nombre: ");
  68.                alumnos = agregar_alumno (alumnos, &alumno, &n);
  69.                cout<<"Registro agregado correctamente"<<endl;
  70.                break;
  71.        }
  72.        if (opcion<2 && opcion>=1)
  73.        {
  74.  
  75.            system ("pause");
  76.        }
  77.    } while (opcion!=2);
  78.    archivo = fopen (ruta, "w");
  79.    if (archivo!=NULL)
  80.    {
  81.        for (i=0; i<n; i++)
  82.            fprintf (archivo, "%d,%s\n", alumnos[i].matricula, alumnos[i].nombre);
  83.        fclose (archivo);
  84.    }
  85.    return EXIT_SUCCESS;
  86. }
  87.  
  88. int comparar_alumno (const void *alumno1, const void *alumno2)
  89. {
  90.    return ((Alumno*)alumno1)->matricula - ((Alumno*)alumno2)->matricula;
  91. }
  92.  
  93. void imprimir_alumno (Alumno *alumno)
  94. {
  95.     printf ("matricula: %d\n", alumno->matricula);
  96. printf ("nombre   : %s\n", alumno->nombre);
  97. }
  98.  
  99. Alumno* agregar_alumno (Alumno *alumnos, Alumno *alumno, int *n)
  100. {
  101.    *n = *n + 1;
  102.    alumnos = (Alumno*) realloc (alumnos, sizeof (Alumno)*(*n));
  103.    memcpy (&alumnos[(*n)-1], alumno, sizeof (Alumno));
  104.    qsort (alumnos, *n, sizeof (Alumno), comparar_alumno);
  105.    return alumnos;
  106. }
  107.  
  108. char *leer_linea (const char *mensaje)
  109. {
  110.    char linea[MAXSTR];
  111.    leer_cadena (mensaje, linea);
  112.    return strdup (linea);
  113. }
  114.  
  115. char* leer_cadena (const char *mensaje, char *cadena)
  116. {
  117.    char *salto;
  118.    cout<<mensaje;
  119.    fgets (cadena, MAXSTR, stdin);
  120.    salto = strchr (cadena, '\n');
  121.    if (salto!=NULL)
  122.        *salto = '\0';
  123.    return cadena;
  124. }
  125.  
  126. int leer_entero (const char *mensaje, int menor, int mayor)
  127. {
  128.    int valor;
  129.    do {
  130.        valor = (int) leer_real (mensaje);
  131.        if (valor<menor || valor>mayor)
  132.            cout<<"Numero no valido"<<endl;
  133.    } while (valor<menor || valor>mayor);
  134.    return valor;
  135. }
  136.  
  137. float leer_real (const char *mensaje)
  138. {
  139.    char cadena[MAXSTR];
  140.    leer_cadena (mensaje, cadena);
  141.    return atof (cadena);
  142. }
  143.  
  144. char leer_caracter (const char *mensaje)
  145. {
  146.    char linea[MAXSTR];
  147.    leer_cadena (mensaje, linea);
  148.    return linea[0];
  149. }
  150.  
  151. int leer_campo (FILE *archivo, char *cadena)
  152. {
  153.    fscanf (archivo, "%[^,\n\r]", cadena);
  154.    if (feof (archivo))
  155.        return 0;
  156.    fgetc (archivo);
  157.    return 1;
  158. }
  159.  
es un trabajo el cual me piden explicar todo el codigo hay cosas q entiendo como otras q no haber si me dan una mano ixplicando algunas partes.
hay posibilidad de que este codigo se redusca a mas y solo usando el cout o q sea mas orientado a C++

PD: sobre los menus me faltan añadir las demas opciones q son 6 ejem: editar busqueda el codigo lo hace solo q lo qite para poder probar algunas cosas

gracias de antemano

saludos
3  Programación / Programación C/C++ / ayuda en: 17 Junio 2010, 07:26 am
hola

bueno soy un novato en esto de programacion y quisiera un poco de ayuda o me brinden ideas de como hacer este pequeño trabajo lo 1ro q deseo saber es como crear un archivo de texto en c++ luego q me permita agregar, guardar y eliminar cosas de ese archivo de texto todo en c++

saludos

PD: gracias de antemano a la ayuda brindada
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines