Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "function.h"
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- Examinee ReadExaminee(string line_info) {
- Examinee e;
- stringstream line(line_info);
- string tmp;
- //ID
- getline(line, tmp, ',');
- e.ID = tmp;
- //name
- getline(line, tmp, ',');
- //math
- getline(line, tmp, ',');
- e.math = (float)atof(tmp.c_str());
- //literature
- getline(line, tmp, ',');
- e.literature = (float)atof(tmp.c_str());
- //physic
- getline(line, tmp, ',');
- e.physic = (float)atof(tmp.c_str());
- //chemistry
- getline(line, tmp, ',');
- e.chemistry = (float)atof(tmp.c_str());
- //biology
- getline(line, tmp, ',');
- e.biology = (float)atof(tmp.c_str());
- //history
- getline(line, tmp, ',');
- e.history = (float)atof(tmp.c_str());
- //geography
- getline(line, tmp, ',');
- e.geography = (float)atof(tmp.c_str());
- //civic_education
- getline(line, tmp, ',');
- e.civic_education = (float)atof(tmp.c_str());
- //natural_science
- getline(line, tmp, ',');
- e.natural_science = (float)atof(tmp.c_str());
- //social_science
- getline(line, tmp, ',');
- e.social_science = (float)atof(tmp.c_str());
- //foreign_language
- getline(line, tmp, ',');
- e.foreign_language = (float)atof(tmp.c_str());
- e.natural_science = e.physic + e.chemistry + e.biology;
- e.social_science = e.history + e.geography + e.civic_education;
- return e;
- }
- vector<Examinee> ReadExamineeList(string file_name) {
- ifstream f(file_name.c_str());
- string line;
- vector<Examinee> result;
- Examinee e;
- if (f.is_open()) {
- //bo qua line dau
- getline(f, line);
- while (getline(f, line)) {
- e = ReadExaminee(line);
- result.push_back(e);
- }
- f.close();
- }
- return result;
- }
- vector<Examinee> sort(vector<Examinee> list_examinee) {
- vector<Examinee> temp(list_examinee.size());
- int h1, h2, l1, l2;
- int i, j, k;
- for(int index=1; index < list_examinee.size(); index*=2 ) {
- l1 = 0;
- k = 0;
- while( l1+index < list_examinee.size()) {
- h1 = l1+index-1;
- l2 = h1+1;
- h2 = l2+index-1;
- if( h2>=list_examinee.size() )
- h2= list_examinee.size() - 1;
- i=l1;
- j=l2;
- while(i<=h1 && j<=h2 ) {
- if( list_examinee[i].literature >= list_examinee[j].literature )
- temp[k++]=list_examinee[i++];
- else
- temp[k++]=list_examinee[j++];
- }
- while(i<=h1)
- temp[k++]=list_examinee[i++];
- while(j<=h2)
- temp[k++]=list_examinee[j++];
- l1=h2+1;
- }
- for(i=l1; k< list_examinee.size() ; i++)
- temp[k++]=list_examinee[i];
- for(i=0;i< list_examinee.size();i++)
- list_examinee[i]=temp[i];
- }
- return list_examinee;
- }
- vector<Examinee> GetTopHighestLiterature(vector<Examinee> list_examinee, int k) {
- vector<Examinee>sorted = sort(list_examinee);
- return vector<Examinee>(sorted.begin(), sorted.begin() + k);
- }
- vector<Examinee> MergeTwoList(vector<Examinee> &list1, vector<Examinee> &list2) {
- vector<Examinee> result;
- int i = 0, j = 0;
- while (i < list1.size() && j < list2.size()) {
- if (list1[i].literature <= list2[j].literature) {
- result.push_back(list1[i]);
- i++;
- }
- else {
- result.push_back(list2[j]);
- j++;
- }
- }
- while (i < list1.size()) {
- result.push_back(list1[i]);
- i++;
- }
- while (j < list2.size()) {
- result.push_back(list2[j]);
- j++;
- }
- }
- int binarySearch(vector<Examinee> arr, int left, int right, float v) {
- int mid;
- while (left <= right) {
- mid = left + (right-left)/2;
- if (arr[mid].literature == v)
- return mid;
- if (arr[mid].literature < v)
- left = mid + 1;
- else
- right = mid - 1;
- }
- return -1;
- }
- string FindExamineeByLiterature(vector<Examinee> sorted_list, float v) {
- int index = binarySearch(sorted_list, 0, sorted_list.size() - 1, v);
- if( index == -1) return "-1";
- return sorted_list[index].ID;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement