图书管理系统

I mean your borrowers of books — those mutilators of collections, spoilers of the symmetry
of shelves, and creators of odd volumes.
– (Charles Lamb, Essays of Elia (1823) ‘The Two Races of Men’)
Like Mr. Lamb, librarians have their problems with borrowers too. People don’t put books back where
they should. Instead, returned books are kept at the main desk until a librarian is free to replace them
in the right places on the shelves. Even for librarians, putting the right book in the right place can
be very time-consuming. But since many libraries are now computerized, you can write a program to
help.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically,
the librarians will ask your program for a list of books that have been returned so the books can be
returned to their correct places on the shelves. Before they are returned to the shelves, the returned
books are sorted by author and then title using the ASCII collating sequence. Your program should
output the list of returned books in the same order as they should appear on the shelves. For each
book, your program should tell the librarian which book (including those previously shelved) is already
on the shelf before which the returned book should go.
Input
First, the stock of the library will be listed, one book per line, in no particular order. Initially, they are
all on the shelves. No two books have the same title. The format of each line will be:
title" by author
The end of the stock listing will be marked by a line containing only the word:
END
Following the stock list will be a series of records of books borrowed and returned, and requests
from librarians for assistance in restocking the shelves. Each record will appear on a single line, in one
of the following formats:
BORROW title
RETURN title
SHELVE
The list will be terminated by a line containing only the word:
END
Output
Each time the SHELVE command appears, your program should output a series of instructions for the
librarian, one per line, in the format:
Put title1 after title2
or, for the special case of the book being the first in the collection:
Put title first
After the set of instructions for each SHELVE, output a line containing only the word:
END
Assumptions & Limitations:
1. A title is at most 80 characters long.
2. An author is at most 80 characters long.
3. A title will not contain the double quote (") character.
Sample Input
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"

END

题意  给你一个图书单子,上面写了图书的名字和作者的名字,之后给你一个单子写了借书还书的情况,要你写出程序输出放到柜子上的顺序。

思路:

首先对作者名字排序,如果作者名字相同的话就按作品排序,之后对借出去的书和还回来的书和没借出去的书进行标记。之后模拟就出答案了。有点麻烦,但是思路简单。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    char tital[120];
    char author[120];
    int flag;
} std1[166666];
char s[1000000];
char s3[100000];
int maxn=100;
char temp[100];
bool cmp (node a, node b)
{
    if (strcmp (a.author, b.author))
    {
        if (strcmp (a.author, b.author) < 0)
            return true;

        return false;
    }

    else if (strcmp (a.tital, b.tital))
    {
        if (strcmp (a.tital, b.tital) < 0)
            return true;

        return false;
    }
}
int len;
int find1(char *s2)
{
    int i,j;
    for(i=0; i<len; i++)
    {
        // printf("%s %s\n",s3,std1[i].tital);
        if(!strcmp(s2,std1[i].tital))
        {

            //  printf("%d\n",i);
            return i;
        }
    }
}

int find2(int n)
{
    int i;
    //printf("%d\n",n);
    for(i=n-1; i>=0; i--)
    {
        // printf("%d\n",std1[i].flag);
        if(std1[i].flag==0)
            //printf("%s\n",std1[i].tital);
            return i;
    }
    return -1;
}

int main()
{

    string s1;
    char  input[100000];
    int j,i,k;
    len=0;
    while(gets(s))
    {
        if(!strcmp(s,"END"))
        {
            break;
        }
        int l=strlen(s);
        for(i=0,j=0; s[i]; i++,j++)
        {
            std1[len].tital[i]=s[i];
            if(i&&s[i]=='"')
                break;
        }
        std1[len].tital[j]='"';
        j++;
        std1[len].tital[j]='\0';
        for(j=i+5,k=0; j<l; j++,k++)
        {
            std1[len].author[k]=s[j];
        }
        std1[len].author[k]='\0';
        // printf("%s\n",std1[len].author);
        std1[len].flag=0;
        len++;
    }
    // printf("%d\n",len);
    sort(std1,std1+len,cmp);
   /* for(i=0;i<len;i++)
    {
        printf("%s\n",std1[i].author);
    }*/
while (cin >> input)
	{
		if (!strcmp (input, "END"))
			break;
		if (!strcmp (input, "BORROW"))
		{
			cin.get ();
			cin.getline (temp, maxn);
			int val = find1 (temp);
			std1[val].flag = -1;
		}
		else if (!strcmp (input, "RETURN"))
		{
			cin.get ();
			cin.getline (temp, maxn);
			int val = find1 (temp);
			std1[val].flag = 1;
		}
		else if (!strcmp (input, "SHELVE"))
		{
			int i;
			for (i = 0; i < len; i++)
			{
				if (std1[i].flag == 1)
				{
					int weizhi = find2 (i);
//printf("%d\n",weizhi);
					if (weizhi == -1)
						cout << "Put " << std1[i].tital << " first" << endl;
					else
						cout << "Put " << std1[i].tital << " after " << std1[weizhi].tital << endl;

					std1[i].flag = 0;
				}


			}

			cout << "END" << endl;
		}
	}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值