[C++程序设计] STL标准模板类库学习随笔

​ 这段时间主要讲的是一些关于C++中STL类库的介绍和使用方法。虽然之前对于一些简单的STL类库中的方法诸如vector、映射以及迭代器的使用等略微有一些了解,以及上个学期写过一些java的集合类程序,但是在实现一个项目上还是感到有部分的困难。

​ 关于这方面的内容需要完成的作业是写一个简单的图书馆后端管理程序。当时做作业的时候就想着这个任务可能比较难实现,在自主实现过程中也确实出现了许多的问题。

​ 一是有些内容实现起来过于麻烦。比方说日期的加减,原本想到的方法是写一个循环一天一天判断。这个方法确实挺好想,但是放在实际应用上,就比后来加上天数再对日期进行调整的方法效率要低不少。以及关于map在查找中的使用。过去想到的方法是如果要对一本书或者一个读者按名字或者编号查找,需要遍历整个图书或读者向量并逐个与字符串进行比对。如果数据规模较大,机器处理数据的效率就会比较低。但是有了map这一结构,就让这一操作更加高效了,只需在相应的map中查找字符串所对应的向量下标然后再输出向量下标对应的元素就可以了。学会使用程序设计的某一个具体函数是比较容易的,但是在实际操作过程中根据需要实现的功能选用合适的特性使程序运行的效率最大化却不是一件容易的事情。

​ 二是对于庞大项目的心理恐惧。之前没有怎么接触过系统设计,刚接触的时候对于这类代码量比较大的项目确实也比较畏惧。不过这也是一个必须迈过的坎儿,虽然目前而言还有些吃力,但是时间长了应该也就能摸清门道了。系统设计结构比较清晰,很多时候有了明确的思路加上对应的工具实现起来并没有想象中的那么难。之前在写文件读写数据的时候就有些犯难,应该也与对文件读写特性不了解的缘故,当时写了很多行,也没有一个明确的似乎。但是经过了后来的讲解,思路就比较明确了,实现起来也比较地容易。现在的模糊查询等那几个查询方式仍然还是没有一个比较明确的思路,大概也与这个有点关系。

​ 这段时间的作业其实也就马马虎虎,比较仓促,心里还是有一点急躁。收获也是有的,多了解多使用所学过的新东西也是一种不错的体验,不过本应该更好。之前看到清华自动化大一的C++大作业,内心在叹服名校学生学习效率之高的同时,也深感自己的知识水平还远远不够,在学习上也有些许懈怠。接下来应该进行前面知识的整理回顾,打好底子了。

附:本次作业代码及要求

图书管理系统(后台管理员)

数据类:

​ 基础类:读者最大借书量、最长借书时间(按天计算)。这两个都是类成员

​ 日期类:包括年月日,重载输入运算符时,要进行数据的合法性检验;重载输出运算符时,按照“年/月/日”形式输出;重载+运算符;

​ 借阅记录类:包括日期、书号、读者学号、类型(借出/还回/续借)、图书类型(基础/文学休闲/专业);

​ 读者类:学号(常成员)、姓名、专业、班级、已借图书数量、借阅记录向量;

​ 图书类:书号(常成员)、书名、作者、出版社、出版日期、图书类型(基础/文学休闲/专业)、馆藏总量、在馆数量、借阅记录向量;

操作类:

​ 数据成员:图书/学生/借阅记录向量

​ 成员函数:对图书进行文件读写、在图书向量内完成对图书基本信息的增删查改;

​ 对学生进行文件读写、在学生向量内完成对学生基本信息的增删查改;

​ 借阅记录的管理和统计功能后续添加;

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

class Base {
  private:
    static int maxNum;
    static int maxTime;

