C++ 数组、排序和二进制搜索
我发现自己有点被当前的项目卡住了。我试图将一个.txt文件(名字和姓氏各用一行空格分隔)读入数组。在读取文件后,这是应该发生的C++ 数组、排序和二进制搜索,c++,C++,我发现自己有点被当前的项目卡住了。我试图将一个.txt文件(名字和姓氏各用一行空格分隔)读入数组。在读取文件后,这是应该发生的 使用函数对数组进行排序(我选择了选择排序) 要求用户输入名称 使用函数进行二进制搜索 如果找到,则显示名称是否为friend 大部分我都能理解。程序在前面读取了文件,我甚至把它写出来只是为了显示,尽管它不是必需的。现在它只显示一个名称。想找点帮助让我重新行动起来。如果我是个讨厌的人或者我的代码被提升了,我会道歉。谢谢你的帮助 #include <iostrea
- 使用函数对数组进行排序(我选择了选择排序)
- 要求用户输入名称
- 使用函数进行二进制搜索
- 如果找到,则显示名称是否为friend
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
void nameSort(字符串[],常量int);
//原型以显示排序后的文件内容
//虽然我不需要这个,但我想看看名字是什么
//正确读入。
void名称显示(字符串[],常量int);
void nameSearch(字符串[],常量int);
int指数=0//循环计数器
int main()
{
常量int数组_SIZE=100;
字符串名称数组[数组大小];
//文件对象
ifstreamfilein;
fileIn.open(“myFriends.dat”);
while(索引数组[count])
{
temp=数组[count-1];
数组[count-1]=数组[count];
数组[计数]=温度;
swap=true;
}
}
}
while(swap);
}
void nameSearch(字符串*数组,常量int数组大小)
{
int first=0;
int last=数组大小-1;
中间;
字符串名;
bool-friends=false;
做
{
coutname;
}
而(!friends&&first您的名称搜索似乎至少有一个错误。而不是:
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这看起来不对:string nameArray[ARRAY\u SIZE][ARRAY\u SIZE];
是否要使用二维来存储名字和姓氏?我认为这样更好:string nameArray[ARRAY\u SIZE][2]
理想情况下是的,我希望一个一维数组可以容纳名字和姓氏,比如Tom Jones,然后是下一行和另一个名字。程序最多可以容纳200个名字,所以我会这样写数组:string nameArray[array_SIZE][200]?对任何类型的二维数组的传统解释(在本例中使用string
是:string nameArray[NUMBER\u ROWS][NUMBER\u COLUMNS]
。我建议您声明nameArray[ARRAY\u SIZE][2]
。然后对于personI
,nameArray[I][0]
将是此人的姓氏,而nameArray[I][1]
将是此人的名字。经过7个多小时的工作,我发现从类中回顾一些旧的程序项目时使用了getline命令。这就是我现在的位置:但它没有正确返回搜索结果。在该部分工作了2个多小时。我现在感觉一文不值。当我能够完成不管你分配了什么任务,但老实说,我真的不明白你们每天是怎么做的。通常需要多长时间才能精通一门编程语言?衷心感谢你们在这方面的帮助。
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;
}