数组、排序和二进制搜索
Arrays, sorting and binary searching
发现自己对当前的项目有点卡住了。我正在尝试将一个.txt文件(名字和姓氏分别用 1 行空格分隔)读入数组。在文件中读取后,这是应该发生的事情。
- 使用函数对数组进行排序(我选择了选择排序)
- 要求用户输入名称
- 使用函数执行二叉搜索
- 如果找到,则显示姓名是否为好友
对于其中的大部分,我都理解。该程序早些时候正在读取文件,我什至将其写出来以显示只是为了查看,即使它不是必需的。现在它只显示一个名称。寻找一点帮助让我再次动起来。如果我的屁股很痛,或者我的代码被抬高了,请道歉。感谢您的任何帮助。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Function prototypes
void nameSort(string[], const int);
//prototype to display the contents of the file after sorting
//Although I do not need this I wanted to see the names were
//read in correctly.
void nameDisplay(string[], const int);
void nameSearch(string[], const int);
int index = 0; //loop counter
int main()
{
const int ARRAY_SIZE = 100;
string nameArray[ARRAY_SIZE];
//file object
ifstream fileIn;
fileIn.open("myFriends.dat");
while (index < ARRAY_SIZE && getline(fileIn, nameArray[index]))
index++;
//function call to sort the array
nameSort(nameArray, index);
//Display names to screen as a test of the sort
nameDisplay(nameArray, ARRAY_SIZE);
//function call for search
nameSearch(nameArray, ARRAY_SIZE);
system("pause");
return 0;
}
//function definitions
void nameSort(string *array, const int ARRAY_SIZE)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 1; count < (ARRAY_SIZE - 1); count++)
{
if(array[count-1] >array[count])
{
temp = array[count-1];
array[count-1] = array[count];
array[count] = temp;
swap = true;
}
}
}
while(swap);
}
void nameSearch(string *array, const int ARRAY_SIZE)
{
int first = 0;
int last = ARRAY_SIZE - 1;
int middle;
string name;
bool friends = false;
do
{
cout<<"Please enter a name or END to terminate:";
cin>>name;
}
while(!friends && first <= last && name != "END");
{
middle = (first + last) / 2;
if (array[middle] == name)
{
friends = true;
cout<<array[middle]<<" is my friend."<<endl;
}
else if (array[middle] > name)
last = middle - 1;
else
last = middle + 1;
}
}
void nameDisplay(string *array, const int ARRAY_SIZE)
{
for(int index = 0; index < ARRAY_SIZE; index++)
cout<<array[index]<<endl;
}
你的名字搜索似乎至少有一个错误。而不是:
while(!friends && first <= last && name != "END");
{
middle = (first + last) / 2;
if (array[middle] == name)
{
friends = true;
cout<<array[middle]<<" is my friend."<<endl;
}
else if (array[middle] > name)
last = middle - 1;
else
last = middle + 1;
}
应该有:
while(!friends && first <= last && name != "END");
{
middle = (first + last) / 2;
if (array[middle] == name)
{
friends = true;
cout<<array[middle]<<" is my friend."<<endl;
}
else if (array[middle] > name)
last = middle - 1;
else
first = middle + 1;
}
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- 我的二进制搜索程序只是关闭了
- 二进制搜索的终止点
- 递归二进制搜索与字符串数组
- 测试树是否为二进制搜索树
- 如何二进制搜索结构向量并在适当的索引处插入
- 如果你有一个固定大小的数组,你需要遍历它!n次,使用二进制搜索如何改变时间复杂性
- 字符串的递归二进制搜索-C++
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 二进制搜索树没有匹配的函数调用
- 二进制搜索不适用于特定输出
- 如何在向量上进行二进制搜索以找到具有特定id的元素
- 二进制搜索树不变的标头文件
- 二进制搜索树插入带参数参考
- 二进制搜索不变以查找元素的首次出现
- 了解一种神秘地起作用的递归二进制搜索算法