Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה משתנים יחד דוגמאות : עובד : שם, טלפון,"— Presentation transcript:

1 Structure

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

3 Structure סינטקס : Structure שם המבנה משתנה 1 משתנה 2... End Structure דוגמא : Structure Oved Dim name As String Dim maskoret As Integer End Structure

4 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

5 מערך של Structure C++ Programming: From Problem Analysis to Program Design, Fifth Edition

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

7 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 מה ההבדל ?

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

9 הפונקציה 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

10 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 מה יופיע פה ?

11 תרגיל – תכנון חנות ברצוננו לייצר טבלה המכילה את רשימת המוצרים בחנות. לכל מוצר יש : שם ברקוד מחיר Structure Shop Dim name As String Dim code As Integer Dim price As Double End Structure

12 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 Sub End Module 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 Product 3 is:, its price is: 0 Product 4 is:, its price is: 0 Product 5 is:, its price is: 0 Product 6 is:, its price is: 0 Product 7 is:, its price is: 0 Product 8 is:, its price is: 0 Product 9 is:, its price is: 0 Product 10 is:, its price is: 0 מימוש חנות

13 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 Sub End Module בלי הדפסת מוצרים ריקים 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

14 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 Next End Sub End Module עם פונקציה 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

15 רוצים יכולת למצוא את שם המוצר הכי זול נכתוב פונקציה נרצה להעביר לפונקציה את המערך של החנות נרצה לקבל מהפונקציה את השם של המוצר שימו לב – הפונקציה לא מדפיסה שום דבר !!! 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

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

17 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.75 Product 1 is: Shnitzel, its price is: 21.45 The cheapest thing in makolet is Cheese הקוד :

18 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.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 ואם יש גם מכולת וגם מאפיה ? נשתמש באותה פונקציה שוב... נעביר לה פרמטר אחר, ונקבל ממנה משהו אחר

19 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.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 אפשר גם לשתול את מה שחוזר מהפונקציה ישר לתוך פקודת הדפסה

20 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 Return total End Function פונקציה לחישוב סכום המוצרים בסל

21 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. המשך חישוב סכום המוצרים בסל

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


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

Similar presentations


Ads by Google