Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按字母顺序排列书名_C++_Sorting_Bubble Sort - Fatal编程技术网

C++ 按字母顺序排列书名

C++ 按字母顺序排列书名,c++,sorting,bubble-sort,C++,Sorting,Bubble Sort,这是我即将完成的家庭作业的一部分。基本上,我必须使用一个结构数组来按标题和作者存储图书库。我有工作的代码,目前它实际上存储,然后按字母顺序排序。如果显示所有书籍或按标题搜索,则按标题的字母顺序打印,按作者搜索也是如此,但按作者的字母顺序打印除外。 我遇到的一个问题是,由于某种原因,这些书和它们的作者被调换了。所以如果你用作者x搜索书x,你会得到作者y的书x 在下面的书籍列表中,一个例子是: C++编程:从问题分析。。。(马利克)//正确的作者 而是返回如下内容: C++编程:从问题分析。。。(布

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

在下面的书籍列表中,一个例子是:

C++编程:从问题分析。。。(马利克)//正确的作者

而是返回如下内容:

C++编程:从问题分析。。。(布兰登)//作者错了

#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;
            }
        }
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
//结构/变量
结构书{
字符串标题;
字符串作者;
};
常量int数组_SIZE=1000;
书本[数组大小];
字符串路径名;
ifstream库;
//全局变量
int LoadData();
void ShowAll(整数计数);
void ShowBooksByAuthor(整数计数、字符串名称);
void ShowBooksByTitle(整数计数、字符串标题);
void sortbytle(整数计数、字符串标题);
void sortByAuthor(整数计数,字符串作者);
int main()
{
//初始化变量
整数计数=0;
字符选择器='q',yesNoAnswer='n';
字符串名;
字符串标题;
//提示用户并获取文件路径

cout乍一看,您似乎将
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;
            }
        }
    }
}
void sortByAuthor(整数计数,字符串名称){
Book temp;//书本实例而不是字符串
for(int i=0;i图书[j+1].作者){
//交换实例本身,但仍由成员进行比较。
温度=书本[j];
图书[j]=图书[j+1];
书籍[j+1]=临时;
}
}
}
}

按标题排序也一样。

谢谢,这很有效!不敢相信我忽略了这一点,但现在它可以跟踪所有内容。:)