0

私はこの構造体の配列を持っています

struct BookInfo
{
    char title[50];
    int numAuthors;
    char authors[50][50];
    int year;
    int checkedout;
};

struct BookInfo library[500];

そして、ユーザーが単語を入力できる検索を実装する方法を理解しようとしています。関数は、その本の名前全体を文字タイトルに出力します。たとえば、2冊の本が

a rose for emily
war of the worlds

ユーザーがワールドを入力すると、関数は出力します

war of the worlds

char データ型ではなく int を使用した線形検索を見た場合、これについてどのように対処しますか

4

2 に答える 2

0

コードは次のとおりです。キーワードをリスト内のタイトルと一致させるために使用strstr()します。コードをテストする便宜上、titleフィールドのみを入力BookInfoし、アイテムの数を 5 に制限しましlibraryた。

#include<stdio.h>
#include<string.h>

struct BookInfo
{
    char title[50];
};

struct BookInfo library[5];

int main()
{
    int i,j=0;
    char keyword[50];

//Getting the titles of books from librarian

    for(i=0; i<5; i++)
    {
        printf("Enter book no.%d ",i+1);
        gets(library[i].title);
    }
 // Search based on keyword

    printf("Enter the title keyword to search\n");
    gets(keyword);
    for(i=0; i<5; i++)
    {
        if(strstr(library[i].title,keyword)==NULL)
            j++;

        else
            printf("Book to have the  word %s in title is %s\n",keyword,library[i].title);

    }
    if(j==0)
        printf("No book with given title/keyword");
}

出力

Enter book no.1 The importance of religion

Enter book no.2 Why sex sells

Enter book no.3 Origin of species

Enter book no.4 Are you obsessed with sex?

Enter book no.5 Why are programmers sexy

Enter the title keyword to search sex

Book to have the  word sex in title is Why sex sells

Book to have the  word sex in title is Are you obsessed with sex?

Book to have the  word sex in title is Why are programmers sexy
于 2013-05-07T06:26:12.463 に答える