2010-07-19 67 views
0

问题是我首先必须将二维转换为一维,然后找到选定的索引,然后再次转换,然后保存新的索引。之后,我必须使用正确的string.format来显示正确的输出..我只是困惑=(如何将双暗阵列转换为单暗?

在程序中有两个文本框要求“行”和“col”的大小,然后你按下按钮和一个列表框显示

............................................................................................................................... 
Row...Col....................................................................................................................................... 
1------1----Vacant ........................................................................ 
1......2.......Vacant................................................................................................... 
1......3.......Vacant.................................................................................................................................... 
2------1----Vacant......................................................................................................................... 
2......2.......Vacant.................................................................................................................................... 
2......3.......Vacant.......................................................................................................................................................... 
3------1----Vacant.................................................................................................................................... 
etc............................................................................................................................................................. 

,当我双击一行它说“保留” 有人可以帮助我该怎么做,请?

这是我的项目文件夹到目前为止,在这里你可以看到我的failed codethis is my assignment

这是程序应该是怎样的样子到底 http://i29.tinypic.com/risq6r.jpg

'Created by: Hans Elias Juneby 
'Started: July 10, 2010 Completed July 0, 2010 

Public Class MainFrame 
'''<summary> 
''' Enum used for choices of showing results 
''' </summary> 
'''<remarks></remarks> 
Private Enum UpdateOptions 
    ShowAllSeats    'reserved and vacant seats 
    ShowOnlyVacantSeats  'Only vacant seats 
    ShowOnlyReservedSeats  'Only reserved seats 
End Enum 

'Instance variables 
Private bookingObj As SeatBooking = New SeatBooking() 
Private showOptions As UpdateOptions = UpdateOptions.ShowAllSeats 

'''<summary> 
''' Default constructor 
''' Initialize components and do other preparations. 
''' This method is called automatically before the form is made visible 
''' </summary> 
''' <remarks></remarks> 
Public Sub New() 

    ' This call is required by the Windows Form Designer. 
    InitializeComponent() 

    ' My initialization 
    InitializeControlvalues() 
    UpdateResults() 
End Sub 

'''<summary> 
''' Organize initiations 
''' Fill comboboxes with options (strings), set default values, etc 
''' </summary> 
'''<remarks></remarks> 
Private Sub InitializeControlvalues() 
    FillUnitTypeList() 
    lstUnits.SelectedIndex = -1 
    cmbShowOptions.Items.AddRange([Enum].GetNames(GetType(UpdateOptions))) 
    cmbShowOptions.SelectedIndex = 0 
    rbtnLetters.Checked = True 
    ' txtNumOfSeats.Text = bookingObj.GetMaxCols.ToString() 'default values 
    ' txtNumOfRows.Text = bookingObj.GetMaxRows.ToString() 'default values 
    lblNumVacants.Text = String.Empty 'default values 
    lblTotal.Text = String.Empty 'default values 
    lblPercent.Text = String.Empty 'default values 
End Sub 

''' <summary> 
''' Clear listbox, format new strings with the help of the bookingObj and 
'''fill in the box. 
''' </summary> 
''' <remarks></remarks> 
Private Sub UpdateResults() 

End Sub 

'''<summary> 
''' Helper function that returns a string containing a string "** Reserved **" or 
'''"Vacant" or no text according the value in showOptions. THe UpdateResults 
''' calls this function in detecting which string to show in the listbox 
''' </summary> 
''' <param name="row">Input</param> 
''' <param name="col">Input</param> 
''' <returns>A formatted string as explained above</returns> 
'''<remarks></remarks> 
Private Function GetReservationStatusString() 

    Select Case (cmbShowOptions.SelectedIndex) 

    Case 0 

    Case 1 

    Case 2 

    End Select 

End Function 

'Fills values in the combobox 
Private Sub FillUnitTypeList() 
    Dim units As String() = {"Bus Seats", "Train Seats", "Plane Seats"} 
    cmbUnitType.Items.AddRange(units) 
    cmbUnitType.SelectedIndex = 0 

End Sub 

Private Sub cmbShowOptions_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.GC) 
    showOptions = DirectCast(cmbShowOptions.SelectedIndex, UpdateOptions) 
    UpdateResults() 
End Sub 


    Private Sub btnMatrix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMatrix.Click 
    Dim SeatBooking As New SeatBooking 

    bookingObj.GetMaxRows.ToString() 
    bookingObj.GetMaxCols.ToString() 

    lblTotal.Text = txtNumOfRows.Text * txtNumOfSeats.Text 
End Sub 


''' <summary> 
''' Event-handler for the double-click event. 
    ''' Reserve/cancel the seat chosen in the listbox (call bookingObj.SetRowAndCOlumnValue), 
''' only if the showOption is Show ALL; otherwise, give an error message and do nothing. 
''' </summary> 
''' <param name="sender">sender-object from the caller</param> 
''' <param name="e">EventArgs object from the caller</param> 
''' <remarks></remarks> 
Private Sub lstUnits_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUnits.DoubleClick 

    lblNumVacants.Text = lblTotal.Text - 1 
    Return 
End Sub 

''' <summary> 
''' Event-handler method for change in the radiobutton 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub rbtnLetters_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnLetters.CheckedChanged 
    UpdateResults() 
    Dim A As Integer = 65 
    Dim myChar As Char 
    Dim col As Integer = 0 
    myChar = Convert.ToChar((A + col)) 

End Sub 

Private Sub UpdateUnit() 
    'Dim strText As String = bookingObj.GetUnitName(cmbUnitType.SelectedIndex) 
    ' lblVacantUnits.Text 
End Sub 
End Class 

那么SeatBook类:

'Created by: Hans Elias Juneby 
'Started: July 10, 2010 Completed July 0, 2010 

Public Class SeatBooking 

Private array As Double() 'Declaration of the array 
Private lastIndex As Integer = 0 'last filled position (lastIndex+1 = number of items 

'''<summary> 
''' Create (or recreate) the matrix with the given size 
''' </summary> 
'''<param name="rowSize">Number of rows</param> 
''' <param name="colSize">Number of columns</param> 
'''<remarks></remarks> 
Public Sub CreateArray(ByVal rowSize As Integer, ByVal colSize As Integer) 
    Debug.Assert(rowSize >= 0)  'Program execution halts here in case size is <0; 
    array = New Double(rowSize - 1) {} 
    Debug.Assert(colSize >= 0)  'Program execution halts here in case size is <0; 
    array = New Double(colSize - 1) {} 
    Dim seatMatrix As Boolean(,) 
    seatMatrix = New Boolean(rowSize, colSize) {} 



    End Sub 
'''<summary> 
''' Calculate the total number of elements(row*col) 
''' </summary> 
'''<returns>The total number of elements</returns> 
'''<remarks></remarks> 
Public Function GetMaxCount() As Integer 

End Function 

'''<summary> 
''' Check if a seat in a specific row and column is reserved. 
''' </summary> 
'''<param name="row">given row</param> 
'''<param name="col">given col</param> 
'''<returns>True if the seat is reserved and false otherwise</returns> 
'''<remarks></remarks> 
Public Function IsReserved(ByVal row As Integer, ByVal col As Integer) 

End Function 

'''<summary> 
''' Make a new reservation or cancel an existing. This process is onde by 
''' reversing the boolean value in the given position in the matrix, from true to 
''' false (reverse the seat) or vice versa(Cancel the reservation) 
''' </summary> 
'''<param name="row">given row</param> 
'''<param name="col">given col</param> 
'''<remarks></remarks> 
Public Sub Reserve(ByVal row As Integer, ByVal col As Integer) 

End Sub 


'''<summary> 
''' Thenumber of rows in the Matrix 
''' </summary> 
''' <returns>The number of rows</returns> 
''' <remarks></remarks> 
Public Function GetMaxRows() As Integer 
    Dim colsize As Integer = MainFrame.txtNumOfSeats.Text 
    Dim rowsize As Integer = MainFrame.txtNumOfRows.Text 



    CreateArray(colsize, rowsize) 

    For i As Integer = 0 To array.Length - 1 

    MainFrame.lstUnits.Items.Add("----" & i & "---" & GetMaxCols.ToString.Length) 
    Next 
End Function 

'''<summary> 
''' Thenumber of columns in the Matrix 
''' </summary> 
''' <returns>The number of columns</returns> 
''' <remarks></remarks> 
Public Function GetMaxCols() As Integer 
    Dim colsize As Integer = MainFrame.txtNumOfSeats.Text 
    Dim rowsize As Integer = MainFrame.txtNumOfRows.Text 

    CreateArray(rowsize, colsize) 

    For h As Integer = 0 To array.Length - 1 

    Next 

End Function 

'''<summary> 
''' The method first finds the first vacant pos in the matrix (row, col) 
''' and then calls another method MatrixIndexToVectorialIndex that determines which 
''' position the element has if the matrix was rolled out into a one-dimensional 
''' array. In a 3x3 matrix, the element in position (1,1) has an index 4 in 
''' one-dimensional array. The method is useful when updating the listbox in the 
''' GUI which contains a one-dimensional array of strings. The method determines which 
''' position (row,col) in the matrix corresponds to an item (row) in the listbox. 
''' </summary> 
'''<returns>The index, considering the matrix as one long vector, to the first vacant 
''' position (the first False value). A value -1 is returned if no vacant element is 
''' found</returns> 
'''<remarks></remarks> 
Public Function GetFirstVacantPosition() As Integer 

End Function 

''' <summary> 
''' Determine a corresponding index for an element at (row,col) in a one-dimensional 
''' presentation of the matrix. Think of the matrix as beeing rolled out into a one-dim 
''' array. In a 3x3 matrix, the element in position (1,1) has an index 4 in 
''' one-dimensional array. 
'''  20 11 22 
'''  33 41 55 
'''  60 7 99 Consider value (1,1)=41 
''' The above matrix can now be represented as one dimensional array. This makes it 
''' easier to update the listbox in the GUI. 
'''  20 11 22 33 41 55 60 7 99 value(4)=41 
''' Index 0 1 2 3 4 5 6 7 8 
'''Hence, index (1,1) in the matrix corresponds to row 4 in the listbox (one-dim array) 
''' </summary> 
''' <param name="row"></param> 
''' <param name="col"></param> 
''' <returns>The new index as explained above</returns> 
''' <remarks></remarks> 
Public Function MatrixIndexToVectorIndex(ByVal row As Integer, ByVal col As Integer) 

End Function 

''' <summary> 
''' Determines the index in the matrix (row,col) that corresponds to a given 
''' index in a one-dim array (listbox). This method actually is reverse process of 
''' the method MatrixIndexToVectorIndex (see above). The parameter row contains 
''' the input, i.e. index of the element in a one-dim array. The results (row and col) 
''' are saved and sent back to the caller via the ref variables row,col. 
''' </summary> 
''' <param name="row">Input and output parameter</param> 
''' <param name="col">Output parameter</param> 
''' <remarks></remarks> 
Public Sub VectorIndexToMatrixIndex(ByRef row As Integer, ByRef col As Integer) 

End Sub 

''' <summary> 
''' This function receives an index in a one-dim array (e.g. listbox) and calls 
''' the method VectorIndexToMatrixIndex to fin the same position in the matrix. 
''' It then calls the function Reserve to either reserve or cancel a booking. 
''' (If False value is saved in the element, it reserves the seat by changing 
''' the value to True, and vice-versa). 
''' </summary> 
''' <param name="oneDimListIndex"></param> 
''' <remarks></remarks> 
Public Sub SetRowAndColumnValue(ByVal oneDimListIndex As Integer) 

End Sub 

''' <summary> 
''' Calculate the total number of the reserved seats, i.e. the total 
''' number of the True values in the matrix. 
''' </summary> 
''' <returns>Number of reserved seats</returns> 
''' <remarks></remarks> 
Public Function GetNumOfReservedSeats() As Integer 

End Function 

''' <summary> 
''' Calculate the total number of the vacant seats, i.e the total 
''' number of the False values in the matrix.   
''' </summary> 
''' <returns>number of vacant seats</returns> 
''' <remarks></remarks> 
Public Function GetNumOfVacantSeats() As Integer 

End Function 

''' <summary> 
''' Relation between the reserved seats and the total number of seats in percent. 
''' </summary> 
''' <returns>Percent of Reservation</returns> 
''' <remarks></remarks> 
Public Function PercentOfReservation() As Double 

