Примерно так:| Код | #include <stdio.h> #include <string.h>
#define BOOKS_MAX 10
struct Book_struct { char author[20]; char title[20]; unsigned year; }; typedef struct Book_struct Book;
void inputBook(Book *book) { printf("Input book author: "); scanf("%20s", book->author); printf("Input book title: "); scanf("%20s", book->title); printf("Input year: "); scanf("%u", &book->year); }
int showMenu(void) { int result = -1; do { printf("\n1. Input book\n"); printf("2. Print books\n"); printf("3. Find book by author\n"); printf("4. Delete book by year\n"); printf("\n0. Quit\n\n"); printf("Make your selection: "); scanf("%d", &result); }while((result >= 0) && (result < 5)) return result; }
void printBook(const Book *book) { printf("Title: %s\n", book->title); printf("Author: %s\n", book->author); printf("Year: %u\n\n", book->year); }
void findBooksByAuthor(const Book *books, unsigned count) { char author[20]; printf("Input author's name: "); scanf("%20s", author); while(count) { if ( strcmp(books->author, author) == 0) printBook(books); ++books; --count; } }
unsigned deleteBooksByYear(Book *books, unsigned count) { unsigned year; unsigned i; printf("Input year: "); scanf("%u", &year); for(i = 0; i < count; ++i) { if ( books[i].year == year) { if (i + 1 < count) memmove(books + i, books + count - 1, sizeof(*book)); --count; if (--i == 0) break; } } return count; }
int main() { Book books[BOOKS_MAX]; unsigned books_count = 0; unsigned i; while(1) { switch( showMenu() ) { case 0: return 0; /* выход из программы */ case 1: if (books_count < BOOKS_MAX) { inputBook(books + books_count); ++books_count; } else printf("Already entered maximum number of books\n"); break; case 2: for(i = 0; i < books_count; ++i) printBook(book + i); break; case 3: findBooksByAthor(books, books_count); break; case 4: books_count = deleteBooksByYear(books, books_count); break; } } return 0; } |
|