Structure
מה לומדים היום ?
• דרך לבנות מבנה נתונים בסיסי
– Structure
• מייצר "טיפוס" חדש
• מתאים כאשר רוצים לאגד כמה משתנים יחד
• דוגמאות:
•
•
•
•
עובד :שם ,טלפון ,דרגה
סטודנט :שם ,ת"ז ,ממוצע
מוצר :שם ,מחיר ,משקל
מספר מרוכב :חלק ממשי ,חלק מדומה
Structure
:סינטקס
Structure שם המבנה
1 משתנה
2 משתנה
...
End Structure
:דוגמא
Structure Oved
Dim name As String
Dim maskoret As Integer
End Structure
Structs (Structures)
Module Module1
Structure Oved
Dim name As String
Dim maskoret As Integer
End Structure
Sub Main()
Dim x As Oved
x.name = "Avi"
x.maskoret = 1234
Dim People(10) As Oved
People(0).name = "Yossi"
People(1).name = "Moshe"
People(2).name = "Lea"
Console.WriteLine("People's Size is " & People.Length())
Console.WriteLine("The first name is " & People(0).name)
Console.Write("The length of the first name is ")
Console.WriteLine(People(0).name.Length())
End Sub
End Module
Structure מערך של
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
דוגמא עם לולאות
Module Module1
Structure Oved
Dim name As String
Dim maskoret As Integer
End Structure
Sub Main()
Dim People(10) As Oved
Dim i As Integer
For i = 0 To 2
Console.WriteLine("Please enter person number " & i)
People(i).name = Console.ReadLine
People(i).maskoret = Console.ReadLine()
Next
Dim min As Integer = People(0).maskoret
Dim temp As String = People(0).name
For i = 1 To 2
If (People(i).maskoret < min) Then
min = People(i).maskoret
temp = People(i).name
End If
Next
Console.WriteLine("The min is " & min & " his name is " & temp)
End Sub
End Module
?מה ההבדל
Module Module1
Structure Oved
Dim name As String
Dim maskoret As Integer
End Structure
Sub Main()
Dim People(10) As Oved
Dim i As Integer
For i = 0 To 2
Console.WriteLine("Please enter person number " & i)
People(i).name = Console.ReadLine
People(i).maskoret = Console.ReadLine()
Next
Dim min As Integer = 0
Dim temp As String = People(0).name
For i = 1 To 2
If (People(i).maskoret < People(min).maskoret) Then
min = i
End If
Next
Console.WriteLine("The min is " & People(min).maskoret & " his name is " &
People(min).name)
End Sub
End Module
...עם פונקציה
Structure Oved
Public name As String
Public maskoret As Integer
End Structure
Sub Main()
Dim People(5) As Oved
Dim i As Integer
For i = 0 To People.Length() - 1
Console.WriteLine("Please enter the person's name")
People(i).name = Console.ReadLine
Console.WriteLine("Please enter their maskoret")
People(i).maskoret = Console.ReadLine
Next
Console.WriteLine("High " & PrintHighest(People))
End Sub
הפונקציה
Function PrintHighest(ByVal x() As Oved) As Integer
Dim i As Integer
Dim high As Integer = x(0).maskoret
For i = 0 To x.Length - 1
If x(i).maskoret > high Then
high = x(i).maskoret
End If
Next
Return high
End Function
Module Module1
Sub Main()
Dim x As Oved
x.name = "Avi"
x.maskoret = 1234
Dim People(10) As Oved
People(0).name = "Yossi"
People(1).name = "Moshe"
People(2).name = "Lea"
Dim value As Integer = PrintHighest(People)
Console.WriteLine("Highest is " & value)
End Sub
Structure Oved
Public name As String
Public maskoret As Integer
End Structure
Function PrintHighest(ByVal x() As Oved) As Integer
Dim i As Integer
Dim high As Integer = x(0).maskoret
For i = 0 To x.Length - 1
If x(i).maskoret > high Then
high = x(i).maskoret
End If
Next
Return high
End Function
End Module
?מה יופיע פה
תרגיל – תכנון חנות
• ברצוננו לייצר טבלה המכילה את רשימת המוצרים בחנות.
• לכל מוצר יש:
•
•
שם
ברקוד
•
מחיר
Structure Shop
Dim name As String
Dim code As Integer
Dim price As Double
End Structure
Module Module1
Structure Shop
Dim name As String
Dim code As Integer
Dim price As Double
End Structure
מימוש חנות
Sub Main()
Product 0 is: Cheese, its price is: 8.75
Dim makolet(10) As Shop
Product 1 is: Shnitzel, its price is: 21.45
makolet(0).name = "Cheese"
Product 2 is: Shoko, its price is: 4.25
makolet(0).code = 111
Product 3 is: , its price is: 0
makolet(0).price = 8.75
Product 4 is: , its price is: 0
makolet(1).name = "Shnitzel"
Product 5 is: , its price is: 0
makolet(1).code = 222
Product 6 is: , its price is: 0
makolet(1).price = 21.45
Product 7 is: , its price is: 0
makolet(2).name = "Shoko"
Product 8 is: , its price is: 0
makolet(2).code = 122
Product 9 is: , its price is: 0
makolet(2).price = 4.25
Product 10 is: , its price is: 0
Dim i As Integer
For i = 0 To makolet.Length() - 1
Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price)
Next
End Sub
End Module
Module Module1
Structure Shop
Dim name As String
Dim code As Integer
Dim price As Double
End Structure
Sub Main()
Dim makolet(10) As Shop
makolet(0).name = "Cheese"
makolet(0).code = 111
makolet(0).price = 8.75
makolet(1).name = "Shnitzel"
Product 0 is: Cheese, its price is: 8.75
makolet(1).code = 222
makolet(1).price = 21.45
Product 1 is: Shnitzel, its price is: 21.45
makolet(2).name = "Shoko"
Product 2 is: Shoko, its price is: 4.25
makolet(2).code = 122
makolet(2).price = 4.25
Dim i As Integer
For i = 0 To makolet.Length() - 1
If makolet(i).name <> Nothing Then
Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price)
End If
Next
End Sub
End Module
בלי הדפסת מוצרים ריקים
Module Module1
Structure Shop
Dim name As String
Dim code As Integer
Dim price As Double
End Structure
Sub Main()
Dim makolet(10) As Shop
makolet(0).name = "Cheese"
makolet(0).code = 111
makolet(0).price = 8.75
makolet(1).name = "Shnitzel"
makolet(1).code = 222
makolet(1).price = 21.45
makolet(2).name = "Shoko"
makolet(2).code = 122
makolet(2).price = 4.25
PrintPrice(makolet)
End Sub
עם פונקציה
Product 0 is: Cheese, its price is: 8.75
Product 1 is: Shnitzel, its price is: 21.45
Product 2 is: Shoko, its price is: 4.25
Sub PrintPrice(ByVal s() As Shop)
Dim i As Integer
For i = 0 To s.Length() - 1
If s(i).name <> Nothing Then
Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, s(i).name, s(i).price)
End If
Next
End Sub
End Module
רוצים יכולת למצוא את שם המוצר הכי זול
• נכתוב פונקציה
• נרצה להעביר לפונקציה את המערך של החנות
• נרצה לקבל מהפונקציה את השם של המוצר
!!!• שימו לב – הפונקציה לא מדפיסה שום דבר
Function GetZol(ByVal s() As Shop) As String
Dim i As Integer
Dim idx As Integer = 0
Dim min As Double = s(0).price
For i = 1 To s.Length() - 1
If s(i).name <> Nothing And s(i).price < min Then
min = s(i).price
idx = i
End If
Next
Return s(idx).name
End Function
הדפסת רשימת המוצרים המלאה ואת
המוצר הכי זול במכולת
•
•
•
•
•
נניח שיש לנו את הפונקציות הבאות (כמו שהגדרנו קודם)
)Sub PrintPrice(ByVal s() As Shop
Function GetZol(ByVal s() As Shop) As String
נדפיס את כל המוצרים המכולת בעזרת פונקציה
נדפיס את שם המוצר הזול ביותר
• נשתמש בפונקציה למציאת שם המוצר ,ואז נדפיס
:הקוד
Sub Main()
Dim makolet(10) As Shop
makolet(0).name = "Cheese"
makolet(0).code = 111
makolet(0).price = 8.75
makolet(1).name = "Shnitzel"
makolet(1).code = 222
makolet(1).price = 21.45
Product 0 is: Cheese, its price is: 8.75
Product 1 is: Shnitzel, its price is: 21.45
The cheapest thing in makolet is Cheese
PrintPrice(makolet)
Dim zol As String
zol = GetZol(makolet)
Console.WriteLine("The cheapest thing in makolet is " & zol)
End Sub
Sub Main()
Dim makolet(10) As Shop
makolet(0).name = "Cheese"
makolet(0).code = 111
makolet(0).price = 8.75
makolet(1).name = "Shnitzel"
makolet(1).code = 222
makolet(1).price = 21.45
Dim bakery(10) As Shop
bakery(0).name = "roll"
bakery(0).code = 333
bakery(0).price = 6.32
bakery(1).name = "pita"
bakery(1).code = 777
bakery(1).price = 3.6
ואם יש גם מכולת וגם
?מאפיה
...נשתמש באותה פונקציה שוב
ונקבל ממנה משהו אחר,נעביר לה פרמטר אחר
Product 0 is: Cheese, its price is: 8.75
Product 1 is: Shnitzel, its price is: 21.45
The cheapest thing in makolet is Cheese
Product 0 is: roll, its price is: 6.32
Product 1 is: pita, its price is: 3.6
The cheapest thing in bakery is pita
PrintPrice(makolet)
Dim zol As String
zol = GetZol(makolet)
Console.WriteLine("The cheapest thing in makolet is " & zol)
PrintPrice(bakery)
zol = GetZol(bakery)
Console.WriteLine("The cheapest thing in bakery is " & zol)
End Sub
Sub Main()
Dim makolet(10) As Shop
makolet(0).name = "Cheese"
makolet(0).code = 111
makolet(0).price = 8.75
makolet(1).name = "Shnitzel"
makolet(1).code = 222
makolet(1).price = 21.45
Dim bakery(10) As Shop
bakery(0).name = "roll"
bakery(0).code = 333
bakery(0).price = 6.32
bakery(1).name = "pita"
bakery(1).code = 777
bakery(1).price = 3.6
PrintPrice(makolet)
Dim zol As String
אפשר גם לשתול את מה
שחוזר מהפונקציה ישר
לתוך פקודת הדפסה
Product 0 is: Cheese, its price is: 8.75
Product 1 is: Shnitzel, its price is: 21.45
The cheapest thing in makolet is Cheese
Product 0 is: roll, its price is: 6.32
Product 1 is: pita, its price is: 3.6
The cheapest thing in bakery is pita
Console.WriteLine("The cheapest thing in makolet is " & GetZol(makolet))
PrintPrice(bakery)
Console.WriteLine("The cheapest thing in makolet is " & GetZol(bakery))
End Sub
פונקציה לחישוב סכום המוצרים בסל
Function Kupa(ByVal cart() As String, ByVal s() As Shop) As Single
Dim total As Single = 0
For i = 0 To cart.Length - 1
For j = 0 To s.Length() - 1
If cart(i) = s(j).name Then
total += s(j).price
End If
Next
Next
Return total
End Function
Sub Main()
Dim super(10) As Shop
super(0).name = "Shoko"
super(0).code = 111
super(0).price = 4.75
super(1).name = "Apple"
super(1).code = 222
super(1).price = 2.45
Lunch costs 9.8 shekel.
super(2).name = "Bread"
super(2).code = 333
super(2).price = 5.05
super(3).name = "Bag"
super(3).code = 444
super(3).price = 1.2
Dim lunch() As String = {"Bread", "Shoko"}
Console.WriteLine("Lunch costs " & Kupa(lunch, super) & " shekel.")
End Sub
המשך חישוב סכום
המוצרים בסל
תרגיל
• בצע השוואת מחירים לסל קניות לחג פסח?
– איזה קלט נבקש מהמשתמש?
– מה צריך להיות מוגדר בתוכנית?
– איך נכתוב את התוכנית?
© Copyright 2026 Paperzz