23
בבבב בבבבב בבבבב בבבבבבב בבבבבב בבבבב7

מבוא למדעי המחשב לתעשייה וניהול

Embed Size (px)

DESCRIPTION

הרצאה 7. מבוא למדעי המחשב לתעשייה וניהול. סברוטינות subroutines. שימוש חוזר בקטעי קוד ומודולריות. מניע 1: נניח שכתבנו קטע קוד המבצע מטלה מסוימת למשל - מציאת מינימום, חישוב ממוצע, הדפסה, קריאת קלט רוצים לחזור על אותה המטלה יותר מפעם אחת (לאו דווקא ברצף) האם חייבים לכתוב שוב את אותו הקוד? - PowerPoint PPT Presentation

Citation preview

Page 1: מבוא למדעי המחשב לתעשייה וניהול

המחשב למדעי מבואוניהול לתעשייה

7הרצאה

Page 2: מבוא למדעי המחשב לתעשייה וניהול

סברוטינות subroutines

Page 3: מבוא למדעי המחשב לתעשייה וניהול

ומודולריות קוד בקטעי חוזר שימוש 1מניע:

מסוימת מטלה המבצע קוד קטע שכתבנו נניח , , , קלט - קריאת הדפסה ממוצע חישוב מינימום מציאת למשל

אחת מפעם יותר המטלה אותה על לחזור רוצים) ברצף) דווקא לאו

? הקוד אותו את שוב לכתוב חייבים האם 2מניע:

, סדר למען משמעות בעלי לחלקים הקוד את לחלק רוצים , נוחה כתיבה נוחה קריאה

?

Page 4: מבוא למדעי המחשב לתעשייה וניהול

subroutineשגרה -

Page 5: מבוא למדעי המחשב לתעשייה וניהול

Subשגרה - ( " המבצעות( subroutineשגרה" פקודות של אוסף היא

מטלה ב מתחילה השגרה

Sub nameOfSub() ב ומסתיימת

End Sub. השגרה את המרכיבות הפקודות יופיעו באמצע

ב פקודות Subנשתמש מספר של באיגוד צורך יש כאשרביחד

הפקודות איןו ביצוע לאחר משוב במתן צורך

Page 6: מבוא למדעי המחשב לתעשייה וניהול

Subל 1דוגמא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

Page 7: מבוא למדעי המחשב לתעשייה וניהול

Subל 2דוגמא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

Page 8: מבוא למדעי המחשב לתעשייה וניהול

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

Sub ל 3דוגמא

Page 9: מבוא למדעי המחשב לתעשייה וניהול

פרמטר- Sub ל 4דוגמא העברת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

Page 10: מבוא למדעי המחשב לתעשייה וניהול

פרמטרים- Sub ל 5דוגמא כמה העברת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

Page 11: מבוא למדעי המחשב לתעשייה וניהול

משתנים של הגדרה תחומי בקוד שונים במקומות משתנים להגדיר ניתן

( המשתנה איפה ההגדרה תחום על משפיע ההגדרה מיקוםמוכר(

( מקומי (localמשתנה שמוכר שגרה רקמשתנה מסוימתבתוך בתוך בתוך ) Subמגדירים (Functionאו

( גלובלי (globalמשתנה התוכנית חלקי בכל שמוכר משתנה , בתוך לא המודול בתוך בתוך ) Subמגדירים לא וגם

Function)

Page 12: מבוא למדעי המחשב לתעשייה וניהול

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

Sub ל 6דוגמא הגדרה תחומי

Page 13: מבוא למדעי המחשב לתעשייה וניהול

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

Sub ל 7דוגמא

Page 14: מבוא למדעי המחשב לתעשייה וניהול

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

!Sub ל 8דוגמא המשתנים- של הגדרה לתחומי לב שימו

Page 15: מבוא למדעי המחשב לתעשייה וניהול

עד שלמדנו מה לתרגול דוגמאותכה...

Page 16: מבוא למדעי המחשב לתעשייה וניהול

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

: ממחרוזת רווחים הורדת

Page 17: מבוא למדעי המחשב לתעשייה וניהול

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)

: ספרות היפוך

Page 18: מבוא למדעי המחשב לתעשייה וניהול

: ' ... י פיבונאצ סדרת מתמטיקה קצת' י פיבונאצ   סדרת הראשונים  שאיבריה הסדרה -1היא , 1ו

קודמיו שני לסכום שווה בה אחר איבר וכל

Page 19: מבוא למדעי המחשב לתעשייה וניהול

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

: ' י פיבונאצ סדרת

Page 20: מבוא למדעי המחשב לתעשייה וניהול

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)

 

: ניחוש משחק

Page 21: מבוא למדעי המחשב לתעשייה וניהול

Dim b As Integer = 2, i As Integer = 3

?'iו bהאם חיוני לקבוע את ערכי

Dim degel_Int As Integer = 0

Randomize()

b = CInt(Rnd() * 60 + 10)

Console.WriteLine(b)

For i = 2 To b - 1

If b Mod i = 0 Then

degel_Int = 1

End IfNext

If degel_Int = 0 Then

Console.WriteLine("yes")

Else

Console.WriteLine("no")

End If

הוא אקראי מספר האם:) א ) ראשוני

Page 22: מבוא למדעי המחשב לתעשייה וניהול

Dim b As Integer = 2, i As Integer = 3

Dim degel As Boolean = False

Randomize()

b = CInt(Rnd() * 60 + 10)

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

הוא אקראי מספר האם:) ב ) ראשוני

Page 23: מבוא למדעי המחשב לתעשייה וניהול

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)

: אקראי מספר של עצרת