  public:
    static void setMaxNum(int input) { maxNum = input; }
    static void setMaxTime(int input) { maxTime = input; }
    static int getMaxNum() { return maxNum; }
    static int getMaxTime() { return maxTime; }
    static void display() { cout << maxNum << " " << maxTime << endl; }
};
int Base::maxNum = 10;
int Base::maxTime = 90;
class Date {
  public:
    int m[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    Date() {}
    Date(int year, int mon, int day) {
        this->year = year;
        this->mon = mon;
        this->day = day;
    }
    Date(Date *date) {
        this->year = date->year;
        this->mon = date->mon;
        this->day = date->day;
    }
    Date(const Date &x) {
        this->year = x.year;
        this->mon = x.mon;
        this->day = x.day;
    }
    bool isRun(int year) {
        return ((year % 4 == 0 && year % 100 != 0) ||
                (year % 4 == 0 && year % 400 == 0));
    }
    int getYear() { return year; }
    int getMon() { return mon; }
    int getDay() { return day; }

    friend istream &operator>>(istream &is, Date &x) {
        while (1) {
            is >> x.year >> x.mon >> x.day;
            if (x.year > 2018 && x.year < 2025 && x.mon <= 12 && x.mon > 0 &&
                x.day <= x.m[x.getMon() - 1] && x.day > 0) {
                break;
            }
        }
        return is;
    }
    friend ostream &operator<<(ostream &os, Date &x) {
        os << x.getYear() << "/" << x.getMon() << "/" << x.getDay() << "/";
        return os;
    }
    Date operator+(int x) {
        int mo = 0, da, i;
        if (isRun(year)) {
            m[1] = 29;
        } else
            m[1] = 28;
        da = day + x;
        if (da > m[mon - 1]) {
            i = mon - 1;
            while (da > m[i]) {
                da -= m[i];
                mo++;
                ++i %= 12;
            }
            mon += mo;
            if (mon > 12) {
                year++;
                mon %= 12;
            }
        }
        day = da;
        return *this;
    }

  private:
    int year, mon, day;
};
// class PDate {
//   public:
//     PDate() {
//         year = 2020;
//         mon = 1;
//         day = 1;
//     }
//     PDate(int year, int mon, int day) : year(year), mon(mon), day(day) {}
//     PDate(PDate *date) {
//         this->year = date->year;
//         this->mon = date->mon;
//         this->day = date->day;
//     }
//     PDate(const PDate &date) {
//         this->year = date.year;
//         this->mon = date.mon;
//         this->day = date.day;
//     }
//     friend istream &operator>>(istream &is, PDate &x) {
//         int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//         char r;
//         while (1) {
//             is >> x.year >> r >> x.mon >> r >> x.day;
//             if (x.year >= 2000 && x.year < 2020 && x.mon >= 1 && x.mon <= 12)
//             {
//                 if ((x.year % 4 == 0 && x.year % 100 != 0) ||
//                     (x.year % 4 == 0 && x.year % 400 == 0)) {
//                     if (x.mon == 2) {
//                         if (x.day >= 1 && x.day <= 29)
//                             return is;
//                     } else if (x.day >= 1 && x.day <= a[x.mon])
//                         return is;
//                 } else if (x.day >= 1 && x.day <= a[x.mon])
//                     return is;
//             }
//         }
//         return is;
//     }
//     friend ostream &operator<<(ostream &os, PDate &x) {
//         os << x.year << "/" << x.mon << "/" << x.day << "/";
//         return os;
//     }
//     PDate operator+(int x) {
//         int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//         for (int i = 1; i <= x; ++i) {
//             day++;
//             if ((year % 4 == 0 && year % 100 != 0) ||
//                 (year % 4 == 0 && year % 400 == 0)) {
//                 if (mon == 2 && day > 29) {
//                     day -= 29;
//                     mon++;
//                 }
//             } else if (day > a[mon]) {
//                 day -= a[mon];
//                 mon++;
//             }
//             if (mon > 12) {
//                 mon -= 12;
//                 year++;
//             }
//         }
//         return PDate(year, mon, day);
//     }

//   private:
//     int year, mon, day;
// };
// class BorrowMemo {
//   public:
//     BorrowMemo(Date borrowDate, string stuNum, string ISBN, string type,
//                string bookType) {
//         this->borrowDate = new Date(borrowDate);
//         this->stuNum = stuNum;
//         this->ISBN = ISBN;
//         this->type = type;
//         this->bookType = bookType;
//     }
//     Date *getBorrowDate() { return borrowDate; }
//     string getISBN() { return ISBN; }
//     string getType() { return type; }
//     string getBookType() { return bookType; }
//     friend ostream &operator<<(ostream &os, BorrowMemo bm) {
//         os << *(bm.borrowDate) << bm.stuNum << ""
//            << " " << bm.ISBN << " " << bm.type << " " << bm.bookType;
//         return os;
//     }

//   private:
//     Date *borrowDate;
//     string stuNum;
//     string ISBN;
//     string type;
//     string bookType;
// };
class Stu {
  public:
    Stu() { borrowedCnt = 0; }
    Stu(string stuNum, string name, string major, string grade)
        : stuNum(stuNum), name(name), major(major), grade(grade) {
        borrowedCnt = 0;
    }
    string getStuNum() { return stuNum; }
    string getName() { return name; }
    string getMajor() { return major; }
    string getGrade() { return grade; }
    int getBorrowedCnt() { return borrowedCnt; }
    void setName(string name) { this->name = name; }
    void setMajor(string major) { this->major = major; }
    void setGrade(string grade) { this->grade = grade; }
    void setBorrowedCnt(int borrowedCnt) { this->borrowedCnt = borrowedCnt; }
    Stu operator=(const Stu &Stu) {
        string *tmp = const_cast(&stuNum);
        *tmp = Stu.stuNum;
        this->name = Stu.name;
        this->major = Stu.major;
        this->grade = Stu.grade;
        this->borrowedCnt = Stu.borrowedCnt;
        return *this;
    }
    friend istream &operator>>(istream &is, Stu &stu) {
        string *temp = const_cast(&stu.stuNum);
        string input;
        is >> input;
        *temp = input;
        is >> stu.name >> stu.major >> stu.grade;
        return is;
    }
    friend ostream &operator<<(ostream &os, Stu &stu) {
        os << stu.stuNum << " " << stu.name << " " << stu.major << " "
             << stu.grade;
        return os;
    }

