为什么我总是在代码中返回位置10?

时间:2020-03-30 00:58:46

标签: c++

在我的用于对字符串数组进行排序的程序中,当二进制搜索名称时,我总是得到10的位置。为什么会发生这种情况?

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

using namespace std;

const int MAXNAMES = 20;

void readArray(string names[])
{
    ifstream inFile("names.txt");
    string line;
    int counter = 0;

    if (!inFile.is_open())
    {
        cout << endl << "Cannot locate file names.txt" << endl;
        exit(0);

    }
    else
    {
        cout << "Succesfully opened \"names.txt\" file." << endl;
    }

    while (getline(inFile, line))
    {
        names[counter++] = line;
    }

    inFile.close();
}

void displayArray(string names[])
{
    for (int i = 0; i < MAXNAMES; i++)
    {
        cout << names[i] << endl;
    }

    cout << endl;
}

void selectionSort(string names[])
{
    int i, j, minIndex;
    string minString;

    for (i = 0; i < MAXNAMES - 1; i++)
    {

        minIndex = i;
        minString = names[i];

        for (j = i + 1; j < MAXNAMES; j++)
        {
            if (minString.compare(names[j]) > 0)
            {

                minString = names[j];
                minIndex = j;
            }
        }

        if (minIndex != i)
        {
            string temp = names[i];
            names[i] = names[minIndex];
            names[minIndex] = temp;
        }
    }
}

void sequentialSearch(string names[], string name)
{
    bool found = false;
    int index = 0;

    for (int i = 0; i < MAXNAMES; i++)
    {
        if (name.compare(names[i]) == 0)
        {
            found = true;
            index = i;
            break;
        }
    }

    if (!found)
        cout << "The name is not found." << endl;
    else
        cout << endl << "The name is found at index: " << index + 1 << "." << endl;
}

void bubbleSort(string names[])
{
    string temp;
    for (int j = 0; j < MAXNAMES - 1; j++)
    {

        for (int i = j + 1; i < MAXNAMES; i++)
        {
            if (names[j].compare(names[i]) > 0)
            {
                temp = names[j];
                names[j] = names[i];
                names[i] = temp;
            }
        }
    }
}

int binarySearch(string names[], string name)
{
    int l = 0;
    int r = MAXNAMES - 1;

    while (l <= r)
    {
        int m = l + (r - l) / 2;
        int res = 0;

        if (name == names[m])
            res = 0;
        if (res == 0)
            return m;

        if (name > (names[m]))
            l = m + 1;
        else
            r = m - 1;
    }

    return -1;
}

int main()
{
    string names[MAXNAMES];
    readArray(names);
    cout << "Array before sort:" << endl << endl;
    displayArray(names);
    cout << "Array after selection sort is:" << endl << endl;
    selectionSort(names);
    displayArray(names);
    string nameToSearch;
    cout << "Enter a name to (sequential) search for: ";
    std::getline(std::cin, nameToSearch);
    sequentialSearch(names, nameToSearch);
    string choice;
    cout << endl << "Replace first element of the array \"" << names[0] << "\" with \"" << nameToSearch << "\" Yes or No: ";
    std::getline(std::cin, choice);

    if (choice.compare("Yes") == 0 || choice.compare("yes") == 0)
    {
        names[0] = nameToSearch;
    }

    bubbleSort(names);
    cout << endl << "Array after Bubble Sort is:" << endl;
    displayArray(names);
    cout << "Enter a name to (binary) search for: ";
    std::getline(std::cin, nameToSearch);
    int index = binarySearch(names, nameToSearch);

    if (index == -1)
        cout << endl << "The name is not found." << endl;
    else
        cout << endl << "The name is found at position " << (index + 1) << endl;

    return 0;
}

这是用于代码的输入文件:

names.txt

Collins, Bill
Smith, Bart
Allen, Jim
Griffin, Jim
Stamey, Marty
Rose, Geri
Taylor, Terri
Johnson, Jill
Allison, Jeff
Looney, Joe
Wolfe, Bill
James, Jean
Weaver, Jim
Pore, Rob
Rutherford, Rose
Javens, Renee
Harison, Rose
Setzer, Cathy
Pike, Gordon
Holland, Beth

1 个答案:

答案 0 :(得分:3)

binarySearch中,您具有以下顺序:

    int res = 0;

    if (name == names[m])
        res = 0;
    if (res == 0)
        return m;

由于res在到达第二个if时始终为零,因此它将始终在第一次循环时返回。

您可以完全删除res,而只需将其替换为

    if (name == names[m])
        return m;