Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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*/
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #define NMAX 100
- using namespace std;
- struct Pupil {
- char name[20], surname[20];
- float average;
- } pupils[NMAX];
- void read(char file[], int &n, Pupil pupils[]) {
- ifstream fin(file);
- fin >> n;
- for (int i = 0; i < n; i++) {
- fin >> pupils[i].name >> pupils[i].surname >> pupils[i].average;
- }
- }
- int cmp(Pupil a, Pupil b) {
- int cmpr = strcmp(a.surname, b.surname);
- return cmpr ? cmpr : strcmp(a.name, b.name);
- }
- void Sort(int n, Pupil pupils[]) {
- for (int i = 0; i < n - 1; i++)
- for (int j = i + 1; j < n; j++)
- if (cmp(pupils[i], pupils[j]) > 0)
- swap(pupils[i], pupils[j]);
- }
- int bsrc(Pupil pupils[], int p, int q, Pupil x) {
- if (p > q)
- return -1;
- int mid = (p + q) / 2;
- int cmpr = cmp(pupils[mid], x);
- if (!cmpr)
- return mid;
- if (cmpr > 0)
- return bsrc(pupils, p, mid - 1, x);
- return bsrc(pupils, mid + 1, q, x);
- }
- void query(int n, Pupil pupils[]) {
- Pupil x;
- cout << "introdu numele si prenumele:";
- cin >> x.name >> x.surname;
- int pos = bsrc(pupils, 0, n - 1, x);
- if (pos != -1)
- cout << pupils[pos].average;
- else
- puts("Elevul nu exista!");
- }
- int main() {
- int n;
- read("catalog.in", n, pupils);
- Sort(n, pupils);
- query(n, pupils);//*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement