Advertisement
Mihai_Preda

Untitled

Jan 17th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. struct Articol
  6. {
  7.     char * titlu;
  8.     char * text;
  9. };
  10.  
  11. void citire(Articol *articol, const char nume_fisier[])
  12. {
  13.     FILE * f = fopen(nume_fisier);
  14.     char line[100];
  15.     fgets(line, 100, f);
  16.     articol->titlu = malloc(strlen(line) + 1);
  17.     strcpy(articol->titlu, line);
  18.  
  19.     // asemanator citesti si textul
  20.     fclose(f);
  21. }
  22.  
  23. int nr_cuvinte(const char sir[])
  24. {
  25.     int cnt = 0;
  26.     int n = strlen(sir);
  27.     for(int i = 0; i < n; i++)
  28.         if(sir[i] == ' ')
  29.             cnt++;
  30.     return cnt;
  31. }
  32. int cmp(const void * a, const void * b)
  33. {
  34.     int n = strlen(a->titlu), m = strlen(b->titlu);
  35.     int minLength;
  36.     if(n < m)
  37.         minLength = n;
  38.     else
  39.         minlength = m;
  40.  
  41.     for(int i = 0; i < minLength; i++)
  42.     {
  43.         if(a[i] < b[i])
  44.             return -1;
  45.         if(a[i] > b[i])
  46.             return 1;
  47.     }
  48.     if(n < m)
  49.         return -1;
  50.     if(n > m)
  51.         return 1;
  52.     return nr_cuvinte(a->text) - nr_cuvinte(b->text);
  53. }
  54.  
  55. int main()
  56. {
  57.     Articol articole[10];
  58.     citire(&articol[0], "Nasa.txt");
  59.     citire(&articol[1], "UPB.txt");
  60.     citire(&articol[2], "UB.txt");
  61.  
  62.     qsort(articole, 3, sizeof(Articol), cmp);
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement