22
Structure

Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

Embed Size (px)

Citation preview

Page 1: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

Structure

Page 2: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

? מה לומדים היום

דרך לבנות מבנה נתונים בסיסי•–Structure

מייצר "טיפוס" חדש•מתאים כאשר רוצים לאגד כמה משתנים יחד•

דוגמאות:•עובד: שם, טלפון, דרגה•סטודנט: שם, ת"ז, ממוצע•מוצר: שם, מחיר, משקל•מספר מרוכב: חלק ממשי, חלק מדומה•

Page 3: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

Structure:סינטקס

Structure שם המבנה

1משתנה

2משתנה

...End Structure

:דוגמאStructure Oved Dim name As String Dim maskoret As Integer End Structure

Page 4: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – 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 SubEnd Module

Page 5: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

Structureמערך של

C++ Programming: From Problem Analysis to Program Design, Fifth Edition

Page 6: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

דוגמא עם לולאות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 SubEnd Module

Page 7: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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 SubEnd Module

?מה ההבדל

Page 8: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

...עם פונקציה 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

Page 9: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – 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

Page 10: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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 FunctionEnd Module

?מה יופיע פה

Page 11: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

תרגיל – תכנון חנות

•. בחנות המוצרים רשימת את המכילה טבלה לייצר ברצוננו•: יש מוצר לכל

שם•ברקוד•

מחיר•

Structure Shop Dim name As String Dim code As Integer Dim price As DoubleEnd Structure

Page 12: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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 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 SubEnd Module

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25Product 3 is: , its price is: 0Product 4 is: , its price is: 0Product 5 is: , its price is: 0Product 6 is: , its price is: 0Product 7 is: , its price is: 0Product 8 is: , its price is: 0Product 9 is: , its price is: 0Product 10 is: , its price is: 0

מימוש חנות

Page 13: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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 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 SubEnd Module

בלי הדפסת מוצרים ריקים

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25

Page 14: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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

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 NextEnd SubEnd Module

עם פונקציה

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25

Page 15: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

רוצים יכולת למצוא את שם המוצר הכי זול

פונקציה • נכתובהחנות • של המערך את לפונקציה להעביר נרצההמוצר • של השם את מהפונקציה לקבל נרצה•!!! דבר – שום מדפיסה לא הפונקציה לב שימו

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

Page 16: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

הדפסת רשימת המוצרים המלאה ואת המוצר הכי זול במכולת

•) קודם ) שהגדרנו כמו הבאות הפונקציות את לנו שיש נניח•Sub PrintPrice(ByVal s() As Shop)•Function GetZol(ByVal s() As Shop) As Stringפונקציה • בעזרת המכולת המוצרים כל את נדפיסביותר • הזול המוצר שם את נדפיס

• , נדפיס ואז המוצר שם למציאת בפונקציה נשתמש

Page 17: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – 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

PrintPrice(makolet) Dim zol As String zol = GetZol(makolet) Console.WriteLine("The cheapest thing in makolet is " & zol)

End Sub

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is Cheese

:הקוד

Page 18: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – 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

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 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

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is CheeseProduct 0 is: roll, its price is: 6.32Product 1 is: pita, its price is: 3.6The cheapest thing in bakery is pita

ואם יש גם מכולת וגם מאפיה?

נשתמש באותה פונקציה שוב...נעביר לה פרמטר אחר, ונקבל ממנה

משהו אחר

Page 19: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – 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

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 Console.WriteLine("The cheapest thing in makolet is " & GetZol(makolet))

PrintPrice(bakery) Console.WriteLine("The cheapest thing in makolet is " & GetZol(bakery))End Sub

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is CheeseProduct 0 is: roll, its price is: 6.32Product 1 is: pita, its price is: 3.6The cheapest thing in bakery is pita

אפשר גם לשתול את מה שחוזר מהפונקציה

ישר לתוך פקודת הדפסה

Page 20: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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

פונקציה לחישוב סכום המוצרים בסל

Page 21: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

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 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

Lunch costs 9.8 shekel.

המשך חישוב סכום המוצרים בסל

Page 22: Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה

תרגיל

בצע השוואת מחירים לסל קניות לחג פסח?•איזה קלט נבקש מהמשתמש?–מה צריך להיות מוגדר בתוכנית?–איך נכתוב את התוכנית?–