zoukankan      html  css  js  c++  java
  • 算法习题---5-8图书管理系统*****<双向迭代器>(UVa230)

    一:题目

    就是输入一系列书本名和作者名,然后输入命令模拟借书和还书,再输出归还的书的摆放位置。要求有两点:
    
    需要对归还的书做特殊排序处理:作者名相同,则书本按书名从小到大排序;否则书本按作者名大小排序
    需要归还的书的位置前面没有书籍时,需要特殊处理

    (一)样例输入

    "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    //结束

    (二)样例输出

    Put "The C Programming Language" after "The Canterbury Tales"
    Put "Algorithms" after "The C Programming Language"
    END

    (三)更多测试样例:推荐https://www.jianshu.com/p/902295e378a5

    二:代码实现

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <map>  //重点:map,set支持双向迭代
    #include <vector>
    #include <string>
    #include <iterator>
    
    using namespace std;
    
    map<string, map<string,int> >Books;    //是支持双向迭代的,用于存放<作者,<书名,借阅状态>>
    map<string, string>Title_Author;    //用于映射<书名,作者>
    vector<string> Books_Title;            //存放所有要归还的书名
    
    int main()
    {
        freopen("data5_8_h.in", "r", stdin);
        freopen("data5_8_h.out", "w", stdout);
    
        string book_t, book_a,all_info;
        //获取图书信息
        while (getline(cin, all_info)&&all_info!="END")
        {
            int start, end;
            start = all_info.find('"');
            end = all_info.find('"', start + 1);
            book_t = all_info.substr(start + 1, end - start -1);
            start = all_info.find("by ");
            book_a = all_info.substr(start + 3);
            
            if (!Books.count(book_a))
                Books[book_a] = map<string, int>();
            (Books[book_a])[book_t] = 1;
            Title_Author[book_t] = book_a;
        }
        
        //进行图书借阅
        while (cin >> all_info&&all_info != "END")
        {
            if (all_info[0] == 'B')    //借书
            {
                getline(cin, book_t);
                book_t = book_t.substr(book_t.find('"') + 1, book_t.rfind('"') - book_t.find('"')-1);
                book_a = Title_Author[book_t];
                
                Books[book_a][book_t] = -1;
            }
            else if (all_info[0] == 'R')    //还书
            {
                getline(cin, book_t);
                book_t = book_t.substr(book_t.find('"') + 1, book_t.rfind('"') - book_t.find('"')-1);
                Books_Title.push_back(book_t);
            }
            else if (all_info[0] == 'S')    //入库
            {
                for (int i = Books_Title.size() - 1; i >= 0; i--)
                {
                    book_t = Books_Title[i];
                    Books_Title.pop_back();
                    book_a = Title_Author[book_t];
                    //在当前自己作者下找
                    map<string, map<string, int> >::iterator a_rit = Books.find(book_a);
                    for (; a_rit != Books.end(); a_rit--)      //如何对双向迭代器进行结束判断??疑惑---有时间调试一下
                    {
                        //进行书籍操作
                        bool flag = false;
                        map<string, int>::iterator t_rit = a_rit->second.find(book_t);
                        for (; t_rit-- != (a_rit->second).begin();)  //疑惑:若是使用上面迭代的形式;t_rit != (a_rit->second).end();t_rit--则输出数据不一致
                        {
                            if (t_rit->second == 1)
                            {
                                cout << "PUT "" << book_t << "" after "" << t_rit->first << """ << endl;
                                (*(Books[book_a]).find(book_t)).second = 1;
                                flag = true;
                                break;
                            }
                        }
                        if (flag)
                            break;
                    }
                    if (a_rit == Books.end())
                    {
                        (*(Books[book_a]).find(book_t)).second = 1;
                        cout << "PUT "" << book_t << "" first" << endl;
                    }
                }
                cout << "END" << endl;
            }
        }
    
        freopen("CON", "r", stdin);
        freopen("CON", "w", stdout);
        return 0;
    }
  • 相关阅读:
    《TomCat与Java Web开发技术详解》(第二版) 第六章节的学习总结 ---- JSP技术
    《Lucene in Action 第二版》第三章节的学习总结----IndexSearcher以及Term和QueryParser
    《TomCat与Java Web开发技术详解》(第二版) 第六章节对应CD附带的helloapp无法启动的问题解决
    《TomCat与Java Web开发技术详解》(第二版) 第五章节的学习总结 ---- Servlet的高级用法
    《Lucene in Action》(第二版) 第二章节的学习总结 ---- IndexWriter+Document+Field
    《Lucene in Action》(第二版) 第一章节的学习总结 ---- 用最少的代码创建索引和搜索
    Windows Server 2008 R2 配置AD(Active Directory)域控制器
    Windows Server 2008 R2 负载平衡入门篇
    Asp.net MVC 的八个扩展点
    C# 使用memcache(memcache安装)
  • 原文地址:https://www.cnblogs.com/ssyfj/p/11556075.html
Copyright © 2011-2022 走看看