מבוא למדעי המחשב לתעשייה וניהול הרצאה 7
סברוטינות subroutines
שימוש חוזר בקטעי קוד ומודולריות מניע 1: נניח שכתבנו קטע קוד המבצע מטלה מסוימת למשל - מציאת מינימום, חישוב ממוצע, הדפסה, קריאת קלט רוצים לחזור על אותה המטלה יותר מפעם אחת ( לאו דווקא ברצף ) האם חייבים לכתוב שוב את אותו הקוד ? מניע 2: רוצים לחלק את הקוד לחלקים בעלי משמעות למען סדר, קריאה נוחה, כתיבה נוחה?
שגרה - subroutine
שגרה - Sub " שגרה " (subroutine) היא אוסף של פקודות המבצעות מטלה השגרה מתחילה ב Sub nameOfSub() ומסתיימת ב End Sub באמצע יופיעו הפקודות המרכיבות את השגרה. נשתמש ב Sub כאשר יש צורך באיגוד של מספר פקודות ביחד ואין צורך במתן משוב לאחר ביצוע הפקודות
דוגמא 1 ל Sub Module Module1 Sub Print() Console.WriteLine("Im in the sub") End Sub Sub Main() Print() Console.WriteLine("Im in main") Print() Console.ReadKey() End Sub End Module
דוגמא 2 ל Sub Module Module1 Sub Print() Console.WriteLine("Im in the sub") End Sub Sub Main() For i As Integer = 0 To 9 Print() Next Console.ReadKey() End Sub End Module
Module Module1 Sub AddNumbers() Dim first As Integer Dim second As Integer Dim answer As Integer Console.WriteLine("Please type a number") first = Console.ReadLine() Console.WriteLine("Please type another number") second = Console.ReadLine() answer = first + second Console.WriteLine("The total is " & answer) End Sub Sub Main() AddNumbers() Console.ReadKey() End Sub End Module דוגמא 3 ל Sub
דוגמא 4 ל Sub- העברת פרמטר Module Module1 Sub Print(ByVal x As Integer) Console.WriteLine("Im in the sub, number " & x) End Sub Sub Main() Dim a As Integer = 1 Print(a) a = 2 Print(a) Console.ReadKey() End Sub End Module
דוגמא 5 ל Sub- העברת כמה פרמטרים Module Module1 Sub Add(ByVal x As Integer, ByVal y As Integer) Dim sum As Integer sum = x + y Console.WriteLine("The sum is " & sum) End Sub Sub Main() Add(2, 3) Dim a As Integer = 20, b As Integer = 15 Add(a, b) 'Console.WriteLine("The sum is " & sum) אי אפשר לעשות את זה. Console.ReadKey() End Sub End Module
תחומי הגדרה של משתנים ניתן להגדיר משתנים במקומות שונים בקוד מיקום ההגדרה משפיע על תחום ההגדרה ( איפה המשתנה מוכר ) משתנה מקומי (local) משתנה שמוכר רק בתוך שגרה מסוימת מגדירים בתוך Sub ( או בתוך Function) משתנה גלובלי (global) משתנה שמוכר בכל חלקי התוכנית מגדירים בתוך המודול, לא בתוך Sub ( וגם לא בתוך Function)
Module Module1 Dim b As Integer Sub mySub() Dim a As Integer = 6 a = a + 1 b = 5 'c = 87 End Sub Sub Main() Dim c As Integer c = c * 2 b = 8 Console.WriteLine(b) mySub() 'a = 4 +2 Console.WriteLine(b) End Sub End Module דוגמא 6 ל Sub תחומי הגדרה
Module Module1 Public Sub print(ByVal j As Integer) Console.WriteLine("Good morning" & j) End Sub Sub Main() Dim i As Integer For i = 1 To 10 print(i) Next Console.ReadKey() End Sub End Module דוגמא 7 ל Sub
Module Module1 Public Sub print(ByVal j As Integer, ByVal k As Integer) Console.WriteLine("Good morning " & j + k) End Sub Sub Main() Dim i As Integer, j As Integer For i = 1 To 10 j = 2 print(i, j) Next Console.ReadKey() End Sub End Module דוגמא 8 ל Sub- שימו לב לתחומי הגדרה של המשתנים !
דוגמאות לתרגול מה שלמדנו עד כה...
Dim x As Integer Dim IsSpace As Boolean = False x = Console.Read() While x <> AscW(vbCr) If x = AscW(" ") Then 'If Not (IsSpace) Then If IsSpace = False Then IsSpace = True Console.Write(ChrW(x)) End If Else Console.Write(ChrW(x)) IsSpace = False End If x = Console.Read() End While הורדת רווחים ממחרוזת :
Dim x As Integer Dim result, val As Integer x = Console.Read() val = x - AscW(0) result = result + val * 1 x = Console.Read() val = x - AscW(0) result = result + val * 10 Console.WriteLine("the reversed number is " & result) היפוך ספרות :
קצת מתמטיקה... סדרת פיבונאצ ' י :
Dim a As Integer = 0, b As Integer = 1, i As Integer, n As Integer Console.WriteLine("Enter a even n") n = Console.ReadLine() Console.WriteLine(a) Console.WriteLine(b) If n Mod 2 = 0 Then For i = 1 To (n - 2) / 2 a = a + b Console.WriteLine(a) b = a + b Console.WriteLine(b) Next Else Console.WriteLine("n has to be even") End If סדרת פיבונאצ ' י :
Dim num, guess As Integer Dim RandomClass As New Random() num = RandomClass.Next(1, 100) Do Console.WriteLine("Please try to guess the number") guess = Console.ReadLine() If (guess > num) Then Console.WriteLine("You guessed too high") ElseIf (guess < num) Then Console.WriteLine("You guessed too low") Else Console.WriteLine("You got it") End If Loop While (num <> guess) משחק ניחוש :
Dim b As Integer = 2, i As Integer = 3 האם חיוני לקבוע את ערכי b ו i?' Dim degel_Int As Integer = 0 Randomize() b = CInt(Rnd() * ) Console.WriteLine(b) For i = 2 To b - 1 If b Mod i = 0 Then degel_Int = 1 End If Next If degel_Int = 0 Then Console.WriteLine("yes") Else Console.WriteLine("no") End If האם מספר אקראי הוא ראשוני ( א ):
Dim b As Integer = 2, i As Integer = 3 Dim degel As Boolean = False Randomize() b = CInt(Rnd() * ) Console.WriteLine(b) For i = 2 To b - 1 If b Mod i = 0 Then degel = True End If Next If degel = False Then 'using a Boolean degel 'If Not (degel) Then 'this does the same as the line above Console.WriteLine("yes") Else Console.WriteLine("no") End If האם מספר אקראי הוא ראשוני ( ב ):
Dim a As Integer = 1, i As Integer = 0, n As Integer = 0 Randomize() n = Int(8 * Rnd()) If n > 0 Then For i = 1 To n a = a * i Next End If Console.WriteLine("the factorial of " & n & "is: " & a) עצרת של מספר אקראי :