From 430a0aa45ea913fbaae67654111769fcd666c0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1ndly=20Gerg=C5=91?= Date: Sun, 22 Mar 2020 22:02:55 +0200 Subject: [PATCH] chore: Lab02 --- lab02/02.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lab02/02.cpp diff --git a/lab02/02.cpp b/lab02/02.cpp new file mode 100644 index 0000000..745f07d --- /dev/null +++ b/lab02/02.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +using namespace std; + +class Societate{ +public: + string nume, adresa; + int rulaj; + + void print(){ + cout<<"Nume: "<>tmp.nume; + cout<<"Adresa: "; + cin>>tmp.adresa; + cout<<"Rulaj: "; + cin>>tmp.rulaj; + v.push_back(tmp); + } + + //sort + bool sorted = false; + while(!sorted){ + sorted=true; + for(vector::iterator it = v.begin(); it < v.end()-1; it++){ + if(it->rulaj > (it+1)->rulaj){ + Societate tmp = *it; + *it = *(it+1); + *(it+1) = tmp; + sorted = false; + } + } + } + + int rulaj; + cout<<"Rulajul de cautat: "; + cin>>rulaj; + + int vStart = 0; + int vEnd = v.size()-1; + int pos = vEnd/2; + + while(vStart <= vEnd && rulaj >= v[vStart].rulaj && rulaj <= v[vEnd].rulaj){ + pos = vStart + (((double)(vEnd - vStart) / (v[vEnd].rulaj - v[vStart].rulaj)) * (rulaj - v[vStart].rulaj)); + + if(vStart == vEnd){ + break; + } + + if(v[pos].rulaj == rulaj){ + break; + } + + if(v[pos].rulaj < rulaj){ + vStart = pos + 1; + } + else{ + vEnd = pos - 1; + } + } + + cout<<"Societatea cu rulajul cel mai apropiat de "<