按字母顺序排序书名

时间:2014-12-08 00:43:01

标签: c++ sorting bubble-sort

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

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

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

但返回的内容如下:

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;
            }
        }
    }
}

这是我正在使用的书籍及其作者的列表。他们只需要复制并粘贴到.txt文件中(只需删除子弹点。它应该看起来与它们在这里发布的相同,只是没有它们)。他们按书然后作者下载,然后预订然后作者。

  • 使用Java的对象
  • Barnes and Kolling
  • 游戏开发基础
  • 诺瓦克
  • 游戏制作者的学徒
  • 奥维马斯
  • C ++编程:来自问题分析......
  • 马立克
  • C ++编程实验手册
  • 舍尔
  • 开始LINUX编程
  • 石头和马修
  • C ++编程:程序设计包括......
  • d。 S. Malik
  • C ++如何编程
  • Deitel和Deitel
  • 使用C ++编程和解决问题
  • Dale,Weems,Headington
  • Maya的游戏角色开发
  • 沃德
  • 用Java开发游戏
  • Brackeen
  • C#编程
  • Harvey,Robinson,Templeman,Watson
  • Java编程
  • 法雷尔
  • 音频游戏
  • 布兰登

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

1 个答案:

答案 0 :(得分:0)

乍一看,看起来你正在将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;
            }
        }
    }
}

按标题排序相同。