Last active
May 11, 2019 11:02
-
-
Save Adefful/0048d603b8b3cee5921a3ec88d6fce5e to your computer and use it in GitHub Desktop.
Lab 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Проект: Библиотека (книга) | |
* | |
* | |
TODO: Создайте структуру book со следующими полями: | |
Назначение поля | |
фамилия автора | |
имя автора | |
название книги | |
издательство | |
год издания | |
Создать массив из n книг (ввод с клавиатуры) | |
Реализовать функции : | |
• добавить новую книгу; | |
• распечатать информацию о книге в табличном виде; | |
• найти все книги заданного автора, результат вывести на экран; | |
• найти всех авторов заданного издательства, результат | |
отсортировать по алфавиту, запомнить в массиве и вывести на экран; | |
*/ | |
#include <stdio.h> | |
#include <malloc.h> | |
#include <iostream> | |
#include <stdlib.h> | |
#include <string.h> | |
using namespace std; | |
struct Book //структура | |
{ | |
char* last_name; | |
char* first_name; | |
char* book_name; | |
char* publish; | |
int year; | |
}; | |
//функция добавляющая экземпляр структуры в динамический массив из структур. Аргумент: ссылка на массив структур | |
void add_book(struct Book* books); | |
void show_book(struct Book* books, int i); | |
void find_book(struct Book* books); | |
bool compare_str(char* text, char* str); | |
void get_author(struct Book* books); | |
void read_line(char* str); | |
void clear_str(char* str, int amount); | |
int amount =0; // количество книг | |
int main() | |
{ | |
//arrary of books | |
struct Book *books = (struct Book*)calloc(1,sizeof(struct Book)); | |
add_book(books); | |
while (true) | |
{ | |
printf("Enter the number\n[1] Add the book\n[2] Show books\n[3] Find the book\n[4] Get authors"); | |
int c=0; | |
scanf("%d", &c); getchar(); | |
switch (c){ | |
case 1: { books = (struct Book*) realloc(books,(amount +1 )* sizeof(struct Book)); //Добавление 1 элемнта массива структур | |
add_book(books); break; } | |
case 2: { | |
int i=0; | |
printf("Enter index(0..%d) of the book:",amount); | |
scanf("%d",&i); getchar(); | |
show_book(books,i); | |
break; | |
} | |
case 3: find_book(books); break; | |
case 4: get_author(books); break; | |
} | |
} | |
return 0; | |
} | |
void add_book(struct Book* books) | |
{ | |
(books+amount)->last_name = (char*)malloc(1*sizeof(char)); | |
(books+amount)->first_name = (char*)malloc(1*sizeof(char)); | |
(books+amount)->book_name = (char*)malloc(1*sizeof(char)); | |
(books+amount)->publish = (char*)malloc(1*sizeof(char)); | |
printf("Enter last name:\n"); | |
read_line((books+amount)->last_name); | |
printf("Enter first name:\n"); | |
read_line((books+amount)->first_name); | |
printf("Enter book name:\n"); | |
read_line((books+amount)->book_name); | |
printf("Enter public:\n"); | |
read_line((books+amount)->publish); | |
printf("Enter year:\n"); | |
scanf("%d", &(books+amount)->year); | |
show_book(books,amount); | |
amount++; | |
} | |
void clear_str(char* str, int amount) | |
{ | |
for (int i=0; i < amount; i++) | |
{ | |
// *(str +i) = NULL; | |
} | |
} | |
void show_book(struct Book* books, int i) | |
{ | |
cout << "__________________________"; | |
cout << "\nLast name: \t|"; | |
// cout << (books + i)->last_name[0] << endl; | |
printf((books + i)->last_name); | |
cout << "__________________________"; | |
cout << "\nFirst name: \t|"; | |
printf((books + i)->first_name); | |
cout << "__________________________"; | |
cout << "\nBook name: \t|"; | |
printf((books + i)->book_name); | |
cout << "__________________________"; | |
cout << "\nPublish: \t|"; | |
printf((books + i)->publish); | |
cout << "__________________________"; | |
cout << "\nYear: \t\t|"; | |
printf("%d\n",(*(books+i)).year); | |
cout << "__________________________\n"; | |
} | |
void find_book(struct Book* books) | |
{ | |
printf("Введите имя или фамилию автора: "); | |
char* str = (char*)malloc(1*sizeof(char)); | |
read_line(str); | |
for (int i=0; i<amount;i++) | |
{ | |
if(compare_str((books + i)->first_name, str)) | |
{printf("%d Найдена Книга: ",i); | |
printf((books + i)->book_name); | |
cout << endl;} | |
if(compare_str((books + i)->last_name, str)) | |
{printf("%d Найдена Книга: ",i); | |
printf((books + i)->book_name); | |
cout << endl;} | |
} | |
} | |
bool compare_str(char* text, char* str) | |
{ | |
int c=0; | |
for(int j=0;;j++) | |
{ | |
if (*(text + j) != '\n' ) | |
{ | |
if (*(str+j) == *(text + j)) | |
c++; | |
} | |
else | |
{ | |
if (c==(j) && c!=0) | |
{ | |
return true; | |
} | |
break; | |
} | |
} | |
return false; | |
} | |
void get_author(struct Book* books) | |
{ | |
char** array = (char** )malloc(sizeof(char*)); | |
printf("Введите Издание автора: "); | |
char* str = (char*)malloc(1*sizeof(char)); | |
read_line(str); | |
int amount_authors=0; | |
for (int i=0; i<amount;i++) | |
{ | |
if(compare_str((books + i)->publish, str)) | |
{ | |
*(array +i) = (books + i)->first_name; | |
amount_authors++; | |
//printf(*(array +i)); | |
} | |
} | |
int flag=1; | |
while (flag!=0) | |
{ | |
flag=0; | |
for(int a=0; a<amount_authors-1; a++){ | |
if (strcmp(*(array +a), *(array +a +1))>0) | |
{ | |
char * tmp=*(array +a); | |
*(array +a) = *(array +a + 1); | |
*(array +a +1)=tmp; | |
flag++; | |
}} | |
} | |
for (int i=0; i<amount_authors;i++) | |
{ | |
printf(*(array + i)); | |
} | |
} | |
void read_line(char* str) | |
{ char c; | |
for(int i=0;;i++) | |
{ | |
str = (char*)realloc(str,(i+1)*sizeof(char)); | |
scanf("%c", &c); | |
if (c == '\n') {*(str + i) = c; break;} | |
*(str + i) = c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment