【问题标题】:Binary search function using strings not getting implemented使用未实现的字符串的二进制搜索功能
【发布时间】:2021-10-01 02:27:34
【问题描述】:
#include <iostream>
#include<cstring>
using namespace std;

struct books
{
    private:
    string name, author;
    int price, copies;

    public:

    void input()
    {
        cout << "\nBook name: ";
        cin >> name;
        cout << "Author name: ";
        cin >> author;
        cout << "Price: ";
        cin >> price;
        cout << "No. of copies: ";
        cin >> copies;
    }
    void display()
    {
        cout << "\nBook name: "<< name;
        cout << "\nAuthor name: "<< author;
        cout << "\nPrice: "<< price;
        cout << "\nNo. of copies: "<< copies;
    }

     void search(string b_name)
    {
        
                if(name.compare(b_name) == 0)
                {
                    cout << "Yes, book is available" << endl;
                    cout << "Number of copies available: " << copies;
                }
    }


    void sort_by_name(books b[], int n)
    {
        int temp;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                books t;
                if(b[i].author > b[j].author)
                {
                    t = b[i];
                    b[i] = b[j];
                    b[j] = t;
                }
            }
        }
         cout<<"\n Authors names are sorted in order : \n\n";
        for(int i=0; i<5; i++)
        {
                cout<<" ";
                cout<<b[i].author<<"\n";
        }
    }

   int search_by_author_name(books b[], string key)
{
    
    int lower = 0, upper = author.length();

    for(int i = 0; i <= upper ; i++)
    {
    while ( lower <= upper) {

        int mid = (lower + (upper - lower)) / 2;
        if (b[mid].author == key)
            return mid;
        else if (b[mid].author < key)
            lower = mid + 1;
        else
            upper = mid - 1;
    }   
    }
    return -1;

}
    
};
int main()
{
    int num;
    string book_name, author_name;
    cout << "Number of details you want to keep"<<endl;
    cin >> num;
    books b[num];
    cout << "Enter book details:\n" ;
    for(int i = 0; i < num; i++)
        b[i].input();

    cout << "\nDetails of the book are: "<<endl;
    for(int i = 0; i < num; i++)
    {
    
    cout<<" \n ";
    
    b[i].display();
    }
    cout << "\nEnter book to be searched: " <<endl;
    cin >> book_name;
    
    for(int i = 0; i < num; i++)
    b[i].search(book_name);

    b[0].sort_by_name(b,num);

    cout << "Enter the author name to be searched: "<<endl;
    cin >> author_name;

    int result = b[0].search_by_author_name(b, author_name);
    if(result == -1)
    {
    cout << "The name searched is present " <<endl;
    }
    else
    cout << "The author name is not found: "<<endl;
    
}
    

程序被编译但是由于某种原因我的程序没有运行二进制搜索功能。我使用 int search_by_author_name() 函数来执行此操作,我认为逻辑也很合适,但我似乎无法找到代码有什么问题。是函数调用错误还是代码本身有问题。请帮我解决这个问题

【问题讨论】:

  • books 不是一个好的类名,因为它只存储关于一本书的信息。此外,VLA:s (books b[num];) 是一个非标准扩展,只有某些编译器支持。请改用std::vector&lt;books&gt; b(num);
  • author.length() 不是数组中的元素个数。

标签: c++ string struct binary-search


【解决方案1】:

代码本身有问题。你的二分搜索函数应该是这样的:

// n - number of elements in b[]
int search_by_author_name(books b[], int n, string key)
{
    int lower = 0, upper = n - 1;

    while (lower <= upper)
    {
        int mid = (lower + upper) / 2;
        if (b[mid].author == key)
            return mid;
        else if (b[mid].author < key)
            lower = mid + 1;
        else
            upper = mid;
    }
    return -1;
}

另外我相信你的最后一个条件应该是result != -1,因为-1 意味着没有这样的作者。 我希望这会有所帮助

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2019-02-20
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多