Алфавитный массив массива данных с обратной пузырьковой сортировкой - PullRequest
2 голосов
/ 17 марта 2011

Я пытаюсь отсортировать массив 2-элементной структуры данных по алфавиту.Вся программа считывает файл данных в массив, затем алфавитирует массив и ищет его по запросу.

Моя проблема заключается в том, что после сортировки очищается первая запись базы данных.Это можно увидеть в выводе в конце этого поста.

Код следует

#include <iostream>                 //Required if your program does any I/O
#include <fstream>                  //Required for file I/O
#include <string>                   //Required if your program uses C++ strings

using namespace std;                //Required for ANSI C++ 1998 standard.

struct Book     // Data structure of database entry
{
       string title;
       string author;
};

const int ARRAY_SIZE = 1000;      //Maximum database size
Book books [ARRAY_SIZE];          //Library database

void loadData ( int& librarySize ); //Function prototype to load data file into database
void showAll ( int librarySize );  //Function prototype to display entire database contents
void menu ( int librarySize ); //Function prototype to process menu of database functions
void searchByAuthor ( int librarySize );  //Function prototype to search database by author
void searchByTitle ( int librarySize );   //Function prototype to search database by title
void sortByAuthor ( int librarySize );    //Function prototype to alphabetically sort database by author
void sortByTitle ( int librarySize );     //Function prototype to alphabetically sort database by title

int main ()
{ 
    int librarySize = 0;           //Declaring and initializing databse size variable

    cout << "Welcome to Greathouse's Library Database" << endl;

    loadData ( librarySize );        //Prompt for and loading of data file into database.

    menu ( librarySize );  //Processing of database functions menu

    system("pause");
    exit(0);
}

void loadData ( int& librarySize )
{
    ifstream inputFile;         //File I/O variable
    string inputFileName;       //Data file path

    //Prompt for data file path
    cout << "Please enter the name of the backup file: ";
    getline(cin, inputFileName);

    // Open the data file.
    inputFile.open(inputFileName.c_str());      // Need .c_str() to convert a C++ string to a C-style string

    // Check the file opened successfully.
    if ( ! inputFile.is_open())
    {
        cout << "Unable to open input file." << endl;
        system("pause");
        exit(-1);
    }

    //Read data file into database
    for ( librarySize = 0; inputFile.peek() != EOF && librarySize < ARRAY_SIZE ; librarySize++ )
    {
        getline( inputFile, books[librarySize].title );
        getline( inputFile, books[librarySize].author );
    }

    //Confirm number of records loaded with user
    cout << librarySize << " records loaded successfully." << endl;

    // Clear EOF flag on file stream
    inputFile.clear();

    // Return to the beginning of the file stream
    inputFile.seekg(0);
}

void menu ( int librarySize )
{
     char command = ' ';

     //Display and processing of menu and commands until escape character 'q' is entered
     while ( command != 'Q' && command != 'q' )
     {
           cout << "Would you like to (Q)uit, (S)howall, Search by (A)uthor, or Search by (T)itle? : ";
           cin >> command;

           switch ( command )
           {
                  case 'S':
                  case 's':

                      showAll ( librarySize );           //Call to function to show database contents in response to user input
                      break;

                 case 'A':
                 case 'a':

                      searchByAuthor ( librarySize );    //Call to function to search database by author and display results alphabetically in response to user input

                      break;

                 case 'T':
                 case 't':

                      searchByTitle ( librarySize );     //Call to function to search database by title and display results alphabetically in response to user input

                      break;

                 case 'Q':
                 case 'q':

                      break;        //Case option to prevent extraneous output when quitting program

                 default:

                         cout << "That is not a valid command." << endl;
                         break;
          }
    }
}

void searchByAuthor ( int librarySize )   //Function to search database by author
{
     string authorSearch = " ";       //User query
     int authorResults = 0;  //Number of query results found
     int iteration = 0;  //Loop counting variable

     //Prompt for and reading of user query
     cout << "Author: : ";
     cin >> authorSearch;

     sortByAuthor ( librarySize ); //Call to sort database alphabetically by author so output will be alphabetical

     //Iterative search of database for all instances of query
     for ( iteration = 0; iteration <= librarySize; iteration++ )
     {
         if ( books[iteration].author.find ( authorSearch ) != string::npos )
         {
            cout << books[iteration].title << " (" << books[iteration].author << ")" << endl;
            authorResults++;
         }
     }

     cout << authorResults << " records found." << endl;                      //Output of number of results
}

void searchByTitle ( int librarySize )
{
     string titleSearch = " ";       //User query
     int titleResults = 0;  //Number of query results found
     int iteration = 0; //Loop counting variable

     //Prompt for and reading of user query
     cout << "Title: ";
     cin >> titleSearch;

     sortByTitle ( librarySize );     //Call to sort database alphabetically by title so output will be alphabetical

     //Iterative search of database for all instances of query
     for ( iteration = 0; iteration <= librarySize; iteration++ )
     {
         if ( books[iteration].title.find ( titleSearch ) != string::npos )
         {
            cout << books[iteration].title << " (" << books[iteration].author << ")" << endl;
            titleResults++;
         }                         
     }

     cout << titleResults << " records found." << endl;                       //Output of number of results
}

void showAll ( int librarySize )       //Function to show database contents
{
     //Iterative walk through database to display contents
     for ( int iteration = 0; iteration < librarySize; iteration++ )
     {
         cout << books[iteration].title << " (" << books[iteration].author << ")" << endl;
     }
}

void sortByAuthor ( int librarySize ) //Function to sort database alphabetically by author
{
     //Bubble sort of databse alphabetically by author
     for ( int pass = 0; pass < librarySize ; pass++ )
     {
         for ( int iteration = 0; iteration < librarySize - pass; iteration++ )
         {             
             if ( books[iteration].author > books[iteration+1].author )
             {
                  swap ( books[iteration] , books[iteration+1] );
             }
         }
     }
}

void sortByTitle ( int librarySize )      //Function to sort database alphabetically by title
{
     //Bubble sort of databse alphabetically by title
     for ( int pass = 0; pass < librarySize ; pass++ )
     {
         for ( int iteration = 0; iteration < librarySize - pass; iteration++ )
         {             
             if ( books[iteration].title > books[iteration+1].title )
             {
                  swap ( books[iteration] , books[iteration+1] );
             }
         }
     }
}

Тестовые данные

Objects First with Java  
Barnes and Kolling  
Game Development Essentials  
Novak  
The Game Maker's Apprentice  
Overmars  
C++ Programming: From Problem Analysis...  
Malik  
C++ Programming Lab Manual  
Scholl  
Beginning LINUX Programming  
Stones and Matthew  
C++ Programming: Program Design Including...  
D. S. Malik  
C++ How to Program  
Deitel and Deitel  
Programming and Problem Solving with C++  
Dale, Weems, Headington  
Game Character Development with Maya  
Ward  
Developing Games in Java  
Brackeen  
C# Programming  
Harvey, Robinson, Templeman, Watson  
Java Programming  
Farrell  
Audio for Games  
Brandon  

Вывод ошибки

Welcome to Greathouse's Library Database
Please enter the name of the backup file: library.txt
14 records loaded successfully.
Would you like to (Q)uit, (S)howall, Search by (A)uthor, or Search by (T)itle? :
 s
Objects First with Java (Barnes and Kolling)
Game Development Essentials (Novak)
The Game Maker's Apprentice (Overmars)
C++ Programming: From Problem Analysis... (Malik)
C++ Programming Lab Manual (Scholl)
Beginning LINUX Programming (Stones and Matthew)
C++ Programming: Program Design Including... (D. S. Malik)
C++ How to Program (Deitel and Deitel)
Programming and Problem Solving with C++ (Dale, Weems, Headington)
Game Character Development with Maya (Ward)
Developing Games in Java (Brackeen)
C# Programming (Harvey, Robinson, Templeman, Watson)
Java Programming (Farrell)
Audio for Games (Brandon)
Would you like to (Q)uit, (S)howall, Search by (A)uthor, or Search by (T)itle? :
 t
Title: Game
Audio for Games (Brandon)
Developing Games in Java (Brackeen)
Game Character Development with Maya (Ward)
Game Development Essentials (Novak)
The Game Maker's Apprentice (Overmars)
5 records found.
Would you like to (Q)uit, (S)howall, Search by (A)uthor, or Search by (T)itle? :
 s
 ()
Audio for Games (Brandon)
Beginning LINUX Programming (Stones and Matthew)
C# Programming (Harvey, Robinson, Templeman, Watson)
C++ How to Program (Deitel and Deitel)
C++ Programming Lab Manual (Scholl)
C++ Programming: From Problem Analysis... (Malik)
C++ Programming: Program Design Including... (D. S. Malik)
Developing Games in Java (Brackeen)
Game Character Development with Maya (Ward)
Game Development Essentials (Novak)
Java Programming (Farrell)
Objects First with Java (Barnes and Kolling)
Programming and Problem Solving with C++ (Dale, Weems, Headington)
Would you like to (Q)uit, (S)howall, Search by (A)uthor, or Search by (T)itle? :

Ответы [ 2 ]

1 голос
/ 17 марта 2011

В большинстве ваших циклов вы используете это условие:

iteration <= librarySize

Индексы вашего массива варьируются от 0 до librarySize - 1 (так как это индексы, заполненные loadData), поэтомупоследняя допустимая запись в вашем массиве books[librarySize - 1].Попробуйте изменить условие цикла на:

iteration < librarySize

Редактировать: Есть и другая проблема в вашей функции сортировки: вы пытаетесь получить доступ к books[iteration+1], который снова выйдет за пределыво время первого прохода.Ваш внутренний цикл должен идти только до librarySize - pass - 1:

iteration < librarySize - pass - 1
0 голосов
/ 17 марта 2011

Проблема в том, где происходит ваш "пузырь".В обоих циклах вы начинаете с массива pos 0, вам нужно двигаться вверх, так как каждый первый элемент находится в правильном месте.Конец никогда не сортируется.

Вот код, который я не тестировал, поэтому он может быть отключен на 1 и т. Д. Но вы поймете идею

 for ( int pass = 0; pass <= librarySize ; pass++ )
 {
     for ( int iteration = 0; iteration <= librarySize - pass; iteration++ )
     {             
         if ( books[iteration+pass].title > books[iteration+pass+1].title )
         {
              swap ( books[iteration+pass] , books[iteration+pass+1] );
         }
     }
 }

Или вы можете сделать это следующим образомэто

 for ( int pass = 0; pass <= librarySize ; pass++ )
 {
     for ( int iteration = pass; iteration <= librarySize ; iteration++ )
     {             
         if ( books[iteration].title > books[iteration+1].title )
         {
              swap ( books[iteration] , books[iteration+1] );
         }
     }
 }
...