/
Roman-git
/
Eltex_tasks
Обзор
Документация
Войти
/
Roman-git
/
Eltex_tasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
task_5/task_5_1/menu.c
170 строк
4 KB
Roman_git
task_5
04 дек 2024, 11:40
04 дек 2024, 11:40
d3c5c10
Код
Авторство
О чём код?
#include "menu.h" #include <stdio.h> #include <stdlib.h> #include <string.h> void Read_file_phonebook() { PB = malloc((n+1)*sizeof(struct phonebook)); file = fopen("phonebook.txt", "r"); if (file == NULL) { printf("\nPhonebook is not found\n"); system("touch phonebook.txt"); file = fopen("phonebook.txt", "r"); printf("Phonebook is created and open\n"); } else printf("\nPhonebook is open\n"); do { n++; PB = realloc(PB, (n+1)*sizeof(struct phonebook)); }while(fscanf(file, "%s%s%s", PB[n].name, PB[n].second_name, PB[n].tel) != EOF); fclose(file); strcpy(PB->name, "Name"); strcpy(PB->second_name, "Second_name"); strcpy(PB->tel ,"Tel"); } ////////////////////////////////////////////////////////////////////////////////////////// void print_menu() { printf("\n1. Add the subscriber \n2. Remove the subscriber "); printf("\n3. Search of the subscriber by name \n4. Display all records \n5. Save \n6. Exit\n\n"); } ////////////////////////////////////////////////////////////////////////////////////////// int input_menu() { int temp; printf("Enter point menu: "); scanf("%d", &temp); return temp; } ////////////////////////////////////////////////////////////////////////////////////////// void Add_sub() { if (n > 100) { printf("\nThe directory is full\n"); } else { printf("\nAdd the subscriber: \n"); n++; PB = realloc(PB, (n)*sizeof(struct phonebook)); printf("\nName: "); scanf("%s", PB[n-1].name); printf("Second_name: "); scanf("%s", PB[n-1].second_name); printf("Tel: "); scanf("%s", PB[n-1].tel); } } ////////////////////////////////////////////////////////////////////////////////////////// void Remove_sub() { //PB = realloc(PB, (n+1)*sizeof(struct phonebook)); int count_name_remove = 0; char remove_name[10]; printf("\nName remove: "); scanf("%s", remove_name); for (int i=1; i < n; i++) { if (strcmp (PB[i].name, remove_name)==0) { strcpy(temp_PB.name, PB[i].name); strcpy(temp_PB.second_name,PB[i].second_name); strcpy(temp_PB.tel , PB[i].tel); strcpy(PB[i].name, PB[i+1].name); strcpy(PB[i].second_name,PB[i+1].second_name); strcpy(PB[i].tel , PB[i+1].tel); strcpy(PB[i+1].name, temp_PB.name); strcpy(PB[i+1].second_name,temp_PB.second_name); strcpy(PB[i+1].tel , temp_PB.tel); count_name_remove++; } } if (count_name_remove == 0) { printf("\nSubscriber is absent\n"); } else { //memset(PB[n].name, 0, 10); //memset(PB[n].second_name, 0, 10); //memset(PB[n].tel, 0, 10); n--; PB = realloc(PB, (n)*sizeof(struct phonebook)); } } ////////////////////////////////////////////////////////////////////////////////////////// void Display_sub() { printf("\nAll subscribers:\n\n"); for (int i=0; i < n; i++) { printf("%s %s %s\r\n", PB[i].name, PB[i].second_name, PB[i].tel); } } ////////////////////////////////////////////////////////////////////////////////////////// void Search_sub() { printf("\nSearch of the subscriber by name\n"); int count_name_search = 0; char search_name[10]; int count = 0; scanf("%s", search_name); for (int i=0; i <= n; i++) { if (strcmp (PB[i].name, search_name)==0) {count++; printf("%s %s %s\n", PB[i].name, PB[i].second_name, PB[i].tel); } else count_name_search = count_name_search + count; } if (count_name_search == 0) printf("\nSubscriber is absent\n"); } ////////////////////////////////////////////////////////////////////////////////////////// void Save_file_phonebook() { printf("\nSave phonebook\n"); file = fopen("phonebook.txt", "w"); for (int i=1; i < n; i++) { fprintf(file, "%s %s %s\n", PB[i].name, PB[i].second_name, PB[i].tel); } fclose(file); } //////////////////////////////////////////////////////////////////////////////////////////