End Function 
End Class 
+0

请张贴一些代码示例。这是vb.net,如果是的话,我会建议你添加一个vb.net标签 – 2010-07-19 21:49:44

+0

你能修复你的格式吗?我不知道其他任何人,但在我的1280 * 800屏幕上尝试绘图时根本不起作用。我建议你选择一个应用程序的屏幕截图来向我们展示你的意思,在paint(或gimp!)中绘制图片,或者使用一些StackOverflow的格式工具。 你也可以重新提出你的问题吗?为什么你需要转换,查找和再次转换? Ben说,发布你的代码将是一个好的开始。 – Stephen 2010-07-19 22:03:40

+0

但我的代码失败大声笑?好吧,我会尝试发布我的代码,但它很多=/ – HansEJC 2010-07-19 22:22:50

回答

1

个人而言,我宁愿用一个hash table完成这个任务[个人喜好对于速度],但由于您需要根据您的项目规格使用二维数组或矩阵,因此使用布尔类型的二维数组可以更容易地完成此操作。

Dim ReservationArray(NumRows - 1, NumColumns - 1) as Boolean 

这些值将默认为假,则表示座位是空的,并预留座位你要做的就是改变这个值设置为true

我基本做到了在同一个项目而回为一个疗程,我学习当时欢迎您通过编码查找并修改它适合您的需求

您可以从以下(Visual Studio 2008的项目)的链接下载我的项目

我们的项目之间的最大区别在于使用datagridview而不是列表框:)看看这是如何简化你的任务

Reservation_Project.zip

+0

P.S. 我知道我的项目几乎是您需要制作的确切程序,而是将其用作参考,而不是将其修改为您想要的内容:)如果您自己编写项目,您将学到更多,并了解其工作原理 - 即使一些'帮助' – 2010-07-20 01:09:06

+0

非常感谢你=) – HansEJC 2010-07-20 13:02:53

+0

快乐希望它可以帮助你 - 我承认在我的编码中没有评论,但我相信它不难读懂我如何把它放在一起 – 2010-07-20 18:10:07

0

如何改变对项目中的每一个新行的输出,如果我想每一个新的行包含额外的“------”的意思? 加上当你有Option Strict代码 Dim ColInt As Integer = Char.ConvertToUtf32(dgvwResults.Rows(e.RowIndex).Cells(“Column”)。Value,0) - 65 失败? 没能解决它。

亲切的问候

Geera