Создать структуру, содержащую название улицы и количество жильцов - Visual Basic .NET
Формулировка задачи:
Список содержит число жителей, проживающих в каждом доме на пяти улицах села.
1. Создать структуру, содержащую название улицы, номер дома, количество жильцов.
2. Получить отчет, содержащий два поля: название улицы и общее количество жильцов, проживающих на данной улице.
1 часть сделала а вот 2 не получается..
Листинг программы
- Public Class Form1
- Dim n As Integer
- Dim AllSum As Decimal
- Private Structure Persons
- Dim ULI As String 'название улицы
- Dim NOM As String 'номер дома
- Dim KOLVO As Double 'колличество жителей
- End Structure
- Dim MyPerson(100) As Persons
- Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
- End Sub
- Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Static k As Integer
- MyPerson(k).ULI = TextBox1.Text
- MyPerson(k).NOM = Val(TextBox2.Text)
- MyPerson(k).KOLVO = Val(TextBox3.Text)
- TextBox4.Text = TextBox4.Text + " Улица " + MyPerson(k).ULI + " № дома " + _
- MyPerson(k).NOM + " Жильцов " + CStr(MyPerson(k).KOLVO) + " человек " + vbCrLf
- MessageBox.Show("Данные введены", "СПИСОК", MessageBoxButtons.OK, MessageBoxIcon.Information)
- TextBox1.Text = ""
- TextBox2.Text = ""
- TextBox3.Text = ""
- k = k + 1
- Label4.Text = "Всего записей: " + CStr(k)
- n = k
- End Sub
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
- End
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- End Sub
- End Class
Решение задачи: «Создать структуру, содержащую название улицы и количество жильцов»
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
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д