1 / 50

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (. ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון. שלום!. המייל של אבי: rosenfa@gmail.com המייל של אריאלה: ariellarich@gmail.com כתובת האתר: www.jct.ac.il /~richards/mivne-algo.htm. מה לומדים?. מבנה נתונים אלגוריתמים תכנות WINDOWS. דרישות הקורס.

nanda
Télécharger la présentation

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. מבנה נתונים ואלגוריתמים) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון

  2. שלום! • המייל של אבי: rosenfa@gmail.com • המייל של אריאלה: ariellarich@gmail.com • כתובת האתר: www.jct.ac.il/~richards/mivne-algo.htm

  3. מה לומדים? • מבנה נתונים • אלגוריתמים • תכנות WINDOWS

  4. דרישות הקורס • חשיבה! • דרך ארץ! • (אין חובת נוכחות!) • תרגילים – 12% • מבחן – 88%

  5. מחרוזות של תוויםStrings

  6. hellokita hello תזכורת: מחרוזות - Strings Dim s As String s = “hello” s = s & “kita” או s = s + “kita” שקף מאת מאיר קומר

  7. זהירות!אי אפשר לעשות השמה לתוך מקום מסוים במחרוזת (זה לא מערך!) Dim s,tAs String s = “hello” t = s(2) t = s.chars(2) פעולות מותרות: t(0) = s(2) t.chars(0) = s.chars(2) פעולות אסורות:

  8. אז מה עושים? • Length מחזירה אורך של מחרוזת • Remove(start, count ) מסירה count תווים החל מהמקום start • ומחזירה את המחרוזת החדשה • Insert(start, str) מוסיפה את המחרוזת str החל מהמקום start • ומחזירה את המחרוזת החדשה • Replace(str1, str2)מחליפה את המחרוזת או התו str1 במחרוזת או תו str2 • בכל מקרה מחזירה את המחרוזת החדשה • IndexOf(str), IndexOf(str, start)מחזירה מיקום המחרוזת (או תו) strבמחרוזת • מיקום מתחיל מ 0 או מ start, אם לא נמצא מחזיר -1 • LastIndexOf(str), LastIndexOf(str, end)מחזירה מיקום מסוףמחרוזת (או התו) str • מיקום מתחיל מסוף המחרוזת או מ end, אם לא נמצא מחזיר -1 • SubString(start, count ) מייצרת מחרוזת של count תווים החל מהמקום start • ומחזירה את המחרוזת החדשה • Chars(start) – לקריאה בלבד! אי אפשר לבצע השמה לתוך

  9. שימו לב! • בכל הפונקציות שהצגנו השינוי אינו מתבצע במחרוזת עליה פעלנו: s = "hello" s.Replace("h", "y") • במחרוזת s עדיין יש "hello" • אם נרצה לשנות את המחרוזת s, ולהפוך בתוכה את המחרוזת: s = "hello" s = s.Replace("h", "y") • עכשיו במחרוזת sיש "yello"

  10. שימוש בסיסי במחרוזת ModuleModule1 SubMain() Dim x AsString x = Console.ReadLine Console.WriteLine("The Length is " & x.Length()) Console.WriteLine("The first letter is " & x(0)) Console.WriteLine("The second letter is " & x(1)) Console.WriteLine("The third letter is " & x(2)) Console.WriteLine("What will this do??? " & x(2000)) EndSub EndModule

  11. שימוש בסיסי במחרוזת ModuleModule1 Sub Main() Dim x AsString x = Console.ReadLine Console.WriteLine("The first letter is " & x(0)) If (x(0) = "A") Then Console.WriteLine("Yeah!") EndIf If (x(1) = " ") Then Console.WriteLine("Space in second position") EndIf EndSub EndModule

  12. פעולות בסיסיות במחזרות ModuleModule1 Sub Main() Dim word AsString word = Console.ReadLine 'word(0) = "B" ' Won't work! word = word.Replace("a", "b") 'word.Replace("a", "b") also won't work Console.WriteLine("The word now is " & word) word = word.Remove(0, 2) 'takes out first 2 letters Console.WriteLine("The word now is " & word) word = word.Insert(0, "B2") 'add string at position Console.WriteLine("The word now is " & word) EndSub EndModule

  13. דוגמא של לולאה במחרוזת ModuleModule1 Sub Main() Dim x AsString Dimi, j AsInteger x = Console.ReadLine Console.WriteLine("The Length is " & x.Length()) Fori = 0 Tox.Length() - 1 For j = 0 Toi Console.Write(x(j)) Next Console.WriteLine() Next EndSub EndModule

  14. פונקציה יותר מסובכת ModuleModule1 Function Change(ByVal x AsString) AsString DimiAsInteger Fori = 0 Tox.Length() - 1 If x(i) = "a"Or x(i) = "e"Or x(i) = "i"Then x = x.Remove(i, 1) 'Takes out that letter Console.WriteLine("The word is now " & x) x = x.Insert(i, "Z") 'Puts something else there EndIf Next Return x EndFunction Sub Main() Dim word AsString word = Console.ReadLine Console.WriteLine("The Word is " & Change(word)) EndSub EndModule

  15. פונקציה יותר מסובכת עם REF ModuleModule1 Sub Change(ByRef x AsString) DimiAsInteger Fori = 0 Tox.Length() - 1 Ifx(i) = "a"Or x(i) = "e"Or x(i) = "i"Then x = x.Remove(i, 1) 'Takes out that letter Console.WriteLine("The word is now " & x) x = x.Insert(i, "Z") 'Puts something else there EndIf Next EndSub Sub Main() Dim word AsString word = Console.ReadLine Change(word) Console.WriteLine("The Word is " & word) EndSub EndModule

  16. שיטות נוספות לביצוע פעולות על מחרוזות ב VB • Len • Left • Right • Mid • הערה: • בשיטות אלו הספירה מתחילה מ 1 ולא מ 0 • (בניגוד למה שהכרנו)

  17. פעולות על מחרוזות מחזירה אורך המחרוזת Len ModuleModule1 Sub Main() Dim word AsString word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) EndSub EndModule שקף מאת מאיר קומר

  18. פעולות על מחרוזות he מחזירה תת-מחרוזת משמאל Left a = “hello kita” x = Left (a,2) איזה מחרוזת כמה תוים שקף מאת מאיר קומר

  19. o kita פעולות על מחרוזות מחזירה תת-מחרוזת מימין Right a = “hello kita” x = Right (a,6) איזה מחרוזת כמה תוים שקף מאת מאיר קומר

  20. ell פעולות על מחרוזות מחזירה תת-מחרוזת Mid a = “hello kita” x = Mid (a,2,3) החל מתו- כמה תוים שקף מאת מאיר קומר

  21. תרגול קטן s = "arurhamanbaruchmordechai" Left (s,10) = Mid (s,1,11)? לא 27 Len (s) arur h Left (s,6) rdechai Right (s,7) haman ba Mid (s,6,8) ord Mid (Right(s,9),2,3) mordechaibaruch Right (s,9) & Mid (s,11,7) שקף מאת מאיר קומר

  22. מערכים

  23. יש לקלוט 10מספרים למערך. להוסיף לכל מספר את המספר שבא אחריו במערך. למספר האחרון במערך לא להוסיף דבר. יש להדפיס את המערך. ModuleModule1 Sub Main() Dim x(10) AsInteger DimiAsInteger DimlenAsInteger = x.Length() Console.WriteLine("Length is {0} ", len) Fori = 0 Tolen - 1 'Familiar? x(i) = Console.ReadLine() Next Fori = 0 Tolen - 2 'why - 2? x(i) = x(i) + x(i + 1) Next Fori = 0 Tolen - 1 Console.WriteLine("In pos{0} I have {1} ", i, x(i)) Next EndSub EndModule

  24. פונקציות קיימות ... במערך חד מימדי: x.Length() Array.Resize(x, i) Array.Sort(x) Array.Reverse(x) במערך דו מימדי: x.Length() x.GetLength(0) x.GetLength(1)

  25. פונקציות במערך – דוגמא Sub Print(ByVal x() AsInteger) DimiAsInteger Console.WriteLine() Console.WriteLine("Now printing the array") Fori = 0 Tox.Length() - 1 'why -1 ??? Console.Write(x(i) & " ") If (i + 1) Mod 15 = 0 ThenConsole.WriteLine("") Next EndSub

  26. המשך דוגמא - שימוש בפונקציה SubMain() DimiAsInteger DimtargetArray(100) AsInteger Dim rand AsNewRandom Fori = 0 To 100 targetArray(i) = rand.Next(-1000, 1000) Next ' Sort the entire targetArray. Array.Sort(targetArray) Print(targetArray) Array.Reverse(targetArray) Print(targetArray) Array.Resize(targetArray, 10) Array.Sort(targetArray) Print(targetArray) Console.WriteLine(vbNewLine) EndSub

  27. המשך דוגמא - פלט Now printing the array -996 -934 -917 -881 -870 -851 -848 -824 -818 -807 -791 -786 -778 -709 -704 -666 -642 -618 -617 -597 -580 -509 -477 -426 -418 -403 -394 -354 -346 -337 -317 -299 -289 -276 -253 -232 -231 -200 -193 -157 -124 -120 -74 -73 -72 -69 -59 -1 20 23 24 113 140 198 228 262 264 272 315 322 324 404 408 408 453 467 479 495 498 513 519 544 554 570 572 584 597 619 633 642 684 703 704 720 731 737 739 776 778 786 798 842 864 876 884 904 971 974 974 997 998 Now printing the array 998 997 974 974 971 904 884 876 864 842 798 786 778 776 739 737 731 720 704 703 684 642 633 619 597 584 572 570 554 544 519 513 498 495 479 467 453 408 408 404 324 322 315 272 264 262 228 198 140 113 24 23 20 -1 -59 -69 -72 -73 -74 -120 -124 -157 -193 -200 -231 -232 -253 -276 -289 -299 -317 -337 -346 -354 -394 -403 -418 -426 -477 -509 -580 -597 -617 -618 -642 -666 -704 -709 -778 -786 -791 -807 -818 -824 -848 -851 -870 -881 -917 -934 -996 Now printing the array 842 864 876 884 904 971 974 974 997 998

  28. עוד דרך לשנות את גודל המערך ReDim ModuleModule1 Sub Main() Dim x() AsInteger = {1, 2, 53, 3, 1, 23} Print(x) ReDimPreserve x(10) Print(x) ReDim x(15) Print(x) EndSub EndModule פלט: Now printing the array 1 2 53 3 1 23 Now printing the array 1 2 53 3 1 23 0 0 0 0 0 Now printing the array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  29. Structure

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

  31. Structure סינטקס: Structure שם המבנה משתנה 1 משתנה 2 ... End Structure דוגמא: StructureOved Dim name AsString DimmaskoretAsInteger EndStructure

  32. Structs (Structures) ModuleModule1 StructureOved Dim name AsString DimmaskoretAsInteger EndStructure Sub Main() Dim x AsOved x.name = "Avi" x.maskoret = 1234 Dim People(10) AsOved 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()) EndSub EndModule

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

  34. דוגמא עם לולאות ModuleModule1 StructureOved Dim name AsString DimmaskoretAsInteger EndStructure Sub Main() Dim People(10) AsOved DimiAsInteger Fori = 0 To 2 Console.WriteLine("Please enter person number " & i) People(i).name = Console.ReadLine People(i).maskoret = Console.ReadLine() Next Dim min AsInteger = People(0).maskoret Dim temp AsString = People(0).name Fori = 1 To 2 If (People(i).maskoret < min) Then min = People(i).maskoret temp = People(i).name EndIf Next Console.WriteLine("The min is " & min & " his name is " & temp) EndSub EndModule

  35. מה ההבדל? ModuleModule1 StructureOved Dim name AsString DimmaskoretAsInteger EndStructure Sub Main() Dim People(10) AsOved DimiAsInteger Fori = 0 To 2 Console.WriteLine("Please enter person number " & i) People(i).name = Console.ReadLine People(i).maskoret = Console.ReadLine() Next Dim min AsInteger = 0 Dim temp AsString = People(0).name Fori = 1 To 2 If (People(i).maskoret < People(min).maskoret) Then min = i EndIf Next Console.WriteLine("The min is " & People(min).maskoret & " his name is " & People(min).name) EndSub EndModule

  36. עם פונקציה... StructureOved Public name AsString PublicmaskoretAsInteger EndStructure Sub Main() Dim People(5) AsOved DimiAsInteger Fori = 0 ToPeople.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)) EndSub

  37. הפונקציה FunctionPrintHighest(ByVal x() AsOved) AsInteger DimiAsInteger Dim high AsInteger = x(0).maskoret Fori = 0 Tox.Length - 1 If x(i).maskoret > high Then high = x(i).maskoret EndIf Next Return high EndFunction

  38. ModuleModule1 Sub Main() Dim x AsOved x.name = "Avi" x.maskoret = 1234 Dim People(10) AsOved People(0).name = "Yossi" People(1).name = "Moshe" People(2).name = "Lea" Dim value AsInteger = PrintHighest(People) Console.WriteLine("Highest is " & value) EndSub StructureOved Public name AsString PublicmaskoretAsInteger EndStructure FunctionPrintHighest(ByVal x() AsOved) AsInteger DimiAsInteger Dim high AsInteger = x(0).maskoret Fori = 0 Tox.Length - 1 If x(i).maskoret > high Then high = x(i).maskoret EndIf Next Return high EndFunction EndModule מה יופיע פה?

  39. תרגיל – תכנון חנות • ברצוננו לייצר טבלה המכילה את רשימת המוצרים בחנות. • לכל מוצר יש: • שם • ברקוד • מחיר StructureShop Dim name AsString Dim code AsInteger Dim price AsDouble EndStructure

  40. ModuleModule1 StructureShop Dim name AsString Dim code AsInteger Dim price AsDouble EndStructure Sub Main() Dimmakolet(10) AsShop 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 DimiAsInteger Fori = 0 Tomakolet.Length() - 1 Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price) Next EndSub EndModule מימוש חנות 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

  41. בלי הדפסת מוצרים ריקים ModuleModule1 StructureShop Dim name AsString Dim code AsInteger Dim price AsDouble EndStructure Sub Main() Dimmakolet(10) AsShop 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 DimiAsInteger Fori = 0 Tomakolet.Length() - 1 Ifmakolet(i).name <> NothingThen Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price) EndIf Next EndSub EndModule 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

  42. ModuleModule1 StructureShop Dim name AsString Dim code AsInteger Dim price AsDouble EndStructure Sub Main() Dimmakolet(10) AsShop 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) EndSub SubPrintPrice(ByVal s() AsShop) DimiAsInteger Fori = 0 Tos.Length() - 1 If s(i).name <> NothingThen Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, s(i).name, s(i).price) EndIf Next EndSub EndModule עם פונקציה 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

  43. רוצים יכולת למצוא את שם המוצר הכי זול • נכתוב פונקציה • נרצה להעביר לפונקציה את המערך של החנות • נרצה לקבל מהפונקציה את השם של המוצר • שימו לב – הפונקציה לא מדפיסה שום דבר!!! FunctionGetZol(ByVal s() AsShop) AsString DimiAsInteger Dim idx AsInteger = 0 Dim min AsDouble = s(0).price Fori = 1 Tos.Length() - 1 If s(i).name <> NothingAnd s(i).price < min Then min = s(i).price idx = i EndIf Next Return s(idx).name EndFunction

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

  45. הקוד: Sub Main() Dimmakolet(10) AsShop 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) DimzolAsString zol = GetZol(makolet) Console.WriteLine("The cheapest thing in makolet is " & zol) EndSub 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

  46. Sub Main() Dimmakolet(10) AsShop 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) AsShop 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) DimzolAsString 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) EndSub ואם יש גם מכולת וגם מאפיה?נשתמש באותה פונקציה שוב...נעביר לה פרמטר אחר, ונקבל ממנה משהו אחר 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

  47. Sub Main() Dimmakolet(10) AsShop 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 Dimbakery(10) AsShop 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) DimzolAsString Console.WriteLine("The cheapest thing in makolet is " & GetZol(makolet)) PrintPrice(bakery) Console.WriteLine("The cheapest thing in makolet is " & GetZol(bakery)) EndSub אפשר גם לשתול את מה שחוזר מהפונקציה ישר לתוך פקודת הדפסה 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

  48. פונקציה לחישוב סכום המוצרים בסל FunctionKupa(ByVal cart() AsString, ByVal s() AsShop) AsSingle Dim total AsSingle = 0 Fori = 0 Tocart.Length - 1 For j = 0 Tos.Length() - 1 If cart(i) = s(j).name Then total += s(j).price EndIf Next Next Return total EndFunction

  49. המשך חישוב סכום המוצרים בסל Sub Main() Dim super(10) AsShop 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() AsString = {"Bread", "Shoko"} Console.WriteLine("Lunch costs " & Kupa(lunch, super) & " shekel.") EndSub Lunch costs 9.8 shekel.

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

More Related