Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <locale.h>
- #include <limits.h>
- #include <conio.h>
- int counter = 1;
- struct hotel {
- char country[50];
- char city[50];
- char hotel_name[50];
- int hotel_class;
- int day_price;
- int transfer_price;
- int id;
- };
- typedef struct hotel Hotel;
- struct List {
- Hotel hotel;
- struct List *next;
- };
- typedef struct List *hotel_list;
- Hotel input_hotel();
- hotel_list add_entry(hotel_list hotels, Hotel hotel);
- hotel_list delete_entry(hotel_list);
- void edit_entry(hotel_list hotels);
- void insert_entry(struct List *cur, Hotel hotel);
- hotel_list find_entry(struct List *cur, int id);
- void print_entities(hotel_list hotels);
- void print_entry(hotel_list hotel);
- void delete_list(hotel_list hotels);
- void find_cheapest(hotel_list hotels, int hotel_class);
- void calculate_trip(hotel_list hotels, int hotel_class);
- hotel_list read_file(char *filename, hotel_list hotels);
- void save_file(char *filename, hotel_list hotels);
- void print_menu();
- int main(int argc, char **argv) {
- setlocale(0, "Russian");
- char filename[30], option;
- int search_id, tempint = 0;
- if (argc > 1) strcpy(filename, argv[1]);
- else {
- puts("Initial data filename:");
- gets(filename);
- }
- hotel_list hotels = NULL;
- hotels = read_file(filename, hotels);
- do {
- print_menu();
- option = getchar();
- switch (option) {
- case '1':
- print_entities(hotels);
- break;
- case '2':
- hotels = add_entry(hotels, input_hotel());
- break;
- case '3':
- edit_entry(hotels);
- break;
- case '4':
- hotels = delete_entry(hotels);
- break;
- case '5':
- puts("Position to put into(e.g. 1): ");
- scanf("%d", &search_id);
- if (search_id < 1 || search_id >= counter) {
- puts("Invalid id");
- system("pause");
- } else insert_entry(find_entry(hotels, search_id), input_hotel());
- break;
- case '6':
- puts("Hotel class to find cheapest: ");
- scanf("%d", &tempint);
- find_cheapest(hotels, tempint);
- break;
- case '7':
- puts("Hotel class to calculate average: ");
- scanf("%d", &tempint);
- calculate_trip(hotels, tempint);
- break;
- default:
- puts("Invalid option");
- }
- } while (option != '0');
- getchar();
- puts("Enter the filename name to save to:");
- gets(filename);
- save_file(filename, hotels);
- delete_list(hotels);
- return 0;
- }
- hotel_list find_entry(struct List *cur, int id) {
- for (int i = 0; i < id - 1; i++) cur = cur->next;
- return cur;
- }
- Hotel input_hotel() {
- Hotel hotel;
- getchar();
- puts("Country:");
- gets(hotel.country);
- puts("City: ");
- gets(hotel.city);
- puts("Hotel name:");
- gets(hotel.hotel_name);
- puts("Hotel class");
- scanf("%d", &hotel.hotel_class);
- puts("Day price");
- scanf("%d", &hotel.day_price);
- puts("Transfer costs:");
- scanf("%d", &hotel.transfer_price);
- hotel.id = counter;
- return hotel;
- }
- void insert_entry(struct List *cur, Hotel hotel) {
- struct List *temp;
- temp = (struct List *) malloc(sizeof(struct List));
- temp->hotel = hotel;
- temp->next = cur->next;
- cur->next = temp;
- }
- hotel_list add_entry(hotel_list hotels, Hotel hotel) {
- hotel_list temp;
- if (hotels == NULL) {
- hotels = (hotel_list) malloc(sizeof(struct List));
- temp = hotels;
- } else {
- temp = hotels;
- while (temp->next) temp = temp->next;
- temp->next = (hotel_list) malloc(sizeof(struct List));
- temp = temp->next;
- }
- temp->hotel = hotel;
- temp->next = NULL;
- return hotels;
- }
- void delete_list(hotel_list hotels) {
- hotel_list temp = hotels;
- while (temp) {
- hotels = temp->next;
- free(temp);
- temp = hotels;
- }
- }
- hotel_list read_file(char *filename, hotel_list hotels) {
- FILE *fin;
- Hotel hotel;
- if ((fin = fopen(filename, "rb")) == NULL) {
- perror("Can't open this file");
- getch();
- return hotels;
- }
- while (fread(&hotel, sizeof(hotel), 1, fin))
- hotels = add_entry(hotels, hotel);
- fclose(fin);
- return hotels;
- }
- void save_file(char *filename, hotel_list hotels) {
- FILE *fout;
- if ((fout = fopen(filename, "wb")) == NULL) {
- perror("Can't create the file");
- system("pause");
- return;
- }
- while (hotels) {
- fwrite(&hotels->hotel, sizeof(Hotel), 1, fout);
- hotels = hotels->next;
- }
- }
- void edit_entry(hotel_list hotels) {
- int n, k = 1;
- char yes;
- system("CLS");
- if (hotels == NULL) {
- puts("No items in the list");
- system("pause");
- return;
- }
- puts("ID to edit:");
- scanf("%d", &n);
- if (n < 1) {
- puts("Error");
- system("pause");
- return;
- }
- while (hotels && k < n) {
- hotels = hotels->next;
- k++;
- }
- if (hotels == NULL) {
- puts("Error");
- system("pause");
- return;
- }
- print_entry(hotels);
- puts("Apply the changes? (y/n)");
- do
- yes = getchar();
- while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
- if (yes == 'y' || yes == 'Y')
- hotels->hotel = input_hotel();
- }
- void print_entities(hotel_list hotels) {
- int b = 1;
- system("CLS");
- if (hotels == NULL) {
- puts("List is empty");
- system("pause");
- return;
- }
- puts("| # | Country | City | Hotel name | Hotel class | Day price | Transfer price");
- while (hotels) {
- hotels->hotel.id++;
- printf("| %-2d | %-10s | %-10s | %-10s | %-6d | %-9d | %-9d \n", b++, hotels->hotel.country,
- hotels->hotel.city, hotels->hotel.hotel_name, hotels->hotel.hotel_class, hotels->hotel.day_price,
- hotels->hotel.transfer_price);
- hotels = hotels->next;
- }
- system("pause");
- }
- void print_entry(hotel_list entry) {
- printf("Country : %s\nCity : %s\nHotel name : %s\nHotel class : %d\nDay price : %d\nTransfer price : %d\n",
- entry->hotel.country,
- entry->hotel.city, entry->hotel.hotel_name, entry->hotel.hotel_class, entry->hotel.day_price,
- entry->hotel.transfer_price);
- }
- void find_cheapest(hotel_list hotels, int hotel_class) {
- int min = INT_MAX;
- hotel_list temp;
- while (hotels) {
- hotels->hotel.id++;
- if (hotels->hotel.hotel_class == hotel_class && hotels->hotel.day_price < min) {
- min = hotels->hotel.day_price;
- temp = hotels;
- }
- hotels = hotels->next;
- }
- if (min != INT_MAX) {
- print_entry(temp);
- } else {
- puts("No hotel found");
- }
- system("pause");
- }
- void calculate_trip(hotel_list hotels, int hotel_class) {
- int counter = 0;
- int cost = 0;
- while (hotels) {
- hotels->hotel.id++;
- if (hotels->hotel.hotel_class == hotel_class) {
- cost += 7 * hotels->hotel.day_price + hotels->hotel.transfer_price;
- counter++;
- }
- hotels = hotels->next;
- }
- cost /= counter;
- printf("Average cost for the %d class is %d", hotel_class, cost);
- system("pause");
- }
- void print_menu() {
- system("CLS");
- puts("1. Print hotels");
- puts("2. Add hotel");
- puts("3. Edit hotel");
- puts("4. Delete hotel");
- puts("5. Insert hotel");
- puts("6. Find cheapest");
- puts("7. Calculate trip's costs");
- puts("0. Exit");
- puts("Type number to proceed:");
- }
- hotel_list delete_entry(hotel_list hotels) {
- int n, j = 1;
- char yes;
- hotel_list temp, temp1;
- system("CLS");
- if (hotels == NULL) {
- puts("List is empty");
- system("pause");
- return NULL;
- }
- puts("Number record for delete?");
- scanf("%d", &n);
- if (n < 1) {
- puts("Error");
- system("pause");
- return hotels;
- }
- if (n == 1) {
- print_entry(hotels);
- puts("Delete? (y/n)");
- do
- yes = getchar();
- while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
- if (yes == 'y' || yes == 'Y') {
- temp = hotels->next;
- free(hotels);
- return temp;
- } else return hotels;
- }
- if (hotels->next == NULL && n > 1) {
- puts("Error");
- system("pause");
- return hotels;
- }
- temp = hotels;
- temp1 = temp->next;
- while (temp1->next && j < n - 1) {
- temp = temp1;
- temp1 = temp->next;
- j++;
- }
- if (j < n - 1) {
- puts("Error");
- system("pause");
- return hotels;
- }
- print_entry(temp1);
- puts("Delete? (y/n)");
- do
- yes = getchar();
- while (yes != 'y' && yes != 'Y' && yes != 'n' && yes != 'N');
- if (yes == 'y' || yes == 'Y') {
- temp->next = temp1->next;
- free(temp1);
- }
- return hotels;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement