Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created September 15, 2022 15:58
Show Gist options
  • Save untodesu/327c19946e7a7e1f573e777a5e919717 to your computer and use it in GitHub Desktop.
Save untodesu/327c19946e7a7e1f573e777a5e919717 to your computer and use it in GitHub Desktop.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book_s {
char author[128];
char name[128];
unsigned int year;
unsigned int value;
struct book_s *next;
};
int main(void)
{
printf("Lab. work #4. Task 1: Structures!\n");
printf("Student: Kirill Pomelov, RI-100007\n\n");
unsigned int count;
printf("count = ");
scanf("%u", &count);
struct book_s *books = (struct book_s *)calloc(count, sizeof(struct book_s));
for(unsigned int i = 0; i < count; i++) {
unsigned int ip = i + 1;
printf("Book #%u's author: ", ip);
fflush(stdin);
fgets(books[i].author, 128, stdin);
books[i].author[strcspn(books[i].author, "\n")] = 0;
printf("Book #%u's name: ", ip);
fflush(stdin);
fgets(books[i].name, 128, stdin);
books[i].name[strcspn(books[i].name, "\n")] = 0;
printf("Book #%u's year: ", ip); scanf("%u", &books[i].year);
printf("Book #%u's value: ", ip); scanf("%u", &books[i].value);
books[i].next = NULL;
}
printf("\n\nSearch books:\n");
char ta[128] = { 0 };
printf("Author: ");
fflush(stdin);
fgets(ta, 128, stdin);
ta[strcspn(ta, "\n")] = 0;
unsigned int year;
printf("Year: ");
scanf("%u", &year);
struct book_s *cur = NULL;
struct book_s *book = &books[0];
for(unsigned int i = 0; i < count; i++) {
if(strcmp(book->author, ta) == 0 && book->year <= year) {
if(!cur) cur = book;
else {
struct book_s *end = cur;
while(end->next) end++;
end->next = book;
}
}
book++;
}
while(cur) {
printf("Book: \"%s\" by %s (%u)\n", cur->name, cur->author, cur->year);
cur = cur->next;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment