Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <ctime>
  5. #include <fstream>
  6. #include <map>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. using namespace std;
  10. unsigned long bad_hash(const string key);
  11. unsigned long good_hash(const string key);
  12. //Описание класса
  13. class service{
  14. private:
  15. unsigned long b_hash;
  16. unsigned long g_hash;
  17. public:
  18. unsigned long getB_hash() const;
  19.  
  20. unsigned long getG_hash() const;
  21.  
  22. private:
  23. string s_name;
  24. public:
  25. const string &getS_name() const;
  26.  
  27. private:
  28. int cost, s_time,prepay;
  29. friend bool Search(vector<service> f, string key);
  30. friend bool BinSearch(vector<service> mas, string key);
  31. friend void fill(multimap<string,service> &Map,vector<service> mas);
  32. friend bool b_hash_search(vector<service> mas, string key);
  33. friend bool g_hash_search(vector<service> mas, string key);
  34. public:
  35. //Конструктор по умолчанию
  36. service()
  37. {
  38. s_name = "";
  39. cost=0;
  40. s_time=0;
  41. prepay=0;
  42. }
  43. //Конструктор класса
  44. service(string const s_name1, int cost1, int s_time1, int prepay1)
  45. {
  46. s_name = s_name1;
  47. cost= cost1;
  48. s_time= s_time1;
  49. prepay= prepay1;
  50. b_hash=bad_hash(s_name1);
  51. }
  52.  
  53. //Печать элементов, важных при сравнении
  54. void print()
  55. {
  56. cout << s_name << " " << cost << " " << s_time << " " << prepay << endl;
  57. }
  58.  
  59. bool operator>(const service &other);
  60. bool operator<(const service &other);
  61. bool operator>=(const service &other);
  62. bool operator<=(const service &other);
  63.  
  64. };
  65.  
  66. bool service::operator<(const service &other)
  67. {
  68. return s_name<other.s_name || ( (s_name==other.s_name) && (cost<other.cost));
  69. }
  70.  
  71. bool service::operator<=(const service &other)
  72. {
  73. return s_name<other.s_name || ( (s_name==other.s_name) && (cost<=other.cost));
  74. }
  75.  
  76. bool service::operator>(const service &other)
  77. {
  78. return s_name>other.s_name || ( (s_name==other.s_name) && (cost>other.cost));
  79. }
  80.  
  81. bool service::operator>=(const service &other)
  82. {
  83. return s_name>other.s_name || ( (s_name==other.s_name) && (cost>=other.cost));
  84. }
  85.  
  86. void read_list(ifstream& in, vector <string> &names, vector <int> &costs, vector <int> &times, vector <int> &prepays)
  87. {
  88. string s;
  89.  
  90. for (int i = 0; i < 10; i++){
  91. in >> s;
  92. names.push_back(s);
  93. }
  94. for (int i = 0; i < 10; i++){
  95. int s;
  96. in >> s;
  97. costs.push_back(s);
  98. }
  99. for (int i = 0; i < 10; i++){
  100. int s;
  101. in >> s;
  102. times.push_back(s);
  103. }
  104. for (int i = 0; i < 10; i++){
  105. int s;
  106. in >> s;
  107. prepays.push_back(s);
  108. }
  109. }
  110.  
  111.  
  112.  
  113. vector <service> make_rand(vector <string>& names, vector <int>& costs, vector <int>& times, vector <int>& prepays, int num)
  114. {
  115. vector <service> res;
  116. srand(time(NULL));
  117. for (int i = 0; i < num; i++){
  118. int n, s, m, k;
  119. n = rand() % 10;
  120. s = rand() % 10;
  121. m = rand() % 10;
  122. k = rand() % 10;
  123. service temp(names[n], costs[s], times[m],prepays[k]);
  124. res.push_back(temp);
  125. }
  126. return res;
  127. }
  128.  
  129. void sort1(vector<service> &a)//простыми вставками
  130. {
  131. for (size_t i = 1; i < a.size(); i++)
  132. {
  133. for (int j=i;j>0 && a[j-1]>a[j];j--)
  134. {
  135. swap(a[j-1],a[j]);
  136. }
  137. }
  138. }
  139.  
  140.  
  141. bool Search (vector<service> f, string key)
  142. {
  143. bool flag = false;
  144. for(int i=0;i<f.size();i++)
  145. {
  146. if(key == f[i].s_name)
  147. {
  148. cout << "index" << i+1;
  149. f[i].print();
  150. flag = true;
  151. }
  152. }
  153. return flag;
  154. }
  155.  
  156. // нужно переделать вывод bool BinSearch(vector<service> mas, string key)/*
  157. /*{
  158. int low, high, middle;
  159. bool flag=false;
  160. low = 0;
  161. high = mas.size() - 1;
  162. while (low <= high)
  163. {
  164. middle = (low + high) / 2;
  165. if (key < mas[middle].s_name)
  166. high = middle - 1;
  167. else if (key > mas[middle].s_name)
  168. low = middle + 1;
  169. else
  170. {
  171. flag=true;
  172. cout << "Index: " << middle+1 << "\nElement: ";
  173. mas[middle].print();
  174. int i=middle, k=middle;
  175. while(((i+1)<mas.size()) && (key == mas[i+1].s_name))
  176. {
  177. cout << "Index: " << i+2 << "\nElement: ";
  178. mas[i+1].print();
  179. i++;
  180. }
  181. while(((k-1)>=0) && (key == mas[k-1].s_name))
  182. {
  183. cout << "Index: " << k << "\nElement: ";
  184. mas[k-1].print();
  185. k--;
  186. }
  187. break;
  188. }
  189. }
  190. return flag;
  191. }
  192. */
  193.  
  194. void fill(multimap<string,service> &Map,vector<service> mas)
  195. {
  196. for (int i = 0; i < mas.size(); i++)
  197. {
  198. Map.insert(pair<string, service>(mas[i].s_name, mas[i]));
  199. }
  200. }
  201.  
  202. unsigned long bad_hash(const string key)
  203. {
  204. unsigned int H = 0;
  205. for (int i = 0; i < key.size(); i++)
  206. {
  207. H+= key[i] * pow(3, i);
  208. }
  209.  
  210. return H;
  211. }
  212.  
  213. unsigned long good_hash(const string key)
  214. {
  215. unsigned int H = 0;
  216. for (int i = 0; i < key.size(); i++)
  217. {
  218. H += key[i] + (H<<i);
  219. }
  220. return H;
  221. }
  222.  
  223. bool b_hash_search(vector<service> mas, string key)
  224. {
  225. bool flag=false;
  226. unsigned int hash_val=bad_hash(key);
  227. for(int i=0;i<mas.size();i++)
  228. {
  229. if(mas[i].b_hash==hash_val)
  230. {
  231. if(mas[i].s_name==key)
  232. {
  233. mas[i].print();
  234. flag=true;
  235. }
  236. }
  237. }
  238. return flag;
  239. }
  240.  
  241. bool g_hash_search(vector<service> mas, string key) {
  242. bool flag=false;
  243. unsigned int hash_val=good_hash(key);
  244. for(int i=0;i<mas.size();i++)
  245. {
  246. if(mas[i].g_hash==hash_val)
  247. {
  248. if(mas[i].b_hash==hash_val)
  249. {
  250. mas[i].print();
  251. flag=true;
  252. }
  253. }
  254. }
  255. return flag;
  256. }
  257.  
  258. unsigned long service::getB_hash() const {
  259. return b_hash;
  260. }
  261.  
  262. unsigned long service::getG_hash() const {
  263. return g_hash;
  264. }
  265.  
  266. const string &service::getS_name() const {
  267. return s_name;
  268. }
  269.  
  270. int main(){
  271. int num;
  272. ifstream in;
  273. double s_time,e_time,f_time;
  274. string key;
  275.  
  276. multimap<string,service> Map;
  277.  
  278. cout << "Write number of elements";
  279. cin >> num;
  280. cout << "Write key" << endl;
  281. cin >> key;
  282.  
  283.  
  284.  
  285. in.open("C:/Users/USER/Desktop/text1.txt");
  286. if(!in.is_open()) return -1;
  287.  
  288. vector <string> names;
  289. vector<int> costs, times, prepays;
  290. read_list(in, names, costs, times, prepays);
  291. vector <service> f = make_rand(names, costs, times, prepays, num);
  292.  
  293.  
  294. s_time = clock() / 1000.0;
  295.  
  296. if (!Search (f,key))
  297. cout << "element is not found" << endl;
  298.  
  299. e_time = clock() / 1000.0;
  300. f_time = e_time - s_time;
  301. cout << f_time << endl;
  302.  
  303.  
  304.  
  305.  
  306. s_time = clock() / 1000.0;
  307.  
  308. if(!g_hash_search(f,key)) cout<<"no elements";
  309.  
  310.  
  311. e_time = clock() / 1000.0;
  312. f_time = e_time - s_time;
  313. cout << f_time << endl;
  314.  
  315.  
  316. s_time = clock() / 1000.0;
  317.  
  318. if(!b_hash_search(f,key)) cout<<"no elements";
  319.  
  320.  
  321. e_time = clock() / 1000.0;
  322. f_time = e_time - s_time;
  323. cout << f_time << endl;
  324.  
  325. std::map<string, bool> keys;
  326. for(int i=0;i<=f.size();i++)
  327. keys.insert(std::pair<string, bool>(f[i].getS_name(), 0));
  328.  
  329. std::map<unsigned int, bool> b_hashs;
  330. for(int i=0;i<=f.size();i++)
  331. b_hashs.insert(std::pair<unsigned int, bool>(f[i].getB_hash(),0));
  332.  
  333. std::map<unsigned int, bool> g_hashs;
  334. for(int i=0;i<=f.size();i++)
  335. g_hashs.insert(std::pair<unsigned int, bool>(f[i].getG_hash(),0));
  336.  
  337. cout<<"\nColis: "<<100-b_hashs.size()/(double)keys.size()*100;
  338. cout<<"\nColis: "<<100-g_hashs.size()/(double)keys.size()*100;
  339.  
  340.  
  341.  
  342. fill(Map,f);
  343.  
  344. sort1(f);
  345.  
  346.  
  347. s_time = clock() / 1000.0;
  348.  
  349. if(!BinSearch(f, key)) {
  350. cout << "element is not found" << endl;
  351. }
  352.  
  353. e_time = clock() / 1000.0;
  354. f_time = e_time - s_time;
  355. cout << f_time << endl;
  356.  
  357.  
  358. s_time = clock() / 1000.0;
  359. pair<map<string,service>::iterator, map<string,service>::iterator> q;
  360. // переделать q = Map.equal_range(key);
  361. for (map<string,service>::iterator it = q.first; it != q.second; it++){
  362. it->second.print();
  363. }
  364. e_time = clock() / 1000.0;
  365. f_time = e_time - s_time;
  366. cout << f_time << endl;
  367.  
  368.  
  369.  
  370.  
  371.  
  372. in.close();
  373. return 0;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement