Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include "function.h"
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. Examinee ReadExaminee(string line_info) {
  11.  
  12. Examinee e;
  13. stringstream line(line_info);
  14.  
  15. string tmp;
  16.  
  17. //ID
  18. getline(line, tmp, ',');
  19.  
  20. e.ID = tmp;
  21.  
  22. //name
  23. getline(line, tmp, ',');
  24.  
  25. //math
  26. getline(line, tmp, ',');
  27.  
  28. e.math = (float)atof(tmp.c_str());
  29.  
  30. //literature
  31. getline(line, tmp, ',');
  32.  
  33. e.literature = (float)atof(tmp.c_str());
  34.  
  35. //physic
  36. getline(line, tmp, ',');
  37.  
  38. e.physic = (float)atof(tmp.c_str());
  39.  
  40. //chemistry
  41. getline(line, tmp, ',');
  42.  
  43. e.chemistry = (float)atof(tmp.c_str());
  44.  
  45. //biology
  46. getline(line, tmp, ',');
  47.  
  48. e.biology = (float)atof(tmp.c_str());
  49.  
  50. //history
  51. getline(line, tmp, ',');
  52.  
  53. e.history = (float)atof(tmp.c_str());
  54.  
  55. //geography
  56. getline(line, tmp, ',');
  57.  
  58. e.geography = (float)atof(tmp.c_str());
  59.  
  60. //civic_education
  61. getline(line, tmp, ',');
  62.  
  63. e.civic_education = (float)atof(tmp.c_str());
  64.  
  65. //natural_science
  66. getline(line, tmp, ',');
  67.  
  68. e.natural_science = (float)atof(tmp.c_str());
  69.  
  70. //social_science
  71. getline(line, tmp, ',');
  72.  
  73. e.social_science = (float)atof(tmp.c_str());
  74.  
  75. //foreign_language
  76. getline(line, tmp, ',');
  77.  
  78. e.foreign_language = (float)atof(tmp.c_str());
  79.  
  80.  
  81.  
  82. e.natural_science = e.physic + e.chemistry + e.biology;
  83. e.social_science = e.history + e.geography + e.civic_education;
  84.  
  85. return e;
  86. }
  87.  
  88. vector<Examinee> ReadExamineeList(string file_name) {
  89.  
  90. ifstream f(file_name.c_str());
  91. string line;
  92. vector<Examinee> result;
  93.  
  94. Examinee e;
  95.  
  96. if (f.is_open()) {
  97.  
  98. //bo qua line dau
  99. getline(f, line);
  100.  
  101. while (getline(f, line)) {
  102. e = ReadExaminee(line);
  103. result.push_back(e);
  104. }
  105.  
  106. f.close();
  107. }
  108.  
  109. return result;
  110. }
  111. vector<Examinee> sort(vector<Examinee> list_examinee) {
  112. vector<Examinee> temp(list_examinee.size());
  113. int h1, h2, l1, l2;
  114. int i, j, k;
  115. for(int index=1; index < list_examinee.size(); index*=2 ) {
  116. l1 = 0;
  117. k = 0;
  118. while( l1+index < list_examinee.size()) {
  119. h1 = l1+index-1;
  120. l2 = h1+1;
  121. h2 = l2+index-1;
  122. if( h2>=list_examinee.size() )
  123. h2= list_examinee.size() - 1;
  124.  
  125. i=l1;
  126. j=l2;
  127.  
  128. while(i<=h1 && j<=h2 ) {
  129. if( list_examinee[i].literature >= list_examinee[j].literature )
  130. temp[k++]=list_examinee[i++];
  131. else
  132. temp[k++]=list_examinee[j++];
  133. }
  134.  
  135. while(i<=h1)
  136. temp[k++]=list_examinee[i++];
  137. while(j<=h2)
  138. temp[k++]=list_examinee[j++];
  139. l1=h2+1;
  140. }
  141.  
  142. for(i=l1; k< list_examinee.size() ; i++)
  143. temp[k++]=list_examinee[i];
  144.  
  145. for(i=0;i< list_examinee.size();i++)
  146. list_examinee[i]=temp[i];
  147.  
  148. }
  149. return list_examinee;
  150. }
  151.  
  152. vector<Examinee> GetTopHighestLiterature(vector<Examinee> list_examinee, int k) {
  153. vector<Examinee>sorted = sort(list_examinee);
  154. return vector<Examinee>(sorted.begin(), sorted.begin() + k);
  155. }
  156.  
  157. vector<Examinee> MergeTwoList(vector<Examinee> &list1, vector<Examinee> &list2) {
  158. vector<Examinee> result;
  159. int i = 0, j = 0;
  160. while (i < list1.size() && j < list2.size()) {
  161. if (list1[i].literature <= list2[j].literature) {
  162.  
  163. result.push_back(list1[i]);
  164. i++;
  165. }
  166. else {
  167. result.push_back(list2[j]);
  168. j++;
  169. }
  170. }
  171.  
  172. while (i < list1.size()) {
  173. result.push_back(list1[i]);
  174. i++;
  175. }
  176. while (j < list2.size()) {
  177. result.push_back(list2[j]);
  178. j++;
  179. }
  180. }
  181. int binarySearch(vector<Examinee> arr, int left, int right, float v) {
  182. int mid;
  183.  
  184. while (left <= right) {
  185. mid = left + (right-left)/2;
  186.  
  187. if (arr[mid].literature == v)
  188. return mid;
  189.  
  190. if (arr[mid].literature < v)
  191. left = mid + 1;
  192. else
  193. right = mid - 1;
  194. }
  195.  
  196. return -1;
  197. }
  198. string FindExamineeByLiterature(vector<Examinee> sorted_list, float v) {
  199.  
  200. int index = binarySearch(sorted_list, 0, sorted_list.size() - 1, v);
  201.  
  202. if( index == -1) return "-1";
  203.  
  204. return sorted_list[index].ID;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement