Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון."— Presentation transcript:

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

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

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

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

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

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

7 זהירות! אי אפשר לעשות השמה לתוך מקום מסוים במחרוזת (זה לא מערך!) Dim s,t As 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 שימוש בסיסי במחרוזת Module Module1 Sub Main() Dim x As String 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)) End Sub End Module

11 שימוש בסיסי במחרוזת Module Module1 Sub Main() Dim x As String x = Console.ReadLine Console.WriteLine("The first letter is " & x(0)) If (x(0) = "A") Then Console.WriteLine("Yeah!") End If If (x(1) = " ") Then Console.WriteLine("Space in second position") End If End Sub End Module

12 פעולות בסיסיות במחזרות Module Module1 Sub Main() Dim word As String 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) End Sub End Module

13 דוגמא של לולאה במחרוזת Module Module1 Sub Main() Dim x As String Dim i, j As Integer x = Console.ReadLine Console.WriteLine("The Length is " & x.Length()) For i = 0 To x.Length() - 1 For j = 0 To i Console.Write(x(j)) Next Console.WriteLine() Next End Sub End Module

14 פונקציה יותר מסובכת Module Module1 Function Change(ByVal x As String) As String Dim i As Integer For i = 0 To x.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 End If Next Return x End Function Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine("The Word is " & Change(word)) End Sub End Module

15 פונקציה יותר מסובכת עם REF Module Module1 Sub Change(ByRef x As String) Dim i As Integer For i = 0 To x.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 End If Next End Sub Sub Main() Dim word As String word = Console.ReadLine Change(word) Console.WriteLine("The Word is " & word) End Sub End Module

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

17 שקף מאת מאיר קומר מחזירה אורך המחרוזת Module Module1 Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) End Sub End Module פעולות על מחרוזות Len

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

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

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

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

22 מערכים

23 יש לקלוט 10מספרים למערך. להוסיף לכל מספר את המספר שבא אחריו במערך. למספר האחרון במערך לא להוסיף דבר. יש להדפיס את המערך. Module Module1 Sub Main() Dim x(10) As Integer Dim i As Integer Dim len As Integer = x.Length() Console.WriteLine("Length is {0} ", len) For i = 0 To len - 1 'Familiar? x(i) = Console.ReadLine() Next For i = 0 To len - 2 'why - 2? x(i) = x(i) + x(i + 1) Next For i = 0 To len - 1 Console.WriteLine("In position {0} I have {1} ", i, x(i)) Next End Sub End Module

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() As Integer) Dim i As Integer Console.WriteLine() Console.WriteLine("Now printing the array") For i = 0 To x.Length() - 1 'why -1 ??? Console.Write(x(i) & " ") If (i + 1) Mod 15 = 0 Then Console.WriteLine("") Next End Sub

26 המשך דוגמא - שימוש בפונקציה Sub Main() Dim i As Integer Dim targetArray(100) As Integer Dim rand As New Random For i = 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) End Sub

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 Module Module1 Sub Main() Dim x() As Integer = {1, 2, 53, 3, 1, 23} Print(x) ReDim Preserve x(10) Print(x) ReDim x(15) Print(x) End Sub End Module פלט: 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


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

Similar presentations


Ads by Google