1022. Digital Library (30)

来源:互联网 发布:电信软件市场 编辑:程序博客网 时间:2024/04/30 16:42
https://www.patest.cn/contests/pat-a-practise/1022
//注意scanf("%s"),字符串读取不能有空格//注意一个字符串是以"\0"结束的,他的int值是0#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;struct Book{    int ID;    char title[200];    char author[200];    char words[200];    char publisher[200];    int year;} book[10005];int num[10005];//存储search时的书籍的IDint main(){    int n,m,count,id,j,k;    char search[200];    while (cin >> n)    {        for (int i = 0; i < n; i++)        {            scanf("%d",&book[i].ID);            getchar();            //用gets()读取的字符串可以有空格            gets(book[i].title);            gets(book[i].author);            gets(book[i].words);            gets(book[i].publisher);            scanf("%d",&book[i].year);        }        memset(num,0,sizeof(num));        scanf("%d",&m);        getchar();        for (int i = 0; i < m; i++)        {            count = 0;            gets(search);            id = search[0] - '0';            for (j = 3,k = 0; j < strlen(search); k++,j++)            {                search[k] = search[j];            }            search[k] = 0;//结束字符串,剔除后面三个            //接下来将每个的user search 和the information of books ,compare            if (id == 1)            {                for (int l = 0; l < n; l++)                {                    if (strcmp(book[l].title,search) == 0)                    {                        num[count++] = book[l].ID;                    }                }            }            else if (id == 2)            {                for (int l = 0; l < n; l++)                {                    if (strcmp(book[l].author,search) == 0)                    {                        num[count++] = book[l].ID;                    }                }            }            else if (id == 4)            {                for (int l = 0; l < n; l++)                {                    if (strcmp(book[l].publisher,search) == 0)                    {                        num[count++] = book[l].ID;                    }                }            }            else if (id == 3)  //书籍的keyword中有要找的keywords //最关键的一部分            {                char str[15] = {0};                for (int l = 0; l < n; l++)                {                    j = 0;                    while (book[l].words[j] != 0)                    {                        //读取book[l].word的每个字符段                        for (k = 0; book[l].words[j] != ' ' && book[l].words[j] != 0; j++)                        {                            str[k++] = book[l].words[j];                        }                        str[k] = 0;                        if (strcmp(search,str) == 0)                        {                            num[count++] = book[l].ID;                            break;                        }                        //如果已经到达了字符串的末尾                        if (book[l].words[j] == 0)break;                        else j++;//跳过一个空格                    }                }            }            else if(id == 5)            {                int temp = 0;                for (int l = 0; search[l] != 0; l++)                {                    temp  = temp*10 + (search[l] - '0');                }                for (int l = 0; l < n; l++)                {                    if (temp == book[l].year)                    {                        num[count++] = book[l].ID;                    }                }            }            printf("%d: %s\n",id,search);            if (count == 0)            {                printf("Not Found\n");            }            else            {                sort(num,num+count);                for (int l = 0; l < count; l++)                {                    printf("%07d\n",num[l]);                }            }        }    }    return 0;}

://www.patest.cn/contests/pat-a-practise/1022
0 0