  private:
    const string stuNum;
    string name;
    string major;
    string grade;
    int borrowedCnt;
    // vector bm;
};
class Book {
  public:
    Book() {
        sum = 0;
        cnt = 0;
    }
    Book(string ISBN, string bookName, string author, string press,
         string pDate, string type, int sum)
        : ISBN(ISBN) {
        this->bookName = bookName;
        this->author = author;
        this->press = press;
        this->pDate = pDate;
        this->type = type;
        this->sum = sum;
        cnt = sum;
    }
    Book(string ISBN, string bookName, string author, string press,
         string pDate, string type, int sum, int cnt)
        : ISBN(ISBN) {
        this->bookName = bookName;
        this->author = author;
        this->press = press;
        this->pDate = pDate;
        this->type = type;
        this->sum = sum;
        this->cnt = cnt;
    }
    Book operator=(const Book &book) {
        string *tmp = const_cast(&ISBN);
        *tmp = book.ISBN;
        this->bookName = book.bookName;
        this->author = book.author;
        this->press = book.press;
        this->pDate = book.pDate;
        this->type = book.type;
        this->cnt = book.cnt;
        this->sum = book.sum;
        return *this;
    }
    string getISBN() { return ISBN; }
    string getBookName() { return bookName; }
    string getAuthor() { return author; }
    string getPress() { return press; }
    string getType() { return type; }
    int getSum() { return sum; }
    int getCnt() { return cnt; }
    void setBookName(string bookName) { this->bookName = bookName; }
    void setAuthor(string author) { this->author = author; }
    void setPress(string press) { this->press = press; }
    void setType(string type) { this->type = type; }
    void setSum(int sum) { this->sum = sum; }
    void setCnt(int cnt) { this->cnt = cnt; }

    friend ostream &operator<<(ostream &os, Book &book) {
        os << book.ISBN << " " << book.bookName << " " << book.author << " "
           << book.press << " " << book.pDate << " " << book.type << " "
           << book.sum << " " << book.cnt;
        return os;
    }

    friend istream &operator>>(istream &is, Book &book) {
        string *temp = const_cast(&book.ISBN);
        string input;
        is >> input;
        *temp = input;
        is >> book.bookName >> book.author >> book.press >> book.pDate >>
            book.type >> book.sum;
        return is;
    }

  private:
    const string ISBN;
    string bookName;
    string author;
    string press;
    string pDate;
    string type;
    int sum;
    int cnt;
    // vector bm;
};
class Operate {
  public:
    Operate() {
        loadBook();
        lodaReader();
    }
    ~Operate() {
        saveBook();
        saveReader();
    }

    void loadBook() {
        Book a;
        ifstream in("E:\\inbook.txt");
        if (!in) {
            return;
        }
        while (!in.eof()) {
            in >> a;
            int temp1, temp2;
            in >> temp1 >> temp2;
            a.setSum(temp1);
            a.setCnt(temp2);
            bk.push_back(a);
            bookname.insert(make_pair(a.getBookName(), bk.size() - 1));
            bookid.insert(make_pair(a.getISBN(), bk.size() - 1));
        }
        in.close();
    }

    void lodaReader() {
        Stu a;
        ifstream in("E:\\inreader.txt");
        if (!in) {
            return;
        }
        while (!in.eof()) {
            in >> a;
            st.push_back(a);
            readername.insert(make_pair(a.getName(), st.size() - 1));
            readerid.insert(make_pair(a.getStuNum(), st.size() - 1));
        }
        in.close();
    }

