20131106【C言語】構造体を構造体でネストする

お題

構造体を構造体でネストする

 

プログラム概要

簡易図書管理データベース

 

ソース

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

#define MAX 100

 

int menu(void);

void display(int i);

void author_search(void);

void title_search(void);

void enter(void);

void save(void);

void load(void);

 

struct book_type {

unsigned date; /* Public date */

unsigned char ed; /* Edition */

unsigned pages; /* Page count */

};

 

struct catalog {

char name[80]; /* Author */

char title[80]; /* Book name */

char pub[80]; /* Publisher */

struct book_type book; /* Data */

} cat[MAX];

 

int top = 0; /* Final pointer */

 

int main(void)

{

int choice;

 

do {

choice = menu();

switch (choice) {

case 1:

enter(); /* Enter book name */

break;

case 2:

author_search(); /* Search by author */

break;

case 3:

title_search(); /* Search by book name */

break;

case 4:

save();

break;

case 5:

load();

}

} while (choice != 6);

 

return 0;

}

 

/* Return menu item */

menu(void)

{

int i;

char str[80];

 

printf("Book catalog:\n");

printf(" 1. Enter\n");

printf(" 2. Search by author\n");

printf(" 3. Search by book name\n");

printf(" 4. Save catalog\n");

printf(" 5. Load catalog\n");

printf(" 6. Exit\n");

 

do {

printf("Please enter item: ");

gets(str);

i = atoi(str);

printf("\n");

} while*1;

 

return i;

}

 

/* Enter book name to data base */

void enter(void)

{

int i;

char temp[80];

 

for (i = top; i < MAX; i++) {

printf("Please enter author(Please input enter to exit) : ");

gets(cat[i].name);

if(!*cat[i].name)

break;

printf("Please input book name: ");

gets(cat[i].title);

printf("Please input publisher: ");

gets(cat[i].pub);

printf("Please input publish date: ");

gets(temp);

cat[i].book.date = (unsigned)atoi(temp);

printf("Please input edition: ");

gets(temp);

cat[i].book.ed = (unsigned char)atoi(temp);

printf("Please input page count: ");

gets(temp);

cat[i].book.pages = (unsigned)atoi(temp);

}

top = i;

}

 

/* Search by author */

void author_search(void)

{

char name[80];

int i, found;

 

printf("Author: ");

gets(name);

 

found = 0;

for (i = 0; i < top; i++)

if (!strcmp(name, cat[i].name)) {

display(i);

found = 1;

printf("\n");

}

if (!found)

printf("Could not be found.\n");

}

 

/* Search by book name */

void title_search(void)

{

char title[80];

int i, found;

 

printf("Book name: ");

gets(title);

 

found = 0;

for (i = 0; i < top; i++)

if (!strcmp(title, cat[i].title)) {

display(i);

found = 1;

printf("\n");

}

if (!found)

printf("Could not be found.\n");

}

 

/* Display catalog */

void display(int i)

{

printf("%s\n", cat[i].title);

printf("Author: %s\n", cat[i].name);

printf("Publisher: %s\n", cat[i].pub);

printf("Copy right: %u, Edition: %u\n",

cat[i].book.date, cat[i].book.ed);

printf("Page count: %u\n", cat[i].book.pages);

}

 

/* Load catalog */

void load(void)

{

FILE *fp;

 

if *2 == NULL) {

printf("Could not find catalog.\n");

return;

}

 

if (fread(&top, sizeof top, 1, fp) != 1) { /* Read count */

printf("Count read error.\n");

exit(1);

}

 

if (fread(cat, sizeof cat, 1, fp) != 1) { /* Read data */

printf("Catalog read error.\n");

exit(1);

}

 

fclose(fp);

}

 

/* Save catalog file */

void save(void)

{

FILE *fp;

 

if *3 == NULL) {

printf("Cannot open file.\n");

exit(1);

}

 

if (fwrite(&top, sizeof top, 1, fp) != 1) { /* Write count */

printf("Count write error.\n");

exit(1);

}

if (fwrite(cat, sizeof cat, 1, fp) != 1) { /* Write data */

printf("Catalog write error.\n");

exit(1);

}

fclose(fp);

}

 

 

実行結果

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 1

 

Please enter author(Please input enter to exit) : author1

Please input book name: book1

Please input publisher: hoge1

Please input publish date: 20130101

Please input edition: 1

Please input page count: 100

Please enter author(Please input enter to exit) : author2

Please input book name: book2

Please input publisher: hoge2

Please input publish date: 20140101

Please input edition: 2

Please input page count: 200

Please enter author(Please input enter to exit) : 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 4

 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 5

 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 2

 

Author: author1

book1

Author: author1

Publisher: hoge1

Copy right: 20130101, Edition: 1

Page count: 100

 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 3

 

Book name: book1

book1

Author: author1

Publisher: hoge1

Copy right: 20130101, Edition: 1

Page count: 100

 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 3

 

Book name: book2

book2

Author: author2

Publisher: hoge2

Copy right: 20140101, Edition: 2

Page count: 200

 

Book catalog:

 1. Enter

 2. Search by author

 3. Search by book name

 4. Save catalog

 5. Load catalog

 6. Exit

Please enter item: 6

 

*1: i < 1) || (i > 6

*2:fp = fopen("catalog", "rb"

*3:fp = fopen("catalog", "wb"