Advertisement
juanjo12x

Clase_LP_Cadenas

Sep 28th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. /*prototipos del archivo h*/
  7. //int leeAlumno(int )
  8. void lea_datos(int**,char***,int*);
  9. void imprimir_datos(int*,char**,int);
  10. void ordenar(int *,char**,int,int);
  11. void cambiar(int *,char**,int,int);
  12.  
  13. void cambiar(int *codigos,char**nombres,int i,int j){
  14.    int auxCod;char * auxNomb;
  15.    auxCod=codigos[i];
  16.    codigos[i]=codigos[j];
  17.    codigos[j]=auxCod;
  18.    auxNomb=nombres[i];//nombres sondirecciones de memoria
  19.    nombres[i]=nombres[j];
  20.    nombres[j]=auxNomb;
  21. }
  22. void ordenar(int *codigos,char** nombres,int izq,int der){
  23.   int limite;
  24.   if(izq>=der) return;
  25.   cambiar(codigos,nombres,izq,(izq+der/2));
  26.   limite=izq;
  27.   for(int i=izq+1;i<der;i++){
  28.    if(strcmp(nombres[i],nombres[izq])<0){
  29.     cambiar(codigos,nombres,limite++,i);
  30.    }
  31.   }
  32.   cambiar(codigos,nombres,izq,limite);
  33.   ordenar(codigos,nombres,izq,limite-1);
  34.   ordenar(codigos,nombres,limite+1,der);
  35. }
  36. int leeAlumno(int &cod,char*&nomb){
  37.   char buffer[100];
  38.   char* b=buffer; //con esto voy a ver si se leyo correctamente , sirve para depurar
  39.   if((scanf("%d",&cod))==EOF) return EOF;
  40.   gets(buffer);//vamos a sacar por espacios en blanco
  41.   /*puede ser tambien scanf("%*d",&aux); para ubicar hasta los epsacios hasta llegar
  42.   */
  43.   while(*b==' ') b++;
  44.   //b va a moveres hasta llegar a un caracter
  45.   nomb= new char[strlen(b)+1];
  46.   strcpy(nomb,b);
  47.   //return &cod;
  48.   return 0;
  49. }
  50. void lea_datos(int *& codigos,char**& nombres,int  &n){
  51.     int buffCod[100],cod;char *buffNom[100],*nomb;
  52.     n=0;
  53.     while(true){
  54.         //if((leeAlumno(cod,nomb)!=NULL)
  55.         if((leeAlumno(cod,nomb))!=EOF){
  56.             break;
  57.         }
  58.         //una vez que ya asigne los valores, lo paso al buffer asignado
  59.         buffCod[n]=cod;buffNom[n]=nomb;
  60.         n++;
  61.     }
  62.     //una vez llenamos el arreglo estatico , comenzamos llenando el
  63.     //arreglo dinamico
  64.     //El primer paso a trabajar e sgestar la memoria
  65.     codigos=new int[n];nombres= new char*[n];
  66.     for(int i=0;i<n;i++){
  67.         codigos[i]=buffCod[i];
  68.         nombres[i]=buffNom[i];
  69.     }
  70.    
  71. }
  72. void imprimir_datos(int *codigos,char** nombres,int n){
  73.  
  74.     for(int i=0;i<n;i++){
  75.         printf("%d ",codigos[i]);
  76.         printf("%s",nombres[i]);
  77.     }
  78.     printf("\n");
  79. }
  80. int main() {
  81.    
  82.     int *codigos; char **nombres;
  83.     int n;//numero de datos
  84.     lea_datos(codigos,nombres,n);
  85.     imprimir_datos(codigos,nombres,n);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement