מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון
שלום! המייל של אבי: המייל של אריאלה: כתובת האתר:
מה לומדים? מבנה נתונים אלגוריתמים תכנות WINDOWS
דרישות הקורס חשיבה! דרך ארץ! (אין חובת נוכחות!) תרגילים – 12% מבחן – 88%
מחרוזות של תווים Strings
שקף מאת מאיר קומר תזכורת: מחרוזות - Strings Dim s As String s = “hello” s = s & “kita” או s = s + “kita” hellohellokita
זהירות! אי אפשר לעשות השמה לתוך מקום מסוים במחרוזת (זה לא מערך!) Dim s,t As String s = “hello” t = s(2) t = s.chars(2) t(0) = s(2) t.chars(0) = s.chars(2) פעולות מותרות: פעולות אסורות:
אז מה עושים? 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) – לקריאה בלבד! אי אפשר לבצע השמה לתוך
שימו לב! בכל הפונקציות שהצגנו השינוי אינו מתבצע במחרוזת עליה פעלנו: s = "hello" s.Replace("h", "y") במחרוזת s עדיין יש "hello" אם נרצה לשנות את המחרוזת s, ולהפוך בתוכה את המחרוזת: s = "hello" s = s.Replace("h", "y") עכשיו במחרוזת s יש "yello"
שימוש בסיסי במחרוזת 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
שימוש בסיסי במחרוזת 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
פעולות בסיסיות במחזרות 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
דוגמא של לולאה במחרוזת 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
פונקציה יותר מסובכת 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
פונקציה יותר מסובכת עם 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
שיטות נוספות לביצוע פעולות על מחרוזות ב VB Len Left Right Mid הערה: בשיטות אלו הספירה מתחילה מ 1 ולא מ 0 (בניגוד למה שהכרנו)
שקף מאת מאיר קומר מחזירה אורך המחרוזת Module Module1 Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) End Sub End Module פעולות על מחרוזות Len
שקף מאת מאיר קומר מחזירה תת - מחרוזת משמאל a = “hello kita” x = Left (a,2) פעולות על מחרוזות Left he כמה תוים איזה מחרוזת
שקף מאת מאיר קומר מחזירה תת - מחרוזת מימין a = “hello kita” x = Right (a,6) פעולות על מחרוזות Right o kita כמה תוים איזה מחרוזת
שקף מאת מאיר קומר מחזירה תת - מחרוזת a = “hello kita” x = Mid (a,2,3) פעולות על מחרוזות Mid ell כמה תוים החל מתו -
שקף מאת מאיר קומר תרגול קטן 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)? לא
מערכים
יש לקלוט 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
פונקציות קיימות... במערך חד מימדי: x.Length() Array.Resize(x, i) Array.Sort(x) Array.Reverse(x) במערך דו מימדי: x.Length() x.GetLength(0) x.GetLength(1)
פונקציות במערך – דוגמא 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
המשך דוגמא - שימוש בפונקציה 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
המשך דוגמא - פלט Now printing the array Now printing the array Now printing the array
עוד דרך לשנות את גודל המערך 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 Now printing the array Now printing the array