Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream> //файлы
  5. #include <stdio.h> //файлы
  6. #include <stdlib.h> //преобразование типов в другие
  7. #include <iomanip> //форматирование вывода
  8. using namespace std;
  9.  
  10. struct train {
  11. string destination;//строка из букв, пробелов, тире и цифр
  12. int number;//целое число
  13. unsigned int departureTime;//целое число минут
  14. };
  15.  
  16. struct list_node {
  17. train* data;
  18. list_node* next;
  19. };
  20.  
  21. void printTrain(train* t) {
  22. int k = 9 - to_string(t->number).length();
  23. cout << t->number << setw (k + 17) << t->departureTime/60 << ":";
  24. if (t->departureTime % 60 > 9) {
  25. cout << t->departureTime % 60 << setw(43);
  26. }
  27. else {
  28. cout << "0" << t->departureTime % 60 << setw(43);
  29. }
  30. cout << t->destination << endl;
  31. cout << endl;
  32. }
  33.  
  34. bool charIsDigit(char c) {
  35. return c >= '0' && c <= '9';
  36. }
  37.  
  38. bool stringIsNumber(string s) {
  39. for (char c : s) {
  40. if (!charIsDigit(c)) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46.  
  47. class TrainList {
  48. list_node *head;
  49.  
  50. public:
  51. TrainList() { head = NULL; }
  52. ~TrainList();
  53.  
  54. bool trainWithNumberExists(int number);
  55. void add();
  56. void remove();
  57. void doRemove(int trainNumber);
  58. void show();
  59. void afterTime();
  60. void sort();
  61.  
  62. void save();
  63. void load();
  64. void modify();
  65.  
  66. void freeList();
  67. void exiting();
  68. };
  69.  
  70. TrainList::~TrainList(void) {
  71. while (head != NULL) {
  72. list_node *tmp = head->next;
  73. delete head;
  74. head = tmp;
  75. }
  76. }
  77.  
  78. bool TrainList::trainWithNumberExists(int number) {
  79. list_node *iter = head;
  80. while (iter != NULL) {
  81. if (iter->data->number == number) {
  82. return true;
  83. }
  84. iter = iter->next;
  85. }
  86.  
  87. return false;
  88. }
  89.  
  90. void TrainList::show() {
  91. if (head == NULL) {
  92. cerr << "!Empty list!" << endl;
  93. return;
  94. }
  95. cout << "Train number Train departureTime Train destination" << endl; //заголовок таблици
  96. list_node *iter = head;
  97. while (iter != NULL) {
  98. printTrain(iter->data);
  99. iter = iter->next;
  100. }
  101. }
  102.  
  103. int readTime() {
  104. string departureTimeString;
  105. int hour = 0;
  106. int minut = 0;
  107. getline(cin, departureTimeString);
  108. int r = sscanf_s(departureTimeString.c_str(), "%d %d", &hour, &minut);
  109. if (r != 2 || hour < 0 || hour > 23 || minut < 0 || minut > 59) {
  110. cerr << "!Wrong time format (H M)!" << endl;
  111. return -1;
  112. }
  113. return hour * 60 + minut;
  114. }
  115.  
  116. void TrainList::add() {
  117. int trainNumber;
  118. int departTime;
  119. while (true) {
  120. cout << "Enter number of a train you want to add (only digits and leangth <= 9): " << endl;
  121. string trainNumberString;
  122. getline(cin, trainNumberString);
  123. if(trainNumberString.length() > 9) {
  124. cerr << "!Incorrect train number!" << endl;
  125. continue;
  126. }
  127. if (!stringIsNumber(trainNumberString)) {
  128. cerr << "!Incorrect train number!" << endl;
  129. continue;
  130. }
  131. trainNumber = stoi(trainNumberString);
  132. if (trainWithNumberExists(trainNumber)) {
  133. cerr << "!Train with such number already exists!" << endl;
  134. continue;
  135. }
  136. break;
  137. }
  138. while (true) {
  139. cout << "Enter departure time of a train you want to add (H M): " << endl;
  140. departTime = readTime();
  141. if (departTime == -1) {
  142. continue;
  143. }
  144. break;
  145. }
  146. cout << "Enter destination of a train you want to add" << endl; //нижний
  147. string destination;
  148. getline(cin, destination);
  149. train* newTrain = new train;
  150.  
  151. newTrain->number = trainNumber;
  152. newTrain->departureTime = departTime;
  153. newTrain->destination = destination;
  154.  
  155. list_node* newNode = new list_node;
  156. newNode->data = newTrain;
  157. newNode->next = head;
  158. head = newNode;
  159. cout << "Element was added!" << endl;
  160. }
  161.  
  162. void TrainList::doRemove(int trainNumber) {
  163. list_node *iter = head;
  164. list_node *prev = NULL;
  165. while (iter != NULL) {
  166. if (iter->data->number == trainNumber) {
  167. if (iter == head) {
  168. head = iter->next;
  169. delete head;
  170. }
  171. else {
  172. prev->next = iter->next;
  173. delete iter;
  174. iter = prev;
  175. }
  176. }
  177.  
  178. prev = iter;
  179. iter = iter->next;
  180. }
  181. }
  182.  
  183. void TrainList::remove() {
  184. if (head == NULL) {
  185. cerr << "!Empty list!" << endl;
  186. return;
  187. }
  188. string trainNumberString;
  189. int trainNumber;
  190. while (true) {
  191. cout << "Enter number of train you want to delete" << endl;
  192. getline(cin, trainNumberString);
  193. if (!stringIsNumber(trainNumberString)) {
  194. cerr << "!Incorrect train number!" << endl;
  195. continue;
  196. }
  197. trainNumber = stoi(trainNumberString);
  198. if (!trainWithNumberExists(trainNumber)) {
  199. cerr << "!There is no train with such number!" << endl;
  200. continue;
  201. }
  202. break;
  203. }
  204. doRemove(trainNumber);
  205. cout << "Train was removed!" << endl;
  206. }
  207.  
  208. void TrainList::afterTime() {
  209. if (head == NULL) {
  210. cerr << "!Empty list!" << endl;
  211. return;
  212. }
  213. int willDeparture;
  214. while (true) {
  215. cout << "After what time do you need trains (H M): " << endl;
  216. willDeparture = readTime();
  217. if (willDeparture == -1) {
  218. continue;
  219. }
  220. break;
  221. }
  222. list_node *iter = head;
  223. bool flag = false;
  224. while (iter != NULL) {
  225. if (iter->data->departureTime >= willDeparture) {
  226. printTrain(iter->data);
  227. flag = true;
  228. }
  229. iter = iter->next;
  230. }
  231. if (!flag) {
  232. cerr << "!No trains after that time!" << endl;
  233. }
  234. }
  235.  
  236. void TrainList::load() {
  237. ifstream fp("trains.txt");
  238. freeList(); //При загрузке очищает память
  239. int trainNumber = 0;
  240. int departureTime = 0;
  241. char destination[100];
  242. string line;
  243. size_t line_size;
  244. int r;
  245. int notLoaded = 0;
  246. char trainNumberString[100];
  247. char departureTimeString[100];
  248. while (getline(fp, line)) {
  249. r = sscanf_s(line.c_str(), "%s %s %[^\n\t]", trainNumberString, (rsize_t)sizeof trainNumberString, departureTimeString, (rsize_t)sizeof departureTimeString, destination, (rsize_t)sizeof destination);
  250. if (r == 3 && stringIsNumber(trainNumberString)) {
  251. trainNumber = stoi(trainNumberString);
  252. if (!trainWithNumberExists(trainNumber) && stringIsNumber(departureTimeString)) {
  253. departureTime = stoi(departureTimeString);
  254. if (departureTime < 1440) {
  255. train* newTrain = new train;
  256. newTrain->number = trainNumber;
  257. newTrain->departureTime = departureTime;
  258. newTrain->destination = (destination);
  259.  
  260. list_node* newNode = new list_node;
  261. newNode->data = newTrain;
  262. newNode->next = head;
  263. head = newNode;
  264. }
  265. else {
  266. notLoaded++;
  267. }
  268. }
  269. else {
  270. notLoaded++;
  271. }
  272. }
  273. else {
  274. notLoaded++;
  275. }
  276. }
  277. if (notLoaded > 0) {
  278. cout << notLoaded << " trains wasnt loaded" << endl;
  279. cout << "File was loaded but not fully!" << endl;
  280. fp.close();
  281. return;
  282. }
  283. cout << "File was loaded fully!" << endl;
  284. fp.close();
  285. }
  286.  
  287. void TrainList::save() {
  288. if (head == NULL) {
  289. cerr << "!Empty list!" << endl;
  290. return;
  291. }
  292. ofstream file;
  293. file.open("trains.txt", ofstream::out | ofstream::trunc);
  294. list_node *iter = head;
  295. while (iter != NULL) {
  296. file << iter->data->number << " " << iter->data->departureTime << " " << iter->data->destination << endl;
  297. iter = iter->next;
  298. }
  299. file.close();
  300. cout << "Data was saved!" << endl;
  301. }
  302.  
  303. void TrainList::modify() {
  304. if (head == NULL) {
  305. cerr << "!Empty list!" << endl;
  306. return;
  307. }
  308. string modiTrainString;
  309. int modiTrain;
  310. int departTime;
  311. while (true) {
  312. cout << "Which train to modify: ";
  313. getline(cin, modiTrainString);
  314. if (!stringIsNumber(modiTrainString)) {
  315. cerr << "!Incorrect train number!" << endl;
  316. continue;
  317. }
  318. modiTrain = stoi(modiTrainString);
  319. if (!trainWithNumberExists(modiTrain)) {
  320. cerr << "!There is no train with such number!" << endl;
  321. continue;
  322. }
  323. break;
  324. }
  325. while (true) {
  326. cout << "Enter new departure time of a train (H M): " << endl;
  327. departTime = readTime();
  328. if (departTime == -1) {
  329. continue;
  330. }
  331. break;
  332. }
  333. cout << "Enter new destination of a train: " << endl;
  334. string destination;
  335. getline(cin, destination);
  336. list_node *iter = head;
  337. while (iter != NULL) {
  338. if (iter->data->number == modiTrain) {
  339. iter->data->departureTime = departTime;
  340. iter->data->destination = destination;
  341. }
  342. iter = iter->next;
  343. }
  344. cout << "Modifying completed!" << endl;
  345. return;
  346. }
  347.  
  348. void TrainList::sort() {
  349. if (head == NULL) {
  350. cerr << "!Empty list!" << endl;
  351. return;
  352. }
  353. list_node *iter = head;
  354. bool bubble = true;
  355. while (bubble) {
  356. bubble = false;
  357. list_node *prev = NULL;
  358. iter = head;
  359. while (iter != NULL) {
  360. list_node *next = iter->next;
  361. if (next != NULL && iter->data->departureTime > next->data->departureTime) {
  362. if (prev) {
  363. prev->next = next;
  364. }
  365. else {
  366. head = next;
  367. }
  368. list_node* tmp = next->next;
  369. next->next = iter;
  370. iter->next = tmp;
  371. bubble = true;
  372. }
  373. prev = iter;
  374. iter = next;
  375. }
  376. }
  377. cout << "List was sorted!" << endl;
  378. }
  379.  
  380. void TrainList::freeList() {
  381. list_node *iter = head;
  382. while (iter != NULL) {
  383. list_node* nIter = iter->next;
  384. free(iter->data);
  385. free(iter);
  386. iter = nIter;
  387. }
  388. head = NULL;
  389. }
  390.  
  391. void TrainList::exiting() {
  392. cout << "Do you want to save data? (y-YES/n-NO): " << endl;
  393. char ans;
  394. cin >> ans;
  395. cin.ignore();
  396. if (ans == 'y') {
  397. save();
  398. freeList();
  399. exit(0);
  400. }
  401. if (ans == 'n') {
  402. freeList();
  403. exit(0);
  404. }
  405. cerr << "!Wrond command, try again!" << endl;
  406. }
  407.  
  408. int main() {
  409. setlocale(LC_ALL, "ru");
  410. TrainList trains;
  411. while (true) {
  412. cout << "Commands:" << endl;
  413. cout << "Enter 1 to add new train;" << endl;
  414. cout << "Enter 2 to modify one of the trains" << endl;
  415. cout << "Enter 3 to show existing trains" << endl;
  416. cout << "Enter 4 to show trains that departures afther certain time" << endl;
  417. cout << "Enter 5 to delete one of the trains" << endl;
  418. cout << "Enter 6 to save trains to file" << endl;
  419. cout << "Enter 7 to load trains from file (existing data will be deleted)" << endl;
  420. cout << "Enter 8 to sort trains by time" << endl;
  421. cout << "Enter 9 to exit" << endl;
  422.  
  423. string command;
  424. getline(cin, command);
  425.  
  426. if (command.size() > 1) {
  427. cerr << "!Wrond command, try again!" << endl;
  428. continue;
  429. }
  430. if (command.size() == 0){
  431. cout << endl;
  432. continue;
  433. }
  434.  
  435. switch (command[0]) {
  436. case '1':
  437. trains.add();
  438. break;
  439. case '2':
  440. trains.modify();
  441. break;
  442. case '3':
  443. trains.show();
  444. break;
  445. case '4':
  446. trains.afterTime();
  447. break;
  448. case '5':
  449. trains.remove();
  450. break;
  451. case '6':
  452. trains.save();
  453. break;
  454. case '7':
  455. trains.load();
  456. break;
  457. case '8':
  458. trains.sort();
  459. break;
  460. case '9':
  461. trains.exiting();
  462. break;
  463. default:
  464. cerr << "!Wrond command, try again!" << endl;
  465. continue;
  466. }
  467. }
  468. return 0;
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement