gpt4 book ai didi

c++ - 为什么我的二进制搜索没有返回要显示的名称?

转载 作者:行者123 更新时间:2023-11-30 02:50:54 25 4
gpt4 key购买 nike

抱歉,但我无法弄清楚为什么我的代码(更具体地说是二进制 searchName() 函数)没有返回要显示的名称。我已验证名称已使用分配不需要的显示功能进行排序。任何帮助表示赞赏。这是从头到尾的代码。我们没有介绍 vector 或数组以外的任何其他内容,因此 vector 不会有帮助,但我等不及了。

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

using namespace std;

//function Prototypes
void nameSort(string friendArray[], int ARRAY_SIZE);
//function to display names from file
void displayNames(string friendArray[], int counter);
//function to binary search for name in file/array
//How do i return a string?? I got the following code from web searches
int searchName(string friendArray[], int ARRAY_SIZE, string result);

int main()
{
const int ARRAY_SIZE = 200; //declare array size
string friendArray[ARRAY_SIZE];//array of 200 strings
ifstream fileIn; //create file object

int counter = 0;
string choice;
string result; //variable to hold the search result

fileIn.open("myFriends.dat");//open the file
if(fileIn.fail())//test to see if file opened
{
cout<<"Check to see if file is in same directory ";
cout<<"as the .cpp file."<<endl;
}

while(getline(fileIn, friendArray[counter]))
{
counter++;
}

//call function to sort array
nameSort(friendArray, ARRAY_SIZE);
//display sorted names to screen with function
displayNames(friendArray, ARRAY_SIZE);
//call to search function
searchName(friendArray, ARRAY_SIZE, result);
cout<<friendArray[]<<" is my friend."<<endl;

system("Pause");
return 0;
}

//sort function
void nameSort(string friendArray[], const int ARRAY_SIZE)
{
bool swap;
string temp;

do
{
swap = false; //set flag to false
for(int counter = 0; counter < (ARRAY_SIZE - 1); counter++)
{
if(friendArray[counter] > friendArray[counter + 1])
{
temp = friendArray[counter];
friendArray[counter] = friendArray[counter + 1];
friendArray[counter + 1] = temp;
swap = true;
}
}//close for loop
}while(swap);
}//end of function

//display names function

void displayNames(string friendArray[], const int ARRAY_SIZE)
{
for(int index = 0; index < ARRAY_SIZE; index++)
cout<<friendArray[index]<<endl;
}

//binary search function
int searchName(string friendArray[], const int ARRAY_SIZE, string result)
{
int first = 0;
int last = (ARRAY_SIZE - 1);
int middle;
int position = -1;
string name;
bool found = false;

cout<<"Please enter a name or END to quit.";
cin>>name;

while(!found && first <= last && name != "END")
{
middle = (first + last) / 2;

if(friendArray[middle] == result)
{
found = true;
position = middle;
}
else if(friendArray[middle] > result)
last = (middle - 1);
else
first = (middle + 1);
}
return position;
}

最佳答案

Rup 是正确的。尝试将您的代码更改为

int friendIndex = searchName(friendArray, ARRAY_SIZE, result);
cout<<friendArray[friendIndex]<<" is my friend."<<endl;

我忘了你应该先检查 friendIndex == -1。这样会更好:

int friendIndex = searchName(friendArray, ARRAY_SIZE, result);
if(friendIndex >= 0)
cout<<friendArray[friendIndex]<<" is my friend."<<endl;
else
cout<<result<<" is not my friend."<<endl;

关于c++ - 为什么我的二进制搜索没有返回要显示的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com