问题描述
基本上,此程序的目的是从文件中读取多达100个名称,使用bubblesort排序,然后通过二分搜索搜索输入的名称。
所有似乎都工作,除非当我输入一个名称,在列表中,没有什么发生,我只是提示输入一个名称。
在Elvis Presley的名单中说出名字。我被提示输入一个名称。我输入Elvis Presley。我应该收到埃尔维斯·普雷斯利是你的朋友。不发生。任何帮助赞赏。
#include< iostream>
#include< fstream>
#include< string>
using namespace std;
void bubblesort(string [],const int);
void search(string [],const int);
int sub = 0;
int main()
{
const int maxsize = 100;
string friendArray [maxsize];
ifstream friends;
friends.open(myFriends.dat);
while(sub< maxsize&& getline(friends,friendArray [sub]))
sub ++;
bubblesort(friendArray,sub);
search(friendArray,maxsize);
系统(暂停);
return 0;
}
void bubblesort(string * array,const int size)
{
bool swap;
string temp;
do
{
swap = false;
for(int count = 1; count<(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 search(string * array,int size)
{
int first = 0;
int last = size - 1;
int middle;
string name;
bool friends = false;
do
{
cout<<请输入名称或END以终止:;
cin>> name;
}
while(!friends&&& first< = last&& name!=END);
{
middle =(first + last)/ 2;
if(array [middle] == name)
{
friends = true;
cout<< array [middle]<<是我的朋友。<<< endl;
}
else if(array [middle]> name)
last = middle - 1;
else
last = middle + 1;
}
}
search()
函数, do {} while; {}
构造有缺陷。它会编译,但它不会做你想要的。我做了一些更改,并重新安排你的代码,所以它更有意义。 void search(string * array,int size)
{
字符串名称;
for(;;)
{
cout<<请输入名称或END以终止:;
getline(cin,name);
if(name ==END)break;
int first = 0;
int last = size - 1;
bool friends = false;
while(!friends&& first< = last)
{
int middle =(first + last)/ 2;
if(array [middle] == name)
{
friends = true;
cout<< array [middle]<<是我的朋友。<<< endl;
}
else if(array [middle]> name)
last = middle - 1;
else
first = middle + 1;
}
}
}
int main()//测试search()函数
{
string array [] = { bird,cat,dog,horse,loon,zebra};
search(array,6);
}
我应该删除吗?
Basically the purpose of this program is to read up to 100 names from file, sort with a bubblesort, and then search for a entered name by binary search.
All seems to be working except when I enter a name that is in the list, nothing happens, I'm just prompted to enter a name again.
Say a name in the list in Elvis Presley. I am prompted to enter a name. I type in Elvis Presley. I SHOULD recieve Elvis Presley is your friend. Not happening. Any help appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void bubblesort(string[], const int);
void search(string[], const int);
int sub = 0;
int main()
{
const int maxsize = 100;
string friendArray[maxsize];
ifstream friends;
friends.open("myFriends.dat");
while (sub < maxsize && getline(friends, friendArray[sub]))
sub++;
bubblesort(friendArray, sub);
search(friendArray, maxsize);
system("pause");
return 0;
}
void bubblesort(string *array, const int size)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 1; count < (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 search(string *array, int size)
{
int first = 0;
int last = 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;
}
}
In your search()
function, the do { } while; { }
construct is flawed. It will compile but it doesn't do what you want. I made a few changes and rearranged your code so it makes more sense.
void search(string *array, int size)
{
string name;
for (;;)
{
cout<<"Please enter a name or END to terminate:";
getline(cin, name);
if (name == "END") break;
int first = 0;
int last = size - 1;
bool friends = false;
while (!friends && first <= last)
{
int 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;
}
}
}
int main () // test the search() function
{
string array[] = { "bird", "cat", "dog", "horse", "loon", "zebra" };
search(array, 6);
}
SO, is this too much homework help? Should I delete this?
这篇关于搜索字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!