Quick actions

cmd+k|ctrl+k

Navigation

Languages

RR

Snippet info

Language

C

Visibility

public

Author

t.m.kozynets

Created

2025-05-22T10:32:26.320042Z

Updated

2025-05-22T10:32:26.320042Z

/**
* file rr_kozynets.c
* author Козинець Т.М. гр. 515, варіант 4
* date 22.05.2025
* brief Розрахункова робота
*
* База даних перельотів
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Flight {
    char departure[50];
    char destination[50];
    int flightNumber;
    char planeType[30];
    int seatCount;
    struct Flight *next;
} Flight;

Flight* loadFlights(const char *filename) {
    FILE *file = fopen(filename, "rb");
    if (!file) return NULL;

    Flight *head = NULL, *last = NULL;
    Flight temp;

    while (fread(&temp, sizeof(Flight) - sizeof(Flight*), 1, file)) {
        Flight *newNode = (Flight*)malloc(sizeof(Flight));
        memcpy(newNode, &temp, sizeof(Flight) - sizeof(Flight*));
        newNode->next = NULL;

        if (!head)
            head = newNode;
        else
            last->next = newNode;

        last = newNode;
    }

    fclose(file);
    return head;
}

void saveFlights(const char *filename, Flight *head) {
    FILE *file = fopen(filename, "wb");
    if (!file) return;

    while (head) {
        fwrite(head, sizeof(Flight) - sizeof(Flight*), 1, file);
        head = head->next;
    }

    fclose(file);
}

void addFlight(Flight **head) {
    Flight *newNode = (Flight*)malloc(sizeof(Flight));
    printf("Enter departure: ");
    scanf("%s", newNode->departure);
    printf("Enter destination: ");
    scanf("%s", newNode->destination);
    printf("Enter flight number: ");
    scanf("%d", &newNode->flightNumber);
    printf("Enter plane type: ");
    scanf("%s", newNode->planeType);
    printf("Enter seat count: ");
    scanf("%d", &newNode->seatCount);

    newNode->next = *head;
    *head = newNode;
}

void deleteFlight(Flight **head, int flightNumber) {
    Flight *curr = *head, *prev = NULL;

    while (curr && curr->flightNumber != flightNumber) {
        prev = curr;
        curr = curr->next;
    }

    if (!curr) {
        printf("Flight not found.\n");
        return;
    }

    if (!prev)
        *head = curr->next;
    else
        prev->next = curr->next;

    free(curr);
}

void editFlight(Flight *head, int flightNumber) {
    while (head && head->flightNumber != flightNumber)
        head = head->next;

    if (!head) {
        printf("Flight not found.\n");
        return;
    }

    printf("New departure: ");
    scanf("%s", head->departure);
    printf("New destination: ");
    scanf("%s", head->destination);
    printf("New plane type: ");
    scanf("%s", head->planeType);
    printf("New seat count: ");
    scanf("%d", &head->seatCount);
}

void viewFlights(Flight *head) {
    while (head) {
        printf("Flight %d: %s -> %s, Plane: %s, Seats: %d\n",
               head->flightNumber, head->departure, head->destination,
               head->planeType, head->seatCount);
        head = head->next;
    }
}

void findByDestination(Flight *head, const char *dest) {
    while (head) {
        if (strcmp(head->destination, dest) == 0)
            printf("Flight %d, Plane: %s\n", head->flightNumber, head->planeType);
        head = head->next;
    }
}

void findByPlaneType(Flight *head, const char *type) {
    while (head) {
        if (strcmp(head->planeType, type) == 0)
            printf("Flight %d: %s -> %s, Seats: %d\n", head->flightNumber,
                   head->departure, head->destination, head->seatCount);
        head = head->next;
    }
}

void freeList(Flight *head) {
    while (head) {
        Flight *tmp = head;
        head = head->next;
        free(tmp);
    }
}

int checkLicenseKey(const char *key) {
    return strncmp(key, "01AF", 4) == 0;
}

void loadConfig(char *filename, char *key) {
    FILE *conf = fopen("config.txt", "r");
    if (!conf) {
        printf("config.txt not found!\n");
        exit(1);
    }

    fscanf(conf, "DB = %s\nKEY = %s", filename, key);
    fclose(conf);
}

int main() {
    char filename[100], key[20];
    loadConfig(filename, key);

    if (!checkLicenseKey(key)) {
        printf("Invalid license key!\n");
        return 1;
    }

    Flight *head = loadFlights(filename);
    int choice;

    while (1) {
        printf("\nMenu:\n1. Add\n2. Delete\n3. Edit\n4. View all\n5. Find by destination\n6. Find by plane type\n7. Exit\n");
        scanf("%d", &choice);

        if (choice == 1)
            addFlight(&head);
        else if (choice == 2) {
            int num;
            printf("Flight number: ");
            scanf("%d", &num);
            deleteFlight(&head, num);
        }
        else if (choice == 3) {
            int num;
            printf("Flight number: ");
            scanf("%d", &num);
            editFlight(head, num);
        }
        else if (choice == 4)
            viewFlights(head);
        else if (choice == 5) {
            char dest[50];
            printf("Destination: ");
            scanf("%s", dest);
            findByDestination(head, dest);
        }
        else if (choice == 6) {
            char type[30];
            printf("Plane type: ");
            scanf("%s", type);
            findByPlaneType(head, type);
        }
        else
            break;
    }

    saveFlights(filename, head);
    freeList(head);
    return 0;
}
INFO