首页 > 解决方案 > 我需要帮助过滤文本文件

问题描述

您好,我收到了一个项目,询问用户是否要按标题、作者或流派对文本文件进行排序。我的问题是,如果用户选择使用流派对其进行排序,他将输入一个流派(如小说、幻想),程序将按字母顺序打印出与该流派相关的书籍。请给我关于我应该如何进行的指示或解决方案。我是 C++ 的初学者,如果我的代码没有意义,我很抱歉。

这是文本文件中内容的摘要

Starting out with c++, Tony Gaddis, technical
Fundamentals of Database Systems, Elmarsi & Navathe, technical
One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction
Ashes, Kenzo Kitakana, fiction

* 编码

#include <iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<sstream>

using namespace std;

struct Book
{
    string title;
    string author;
    string genre;
};

void readFile(ifstream&);
string GetFileName();

vector<Book> Books;
bool compareByTitle(Book, Book);
bool compareByAuthor(Book, Book);
bool compareByGenre(Book, Book);

void Option1();
void Option2();
void Option3();

void print(vector<Book>);
//void filtered_genre(vector<Book>, string);


int main()
{
    int UserChoice;
    string Newgenre;

    ifstream books("books.txt"); 
    while(!books.is_open())
    {
        books.open(GetFileName());
    }
    cout << "\nThe file is successfully open\n";
    readFile(books);

    cout << "\n 1. Sort by Title"
         << "\n 2. Sort by Author"
         << "\n 3. Sort by Genre"
         << "\n 4. EXIT\n"
         << "\n Pls enter your choice: ";
    do
    {
        cin >> UserChoice;
        if(UserChoice == 1)
        {
            Option1();
            break;
        }
        else if(UserChoice == 2)
        {
            Option2();
            break;
        }
        else if(UserChoice == 3)
        {
            cout << "Pls entere a genre : ";
            cin >> Newgenre;
            //Option3();
            break;
        }
        else if(UserChoice == 4)
        {
            cout << "\nExiting....\n";
        }
        else
        {
            cout << "\nInvalid Input\n"
                 << "\nPls enter your choice again: ";
        }
    }
    while (UserChoice != 4);

    books.close();
}

void print(const vector<Book> BookContents)
{
    for(int i=0; i<BookContents.size(); i++)
    {
        cout << "\n\nTitle: " << BookContents[i].title << endl;
        cout << "Author: " << BookContents[i].author << endl;
        cout << "Genre: " << BookContents[i].genre << endl;
    }
}

void readFile(ifstream& file)
{
    Book books;
    string line;
    while(!file.eof())
    {
        getline(file, line);
        stringstream ss(line);
        getline(ss, books.title,',');// Extracts title from the file
        getline(ss, books.author,',');// Extracts author name from the file
        getline(ss, books.genre);// Extracts genre from the file
        Books.push_back(books);// Adds books object to vector
    }
}

string GetFileName()
{
    string FileNameByUser;
    cout << "\nThe file you are trying to open doesn't exist\n"
         << "\nPls enter the file again : ";
    getline(cin, FileNameByUser);
    cout << "\nThe path you entered is : ";
    cout << FileNameByUser + "\n";
    return FileNameByUser;
}

bool compareByTitle(Book book1, Book book2)
{
    return book1.title < book2.title;
}

bool compareByAuthor(Book book1, Book book2)
{
    return book1.author < book2.author;
}

bool compareByGenre(Book book1, Book book2)
{
    return book1.genre < book2.genre;
}

void Option1()
{
    cout << "\nSorting the book contents by title\n";
    std::sort(Books.begin(), Books.end(), compareByTitle);
    print(Books);
}
void Option2()
{
    cout << "\nSorting the book contents by author\n";
    std::sort(Books.begin(), Books.end(), compareByAuthor);
    print(Books);
}
void Option3()
{
    cout << "\nSorting the book contents by genre\n";
    std::sort(Books.begin(), Books.end(), compareByGenre);
    print(Books);
}

标签: c++

解决方案


所以我认为你具备解决这个问题所需的所有技能。您知道如何将项目添加到向量中,并且您知道如何使用不同的标准对向量进行排序。正如您在标题中所说,剩下的唯一部分是如何过滤数据。

有很多不同的方法可以做到这一点,我将建议一种非常简单的方法。首先将流派参数添加到您的 Option3 函数,以便您可以将您在 main 中读取的流派传递给该函数。

void Option3(string genre);

int main()
{
    ...
        else if(UserChoice == 3)
        {
            cout << "Pls entere a genre : ";
            cin >> Newgenre;
            Option3(Newgenre);
            break;
        }
    ...
}

void Option3(string genre)
{
    ...
}

现在在函数中Option3我们要进行过滤。有四个简单的步骤

  1. 声明一个新的向量来保存过滤后的数据,调用它(比如说)FilteredBooks

  2. 现在编写一个遍历所有书籍的循环,并将书籍的类型与用户感兴趣的类型进行比较。如果类型相同,则将书籍添加到FilteredBooks向量中。

  3. 现在FilteredBooks按字母顺序对向量进行排序(这就是您所说的,我猜您的意思是按标题的字母顺序)。

  4. 现在打印FilteredBooks向量。您已经拥有执行此操作的功能。

而已。希望这可以帮助。


推荐阅读