PCLP2/lab05/03.cpp
Fándly Gergő b379440f66 chore: Lab05
2020-04-05 12:38:41 +03:00

79 lines
1.4 KiB
C++

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Subscriber{
public:
string name, address, phone;
double invoice;
void print(){
cout<<"- Name: "<<name<<"\n";
cout<<" Address: "<<address<<"\n";
cout<<" Phone: "<<phone<<"\n";
cout<<" Invoice value: "<<invoice<<"\n";
}
};
int sort_quick_partition(vector<Subscriber>* v, int low, int high){
int pivot = v->at(high).invoice;
int i = low - 1;
for(int j = low; j <= high - 1; j++){
if(v->at(j).invoice > pivot){
i++;
swap(v->at(i), v->at(j));
}
}
swap(v->at(i+1), v->at(high));
return i + 1;
}
void sort_quick(vector<Subscriber>* v, int low, int high){
if(low < high){
int parti = sort_quick_partition(v, low, high);
sort_quick(v, low, parti - 1);
sort_quick(v, parti + 1, high);
}
}
void sort_quick_driver(vector<Subscriber>* v){
sort_quick(v, 0, v->size() - 1);
}
int main(){
ios::sync_with_stdio(false);
cout<<"Number of subscribers: ";
int n;
cin>>n;
vector<Subscriber> v;
for(int i=0; i<n; i++){
Subscriber s;
cin.ignore(5, '\n');
cout<<"- Name: ";
getline(cin, s.name);
cout<<" Address: ";
getline(cin, s.address);
cout<<" Phone: ";
getline(cin, s.phone);
cout<<" Invoice value: ";
cin>>s.invoice;
v.push_back(s);
}
sort_quick_driver(&v);
cout<<"\n\n\nTop 3 invoice values:\n";
for(int i=0; i<3; i++){
v[i].print();
}
cout.flush();
return 0;
}