PCLP2/lab02/02.cpp
Fándly Gergő 430a0aa45e chore: Lab02
2020-03-22 22:02:55 +02:00

81 lines
1.4 KiB
C++

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Societate{
public:
string nume, adresa;
int rulaj;
void print(){
cout<<"Nume: "<<nume<<"\n";
cout<<"Adresa: "<<adresa<<"\n";
cout<<"Rulaj: "<<rulaj<<"\n";
}
};
int main(){
int n;
cout<<"N = ";
cin>>n;
vector<Societate> v;
for(int i=0; i<n; i++){
Societate tmp;
cout<<"Societate nr. "<<i+1<<":\n";
cout<<"Nume: ";
cin>>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<Societate>::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 "<<rulaj<<":\n";
v[pos].print();
return 0;
}