Загрузка данных


#include <stdio.h>
#include <string.h>
#include <locale.h>

#define MAX 100
#define NAME_LEN 64

typedef struct {
    char name[NAME_LEN];      /* наименование государства */
    char capital[NAME_LEN];   /* столица */
    long population;          /* численность населения */
    double area;              /* площадь, км² */
    char president[NAME_LEN]; /* фамилия президента */
} Country;

/* ---------- прототипы ---------- */
void inputCountries(Country arr[], int *n);
void printCountry(const Country *c);
void printAll(const Country arr[], int n);
void searchByName(const Country arr[], int n);
void searchByArea(const Country arr[], int n);
int menu(void);

/* ---------- main ---------- */
int main(void) {
    setlocale(LC_ALL, "Russian");

    Country countries[MAX];
    int n = 0;
    int choice;

    do {
        choice = menu();
        switch (choice) {
            case 1: inputCountries(countries, &n); break;
            case 2: printAll(countries, n);        break;
            case 3: searchByName(countries, n);    break;
            case 4: searchByArea(countries, n);    break;
            case 0: printf("Выход.\n");            break;
            default: printf("Неверный пункт меню.\n");
        }
    } while (choice != 0);

    return 0;
}

/* ---------- меню ---------- */
int menu(void) {
    int ch;
    printf("\n===== МЕНЮ =====\n");
    printf("1. Ввод государств с клавиатуры\n");
    printf("2. Вывод всех государств\n");
    printf("3. Поиск по названию\n");
    printf("4. Поиск по площади (больше заданной)\n");
    printf("0. Выход\n");
    printf("Выбор: ");
    if (scanf("%d", &ch) != 1) { ch = -1; while(getchar()!='\n'); }
    return ch;
}

/* ---------- ввод ---------- */
void inputCountries(Country arr[], int *n) {
    int cnt;
    printf("Сколько государств добавить (свободно %d): ", MAX - *n);
    if (scanf("%d", &cnt) != 1 || cnt <= 0) {
        printf("Некорректное количество.\n");
        while(getchar()!='\n');
        return;
    }
    if (*n + cnt > MAX) {
        printf("Превышен лимит массива (макс. %d).\n", MAX);
        return;
    }
    for (int i = 0; i < cnt; i++) {
        Country *c = &arr[*n + i];
        printf("\n--- Государство #%d ---\n", *n + i + 1);

        printf("Наименование: ");
        scanf(" %63[^\n]", c->name);

        printf("Столица: ");
        scanf(" %63[^\n]", c->capital);

        printf("Численность населения: ");
        scanf("%ld", &c->population);

        printf("Площадь (км²): ");
        scanf("%lf", &c->area);

        printf("Фамилия президента: ");
        scanf(" %63[^\n]", c->president);
    }
    *n += cnt;
    printf("Добавлено %d записей.\n", cnt);
}

/* ---------- вывод одной записи ---------- */
void printCountry(const Country *c) {
    printf("------------------------------------------\n");
    printf("Наименование : %s\n", c->name);
    printf("Столица      : %s\n", c->capital);
    printf("Население    : %ld\n", c->population);
    printf("Площадь      : %.2f км²\n", c->area);
    printf("Президент    : %s\n", c->president);
}

/* ---------- вывод всех ---------- */
void printAll(const Country arr[], int n) {
    if (n == 0) { printf("Список пуст.\n"); return; }
    printf("\n=== Всего государств: %d ===\n", n);
    for (int i = 0; i < n; i++) printCountry(&arr[i]);
}

/* ---------- поиск по названию ---------- */
void searchByName(const Country arr[], int n) {
    if (n == 0) { printf("Список пуст.\n"); return; }
    char query[NAME_LEN];
    printf("Введите название (или часть): ");
    scanf(" %63[^\n]", query);

    int found = 0;
    for (int i = 0; i < n; i++) {
        if (strstr(arr[i].name, query) != NULL) {  /* подстрока, регистрозависимо */
            printCountry(&arr[i]);
            found++;
        }
    }
    if (!found) printf("Ничего не найдено.\n");
    else printf("Найдено записей: %d\n", found);
}

/* ---------- поиск по площади ---------- */
void searchByArea(const Country arr[], int n) {
    if (n == 0) { printf("Список пуст.\n"); return; }
    double limit;
    printf("Введите минимальную площадь: ");
    if (scanf("%lf", &limit) != 1) {
        printf("Некорректное значение.\n");
        while(getchar()!='\n');
        return;
    }

    int found = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i].area > limit) {
            printCountry(&arr[i]);
            found++;
        }
    }
    if (!found) printf("Нет государств с площадью больше %.2f км².\n", limit);
    else printf("Найдено записей: %d\n", found);
}