Advertisement
pmanriquez93

Qsort ejemplo

Oct 23rd, 2014
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. // MAIN
  2.  
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <cstring>
  6.  
  7. #include "funcs.h"
  8.  
  9. int compar(const void*, const void*);
  10.  
  11. using namespace std;
  12.  
  13. int main(int argc, char** argv) {
  14.    
  15.     char **nom;
  16.     int numDat;
  17.     leer(nom,numDat);
  18.     qsort(nom,numDat,sizeof(char*),compar);
  19.     imp(nom,numDat);
  20.  
  21.     return 0;
  22. }
  23.  
  24. int compar(const void* d1, const void* d2){
  25.     char **nom1 = (char**)d1;
  26.     char **nom2 = (char**)d2;
  27.     return strcmp(*nom1,*nom2);
  28. }
  29.  
  30.  
  31.  
  32. // HEADER
  33.  
  34. #ifndef FUNCS_H
  35. #define FUNCS_H
  36.  
  37. void leer(char **&, int &);
  38.  
  39. void imp(char **, int);
  40.  
  41. #endif  /* FUNCS_H */
  42.  
  43.  
  44.  
  45.  
  46. // IMPLEMENTACION
  47.  
  48. #include <cstdlib>
  49. #include <cstdio>
  50. #include <cstring>
  51.  
  52. #include "funcs.h"
  53.  
  54. void leer(char **&nom, int &numDat){
  55.    
  56.     char *buff[50], aux[50];    numDat = 0;
  57.     while (gets(aux) != NULL){
  58.         buff[numDat] = new char[strlen(aux)+1];
  59.         strcpy(buff[numDat],aux);
  60.         numDat++;
  61.     }
  62.    
  63.     nom = new char*[numDat];
  64.     for (int i = 0; i<numDat; i++)
  65.         nom[i] = buff[i];
  66. }
  67.  
  68. void imp(char **nom, int numDat){
  69.     for (int i = 0; i<numDat; i++)
  70.         printf("%-10s \n",nom[i]);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement