数组选择输出问题和输出

时间:2014-04-11 02:36:58

标签: c++ arrays binary-search selection-sort linear-search

您好我不确定我的代码有什么问题。 选择排序有效,但当程序再次要求用户输入名称时,他们无法找到正确的人。有人能帮我吗?我不确定是什么问题。

编辑:我第二次要求输入时出现的错误是"找不到姓名"。我不明白为什么

图片在这里: http://i.imgur.com/2Gkd0gh.pngh

这是我的完整代码:

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

using namespace std;

// GLOBAL CONSTANTS
const int NUM_NAMES = 20;

// FUNCTION PROTOTYPES
void getNames(ifstream &, string[], int[], int);
int linearSearch(const string[], int, string);
int binarySearch(const string[], int, string);
void selectionSort(string[], int[], int);
void displayData(const string[], const int[], int);
void displaySearch(const string[], const int[], int);

int main()
{
// LOCAL VARIABLES
string names [NUM_NAMES];
int marks [NUM_NAMES];
ifstream inStream;              // Input file stream variable
int index = 0;
string searchWho;

// FUNCTION CALL 1
getNames (inStream, names, marks, NUM_NAMES);
cout << endl;
cout << " Students and Marks:" << endl;
cout << " ______________________________" << endl << endl;
// FUNCTION CALL 2: DisplayData
displayData(names, marks, NUM_NAMES);
// OUTPUT - Perform search - USER INPUT
cout << endl;
cout << " Please enter the first and last name of who who want to look up, seperated with a space." << endl << endl;
cout << " "; cin >> searchWho;
cout << endl << endl;
// FUNCTION CALL 3: linearSearch
index = linearSearch (names, NUM_NAMES, searchWho); 
// FUNCTION CALL 4: displaySearch
displaySearch(names, marks, index);
// FUNCTION CALL 5: selectionSort
selectionSort (names, marks, NUM_NAMES);
cout << endl;
cout << " Students and Marks:" << endl;
cout << " ______________________________" << endl << endl;
displayData(names, marks, NUM_NAMES);
// OUTPUT - Perform search - USER INPUT
cout << endl;
cout << " Please enter the first and last name of who who want to look up, seperated with a space." << endl << endl;
cout << " "; cin >> searchWho;
cout << endl << endl;
// FUNCTION CALL 4
index = binarySearch (names, NUM_NAMES, searchWho);
displaySearch(names, marks, index);

cout << " "; return 0;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FUNCTION 1:      getNames
// DESCRIPTION:     Function opens data file students.txt
//                  Function reads data from students.txt and stores data
//                  appropriately according to customerCode and utilityCharge
//                  in parallel arrays Customer[] and Charge[]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void getNames (ifstream &inStream, string names[], int marks[], int numElts)
{
    // Open input file
inStream.open ("students.txt");
string studentNames;            // Student names - Last name followed by first
int studentMarks = 0;           // Student mark for text/exam

// Read in data from students.txt
while (!inStream.eof())
{
    for(int count = 0; count < numElts; count ++)
    {
        inStream >> names[count];
        inStream >> marks[count];
    }

}
inStream.close();
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  FUNCTION: LinearSearch
//  DESCRIPTION: 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int linearSearch (const string names[], int numElts, string who)
{
int index = 0;      // Used as a subscript to search array
int position = -1;  // To record position of search value
bool found = false; // Flag to indicate if value was found

 while (index < numElts && !found)
{
    if (names[index] == who) // If the name is found 
    {
        found = true; // Set the flag 
        position = index; // Record the value's subscript
    }
    index++; // Go to the next element
}
return position; // Return the position, or -1
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  FUNCTION: SelectionSort
//  DESCRIPTION: 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void selectionSort(string names[], int marks[], int numElts)
{
int startScan;
int minIndex;
int startName;

for (startScan = 0; startScan < (numElts - 1); startScan++)
{
    startName = startScan;
    for (int minIndex = startScan + 1; minIndex < numElts; minIndex++)
    {
        if (names[minIndex] > names[startName])
        {
            startName = minIndex;
            string tempString = names[startScan];
            names[startScan] = names[minIndex];
            names[minIndex] = tempString;

            // Aligning arrays
            int tempInt = marks[startScan];
            marks[startScan] = marks[minIndex];
            marks[minIndex] = tempInt;
        }
    }

}
}

int binarySearch(const string names[], int numElts, string who)
{
int first = 0;              // First array element
int last = numElts - 1;     // Last array element
int middle;                 // Mid point of search
int position = -1;          // Position of search value
    bool found = false;         // Flag

while (!found && first <= last)
{
    middle = (first + last) / 2;     // Calculate mid point

    if (names[middle] == who)      // If value is found at mid
    {
        found = true;
        position = middle;
    }
    else if (names[middle] > who)  // If value is in lower half
    {
        last = middle - 1;
    }
    else
    {
        first = middle + 1;           // If value is in upper half
    }
}
return position;
}


void displayData(const string names[], const int marks[], int numElts)
{

// OUTPUT
for (int count = 0; count < numElts; count++)
{
    cout << " " << left << setw(15) << names[count] << right << setw(15) << marks[count] << endl;
}
}

void displaySearch(const string names[], const int marks[], int index)
{   
if (index == -1)
{
    cout << " Name not found. Restart the program to search again." << endl << endl;
}
else
{
    cout <<  names[index] <<  " scored " <<  marks[index] << " marks." << endl << endl;
}
}

1 个答案:

答案 0 :(得分:0)

从共享图像http://i.imgur.com/2Gkd0gh.pngh中,数组未正确排序。例如,

艾莉森,杰夫45 柯林斯,比尔80 艾伦,吉姆82

如下面的代码所示:

if (names[minIndex] > names[startName])
{
        startName = minIndex;
        //...
}

startName表示names数组中最大名称的索引,不是吗? (数组是降序排序的,对吧?) 因此,它假设将索引startName处的最大名称与索引startScan处的当前扫描名称交换。那应该是

startName = startScan;
for (int minIndex = startScan + 1; minIndex < numElts; minIndex++)
{
    if (names[minIndex] > names[startName])
    {
        startName = minIndex;
    }
}

string tempString = names[startScan];
names[startScan] = names[startName];
names[startName] = tempString;

// Aligning arrays
int tempInt = marks[startScan];
marks[startScan] = marks[startName];
marks[startName] = tempInt;

binarySearch中,它假设数组按升序排列。 因此,要按升序对数组进行排序,可以更改

if (names[minIndex] > names[startName])
{
        startName = minIndex;

}

if (names[minIndex] < names[startName])
{
        startName = minIndex;
}