【问题标题】:Linear Search Using Text File C# [duplicate]使用文本文件 C# 进行线性搜索 [重复]
【发布时间】:2017-05-03 20:16:06
【问题描述】:

所以我有一个文本文件,我想搜索一个数字,如果找到,则输出一个消息号,如果没有,则找不到。

目前我已将文本文件读入一个字符串,并询问用户他们想要搜索的数字。我坚持的部分是线性搜索字符串的代码。我想查找一个字符串,因为我将使用的其他文件可能包含单词。

到目前为止的代码:

    public static void search()   // Search for values
    {
        Console.WriteLine("Enter 1 to search through days");
        int operation = Convert.ToInt32(Console.ReadLine());

        if (operation == 1)
        {

            String[] myArray = File.ReadAllLines("Data1/Day_1.txt");

            Console.WriteLine("Enter number to search");
            String myString = Console.ReadLine();
        }
    }

我看到了一些用于线性搜索的代码,但不确定如何将其正确应用到我的示例中。我了解它是如何工作的,但问题在于以正确的顺序获取代码。不太擅长用c#编码。任何帮助将非常感激。谢谢

通用线性搜索代码:

static int Search(string[] list, string elementSought)
{
bool found = false;
int max = list.Length - 1;
int currentElement = 0;

do
{
    if (list[currentElement] == elementSought)
    {
        found = true;
    }
    else
    {
        currentElement = currentElement + 1;
    }
} while (!(found == true || currentElement > max));

if (found == true)
{
    return currentElement;
}
else
{
    return -1;
}
}

更新:

   public static void search()   // Search for values
    {
  Console.WriteLine("1=Day 2=Depth");
  int operation = Convert.ToInt32(Console.ReadLine());
  if (operation == 1)
        {
            String[] myArray = File.ReadAllLines("Data1/Day_1.txt");
        }

        else if (operation == 2)
        {
            String[] myArray = File.ReadAllLines("Data1/Months_1.txt");
        }


            Console.WriteLine("Enter number to search");
            String myString = Console.ReadLine();
            int i = 0;
            int j = 0;
            var regex = new Regex(myString);
            foreach (string array in myArray)
            {
                if (regex.IsMatch(array))
                {
                    i++;
                }

                else if (regex.IsMatch(array))
                {
                    j++; 
                }
            }

            if (i > 0)
            {
                Console.WriteLine("Found match! - {1} Appeared {0} time(s)",i,myString);
            }

            else if(j == 0)
            {
                Console.WriteLine("No Match for {0} in Data", myString);
            }

            Console.ReadLine();
        }

如何更改它,以便如果我选择第二个文件,它会选择它作为我的数组使用的文件?

【问题讨论】:

    标签: c# linear-search


    【解决方案1】:

    您可以使用Array.IndexOf。如果在数组中没有找到该项目,它将返回 -1,否则返回其第一次出现的索引。

    示例(控制台应用)

        static void Main(string[] args)
        {
            string[] list = {"xxx", "yyy", "aaa", "bbb"};
            var found = Search(list, "gggg");
        }
    
        static bool Search(string[] list, string elementSought)
        {
            var index = Array.IndexOf(list, elementSought);
    
            var found = index != -1;
    
            return found;
        }
    

    【讨论】:

      【解决方案2】:

      您手头的任务过于复杂,只需使用 for 循环进行线性搜索或使用 Linq

      static int Search(string[] list, string elementSought)
      {
          return list.ToList().FindIndex(s => s == elementSought);
      }
      

      【讨论】:

      • 你怎么知道你在哪一行找到了它?... :-)
      • @Juan 编辑了答案以适应它。
      • @Juan OP 没有提到这一点。只提到了数组的线性搜索。与第 #s 行无关。
      • @WilliamXifaras:是的,它没有提到这一点。但是,这是一种搜索,如果您查看原始算法,它会返回行号。
      【解决方案3】:

      您需要遍历字符串数组以寻找匹配项。使用正则表达式检查匹配项。

      Console.WriteLine("Enter number to search");
      String myString = Console.ReadLine();
      
      var regex = new Regex(myString);
      foreach (string array in myArray)
          if (regex.IsMatch(array))
              Console.WriteLine("Found match!");
      

      【讨论】:

      • 得到一个错误 Regex could not be found
      • 添加您的使用语句“使用 System.Text.RegularExpressions;”在 .cs 文件的顶部。
      • 修好了没关系
      • 我怎样才能让它出现一次。或者如果它只是出现的次数而不是一次又一次地写出来?
      • 不要写找到匹配,而是增加一个变量并在foreach循环之后写入变量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多