Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <iomanip>
  9. using namespace std;
  10. struct poezda
  11. {
  12. char nazv[20];
  13. char time[20];
  14. char punkt[20];
  15. int mesta;
  16. };
  17. void vvsp(char *fname); //Ввести список
  18. void prsp(char *fname); //Прочитать список
  19. void rezc(char *fname); //Вывести результат
  20. void rezf(char *fname); //Ввести результат в файл
  21. void textf(char *fnazv, char *fname); //Ввести список в текстовый файл
  22. void qsort(int left, int right, poezda *poe);
  23. void shella(poezda *poe, int n);
  24. int menu(); //Меню
  25. int _tmain(int argc, _TCHAR* argv[])
  26. {
  27. char fname[30];
  28. cout<<"Vvedite imya fila";
  29. cin>>fname;
  30. while (true)
  31. {
  32. switch (menu())
  33. {
  34. case 1: vvsp(fname); break; //Ввести список в файл
  35. case 2: prsp(fname); break; //Прочитать
  36. case 3: rezc(fname); break; //Вывести результат на экран
  37. case 4: return 0;
  38. default: "Viberite pravilno!";
  39. }
  40. puts("Press any key to continue");
  41. system("cls");
  42. }
  43. system("pause");
  44. return 0;
  45. }
  46.  
  47. int menu()
  48. {
  49. int i;
  50. cout << "Vyberite:" << endl;
  51. cout << "1.Vyvesti dannye v fail" << endl;
  52. cout << "2.Proschitat dannye iz faila" << endl;
  53. cout << "3.Vyvesti rezultat na ekran" << endl;
  54. cout << "4.Exit" << endl;
  55. cin >> i;
  56. return i;
  57. }
  58.  
  59. void vvsp(char *fname)
  60. {
  61. poezda poe;
  62. FILE *fl;
  63. if ((fl = fopen(fname, "ab")) == NULL)
  64. if ((fl = fopen(fname, "wb")) == NULL)
  65. {
  66. cout << "Oshibka pri sozdanii" << endl;
  67. }
  68. char ch;
  69. do
  70. {
  71. cout << " Vvedite nazvanie poezda"; cin >> poe.nazv;
  72. cout << " Vvedite vremya otpravleniya"; cin >> poe.time;
  73. cout << "Vvedite punkt naznacheniya"; cin >> poe.punkt;
  74. cout << "Vvedite chislo svobodnih mest"; cin >> poe.mesta;
  75. fwrite(&poe, sizeof(poezda), 1, fl);
  76. cout << endl << " Budete vvodit dalshe? (y/n) ";
  77. cin >> ch;
  78. cout << endl;
  79. }
  80. while (ch == 'y');
  81. fclose(fl);
  82. menu();
  83. getch();
  84. }
  85.  
  86. void prsp(char *fname)
  87. {
  88. FILE *fl;
  89. poezda poe;
  90. if ((fl = fopen(fname, "rb")) == NULL)
  91. {
  92. cout << "oshibka pri otktitii" << endl;
  93. return;
  94. }
  95. long int n=0;
  96. n=filelength(fileno(fl))/sizeof(poezda);
  97. for (int i=0; i<n; i++)
  98. {
  99. fread(&poe, sizeof(poezda), 1, fl);
  100. cout << "\n Nazvanie poezda " << poe.nazv << "\n";
  101. cout << "\n Vremya otpravleniya " << poe.time << "\n";
  102. cout << "\n Punkt naznacheniya" << poe.punkt << "\n";
  103. cout << "\n Chislo svobodnih mest" << poe.mesta << endl;
  104. }
  105. fclose(fl);
  106. }
  107.  
  108. void rezc(char *fname)
  109. {
  110. poezda *poe;
  111. FILE *fl;
  112. int n;
  113. if ((fl = fopen(fname, "r")) == NULL)
  114. {
  115. cout << "Oshibka pri otkritii" << endl;
  116. return;
  117. }
  118. n=filelength(fileno(fl)) / sizeof(fname);
  119. for (int i = 0; i<n; i++)
  120. {
  121. fread(&poe, sizeof(poezda), 1, fl);
  122. if (poe[i].punkt == "Brest")
  123. {
  124. cout <<"Nazvanie poezda"<<poe[i].nazv<<"Vremya otpravleniya"<< poe[i].time<<"Punkt naznacheniya"<<poe[i].punkt<<"Chislo svobodnih mest"<<poe[i].mesta<<endl;
  125. }
  126. int b;
  127. cout<<"viberite metod sortirovki" << endl;
  128. cout<<"1.QuickSort" << endl;
  129. cout<<"2.Shella" << endl;
  130. switch (b)
  131. {
  132. case 1: qsort(0, n-1, poe);break;
  133. case 2: shella(poe, n);break;
  134. default: cout <<"Ne vibran metod sortirovki!"<<endl;
  135. }
  136. cin>>b;
  137. char q;
  138. cout <<"Zanesty otsortyrowannye dannye v textowiy file? (y/n)"<< endl;
  139. cin >> q;
  140. if (q == 'y')
  141. {
  142. char fnazv[20];
  143. cout << "Vvedite textfile nazv" << endl;
  144. cin >> fnazv;
  145. textf(fnazv,fname);
  146. }
  147. fclose(fl);
  148. return;
  149. }
  150.  
  151. void textf(char *fnazv, char *fname)
  152. {
  153. poezda poe;
  154. FILE *fl, *ft;
  155. if ((ft = fopen(fnazv, "w")) == NULL)
  156. {
  157. cout << "Oshibka pri sozdanii textfile" << endl;
  158. return;
  159. }
  160. if ((fl = fopen(fname, "rb")) == NULL)
  161. {
  162. cout << "Oshibka pri otkritii" << endl;
  163. return;
  164. }
  165. n=filelength(fileno(fl))/sizeof(poezda);
  166. for (int i=0; i<n; i++)
  167. {
  168. fread(&poe, sizeof(poezda), 1, fl);
  169. if (poe[i].punkt == "Brest")
  170. {
  171. fprintf(ft, "%s %s %s %d \n", poe[i].nazv, poe[i].time, poe[i].punkt, poe[i].mesta);
  172. }
  173. }
  174. cout << "Successfully complited" << endl;
  175. fclose(ft);
  176. fclose(fl);
  177. }
  178.  
  179. void qsort(int left, int right, poezda *poe)
  180. {
  181. int i=left, j=right, t;
  182. poezda l;
  183. t=(left+right)/2;
  184. do
  185. {
  186. while(poe[i].time<poe[t].time&&i<right) i++;
  187. while(poe[j].time>poe[t].time&&j>left) j--;
  188. if(i<=j)
  189. {
  190. l=poe[i];
  191. poe[i]=poe[j];
  192. poe[j]=l;
  193. i++; j--;
  194. }
  195. }
  196. while(i<=j);
  197. if(left<j)qsort(left, j, poe);
  198. if(i<right)qsort(i, right, poe);
  199. return;
  200. }
  201. void shella(poezda *poe, int n)
  202. {
  203. int i, j, d;
  204. poezda t;
  205. for(d=3; d>0; d--)
  206. for(i=d; i<n; i++)
  207. {
  208. t=poe[i];
  209. for(j=i-d; j>=0&&t.time<poe[i].time; j-=d) poe[j+d]=poe[j];
  210. poe[j+d]=t;
  211. }
  212. return;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement