C# 如何在二进制搜索算法中编写比较方法来比较字符串值
编辑:(编辑二进制搜索方法) 我用的是c夏普。我有一个排序列表,其中有一些值,比如名称和数量 我的代码:C# 如何在二进制搜索算法中编写比较方法来比较字符串值,c#,list,binary-search,compareto,sortedlist,C#,List,Binary Search,Compareto,Sortedlist,编辑:(编辑二进制搜索方法) 我用的是c夏普。我有一个排序列表,其中有一些值,比如名称和数量 我的代码: using System; using System.Collections.Generic; using static System.Console; namespace InventoryManagementSystem { public class Tool { public Tool(string name, int quantity)
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
}
}
}
使用系统;
使用System.Collections.Generic;
使用静态系统控制台;
命名空间目录管理系统
{
公共类工具
{
公共工具(字符串名称、整数数量)
{
this.Name=Name;
这个。数量=数量;
}
公共字符串名称{get;}
公共整数数量{get;}
公共整数比较(SortedList other,字符串名称)
{
return;//不确定如何正确写入
}
}
}
//工具项类
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;
}
公共类工具项
{
SortedList GardingTools=新的SortedList();
public void gardingtooldata()
{
工具园艺工具1=新工具(“园艺手套”,50);
添加(gardingtool1.Name,gardingtool1);
WriteLine(“请输入工具名称”);
字符串user=ReadLine();
int number=二进制搜索(用户);
}
公共int二进制搜索(字符串用户)
{
while(低0)
高=中-1;
其他的
低=中+1;
}
返回-1;
}
已编辑:我的代码现在可以工作,但如果值介于列表的第一个索引和最后一个索引之间,它将返回索引0。如何根据用户搜索的名称从sortedlist中获取准确的索引
简言之,我所要做的就是当用户输入一些字符串值时,如果它在sortedlist中匹配(我们使用二进制搜索算法进行搜索),我们将从排序列表中获取该值的索引,并更新该特定值的数量(如果用户选择“A”,并且它位于索引0(“A”,5)然后我们向用户询问您想借多少
A
,用户说2,因此数量现在更新为3。SortedList正在实现IDictionary。基本上,您的排序列表将具有IDictionary所具有的方法。您可以签出此链接
在您的问题中,我的理解是您希望从SortedList中找到您的工具对象并更新其数量。我不是直接回答您关于如何编写CompareTo
方法的问题,而是提出如何实现实际目标的替代解决方案。请查看以下示例代码:
您的工具
类稍有改动,将setter添加到数量
属性中,并删除了比较方法
:
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;
}
}
SortedList GardingTools=新建SortedList();
public void gardingtooldata()
{
var gardingtool=新工具(“园艺手套”,50);
添加(gardingtool.Name,gardingtool);
Console.WriteLine(“请输入工具名称”);
var toolName=Console.ReadLine();
Console.WriteLine(“请输入工具数量”);
var quantity=int.Parse(Console.ReadLine());//理想情况下,您应该检查用户是否输入了有效的整数
if(GardingTools.TryGetValue(工具名称,输出变量工具))
{
//在减少数量之前,您应该在这里检查您是否有足够的数量
工具数量-=数量;
}
}
SortedList正在实现IDictionary。基本上,您的排序列表将具有IDictionary所具有的方法。您可以签出此链接
在您的问题中,我的理解是您希望从SortedList中找到您的工具对象并更新其数量。我不是直接回答您关于如何编写CompareTo
方法的问题,而是提出如何实现实际目标的替代解决方案。请查看以下示例代码:
您的工具
类稍有改动,将setter添加到数量
属性中,并删除了比较方法
:
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;
}
}
SortedList GardingTools=新建SortedList();
public void gardingtooldata()
{
var gardingtool=新工具(“园艺手套”,50);
添加(gardingtool.Name,gardingtool);
Console.WriteLine(“请输入工具名称”);
var toolName=Console.ReadLine();
Console.WriteLine(“请输入工具数量”);
var quantity=int.Parse(Console.ReadLine());//理想情况下,您应该检查用户是否输入了有效的整数
if(GardingTools.TryGetValue(工具名称,输出变量工具))
{
//在减少数量之前,您应该在这里检查您是否有足够的数量
工具数量-=数量;
}
}
更新
我对其进行了修改,以便回答如何实现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;
}
公共类工具:IComparable,IComparable
{
公共工具(字符串名称、整数数量)
{
this.Name=Name;
这个。数量=数量;
}
公共字符串名称{get;}
公共整数数量{get;}
公共int比较(工具其他)
{
//您可以手动进行比较,而不是这样
//返回this.Name==other?.Name?0:this.Name 而(低更新
我对其进行了修改,以便回答如何实现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;
}
公共类工具:IComparable,IComparable
{
公共工具(字符串名称、整数数量)
{
this.Name=Name;
这个。数量=数量;
}
公共字符串名称{get;}
公共整数数量{get;}
公共内部比较(工具o