【问题标题】:Sorting book titles alphabetically issue按字母顺序排列书名
【发布时间】:2014-12-08 00:43:01
【问题描述】:

这是我几乎完成的家庭作业的一部分。基本上,我必须使用一系列结构来按标题和作者存储图书库。我有代码工作,目前它实际上存储然后按字母顺序排序。如果您显示所有书籍或按标题搜索,它将按标题的字母顺序打印,对于按作者搜索也是如此,除了它按作者的字母顺序打印。 我遇到的一个问题是,出于某种原因,书籍及其作者被调换了。因此,如果您搜索作者 x 的书 x,则会得到作者 y 的书 x。

在下面的书籍列表中,示例如下:

C++ 编程:来自问题分析... (Malik) // 正确作者

而是返回类似的东西:

C++ 编程:来自问题分析... (Brandon) // 错误的作者

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

// struct/variables
struct Book {
    string title;
    string author;
};

const int ARRAY_SIZE = 1000;
Book books [ARRAY_SIZE];
string pathname;
ifstream library;

// global variables
int LoadData();
void ShowAll(int count);
void ShowBooksByAuthor(int count, string name);
void ShowBooksByTitle(int count, string title);
void sortByTitle(int count, string title);
void sortByAuthor(int count, string author);

int main()
{
    // init vars
    int count = 0;
    char selector = 'q', yesNoAnswer = 'n';
    string name;
    string title;

    // prompt user and get file path
    cout << "Welcome to Forrest's Library Database." << endl;
    cout << "Please enter the name of the backup file: ";
    getline(cin, pathname);
    LoadData();
    count = LoadData();
    cout  << count << " records loaded successfully." << endl;

    // build 'case' menu 
    do 
    {
        cout << endl << "\t(S)how All, Search (A)uthor, Search (T)itle, (Q)uit: "; //menu options
        cin >> selector;
        selector = toupper(selector);
        switch(selector)
        {
            case 'S': // show all the book titles and authors
                sortByTitle(count, title);
                if (count <= 0)
                    cout << "No counts found!\n";
                else
                    ShowAll(count);
                break;
            case 'A': // search by author name 
                sortByAuthor(count, name);
                cout << "bookAuthor: ";
                cin.ignore();
                getline(cin, name);
                if (count <= 0)
                    cout << "No records found!\n";
                else
                    ShowBooksByAuthor(count, name); 
                break;
            case 'T': // search by book title
                sortByTitle(count, title);
                cout << "bookTitle: ";
                cin.ignore();
                getline(cin, title);
                if (count <= 0)
                    cout << "No records found!\n";
                else
                    ShowBooksByTitle(count, title);      
                break; 
        }
    }
    while (selector != 'q' && selector != 'Q'); // the condition that will break the do loop and exit
    return 0;
}
int LoadData() // loads the titles and authors into two arrays
{
    int count = 0;
    int i = 0;

    library.open(pathname);
    ifstream library(pathname);

    if (!library)
    {
        cout << "Cannot open backup file" << endl;
        return 0;
    }

    while (!library.eof())
    {

        getline(library, books[count].title);
        getline(library, books[count].author);

        count++;
    }
    return count;

}
void ShowAll(int count) // displays all book titles beside the author names
{

    for (int i = 0; i < count; i++)
    {
       cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
    }

}

void ShowBooksByAuthor(int count, string name) // displays all books by author
{
    int j = 0;

    for (int i = 0; i < count; i++)
    {
        if(books[i].author.find(name) < 100) 
        {
            cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
            j++;
        }
    }
    cout << j << " records found";
}

void ShowBooksByTitle(int count, string title) // shows all books by title
{
    int j = 0;

    for (int i = 0; i < count; i++)
    {
        if(books[i].title.find(title) < 100)
        {
            cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
            j++;
        }
    }
    cout << j << " records found";
}

void sortByTitle(int count, string title) {
    string temp;

    for (int i = 0; i < count; i++) {
        for(int j = 0; j < count - i; j++) {
            if (books[j].title > books[j + 1].title) {
                temp = books[j].title;
                books[j].title = books[j + 1].title;
                books[j + 1].title = temp;
            }
        }
    }
}

void sortByAuthor(int count, string name) {
    string temp;

    for (int i = 0; i < count; i++) {
        for(int j = 0; j < count - i; j++) {
            if (books[j].author > books[j + 1].author) {
                temp = books[j].author;
                books[j].author = books[j + 1].author;
                books[j + 1].author = temp;
            }
        }
    }
}

这是我正在使用的书籍及其作者的列表。它们只需要被复制并粘贴到一个 .txt 文件中(只需删除项目符号点。它应该看起来和这里发布的一样,只是没有它们)。他们先是书,然后是作者,然后是书,然后是作者。

  • Java 对象优先
  • 巴恩斯和科林
  • 游戏开发要点
  • 诺瓦克
  • 游戏制作者的学徒
  • 奥弗马斯
  • C++ 编程:从问题分析...
  • 马利克
  • C++ 编程实验手册
  • 舒尔
  • 开始 LINUX 编程
  • 石头和马修
  • C++ 编程:程序设计包括...
  • D. S.马利克
  • C++ 如何编程
  • 代特尔和代特尔
  • 使用 C++ 编程和解决问题
  • 戴尔、威姆斯、海丁顿
  • 使用 Maya 开发游戏角色
  • 病房
  • 用 Java 开发游戏
  • 蕨菜
  • C# 编程
  • 哈维、罗宾逊、坦普尔曼、沃森
  • Java 编程
  • 法瑞尔
  • 游戏音频
  • 布兰登

感谢所有可以帮助我解决问题的人!

【问题讨论】:

    标签: c++ sorting bubble-sort


    【解决方案1】:

    乍一看,您似乎将 Book 对象留在了数组中的相同位置,只是交换了标题或作者(取决于排序函数),其余成员保持不变。

    您应该交换对象本身,例如:

    void sortByAuthor(int count, string name) {
        Book temp; //Book instance instead of string
    
        for (int i = 0; i < count; i++) {
            for(int j = 0; j < count - i; j++) {
                if (books[j].author > books[j + 1].author) {
                    //swapping the instances themselves, but still comparing by the member.
                    temp = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = temp;
                }
            }
        }
    }
    

    按标题排序也是如此。

    【讨论】:

    • 谢谢,这成功了!不敢相信我忽略了这一点,但是是的,它现在可以跟踪所有内容。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多