Advertisement
Guest User

lab4

a guest
May 26th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <locale.h>
  6. #include <limits.h>
  7. #include <conio.h>
  8.  
  9. int counter = 1;
  10.  
  11. struct hotel {
  12. char country[50];
  13. char city[50];
  14. char hotel_name[50];
  15. int hotel_class;
  16. int day_price;
  17. int transfer_price;
  18. int id;
  19. };
  20. typedef struct hotel Hotel;
  21.  
  22. struct List {
  23. Hotel hotel;
  24. struct List *next;
  25. };
  26. typedef struct List *hotel_list;
  27.  
  28. Hotel input_hotel();
  29.  
  30. hotel_list add_entry(hotel_list hotels, Hotel hotel);
  31.  
  32. hotel_list delete_entry(hotel_list);
  33.  
  34. void edit_entry(hotel_list hotels);
  35.  
  36. void insert_entry(struct List *cur, Hotel hotel);
  37.  
  38. hotel_list find_entry(struct List *cur, int id);
  39.  
  40. void print_entities(hotel_list hotels);
  41.  
  42. void print_entry(hotel_list hotel);
  43.  
  44. void delete_list(hotel_list hotels);
  45.  
  46. void find_cheapest(hotel_list hotels, int hotel_class);
  47.  
  48. void calculate_trip(hotel_list hotels, int hotel_class);
  49.  
  50. hotel_list read_file(char *filename, hotel_list hotels);
  51.  
  52. void save_file(char *filename, hotel_list hotels);
  53.  
  54. void print_menu();
  55.  
  56. int main(int argc, char **argv) {
  57. setlocale(0, "Russian");
  58. char filename[30], option;
  59. int search_id, tempint = 0;
  60.  
  61. if (argc > 1) strcpy(filename, argv[1]);
  62. else {
  63. puts("Initial data filename:");
  64. gets(filename);
  65. }
  66. hotel_list hotels = NULL;
  67. hotels = read_file(filename, hotels);
  68. do {
  69. print_menu();
  70. option = getchar();
  71. switch (option) {
  72. case '1':
  73. print_entities(hotels);
  74. break;
  75. case '2':
  76. hotels = add_entry(hotels, input_hotel());
  77. break;
  78. case '3':
  79. edit_entry(hotels);
  80. break;
  81. case '4':
  82. hotels = delete_entry(hotels);
  83. break;
  84. case '5':
  85. puts("Position to put into(e.g. 1): ");
  86. scanf("%d", &search_id);
  87. if (search_id < 1 || search_id >= counter) {
  88. puts("Invalid id");
  89. system("pause");
  90. } else insert_entry(find_entry(hotels, search_id), input_hotel());
  91. break;
  92. case '6':
  93. puts("Hotel class to find cheapest: ");
  94. scanf("%d", &tempint);
  95. find_cheapest(hotels, tempint);
  96. break;
  97. case '7':
  98. puts("Hotel class to calculate average: ");
  99. scanf("%d", &tempint);
  100. calculate_trip(hotels, tempint);
  101. break;
  102. default:
  103. puts("Invalid option");
  104. }
  105. } while (option != '0');
  106. getchar();
  107. puts("Enter the filename name to save to:");
  108. gets(filename);
  109. save_file(filename, hotels);
  110. delete_list(hotels);
  111. return 0;
  112. }
  113.  
  114. hotel_list find_entry(struct List *cur, int id) {
  115. for (int i = 0; i < id - 1; i++) cur = cur->next;
  116. return cur;
  117. }
  118.  
  119. Hotel input_hotel() {
  120. Hotel hotel;
  121. getchar();
  122. puts("Country:");
  123. gets(hotel.country);
  124. puts("City: ");
  125. gets(hotel.city);
  126. puts("Hotel name:");
  127. gets(hotel.hotel_name);
  128. puts("Hotel class");
  129. scanf("%d", &hotel.hotel_class);
  130. puts("Day price");
  131. scanf("%d", &hotel.day_price);
  132. puts("Transfer costs:");
  133. scanf("%d", &hotel.transfer_price);
  134.  
  135. hotel.id = counter;
  136. return hotel;
  137. }
  138.  
  139. void insert_entry(struct List *cur, Hotel hotel) {
  140. struct List *temp;
  141. temp = (struct List *) malloc(sizeof(struct List));
  142. temp->hotel = hotel;
  143. temp->next = cur->next;
  144. cur->next = temp;
  145. }
  146.  
  147. hotel_list add_entry(hotel_list hotels, Hotel hotel) {
  148. hotel_list temp;
  149. if (hotels == NULL) {
  150. hotels = (hotel_list) malloc(sizeof(struct List));
  151. temp = hotels;
  152. } else {
  153. temp = hotels;
  154. while (temp->next) temp = temp->next;
  155. temp->next = (hotel_list) malloc(sizeof(struct List));
  156. temp = temp->next;
  157. }
  158. temp->hotel = hotel;
  159. temp->next = NULL;
  160. return hotels;
  161. }
  162.  
  163.  
  164. void delete_list(hotel_list hotels) {
  165. hotel_list temp = hotels;
  166. while (temp) {
  167. hotels = temp->next;
  168. free(temp);
  169. temp = hotels;
  170. }
  171. }
  172.  
  173. hotel_list read_file(char *filename, hotel_list hotels) {
  174. FILE *fin;
  175. Hotel hotel;
  176. if ((fin = fopen(filename, "rb")) == NULL) {
  177. perror("Can't open this file");
  178. getch();
  179. return hotels;
  180. }
  181. while (fread(&hotel, sizeof(hotel), 1, fin))
  182. hotels = add_entry(hotels, hotel);
  183. fclose(fin);
  184. return hotels;
  185. }
  186.  
  187. void save_file(char *filename, hotel_list hotels) {
  188. FILE *fout;
  189. if ((fout = fopen(filename, "wb")) == NULL) {
  190. perror("Can't create the file");
  191. system("pause");
  192. return;
  193. }
  194. while (hotels) {
  195. fwrite(&hotels->hotel, sizeof(Hotel), 1, fout);
  196. hotels = hotels->next;
  197. }
  198. }
  199.  
  200. void edit_entry(hotel_list hotels) {
  201. int n, k = 1;
  202. char yes;
  203. system("CLS");
  204. if (hotels == NULL) {
  205. puts("No items in the list");
  206. system("pause");
  207. return;
  208. }
  209. puts("ID to edit:");
  210. scanf("%d", &n);
  211. if (n < 1) {
  212. puts("Error");
  213. system("pause");
  214. return;
  215. }
  216. while (hotels && k < n) {
  217. hotels = hotels->next;
  218. k++;
  219. }
  220. if (hotels == NULL) {
  221. puts("Error");
  222. system("pause");
  223. return;
  224. }
  225. print_entry(hotels);
  226. puts("Apply the changes? (y/n)");
  227. do
  228. yes = getchar();
  229. while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
  230. if (yes == 'y' || yes == 'Y')
  231. hotels->hotel = input_hotel();
  232. }
  233.  
  234. void print_entities(hotel_list hotels) {
  235.  
  236. int b = 1;
  237. system("CLS");
  238. if (hotels == NULL) {
  239. puts("List is empty");
  240. system("pause");
  241. return;
  242. }
  243.  
  244. puts("| # | Country | City | Hotel name | Hotel class | Day price | Transfer price");
  245. while (hotels) {
  246. hotels->hotel.id++;
  247. printf("| %-2d | %-10s | %-10s | %-10s | %-6d | %-9d | %-9d \n", b++, hotels->hotel.country,
  248. hotels->hotel.city, hotels->hotel.hotel_name, hotels->hotel.hotel_class, hotels->hotel.day_price,
  249. hotels->hotel.transfer_price);
  250. hotels = hotels->next;
  251. }
  252. system("pause");
  253. }
  254.  
  255. void print_entry(hotel_list entry) {
  256. printf("Country : %s\nCity : %s\nHotel name : %s\nHotel class : %d\nDay price : %d\nTransfer price : %d\n",
  257. entry->hotel.country,
  258. entry->hotel.city, entry->hotel.hotel_name, entry->hotel.hotel_class, entry->hotel.day_price,
  259. entry->hotel.transfer_price);
  260. }
  261.  
  262. void find_cheapest(hotel_list hotels, int hotel_class) {
  263. int min = INT_MAX;
  264. hotel_list temp;
  265. while (hotels) {
  266. hotels->hotel.id++;
  267. if (hotels->hotel.hotel_class == hotel_class && hotels->hotel.day_price < min) {
  268. min = hotels->hotel.day_price;
  269. temp = hotels;
  270. }
  271. hotels = hotels->next;
  272. }
  273. if (min != INT_MAX) {
  274. print_entry(temp);
  275. } else {
  276. puts("No hotel found");
  277. }
  278. system("pause");
  279. }
  280.  
  281. void calculate_trip(hotel_list hotels, int hotel_class) {
  282. int counter = 0;
  283. int cost = 0;
  284. while (hotels) {
  285. hotels->hotel.id++;
  286. if (hotels->hotel.hotel_class == hotel_class) {
  287. cost += 7 * hotels->hotel.day_price + hotels->hotel.transfer_price;
  288. counter++;
  289. }
  290. hotels = hotels->next;
  291. }
  292. cost /= counter;
  293. printf("Average cost for the %d class is %d", hotel_class, cost);
  294. system("pause");
  295. }
  296.  
  297.  
  298. void print_menu() {
  299. system("CLS");
  300. puts("1. Print hotels");
  301. puts("2. Add hotel");
  302. puts("3. Edit hotel");
  303. puts("4. Delete hotel");
  304. puts("5. Insert hotel");
  305. puts("6. Find cheapest");
  306. puts("7. Calculate trip's costs");
  307. puts("0. Exit");
  308. puts("Type number to proceed:");
  309. }
  310.  
  311. hotel_list delete_entry(hotel_list hotels) {
  312. int n, j = 1;
  313. char yes;
  314. hotel_list temp, temp1;
  315. system("CLS");
  316. if (hotels == NULL) {
  317. puts("List is empty");
  318. system("pause");
  319. return NULL;
  320. }
  321. puts("Number record for delete?");
  322. scanf("%d", &n);
  323. if (n < 1) {
  324. puts("Error");
  325. system("pause");
  326. return hotels;
  327. }
  328. if (n == 1) {
  329. print_entry(hotels);
  330. puts("Delete? (y/n)");
  331. do
  332. yes = getchar();
  333. while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
  334. if (yes == 'y' || yes == 'Y') {
  335. temp = hotels->next;
  336. free(hotels);
  337. return temp;
  338. } else return hotels;
  339. }
  340. if (hotels->next == NULL && n > 1) {
  341. puts("Error");
  342. system("pause");
  343. return hotels;
  344. }
  345. temp = hotels;
  346. temp1 = temp->next;
  347. while (temp1->next && j < n - 1) {
  348. temp = temp1;
  349. temp1 = temp->next;
  350. j++;
  351. }
  352. if (j < n - 1) {
  353. puts("Error");
  354. system("pause");
  355. return hotels;
  356. }
  357. print_entry(temp1);
  358. puts("Delete? (y/n)");
  359. do
  360. yes = getchar();
  361. while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
  362. if (yes == 'y' || yes == 'Y') {
  363. temp->next = temp1->next;
  364. free(temp1);
  365. }
  366. return hotels;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement