Advertisement
Guest User

Untitled

a guest
May 25th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.03 KB | None | 0 0
  1. // Kursach2.cpp: главный файл проекта.
  2.  
  3. #include "stdafx.h"
  4. #include "iostream"
  5. #include "fstream"
  6. #include "string"
  7.  
  8. using namespace System;
  9. using namespace std;
  10.  
  11. int AskUserNameAndPass();
  12. int CountsOfProviders();
  13. int StartMenu();
  14. void ReadProvider();
  15. void AddProvidersBook();
  16. void PrintProvidersBook();
  17. void SelectSort();
  18. void ShellSortName();
  19. void ShellSortCount();
  20. void ShellSortAverage();
  21. void ShellSortMaxSpeed();
  22. void ClearProvidersBook();
  23. void ChangeInformation();
  24.  
  25. struct Provider
  26. {
  27. string NameOfProvider;
  28. int CountOfTariffs;
  29. long long int AverageCostTariffs;
  30. long long int MaxSpeedOfTarrifs;
  31. };
  32.  
  33. Provider InformationAboutProvider[100];
  34.  
  35. string UserName[5] =
  36. {
  37. "Admin",
  38. "User",
  39. "Guest",
  40. "Ghost",
  41. "Killer"
  42. };
  43.  
  44. int CountOfProviders = 0;
  45.  
  46. string Password[5] =
  47. {
  48. "administrator",
  49. "user",
  50. "guest",
  51. "ghost",
  52. "killer"
  53. };
  54.  
  55. int main(array<System::String ^> ^args)
  56. {
  57. setlocale ( 0, "Rus" );
  58. if ( AskUserNameAndPass() == 1 )
  59. {
  60. do
  61. {
  62. ReadProvider();
  63. } while ( StartMenu() != 2 );
  64. }
  65. return 0;
  66. }
  67.  
  68. int AskUserNameAndPass()
  69. {
  70. int Flag = 0;
  71. char UserChoice = ' ';
  72. string User;
  73. string Pass;
  74. cout << "Добро пожловать, введите имя пользователя: ";
  75. cin >> User;
  76. cout << "Введите пароль: ";
  77. cin >> Pass;
  78. for (int i = 0; i < 5; i++)
  79. {
  80. if ( User == UserName[i] )
  81. {
  82. if ( Pass == Password[i] )
  83. {
  84. cout << "Вход в систему успешен, добро пожаловать, " << User << endl;
  85. Flag = 1;
  86. }
  87. }
  88. }
  89. if ( Flag == 0 )
  90. {
  91. cout << "Вы ввели не верные данные! Хотите попробовать еще? y/n?";
  92. cin >> UserChoice;
  93. if ( UserChoice == 'y' )
  94. {
  95. return AskUserNameAndPass();
  96. }
  97. else
  98. {
  99. cout << "Работа программы завершена.";
  100. return 0;
  101. }
  102. }
  103. else
  104. {
  105. return 1;
  106. }
  107. }
  108.  
  109. int CountsOfProviders()
  110. {
  111. CountOfProviders = 0;
  112. fstream WorkWithFile;
  113. WorkWithFile.open( "ProvidersBook.txt", ios::in );
  114. string text;
  115. while ( !WorkWithFile.eof() )
  116. {
  117. getline( WorkWithFile, text );
  118. CountOfProviders++;
  119. }
  120. WorkWithFile.close();
  121. return CountOfProviders - 1;
  122. }
  123.  
  124. int StartMenu()
  125. {
  126. int UserSelect;
  127. cout << "Добро пожаловать в справочник провайдеров! Что бы вы хотели сделать?" << endl;
  128. cout << "1) Добавление провайдера в список." << endl;
  129. cout << "2) Вывод списка провайдеров на экран." << endl;
  130. cout << "3) Вывод сортированного списка провайдеров. " << endl;
  131. cout << "4) Очистить список провайдеров." << endl;
  132. cout << "5) Изменить данные." << endl;
  133. cout << "6) Выход из программы." << endl;
  134. cout << "Введите ваш выбор: ";
  135. cin >> UserSelect;
  136. switch ( UserSelect )
  137. {
  138. case 1:
  139. {
  140. AddProvidersBook();
  141. return 1;
  142. break;
  143. }
  144. case 2:
  145. {
  146. PrintProvidersBook();
  147. return 1;
  148. break;
  149. }
  150. case 3:
  151. {
  152. SelectSort();
  153. return 1;
  154. break;
  155. }
  156. case 4:
  157. {
  158. ClearProvidersBook();
  159. return 1;
  160. break;
  161. }
  162. case 5:
  163. {
  164. ChangeInformation();
  165. return 1;
  166. break;
  167. }
  168. case 6:
  169. {
  170. return 2;
  171. }
  172. default:
  173. {
  174. cout << "Попробуйте еще раз!" << endl;
  175. StartMenu();
  176. return 1;
  177. break;
  178. }
  179. }
  180. };
  181.  
  182. void ReadProvider()
  183. {
  184. CountOfProviders = CountsOfProviders();
  185. int Numbers = 0;
  186. if ( CountOfProviders != 0 )
  187. {
  188. fstream WorkWithFile;
  189. WorkWithFile.open( "ProvidersBook.txt" );
  190. string text, HelpToWright = "";
  191. int LineCounter;
  192. while ( !WorkWithFile.eof() )
  193. {
  194. for (int i = 0; i < CountOfProviders; i++)
  195. {
  196. getline ( WorkWithFile, text );
  197. LineCounter = 1;
  198. for (int j = 0; j < text.length(); j++)
  199. {
  200. if ( text[j] != ' ' )
  201. {
  202.  
  203. if ( LineCounter == 1 )
  204. {
  205. HelpToWright += text[j];
  206. }
  207.  
  208. if ( LineCounter == 2 )
  209. {
  210. HelpToWright += text[j];
  211. }
  212.  
  213. if ( LineCounter == 3 )
  214. {
  215. HelpToWright += text[j];
  216. }
  217.  
  218. if ( LineCounter == 4 )
  219. {
  220. HelpToWright += text[j];
  221. }
  222. }
  223. else
  224. {
  225. if ( LineCounter == 1 )
  226. InformationAboutProvider[i].NameOfProvider = HelpToWright;
  227.  
  228. if ( LineCounter == 2 )
  229. {
  230. Numbers = stoi(HelpToWright);
  231. InformationAboutProvider[i].CountOfTariffs = Numbers;
  232. }
  233.  
  234. if ( LineCounter == 3 )
  235. {
  236. Numbers = stoi(HelpToWright);
  237. InformationAboutProvider[i].AverageCostTariffs = Numbers;
  238. }
  239.  
  240. if ( LineCounter == 4 )
  241. {
  242. Numbers = stoi(HelpToWright);
  243. InformationAboutProvider[i].MaxSpeedOfTarrifs = Numbers;
  244. }
  245.  
  246. Numbers = 0;
  247. HelpToWright = "";
  248. LineCounter++;
  249. }
  250. }
  251. }
  252. }
  253. WorkWithFile.close();
  254. }
  255. else
  256. {
  257. cout << "В данный момент файл пуст!" << endl;
  258. }
  259. };
  260.  
  261. void AddProvidersBook()
  262. {
  263. CountOfProviders = CountsOfProviders() + 1;
  264. ofstream WorkWIthFile;
  265. WorkWIthFile.open( "ProvidersBook.txt", ios::app );
  266. cout << "Функция добавляет провайдера в список." << endl;
  267. cout << "Введите наименование провайдера: ";
  268. cin >> InformationAboutProvider[CountOfProviders].NameOfProvider;
  269. cout << "Введите количество тарифов: ";
  270. cin >> InformationAboutProvider[CountOfProviders].CountOfTariffs;
  271. cout << "Введите среднюю стоимость тарифов: ";
  272. cin >> InformationAboutProvider[CountOfProviders].AverageCostTariffs;
  273. cout << "Введите максимальную скорость которую предоставляет провайдер: ";
  274. cin >> InformationAboutProvider[CountOfProviders].MaxSpeedOfTarrifs;
  275. WorkWIthFile << InformationAboutProvider[CountOfProviders].NameOfProvider << " " << InformationAboutProvider[CountOfProviders].CountOfTariffs << " " << InformationAboutProvider[CountOfProviders].AverageCostTariffs << " " << InformationAboutProvider[CountOfProviders].MaxSpeedOfTarrifs << " " << endl;
  276. cout << "Данные успешно занесены в книгу!" << endl;
  277. WorkWIthFile.close();
  278. };
  279.  
  280. void PrintProvidersBook()
  281. {
  282. CountOfProviders = CountsOfProviders();
  283. cout << "Провайдеров в списке: " << CountOfProviders << endl;
  284. for (int i = 0; i < CountOfProviders; i++)
  285. {
  286. cout << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  287. }
  288. };
  289.  
  290. void SelectSort()
  291. {
  292. int MetodOfSort;
  293. cout << "По какому параметру произвести сортировку? " << endl;
  294. cout << "1) Наименование провайдера." << endl;
  295. cout << "2) Количество тарифов." << endl;
  296. cout << "3) Средняя стоимость тарифов." << endl;
  297. cout << "4) Максимальная скорость." << endl;
  298. cout << "Ваш выбор: ";
  299. cin >> MetodOfSort;
  300. cout << endl;
  301. switch ( MetodOfSort )
  302. {
  303. case 1:
  304. {
  305. ShellSortName();
  306. break;
  307. }
  308. case 2:
  309. {
  310. ShellSortCount();
  311. break;
  312. }
  313. case 3:
  314. {
  315. ShellSortAverage();
  316. break;
  317. }
  318. case 4:
  319. {
  320. ShellSortMaxSpeed();
  321. break;
  322. }
  323. default:
  324. {
  325. cout << "Попробуйте еще раз!" << endl;
  326. SelectSort();
  327. break;
  328. }
  329. }
  330. };
  331.  
  332. void ClearProvidersBook()
  333. {
  334. fstream WorkWithFile;
  335. WorkWithFile.open ( "ProvidersBook.txt", ios::out );
  336. WorkWithFile.clear();
  337. WorkWithFile.close();
  338. };
  339.  
  340. void ChangeInformation()
  341. {
  342. fstream WorkWithFile;
  343. WorkWithFile.open( "ProvidersBook.txt", ios::out );
  344. int NumberOfLine = 0;
  345. int NumberOfRed, UserChoice;
  346. for (int i = 0; i < CountOfProviders ; i++)
  347. {
  348. cout << NumberOfLine << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  349. NumberOfLine++;
  350. }
  351. cout << "Какую строку вы хотели бы редактировать: ";
  352. cin >> NumberOfRed;
  353. cout << "Введите наименование провайдера: " ;
  354. cin >> InformationAboutProvider[NumberOfRed].NameOfProvider;
  355. cout << "Введите количетсво тарифов: ";
  356. cin >> InformationAboutProvider[NumberOfRed].CountOfTariffs;
  357. cout << "Введите среднюю стоимость тарифов: ";
  358. cin >> InformationAboutProvider[NumberOfRed].AverageCostTariffs;
  359. cout << "Введите максимальную скорость ";
  360. cin >> InformationAboutProvider[NumberOfRed].MaxSpeedOfTarrifs;
  361. for (int i = 0; i < CountOfProviders ; i++)
  362. {
  363. cout << NumberOfLine << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  364. NumberOfLine++;
  365. }
  366. cout << "Перезаписать файл? 1/0 " << endl;
  367. cin >> UserChoice;
  368. if (UserChoice = 1 )
  369. {
  370. for (int i = 0; i < CountOfProviders; i++)
  371. {
  372. WorkWithFile << InformationAboutProvider[CountOfProviders].NameOfProvider << " " << InformationAboutProvider[CountOfProviders].CountOfTariffs << " " << InformationAboutProvider[CountOfProviders].AverageCostTariffs << " " << InformationAboutProvider[CountOfProviders].MaxSpeedOfTarrifs << " " << endl;
  373. }
  374. cout << "Сохранено" << endl;
  375. }
  376. else
  377. {
  378. cout << "Не записано" << endl;
  379. }
  380. WorkWithFile.close();
  381. };
  382.  
  383. void ShellSortName()
  384. {
  385. CountOfProviders = CountsOfProviders();
  386. int Step = CountOfProviders;
  387. while ( Step > 0 )
  388. {
  389. for (int i = 0; i < ( CountOfProviders - Step ); i++)
  390. {
  391. int j = i ;
  392. while ( j >= 0 && InformationAboutProvider[j].NameOfProvider > InformationAboutProvider[ j + Step ].NameOfProvider )
  393. {
  394. Provider temp = InformationAboutProvider[j];
  395. InformationAboutProvider[j] = InformationAboutProvider[ j + Step ];
  396. InformationAboutProvider[ j + Step ] = temp;
  397. j--;
  398. }
  399. }
  400. Step = Step / 2;
  401. }
  402. for (int i = 0; i < CountOfProviders; i++)
  403. {
  404. cout << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  405. }
  406. };
  407.  
  408. void ShellSortCount()
  409. {
  410. CountOfProviders = CountsOfProviders();
  411. int Step = CountOfProviders;
  412. while ( Step > 0 )
  413. {
  414. for (int i = 0; i < ( CountOfProviders - Step ); i++)
  415. {
  416. int j = i ;
  417. while ( j >= 0 && InformationAboutProvider[j].CountOfTariffs > InformationAboutProvider[ j + Step ].CountOfTariffs)
  418. {
  419. Provider temp = InformationAboutProvider[j];
  420. InformationAboutProvider[j] = InformationAboutProvider[ j + Step ];
  421. InformationAboutProvider[ j + Step ] = temp;
  422. j--;
  423. }
  424. }
  425. Step = Step / 2;
  426. }
  427. for (int i = 0; i < CountOfProviders; i++)
  428. {
  429. cout << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  430. }
  431. };
  432.  
  433. void ShellSortAverage()
  434. {
  435. CountOfProviders = CountsOfProviders();
  436. int Step = CountOfProviders;
  437. while ( Step > 0 )
  438. {
  439. for (int i = 0; i < ( CountOfProviders - Step ); i++)
  440. {
  441. int j = i ;
  442. while ( j >= 0 && InformationAboutProvider[j].AverageCostTariffs > InformationAboutProvider[ j + Step ].AverageCostTariffs)
  443. {
  444. Provider temp = InformationAboutProvider[j];
  445. InformationAboutProvider[j] = InformationAboutProvider[ j + Step ];
  446. InformationAboutProvider[ j + Step ] = temp;
  447. j--;
  448. }
  449. }
  450. Step = Step / 2;
  451. }
  452. for (int i = 0; i < CountOfProviders; i++)
  453. {
  454. cout << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  455. }
  456. };
  457.  
  458. void ShellSortMaxSpeed()
  459. {
  460. CountOfProviders = CountsOfProviders();
  461. int Step = CountOfProviders;
  462. while ( Step > 0 )
  463. {
  464. for (int i = 0; i < ( CountOfProviders - Step ); i++)
  465. {
  466. int j = i ;
  467. while ( j >= 0 && InformationAboutProvider[j].MaxSpeedOfTarrifs > InformationAboutProvider[ j + Step ].MaxSpeedOfTarrifs )
  468. {
  469. Provider temp = InformationAboutProvider[j];
  470. InformationAboutProvider[j] = InformationAboutProvider[ j + Step ];
  471. InformationAboutProvider[ j + Step ] = temp;
  472. j--;
  473. }
  474. }
  475. Step = Step / 2;
  476. }
  477. for (int i = 0; i < CountOfProviders; i++)
  478. {
  479. cout << InformationAboutProvider[i].NameOfProvider << " " << InformationAboutProvider[i].CountOfTariffs << " " << InformationAboutProvider[i].AverageCostTariffs << " " << InformationAboutProvider[i].MaxSpeedOfTarrifs << endl;
  480. }
  481. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement