daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8.  
  9. // GET MAX ---------------------------
  10. int get_max(char **tab, int n)
  11. {  
  12.   int max = 0;
  13.   int k;
  14.  
  15.   for (int i = 1; i <= n; i++)
  16.     if (strlen(tab[i]) > max)
  17.     {
  18.       max = strlen(tab[i]);
  19.       k = i;
  20.     }
  21.   return max;
  22. }
  23. //++++++++++++++++++++++++++++++++++++++++++++++++
  24.  
  25. //GET SIZE ---------------------------
  26. int get_size()
  27. {
  28.   FILE *file = fopen("pop.txt","r");
  29.   char c = 0;
  30.   int ilosc = 0;
  31.   for (c = getc(file); c != EOF; c = getc(file))
  32.   {
  33.     if (c == '\n')
  34.     {
  35.       ilosc++;
  36.     }
  37.   }
  38.     fclose(file);
  39.     return ilosc;
  40. }
  41. //++++++++++++++++++++++++++++++++++++++++++++++++
  42.  
  43. // COUNTING SORT ------------------------------------------
  44. void counting_sort(char **Tab1, char **Tab2, int n, int k)
  45. {
  46.   int ASCII = 127;
  47.   int Tab_pom[1000]; //Tablica pomocnicza do zliczania
  48.  
  49.   // Zerowanie tablicy
  50.   for(int i = 0; i <= ASCII; i++)
  51.   {
  52.       Tab_pom[i] = 0;
  53.   }
  54.  
  55.   for(int i = 1; i <= n; i++)
  56.   {
  57.     Tab_pom[Tab1[i][k]] = Tab_pom[Tab1[i][k]] + 1;
  58.   }
  59.  
  60.   for(int i = 1; i <= ASCII; i++)
  61.   {
  62.       Tab_pom[i] = Tab_pom[i] + Tab_pom[i-1];
  63.   }
  64.  
  65.   //Sortowanie
  66.   for(int i = n; i >= 1;i--)
  67.   {
  68.       Tab2[Tab_pom[Tab1[i][k]]] = Tab1[i];
  69.       Tab_pom[Tab1[i][k]] = Tab_pom[Tab1[i][k]]-1;
  70.   }
  71. }
  72. // ++++++++++++++++++++++++++++++++++++++++++++++++
  73.  
  74. //RADIX SORT ---------------------------
  75. void RADIX_SORT(char **Tab1, char **Tab2,char **Tab3, int n,int max)
  76. {
  77.   for(int i = max; i >= 0; i--)
  78.   {
  79.     counting_sort(Tab1,Tab2,n,i);
  80.     Tab3 = Tab1;
  81.     Tab1 = Tab2;
  82.     Tab2 = Tab3;
  83.   }
  84. }
  85. //++++++++++++++++++++++++++++++++++++++++++++++++
  86.  
  87.  
  88.  
  89.  
  90. //==============================================================
  91. // MAIN --------------------------------------------------------
  92.  
  93. int main()
  94. {
  95.  
  96. int a = get_size();
  97.    
  98. char **A = (char**) malloc(a*sizeof(char*));
  99. char **B = (char**) malloc(a*sizeof(char*));
  100. char **pom = (char**) malloc(a*sizeof(char*));
  101.  
  102.  
  103.  
  104. // ODCZYT PLIKU ----------------------------  
  105.  
  106. FILE *file=fopen("pop.txt","r");
  107. char slowo[100];
  108. char c[1024];
  109. int o=1;
  110. int ilosc=0;
  111. bool digit;
  112.  
  113.   while(fscanf(file,"%s",c)==true)
  114.   {
  115.     digit=false;
  116.     ilosc=strlen(c);
  117.     for(int i=0;i<ilosc;i++)
  118.     {
  119.       if(c[i]>=48&&c[i]<=57) // od 0 do 9
  120.       {
  121.         digit=true;
  122.       }
  123.     }
  124.       if(!digit)
  125.       {
  126.         A[o]=(char*) malloc(sizeof(char)*100);
  127.         strcpy(A[o],c);
  128.         o++;
  129.       }
  130.   }
  131. fclose(file);
  132.  
  133. //++++++++++++++++++++++++++++++++++++++++++++++++
  134.  
  135. int max = get_max(A,a); // Znajdujemy najdluzszy napis
  136. RADIX_SORT(A,B,pom,a,max); //RADIX SORT
  137.  
  138. //++++++++++++++++++++++++++++++++++++++++++++++++
  139.  
  140. // POSORTOWANE - ZAPIS DO PLIKU -----------------------
  141.  
  142. FILE *f=fopen("pop_sorted.txt","w");
  143.     for(int i=1;i<=a;i++)
  144.     {
  145.         fprintf(f,"%s\n",A[i]);
  146.     }
  147. fclose(f);
  148.  
  149. //++++++++++++++++++++++++++++++++++++++++++++++++
  150.  
  151.     return 0;
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top