Advertisement
Stoycho_KK

Untitled

Apr 13th, 2021
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <fstream>
  2. #include<iostream>
  3. #include "Flight.h"
  4.  
  5. using namespace std;
  6.  
  7. const char* FILE_NAME = "Planes.db";
  8. const int NUM_OF_FLIGHTS = 1024;
  9. const int MAX_SIZE = 1024;
  10.  
  11. bool saveFlights(const Flight& toAdd)
  12. {
  13. ofstream file(FILE_NAME, ios::app);
  14.  
  15. if (!file.is_open())
  16. return false;
  17.  
  18. file << toAdd.getId() << " " << toAdd.getPlane() << " " << toAdd.getType() << " " << toAdd.getFlights() << endl;
  19.  
  20. file.close();
  21. return true;
  22. }
  23.  
  24. int searchFlight(const Flight* collection, int planesCount, int id)
  25. {
  26. for (int i = 0; i < planesCount; i++)
  27. {
  28. if (collection[i].getId() == id)
  29. {
  30. return i;
  31. }
  32. }
  33. return -1;
  34. }
  35.  
  36. int CountOfPlanes(const char* fileName)
  37. {
  38. ifstream file(FILE_NAME);
  39.  
  40. if (!file.is_open())
  41. return -1;
  42.  
  43. int planesCount = 0;
  44.  
  45. while (!file.eof())
  46. {
  47. char buff[MAX_SIZE];
  48. file.getline(buff, MAX_SIZE);
  49. planesCount++;
  50. }
  51. file.close();
  52.  
  53. return planesCount;
  54. }
  55.  
  56. void create(Flight& init) {
  57. std::cout << "Enter id: ";
  58. int id;
  59. std::cin >> id;
  60.  
  61. std::cout << "Enter flights: ";
  62. int flights;
  63. std::cin >> flights;
  64.  
  65. std::cout << "Enter plane type: ";
  66. char planeType[1024];
  67. std::cin >> planeType;
  68.  
  69. char planeName[1024];
  70. std::cout << "Enter plane name: ";
  71. std::cin >> planeName;
  72.  
  73. Flight fl(id, planeName, planeType, flights); //creating object
  74.  
  75. init = fl;
  76. }
  77.  
  78. void readFlight(const char* input, Flight* collection, int num)
  79. {
  80. int index = 0;
  81.  
  82. int id = 0;
  83. int flights = 0;
  84. char plane[MAX_SIZE];
  85. char type[20];
  86.  
  87. while (input[index] != ' ')
  88. {
  89. id *= 10;
  90. id += input[index++] - '0';
  91. }
  92. index++;
  93.  
  94. int newInd = 0;
  95. while (input[index] != ' ')
  96. plane[newInd++] = input[index++];
  97.  
  98. plane[newInd] = '\0';
  99. index++;
  100.  
  101. newInd = 0;
  102. while (input[index] != ' ')
  103. type[newInd++] = input[index++];
  104.  
  105. type[newInd] = '\0';
  106. index++;
  107.  
  108. while (input[index] != '\0')
  109. {
  110. flights *= 10;
  111. flights += input[index++] - '0';
  112. }
  113.  
  114. collection[num].setId(id);
  115. collection[num].setPlane(plane);
  116. collection[num].setType(type);
  117. collection[num].setFlights(flights);
  118. }
  119. void Command(char command[], char input[], Flight* collection, int num, int planesCount, int id)
  120. {
  121. if (strcmp(command, "create") == 0)
  122. {
  123. Flight addMe;
  124. create(addMe);
  125. saveFlights(addMe);
  126. }
  127. else if (strcmp(command, "search") == 0)
  128. {
  129. cin >> id;
  130. cout << "Results:" << endl;
  131. searchFlight(collection, planesCount, id);
  132. }
  133. else if (strcmp(command, "exit") == 0)
  134. return;
  135. }
  136. int main()
  137. {
  138.  
  139. }
  140.  
  141. /*
  142. 1. работещ интерфейс
  143. 2. rething readFlight
  144. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement