Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <conio.h>
  4.  
  5. class Railway
  6. {
  7. char* destination = new char[15];
  8. char* time = new char[15];
  9. int numberOfFreeTickets;
  10. int ticketPrice;
  11. int profit;
  12. public:
  13. Railway()
  14. {
  15. inputInfo();
  16. }
  17.  
  18. void inputInfo()
  19. {
  20. printf("\nDestination: ");
  21. scanf("%s", destination);
  22. printf("Time: ");
  23. scanf("%s", time);
  24. printf("Number of tickets: ");
  25. scanf("%d", &numberOfFreeTickets);
  26. printf("Ticket price: ");
  27. scanf("%d", &ticketPrice);
  28. profit = (int)numberOfFreeTickets * (int)ticketPrice;
  29. }
  30.  
  31. void outputInfo()
  32. {
  33. printf("%15s|", destination);
  34. printf("%15s|", time);
  35. printf("%10d|", numberOfFreeTickets);
  36. printf("%10d|", ticketPrice);
  37. printf("%10d|", profit);
  38. printf("\n");
  39. }
  40.  
  41. void setDestination(char *destination)
  42. {
  43. delete [] destination;
  44. this->destination = destination;
  45. }
  46.  
  47. char* getDestination()
  48. {
  49. return destination;
  50. }
  51.  
  52. void setTime(char *time)
  53. {
  54. delete [] time;
  55. this->time = time;
  56. }
  57.  
  58. char* getTime()
  59. {
  60. return time;
  61. }
  62.  
  63. void setNumberOfFreeTickets(int numberOfFreeTickets)
  64. {
  65. this->numberOfFreeTickets = numberOfFreeTickets;
  66. profit = (int)numberOfFreeTickets * (int)ticketPrice;
  67. }
  68.  
  69. int getNumberOfFreeTickets()
  70. {
  71. return numberOfFreeTickets;
  72. }
  73.  
  74. void setTicketPrice(int ticketPrice)
  75. {
  76. this->ticketPrice = ticketPrice;
  77. profit = (int)numberOfFreeTickets * (int)ticketPrice;
  78. }
  79.  
  80. int getTicketPrice()
  81. {
  82. return ticketPrice;
  83. }
  84.  
  85. int getProfit()
  86. {
  87. return profit;
  88. }
  89.  
  90. ~Railway()
  91. {
  92. delete [] destination;
  93. delete [] time;
  94. }
  95. };
  96.  
  97.  
  98. class Admin
  99. {
  100. int count;
  101. Railway** Array;
  102.  
  103. void outputHead()
  104. {
  105. printf(" Destination|");
  106. printf(" Time|");
  107. printf(" Count|");
  108. printf(" Price|");
  109. printf(" Profit|");
  110. printf("\n");
  111. }
  112.  
  113. void outputLine()
  114. {
  115. printf("-----------------------------------------------------------------\n");
  116. }
  117.  
  118. public:
  119.  
  120. Admin()
  121. {
  122. count = 0;
  123. }
  124.  
  125. void inputArray()
  126. {
  127. printf("Input number of tickets: ");
  128. scanf("%d", &count);
  129. Array = new Railway*[count];
  130. for(int i = 0; i < count; i++)
  131. Array[i] = new Railway();
  132. }
  133.  
  134. void outputArray()
  135. {
  136. outputHead();
  137. outputLine();
  138. for(int i = 0; i < count; i++)
  139. {
  140. Array[i]->outputInfo();
  141. outputLine();
  142. }
  143.  
  144. }
  145.  
  146. void addElement()
  147. {
  148. count++;
  149. Railway** temp = new Railway*[count];
  150. for(int i = 0; i < count - 1; i++)
  151. temp[i] = Array[i];
  152. temp[count - 1] = new Railway();
  153. delete [] Array;
  154. Array = temp;
  155. }
  156.  
  157. void findByDestination()
  158. {
  159. char destination[15];
  160. printf("\nDestination: ");
  161. scanf("%s", destination);
  162. outputHead();
  163. outputLine();
  164. for(int i = 0; i < count; i++)
  165. {
  166. if(strcmp(destination, Array[i]->getDestination()) == 0)
  167. {
  168. Array[i]->outputInfo();
  169. outputLine();
  170. }
  171. }
  172. }
  173.  
  174. void deleteElement()
  175. {
  176. int i = 0;
  177. char destination[15];
  178. printf("\nDestination: ");
  179. scanf("%s", destination);
  180. while(i < count)
  181. {
  182. if(strcmp(destination, Array[i]->getDestination()) == 0)
  183. {
  184. for(int j = i + 1; j < count; j++)
  185. Array[j - 1] = Array[j];
  186. count--;
  187. }
  188. i++;
  189. }
  190. }
  191.  
  192. void editElement()
  193. {
  194. char time[15];
  195. char* newTime = new char[15];
  196. printf("\nTime: ");
  197. scanf("%s", time);
  198. printf("\nNew Time: ");
  199. scanf("%s", newTime);
  200. for(int i = 0; i < count; i++)
  201. {
  202. if(strcmp(time, Array[i]->getTime()) == 0)
  203. {
  204. Array[i]->setTime(newTime);
  205. }
  206. }
  207. printf("Well done");
  208. }
  209.  
  210. void sortArray()
  211. {
  212. for (int i = 0; i < count - 1; i++)
  213. {
  214. for (int j = i + 1; j < count; j++)
  215. {
  216. if (Array[i]->getTicketPrice() < Array[j]->getTicketPrice())
  217. {
  218. Railway* temp = Array[i];
  219. Array[i] = Array[j];
  220. Array[j] = temp;
  221. }
  222. }
  223. }
  224. printf("Well done");
  225. }
  226.  
  227. void menu()
  228. {
  229. while(true)
  230. {
  231. system("cls");
  232. printf("1.Fill array\n2.Output array\n3.Add element\n4.Find by destination\n5.Delete element\n6.Edit element\n7.Sort array\n8.exit\n");
  233. int choose;
  234. scanf("%d", &choose);
  235. switch(choose)
  236. {
  237. case 1:
  238. {
  239. inputArray();
  240. break;
  241. }
  242. case 2:
  243. {
  244. outputArray();
  245. break;
  246. }
  247. case 3:
  248. {
  249. addElement();
  250. break;
  251. }
  252. case 4:
  253. {
  254. findByDestination();
  255. break;
  256. }
  257. case 5:
  258. {
  259. deleteElement();
  260. break;
  261. }
  262. case 6:
  263. {
  264. editElement();
  265. break;
  266. }
  267. case 7:
  268. {
  269. sortArray();
  270. break;
  271. }
  272. case 8:
  273. {
  274. return;
  275. }
  276. }
  277. getch();
  278. }
  279. }
  280.  
  281. ~Admin()
  282. {
  283. delete [] Array;
  284. }
  285. };
  286.  
  287. int main()
  288. {
  289. Admin a;
  290. a.menu();
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement