在Visual Basic中执行线性搜索 [英] Performing a Linear Search in Visual Basic

查看:65
本文介绍了在Visual Basic中执行线性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行线性搜索,该线性搜索遍历数组以在Visual Basic中查找元素(由用户输入并在我的算法中称为"SearchItem").用户可以在两个并行的数组中输入名称和相应的编号.然后,用户输入一个数字,然后搜索数字数组以查看该数字是否位于此处.我创建了一个基本算法,如下所示:

I am trying to perform a linear search which loops through an array to find an element (which is entered by the user and is referred to as ''SearchItem'' in my algorithm) in Visual Basic. The user can enter a name and corresponding number into two parralel separate arrays. The user then enters a number and the number array is searched to see if that number lies there. I have created a basic algorithm which goes as follows:

BEGIN
	Set Index to 0
	Prompt User for ‘SearchItem’
	Get ‘Num’
	Found = False
	While Index < 7 and Found = False
			IF ‘SearchItem’ = ‘Num’ THEN
				Found = True
			ELSE
				Found = False
			ENDIF
			Increment Index
	ENDWHILE
	IF Found = True THEN
			Display "Match Found – Num’" 
	ELSE
			Display "Match not Found"
END


我以前从未执行过线性搜索,而且我不确定如何将搜索项与数组中的值进行比较.任何帮助将不胜感激,因为我没有执行这些搜索的经验.到目前为止,我拥有的代码是:


I have never performed a linear search before and I am really unsure of how to compare the search item with the values in the array. Any help would be appreciated as I have have no experience with performing these searches. So far the code I have goes:

Public Class Form1
    Dim Name(7) As String
    Dim Number(7) As Integer
    Dim Index1 As Integer
    Dim LastIndex1 As Double
    Dim SearchItem As Integer
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        If txtName.Text = "" Then
            MsgBox("Enter a name")
        ElseIf txtNumber.Text = "" Then
            MsgBox("Enter a Number")
        ElseIf txtNumber.Text = "" And txtName.Text = "" Then
            MsgBox("Enter a name and corresponding number")
        ElseIf Index1 = 7 Then
            MsgBox("Maximum data entered, reset for additional input")
            txtName.Clear()
            txtNumber.Clear()
        Else

            Index1 = Index1 + 1

            Name(Index1) = txtName.Text
            Double.TryParse(txtNumber.Text, Number(Index1))


            For i = LastIndex1 + 1 To Index1
                NameArray.Items.Add(Name(i))
                    NumberArray.Items.Add(Number(i))
            Next i
            LastIndex1 = Index1
        End If
        txtName.Clear()
        txtNumber.Clear()

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        SearchItem = txtSearchItem.Text
    End Sub
End Class


===========

我确实需要有关搜索按钮的帮助,该按钮可以检查搜索项的数字数组.如果找到该项目,则会弹出一个消息框,提示匹配-数字",如果不是,则消息框将显示找不到".


===========

I really need help with the search button that checks the number array for the search item. If the item is found a msgbox will pop up saying "Match Found - Number" and if it isnt, then the msgbox will say "Not Found".

推荐答案

否在您的逻辑中发现缺陷.

我通常避免处理Found标志,例如:
No flaw spotted in your logics.

I use to avoid handling a Found flag, like this:
WHILE Index < 7 and  SearchItem <> Num
  Increment Index
ENDWHILE

IF Index < 7 THEN
  Display "Match Found – Num" 
ELSE
  Display "Match not Found"
END


这篇关于在Visual Basic中执行线性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