Создать структуру, содержащую название улицы и количество жильцов - Visual Basic .NET
Формулировка задачи:
Список содержит число жителей, проживающих в каждом доме на пяти улицах села.
1. Создать структуру, содержащую название улицы, номер дома, количество жильцов.
2. Получить отчет, содержащий два поля: название улицы и общее количество жильцов, проживающих на данной улице.
1 часть сделала а вот 2 не получается..
Решение задачи: «Создать структуру, содержащую название улицы и количество жильцов»
textual
Листинг программы
Public Class Form2
Private Structure Persons
Public street As String 'название улицы
Public number As String 'номер дома
Public population As Integer 'количество жителей
Public Sub New(ByVal st As String, ByVal nm As String, ByVal pp As Integer)
street = st
number = nm
population = pp
End Sub
End Structure
Private MyPerson As List(Of Persons)
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
MyPerson = New List(Of Persons)
Label4.Text = ""
TextBox4.Font = New Font(FontFamily.GenericMonospace, 8)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim s1 As String = TextBox1.Text
Dim s2 As String = TextBox2.Text
MyPerson.Add(New Persons(s1, s2, CInt(TextBox3.Text)))
Dim ss As String = ("Улица " & s1).PadRight(25) & ("№ дома " & s2).PadRight(15) & _
("Жильцов " & TextBox3.Text) & vbCrLf
TextBox4.Text &= ss
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
Label4.Text = "Записей: " & MyPerson.Count
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim sum As Integer
Dim myPrs() As Persons = sortMyPerson()
Dim w1 As String = "", w2 As String = myPrs(0).street
Dim rep As New List(Of Persons)
For i = 0 To myPrs.Length - 1
w1 = myPrs(i).street
If w1 = w2 Then
sum += myPrs(i).population
Else
rep.Add(New Persons(w2, "", sum))
w2 = w1
sum = 0
sum += myPrs(i).population
End If
Next
rep.Add(New Persons(w2, "", sum))
w1 = ""
For Each prs As Persons In rep
w1 &= prs.street & " = " & prs.population & vbCrLf
Next
MsgBox(w1)
End Sub
Private Function sortMyPerson() As Persons()
Dim str(MyPerson.Count - 1) As String
For i = 0 To MyPerson.Count - 1
str(i) = MyPerson(i).street
Next
Dim mp() As Persons = MyPerson.ToArray
Array.Sort(str, mp)
Return mp
End Function
End Class