    void saveBook() {
        ofstream out("E:\\outbook.txt");
        vector::iterator i;
        for (i = bk.begin(); i < bk.end(); ++i) {
            out << *i << endl;
        }
        out.close();
    }

    void saveReader() {
        ofstream out("E:\\outreader.txt");
        vector::iterator i;
        for (i = st.begin(); i < st.end(); ++i) {
            out << *i << endl;
        }
        out.close();
    }

    void addBook() {
        Book b;
        cin >> b;
        multimap::iterator i;
        i = bookid.find(b.getISBN());
        if (i == bookid.end()) {
            b.setSum(b.getSum() + 1);
            b.setCnt(b.getCnt() + 1);
            bk.push_back(b);
            bookname.insert(make_pair(b.getBookName(), bk.size() - 1));
            bookid.insert(make_pair(b.getISBN(), bk.size() - 1));
        } else {
            b.setSum(bk[i->second].getSum() + 1);
            b.setCnt(bk[i->second].getCnt() + 1);
            bk[i->second] = b;
        }
    }
    void deleteBook(string ISBN) {
        multimap::iterator i;
        vector::iterator p = bk.begin();
        i = bookid.find(ISBN);
        if (i != bookid.end()) {
            int index = i->second;
            bk.erase(p + index);
        } else
            return;
    }
    void searchBookByName(string n) {
        multimap::iterator i, p1, p2, p;
        i = bookname.find(n);
        if (i == bookname.end()) {
            return;
        } else {
            p1 = bookname.lower_bound(n);
            p2 = bookname.upper_bound(n);
            for (p = p1; p != p2; ++p) {
                cout << bk[p->second] << endl;
            }
        }
    }
    void searchBookByISBN(string ISBN) {
        multimap::iterator i;
        i = bookid.find(ISBN);
        if (i == bookid.end()) {
            return;
        } else {
            cout << bk[i->second];
        }
    }
    void modifyBook(string id) {
        multimap::iterator i;
        i = bookid.find(id);
        if (i == bookid.end()) {
            return;
        } else {
            cin >> bk[i->second];
            cout << bk[i->second];
        }
    }
    // void addMemo() {
    //     string ISBN, stuNum, type, bookType;
    //     Date date;
    //     cin >> date >> stuNum >> ISBN >> type >> bookType;
    //     bm.push_back(BorrowMemo(date, stuNum, ISBN, type, bookType));
    // }

    void addStu() {
        Stu r;
        cin >> r;
        st.push_back(r);
        readername.insert(make_pair(r.getName(), st.size() - 1));
        readerid.insert(make_pair(r.getStuNum(), st.size() - 1));
    }
    void deleteStu(string stuNum) {
        multimap::iterator i;
        vector::iterator p = st.begin();
        i = readerid.find(stuNum);
        if (i != readerid.end()) {
            int index = i->second;
            st.erase(p + index);
        } else
            return;
    }
    void searchStuByName(string n) {
        multimap::iterator i, p1, p2, p;
        i = readername.find(n);
        if (i == readername.end())
            return;
        else {
            p1 = readername.lower_bound(n);
            p2 = readername.upper_bound(n);
            for (p = p1; p != p2; ++p) {
                cout << st[p->second] << endl;
            }
        }
    }
    void searchStuByID(string n) {
        multimap::iterator i;
        i = readerid.find(n);
        if (i == readerid.end()) {
            return;
        } else {
            cout << st[i->second] << endl;
        }
    }
    void modifyReader(string id) {
        multimap::iterator i;
        i = readerid.find(id);
        if (i == readerid.end()) {
            return;
        } else {
            cin >> st[i->second];
            cout << st[i->second];
        }
    }

  private:
    vector bk;
    vector st;
    multimap bookname;
    multimap readername;
    multimap bookid;
    multimap readerid;
    // vector bm;
};
int main() {
    Operate op;
    string s;
    op.addBook();
    op.addBook();
    op.addBook();
    op.addBook();
    op.addStu();
    op.addStu();
    op.addStu();
    op.addStu();
    op.searchBookByName("计算机组成原理");
    op.searchBookByISBN("9787564097165");
    op.searchStuByName("杨思源");
    op.searchStuByID("2018215852");
    op.deleteBook("9787513591218");
    op.deleteStu("2018216018");
    op.modifyBook("9787302423287");
    op.modifyReader("2018215906");
    return 0;
}

你可能感兴趣的:(C++)