Advertisement
STANAANDREY

catalog dei

Oct 12th, 2020 (edited)
2,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. /*in fisierul catalog.in se afla un nr n apoi pe n linii se afla numele,prenumele si media anuala a unui elev sa se scrie un program ce citste de la tastatura un nume si un prenume si folosind cautarea binara (cu DEI) sa afiseze media elevului respectiv daca exista*/
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstring>
  5. #define NMAX 100
  6. using namespace std;
  7.  
  8. struct Pupil {
  9.     char name[20], surname[20];
  10.     float average;
  11. } pupils[NMAX];
  12.  
  13. void read(char file[], int &n, Pupil pupils[]) {
  14.     ifstream fin(file);
  15.     fin >> n;
  16.     for (int i = 0; i < n; i++) {
  17.         fin >> pupils[i].name >> pupils[i].surname >> pupils[i].average;
  18.     }
  19. }
  20.  
  21. int cmp(Pupil a, Pupil b) {
  22.     int cmpr = strcmp(a.surname, b.surname);
  23.     return cmpr ? cmpr : strcmp(a.name, b.name);
  24. }
  25.  
  26. void Sort(int n, Pupil pupils[]) {
  27.     for (int i = 0; i < n - 1; i++)
  28.         for (int j = i + 1; j < n; j++)
  29.             if (cmp(pupils[i], pupils[j]) > 0)
  30.                 swap(pupils[i], pupils[j]);
  31. }
  32.  
  33. int bsrc(Pupil pupils[], int p, int q, Pupil x) {
  34.     if (p > q)
  35.         return -1;
  36.     int mid = (p + q) / 2;
  37.     int cmpr = cmp(pupils[mid], x);
  38.     if (!cmpr)
  39.         return mid;
  40.     if (cmpr > 0)
  41.         return bsrc(pupils, p, mid - 1, x);
  42.     return bsrc(pupils, mid + 1, q, x);
  43. }
  44.  
  45. void query(int n, Pupil pupils[]) {
  46.     Pupil x;
  47.     cout << "introdu numele si prenumele:";
  48.     cin >> x.name >> x.surname;
  49.     int pos = bsrc(pupils, 0, n - 1, x);
  50.     if (pos != -1)
  51.         cout << pupils[pos].average;
  52.     else
  53.         puts("Elevul nu exista!");
  54. }
  55.  
  56. int main() {
  57.     int n;
  58.     read("catalog.in", n, pupils);
  59.     Sort(n, pupils);
  60.     query(n, pupils);//*/
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement