【问题标题】:No output for sorted and searched array排序和搜索的数组没有输出
【发布时间】:2012-10-29 21:39:23
【问题描述】:

我一直在研究我在编程的第一学期学习过的所有内容。我有一个期末考试,所以我一直在尝试编写示例程序,结合我所学的一切准备。下面的程序应该从文件中读取名称,通过冒泡搜索对其进行排序,然后提示用户输入一个名称,二分搜索将查找该名称并告诉您此人是否为朋友。

我的问题是,当我输入名称时,只会提示我再次输入名称。没有输出。

请记住,这里的所有内容大部分都是我到目前为止所学到的(所以我不知道如何使用向量、指针等)。

#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;
    }
 }

【问题讨论】:

  • Searching a array of strings 的可能重复项
  • 这也是我的问题,它已经纠正了一些问题,建议开始一个新问题。
  • 在你的排序函数中,for (int count = 1; count &lt; (size - 1); count++) 应该是for (int count = 1; count &lt; size; count++);
  • @jrok 这不会让我超出数组中的元素数量吗?
  • 不,count 会小于 size 并且你永远不会访问索引 [count+1],所以没关系。

标签: c++ arrays sorting binary-search


【解决方案1】:

我不想透露太多,因为这是家庭作业,所以我将移动你的一些代码,使其尽可能靠近,并告诉你为什么它目前不起作用。

目前你有点认为do...while 循环是某种双块,它不是。代码中while(...); 之后的代码只会执行一次,在你跳出do...while 循环之后,它就没有任何联系。您将需要两个循环,一个用于提示输入名称的外部循环,一个用于在列表中查找该名称的内部循环。

在要求用户输入另一个名称后,您也不会重置 friends 和 last。一个简单的解决方法是将您的声明(包含初始化)移动到第一个循环中。

这是您的代码在大部分重新排列并应用上述更改后的样子:

void search(string *array, int size)
{
   string name;

   cout<<"Please enter a name or END to terminate:";
   cin>>name;

   while (name != "END")
   {
      bool friends = false;
      int first = 0;
      int last = size - 1;
      int middle;

      while(!friends && first <= last)
      {
         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;
      }

      cout<<"Please enter another name or END to terminate:";
      cin>>name;
   }      
}

这次有两种不同的提示,所以如果用户输入"END",外部循环会立即终止,而不必在循环内添加额外的检查。

另外,和你的其他问题一样,search(friendArray, maxsize); 应该是search(friendArray, sub);,因为我上次告诉你的原因 - sub 是数组中有效项目的计数,maxsize 是数组的容量数组。

注意:如果您的列表中不存在该名称,则会导致无限循环。我会让你解决这个问题,因为这是家庭作业,我不想改变你的任何实际逻辑。一个提示是考虑实际发生的情况 - 如果值不存在,您只需在值应该存在的区域周围不断增加和减少 last。

也许如果您的逻辑合并了 first 在某处被修改,那么条件 first &lt;= last 将失败并且您将跳出循环...

【讨论】:

  • 谢谢伙计,但目前我的逻辑是行不通的,大脑是炸的,但我会定义的。明天读。
  • @sircrisp:不用担心。要详细说明该提示,也许如果您尝试将first 或last 设置为middle +- 1,具体取决于名称是&lt; 还是&gt; 当前元素。二分搜索背后的想法是每次缩小搜索区域,这是通过增加first的值和减少last的值来实现的。
【解决方案2】:

你的 do while 语句是错误的,它运行这个命令:

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;
}

修改:

do
{
    cout<<"Please enter a name or END to terminate:";
    cin>>name;
    first = 0;
    last = size - 1;
    middle=0;
    friends = false;
    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(name != "END");

【讨论】:

  • 我把 while 改成了 while(!friends && name != "END");虽然没有运气。我已经为此工作了 4 个小时,我无法再确定任何事情了,我的逻辑已经死了。
  • 感谢您的帮助,更改为您的代码后,我的结果有所不同但仍然不正确。我在列表中输入一个名称,然后什么也没有发生,没有按键继续,没有输入名称提示,什么也没有。我可以听到我的 CPU 风扇声音越来越大……啊
  • 如果没有找到搜索键,则不显示,所以在一段时间后如果朋友=假和姓名!=“END”写cout
  • 我已经这样做了,但问题和以前一样。编译时会发生什么?
  • @sircrisp:如果数组没有正确排序,可能会导致二分搜索无限循环。通过在排序后打印数组内容来验证您的bubblesort()。
猜你喜欢
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
相关资源
最近更新 更多