【问题标题】:How to write a CompareTo method to compare string values in binary search algorithm如何编写一个 CompareTo 方法来比较二进制搜索算法中的字符串值
【发布时间】:2020-06-04 19:33:37
【问题描述】:

EDITED:(编辑二分查找法) 我正在使用升c。我有一个排序列表,其中包含一些值,例如名称及其数量。

我的代码:

using System;
using System.Collections.Generic;
using static System.Console;
namespace InventoryManagementSystem
{
    public class Tool
    {


            public Tool(string name, int quantity)
            {
                this.Name = name;
                this.Quantity = quantity;
            }

            public string Name { get; }
            public int Quantity { get; }


        public  int CompareTo(SortedList<string, Tool> other, String name)
        {
            return; //not sure how to write this properly
        }
    }
}

//工具类

 public class ToolItems

    {


        SortedList<string, Tool> gardeningTools = new SortedList<string, Tool>();

 public void gardeningToolData()
        {
            Tool gardeningTool1 = new Tool("Garden gloves", 50);
            gardeningTools.Add(gardeningTool1.Name, gardeningTool1);

                    WriteLine("Please enter a name of the tool");
                    String user = ReadLine();
                    int number = binarySearch(user);
}

        public int binarySearch(String user)
        {
            while (low <= high)
            {
                mid = (high + low) / 2;
                if (gardeningTools.Values[high].Name.CompareTo(user) == 0)
                {
                    WriteLine("mid is: " + mid);
                    return mid;
                }
                else if (gardeningTools.Values[high].Name.CompareTo(user) > 0)
                    high = mid - 1;
                else
                    low = mid + 1;

            }
            return -1;

        }

已编辑:我的代码现在可以正常工作,但如果值介于列表的第一个和最后一个索引之间,它会返回索引 0。如何根据用户搜索的名称从排序列表中获取确切的索引?

简而言之,我要做的就是当用户输入一些字符串值并且如果它在排序列表中匹配(我们使用二进制搜索算法搜索)我们从排序列表中获取该值的索引并更新该值的数量具体值(如果用户选择'A'并且它位于索引0(“A”,5)那么我们询问用户你想借多少A并且用户说2,所以数量现在更新为3。

【问题讨论】:

  • if (gardeningTools.Values[high].Name.CompareTo(user) == 0) string 类CompareTo 方法?
  • 我可以试试,但请告诉我如何编写 CompareTo() 方法?所以它实际上可以比较这两个值
  • 能否告诉我如何使用字符串类 CompareTo?
  • 不。因为编写一个将SortedList&lt;string, Tool&gt;string 进行比较的CompareTo 方法没有任何意义(无论如何对我而言)。
  • 我在第一条评论中给了你一个例子。基本上是Name.CompareTo(user)

标签: c# list binary-search compareto sortedlist


【解决方案1】:

SortedList 正在实现 IDictionary。基本上,您的排序列表将具有 IDictionary 具有的方法。您可以查看此链接Microsoft Docs SortedList out。

在您的问题中,我的理解是您想从 SortedList 中找到您的 Tool 对象并更新其数量。我不是直接回答你关于如何编写CompareTo 方法的问题,而是提出如何实现实际目标的替代解决方案。请在下面找到示例代码:

对您的 Tool 类稍作更改,将 setter 添加到 Quantity 属性并删除您的 CompareTo 方法:

public class Tool
{
    public Tool(string name, int quantity)
    {
        this.Name = name;
        this.Quantity = quantity;
    }

    public string Name { get; }
    public int Quantity { get; set; }
}

这里是您的控制台应用程序部分如何通过用户输入查找和更新数量:

SortedList<string, Tool> gardeningTools = new SortedList<string, Tool>();

public void gardeningToolData()
{
    var gardeningTool = new Tool("Garden gloves", 50);
    gardeningTools.Add(gardeningTool.Name, gardeningTool);

    Console.WriteLine("Please enter a name of the tool");
    var toolName = Console.ReadLine();

    Console.WriteLine("Please enter quantity of tools");
    var quantity = int.Parse(Console.ReadLine()); // Ideally you should check whether user enters valid integer

    if(gardeningTools.TryGetValue(toolName, out var tool))
    {
        // You should check here whether you have sufficient quantity before decreasing quantity
        tool.Quantity -= quantity;
    }
}

【讨论】:

  • 啊,这不是作业。我只是想了解二进制搜索如何在内部工作,但无法弄清楚。我只是想学习工具系统来借用和东西。如果您能帮助我理解,我将不胜感激。谢谢
【解决方案2】:

更新

我已经对此进行了修改,因此我也在回答如何实现 compareto。

public class Tool : IComparable<Tool>, IComparable<string>
{
    public Tool(string name, int quantity)
    {
        this.Name = name;
        this.Quantity = quantity;
    }

    public string Name { get; }
    public int Quantity { get; }

    public int CompareTo(Tool other)
    {

        // You could manually compare rather than this
        // return this.Name == other?.Name ? 0 : this.Name < other?.Name ? -1 : 1;
        return string.Compare(this.Name, other?.Name);
    }


    public int CompareTo(string other) {
      return string.Compare(this.Name, other);
    }
}

public int binarySearch(String user)
    {
        while (low <= high)
        {
            mid = (high + low) / 2;
            switch(gardeningTools.Values[mid].CompareTo(user)) {
               case 0: // Found
                 WriteLine("mid is: " + mid);
                 return mid;
               case -1: // Mid is too small.
                 low = mid + 1;
                 break;
               default: // Mid is too high
                 high = mid - 1;
                 break;
            }
        // probably should also check that high and low != at this point
        return -1;

    }

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多