Advertisement
Guest User

Untitled

a guest
Dec 16th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <windows.h>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. struct table
  9. {
  10. int bilet;// Номер билета
  11. string marshrut;// Маршрут
  12. string data; // Дата отправки
  13. int vagon; // Номер вагона
  14. int mesto; // Номер места
  15. };
  16.  
  17. bool StrComp(string s1,string s2)
  18. {
  19. int d1=0, d2=0,mon1=0,mon2=0,ye1=0,ye2=0;
  20. string sd1 = s1.substr(0,2);
  21. d1 = atoi(sd1.c_str());
  22. string sd2 = s2.substr(0,2);
  23. d2 = atoi(sd2.c_str());
  24. string m1 = s1.substr(3,2);
  25. mon1 = atoi(m1.c_str());
  26. string m2 = s2.substr(3,2);
  27. mon2 = atoi(m2.c_str());
  28. string y1 = s1.substr(6,4);
  29. ye1 = atoi(y1.c_str());
  30. string y2 = s2.substr(6,4);
  31. ye2 = atoi(y2.c_str());
  32. if(ye1>ye2)
  33. {
  34. return 1;
  35. }
  36. if(ye1==ye2)
  37. {
  38. if(m1>m2)
  39. {
  40. return 1;
  41. }
  42. if(m1==m2)
  43. {
  44. if(d1>=d2) return 1;
  45. }
  46. }
  47. if(ye1<ye2)
  48. {
  49. return 0;
  50. }
  51. }
  52.  
  53. void BinSortV(vector<table> T)
  54. {
  55. int i=0,j=0,l=0,r=0,m=0;
  56. table x;
  57. for( i=2;i<T.size();i++)
  58. {
  59. x=T[i]; l=1; r=i-1;
  60. while(l<=r)
  61. {
  62. m=(l+r)/2;
  63. if(x.vagon<T[m].vagon)
  64. {
  65. r = m-1;
  66. } else l=m+1;
  67. }
  68. for(j=i-1;j>l;j--)
  69. {
  70. T[j+1] = T[j];
  71. }
  72. T[l]=x;
  73. }
  74. }
  75.  
  76. void BinSortD(vector<table> T)
  77. {
  78. int i=0,j=0,l=0,r=0,m=0;
  79. table x;
  80. for( i=2;i<T.size();i++)
  81. {
  82. x=T[i]; l=1; r=i-1;
  83. while(l<=r)
  84. {
  85. m=(l+r)/2;
  86. if(StrComp(x.data,T[m].data)==0)
  87. {
  88. r = m-1;
  89. } else l=m+1;
  90. }
  91. for(j=i-1;j>l;j--)
  92. {
  93. T[j+1] = T[j];
  94. }
  95. T[l]=x;
  96. }
  97. }
  98.  
  99. void QuickSortV(vector<table> s_arr, int first, int last)
  100. {
  101. int i = first, j = last, x = s_arr[(first + last) / 2].vagon;
  102.  
  103. do {
  104. while (s_arr[i].vagon < x) i++;
  105. while (s_arr[j].vagon > x) j--;
  106.  
  107. if(i <= j) {
  108. if (i < j) swap(s_arr[i], s_arr[j]);
  109. i++;
  110. j--;
  111. }
  112. } while (i <= j);
  113.  
  114. if (i < last)
  115. QuickSortV(s_arr, i, last);
  116. if (first < j)
  117. QuickSortV(s_arr, first,j);
  118. }
  119.  
  120. void QuickSortD(vector<table> s_arr, int first, int last)
  121. {
  122. int i = first, j = last; string x = s_arr[(first + last) / 2].data;
  123.  
  124. do {
  125. while (StrComp(s_arr[i].data, x)==0) i++;
  126. while (StrComp(s_arr[i].data, x)==1) j--;
  127.  
  128. if(i <= j) {
  129. if (i < j) swap(s_arr[i], s_arr[j]);
  130. i++;
  131. j--;
  132. }
  133. } while (i <= j);
  134.  
  135. if (i < last)
  136. QuickSortD(s_arr, i, last);
  137. if (first < j)
  138. QuickSortD(s_arr, first,j);
  139. }
  140.  
  141. void BubbleV(vector<table> s_arr)
  142. {
  143. int a, b;
  144. int t;
  145.  
  146. for(a=1; a < s_arr.size(); ++a)
  147. for(b=s_arr.size()-1; b >= a; --b) {
  148. if(s_arr[b-1].vagon > s_arr[b].vagon) {
  149. t = s_arr[b-1].vagon;
  150. s_arr[b-1] = s_arr[b];
  151. s_arr[b].vagon = t;
  152. }
  153. }
  154. }
  155.  
  156. void BubbleD(vector<table> s_arr)
  157. {
  158. int a, b;
  159. string t;
  160.  
  161. for(a=1; a < s_arr.size(); ++a)
  162. for(b=s_arr.size()-1; b >= a; --b) {
  163. if(StrComp(s_arr[b-1].data ,s_arr[b].data)) {
  164. t = s_arr[b-1].data;
  165. s_arr[b-1] = s_arr[b];
  166. s_arr[b].data = t;
  167. }
  168. }
  169. }
  170.  
  171. int main()
  172. {
  173. ifstream in; // Поток in будем использовать для чтения
  174. ofstream out; // Поток out будем использовать для записи
  175. out.open("D:\input.txt");
  176. struct table t;
  177. setlocale(LC_ALL, "Russian");
  178. int k,i=1;
  179. cout<<"Введите количество строк таблицы: ";
  180. cin>>k;
  181. while (i<=k)
  182. {
  183. cout<<"Введите номер билета:\n";
  184. cin>>t.bilet;
  185. out<<t.bilet<<' ';
  186. cout<<"Введите маршрут:\n";
  187. SetConsoleCP(1251);
  188. cin>>t.marshrut;
  189. out<<t.marshrut<<' ';
  190. SetConsoleCP(866);
  191. cout<<"Введите дату отправки:\n";
  192. SetConsoleCP(1251);
  193. cin>>t.data;
  194. out<<t.data<<' ';
  195. SetConsoleCP(866);
  196. cout<<"Введите номер вагона:\n";
  197. cin>>t.vagon;
  198. out<<t.vagon<<' ';
  199. cout<<"Введите номер места:\n";
  200. cin>>t.mesto;
  201. out<<t.mesto<<'\n';
  202. i++;
  203. }
  204. out.close();
  205. out.open("D:\output.txt");
  206. in.open("D:\input.txt");
  207. int b,v,mes;
  208. string m,d;
  209. vector< table > T;
  210. while (!in.eof())
  211. {
  212. in>>b;
  213. in>>m;
  214. in>>d;
  215. in>>v;
  216. in>>mes;
  217. t.bilet=b;
  218. t.marshrut=m;
  219. t.data=d;
  220. t.vagon=v;
  221. t.mesto=mes;
  222. T.push_back(t);
  223. }
  224. int k1,k2;
  225. cout<<"Ввыберите ключ сортировки 3-Дата 4-Вагон:\n";
  226. cin>>k1;
  227.  
  228. if (k1==3)
  229. { cout<<"Ввыберите тип сортировки 1-Бинарными вставками 2-Быстрая 3-Простого обмена\n";
  230. cin>>k2;
  231. if (k2==1) BinSortD(T);
  232. if (k2==2) QuickSortD(T, 0, T.size()-1);
  233. if (k2==3) BubbleD(T);}
  234. if (k1==4)
  235. { cout<<"Ввыберите тип сортировки 1-Бинарными вставками 2-Быстрая 3-Простого обмена\n";
  236. cin>>k2;
  237. if (k2==1) BinSortV(T);
  238. if (k2==2) QuickSortV(T, 0, T.size()-1);
  239. if (k2==3) BubbleV(T);}
  240.  
  241. for (int i=0;i<T.size()-1;i++)
  242. {
  243. cout<<T[i].bilet<<' ';
  244. out<<T[i].bilet<<' ';
  245. cout<<T[i].marshrut<<' ';
  246. out<<T[i].marshrut<<' ';
  247. cout<<T[i].data<<' ';
  248. out<<T[i].data<<' ';
  249. cout<<T[i].vagon<<' ';
  250. out<<T[i].vagon<<' ';
  251. cout<<T[i].mesto<<'\n';
  252. out<<T[i].mesto<<'\n';
  253. }
  254. out.close();
  255. in.close();
  256. system("PAUSE");
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement