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

50
םםםם םםםםםם םםםםםםםםםםם( םםםםם םםםם- םםםםם) ם"ם םםם םםםםםםם ם"ם םםםםםם םםםםםםםם

Upload: ellery

Post on 22-Jan-2016

57 views

Category:

Documents


0 download

DESCRIPTION

מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע (. ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון. שלום!. המייל של אבי: [email protected] המייל של אריאלה: [email protected] כתובת האתר: www.jct.ac.il /~richards/mivne-algo.htm. מה לומדים?. מבנה נתונים אלגוריתמים תכנות WINDOWS. דרישות הקורס. - PowerPoint PPT Presentation

TRANSCRIPT

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

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

ד"ר אבי רוזנפלד

ד"ר אריאלה ריכרדסון

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

שלום!

[email protected]המייל של אבי: •[email protected]המייל של אריאלה: •

כתובת האתר:•

www.jct.ac.il/~richards/mivne-algo.htm

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

מה לומדים?

מבנה נתונים•אלגוריתמים•WINDOWSתכנות •

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

דרישות הקורס

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

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

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

Strings

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

שקף מאת מאיר קומר

Strings - תזכורת: מחרוזות

Dim s As String

s = “hello”

s = s & “kita”

או

s = s + “kita”

hellohellokita

Page 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(

:פעולות מותרות

:פעולות אסורות

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

אז מה עושים?•Lengthמחזירה אורך של מחרוזת •Remove)start, count ( מסירה count תווים החל מהמקום start

ומחזירה את המחרוזת החדשה–

•Insert)start, str( מוסיפה את המחרוזת str החל מהמקום start ומחזירה את המחרוזת החדשה–

•Replace)str1, str2( מחליפה את המחרוזת או התו str1 במחרוזת או תו str2בכל מקרה מחזירה את המחרוזת החדשה–

•IndexOf)str(, IndexOf)str, start( )מחזירה מיקום המחרוזת )או תו str במחרוזת

1, אם לא נמצא מחזיר -start או מ 0מיקום מתחיל מ –

•LastIndexOf)str(, LastIndexOf)str, end( מחרוזת )או מסוף מחזירה מיקום strהתו(

1, אם לא נמצא מחזיר -endמיקום מתחיל מסוף המחרוזת או מ –

•SubString)start, count ( מייצרת מחרוזת של count תווים החל מהמקום startומחזירה את המחרוזת החדשה–

•Chars)start(לקריאה בלבד! אי אפשר לבצע השמה לתוך –

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

שימו לב!

בכל הפונקציות שהצגנו השינוי אינו מתבצע במחרוזת עליה פעלנו:•

s = "hello"

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

s = "hello"

s = s.Replace)"h", "y"( "yello יש "sבמחרוזת עכשיו •

Page 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

Page 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 SubEnd Module

Page 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 SubEnd Module

Page 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

Page 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 SubEnd Module

Page 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 SubEnd Module

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

שיטות נוספות לביצוע פעולות על VBמחרוזות ב

• Len• Left• Right• Mid

הערה:•0 ולא מ 1בשיטות אלו הספירה מתחילה מ •)בניגוד למה שהכרנו(•

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

שקף מאת מאיר קומר

מחזירה אורך המחרוזת

Module Module1 Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) End SubEnd Module

מחרוזותפעולות על

Len

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

שקף מאת מאיר קומר

מחזירה תת-מחרוזת משמאל

a = “hello kita”x = Left )a,2(

מחרוזותפעולות על

Left

heאיזה כמה תויםמחרוזת

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

שקף מאת מאיר קומר

מחזירה תת-מחרוזת מימין

a = “hello kita”x = Right )a,6(

מחרוזותפעולות על

Right

o kitaאיזה כמה תויםמחרוזת

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

שקף מאת מאיר קומר

מחזירה תת-מחרוזת

a = “hello kita”x = Mid )a,2,3(

מחרוזותפעולות על

Mid

ellהחל מתו-כמה תוים

Page 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(? לא

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

מערכים

Page 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 pos {0} I have {1} ", i, x(i)) Next End SubEnd Module

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

פונקציות קיימות ...

במערך חד מימדי:x.Length)(Array.Resize)x, i(Array.Sort)x(Array.Reverse)x(

במערך דו מימדי:x.Length)(x.GetLength)0(x.GetLength)1(

Page 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

Page 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

Page 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 322324 404 408 408 453 467 479 495 498 513 519 544 554 570 572584 597 619 633 642 684 703 704 720 731 737 739 776 778 786798 842 864 876 884 904 971 974 974 997 998Now printing the array998 997 974 974 971 904 884 876 864 842 798 786 778 776 739737 731 720 704 703 684 642 633 619 597 584 572 570 554 544519 513 498 495 479 467 453 408 408 404 324 322 315 272 264262 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 -996Now printing the array842 864 876 884 904 971 974 974 997 998

Page 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 SubEnd Module

פלט:Now printing the array1 2 53 3 1 23Now printing the array1 2 53 3 1 23 0 0 0 0 0Now printing the array0 0 0 0 0 0 0 0 0 0 0 0 0 0 00

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

Structure

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

תזכורת

דרך לבנות מבנה נתונים בסיסי•–Structure

מייצר "טיפוס" חדש•מתאים כאשר רוצים לאגד כמה משתנים יחד•

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

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

Structure

סינטקס:

Structure שם המבנה

1משתנה

2משתנה

...

End Structure

:דוגמא

Structure Oved Dim name As String Dim maskoret As IntegerEnd Structure

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

Structs )Structures(Module Module1 Structure Oved Dim name As String Dim maskoret As Integer End Structure

Sub Main() Dim x As Oved x.name = "Avi" x.maskoret = 1234 Dim People(10) As Oved 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()) End SubEnd Module

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

Structureמערך של

C++ Programming: From Problem Analysis to Program Design, Fifth Edition

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

דוגמא עם לולאותModule Module1 Structure Oved Dim name As String Dim maskoret As Integer End Structure Sub Main() Dim People(10) As Oved Dim i As Integer For i = 0 To 2 Console.WriteLine("Please enter person number " & i) People(i).name = Console.ReadLine People(i).maskoret = Console.ReadLine() Next Dim min As Integer = People(0).maskoret Dim temp As String = People(0).name For i = 1 To 2 If (People(i).maskoret < min) Then min = People(i).maskoret temp = People(i).name End If Next Console.WriteLine("The min is " & min & " his name is " & temp) End SubEnd Module

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

Module Module1 Structure Oved Dim name As String Dim maskoret As Integer End Structure Sub Main() Dim People(10) As Oved Dim i As Integer For i = 0 To 2 Console.WriteLine("Please enter person number " & i) People(i).name = Console.ReadLine People(i).maskoret = Console.ReadLine() Next Dim min As Integer = 0 Dim temp As String = People(0).name For i = 1 To 2 If (People(i).maskoret < People(min).maskoret) Then min = i End If Next Console.WriteLine("The min is " & People(min).maskoret & " his name is " & People(min).name) End SubEnd Module

מה ההבדל?

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

עם פונקציה...

Structure Oved Public name As String Public maskoret As IntegerEnd Structure

Sub Main() Dim People(5) As Oved Dim i As Integer For i = 0 To People.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))End Sub

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

הפונקציה

Function PrintHighest(ByVal x() As Oved) As Integer Dim i As Integer Dim high As Integer = x(0).maskoret For i = 0 To x.Length - 1 If x(i).maskoret > high Then high = x(i).maskoret End If Next Return highEnd Function

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

Module Module1 Sub Main() Dim x As Oved x.name = "Avi" x.maskoret = 1234 Dim People(10) As Oved People(0).name = "Yossi" People(1).name = "Moshe" People(2).name = "Lea" Dim value As Integer = PrintHighest(People) Console.WriteLine("Highest is " & value) End Sub Structure Oved Public name As String Public maskoret As Integer End Structure Function PrintHighest(ByVal x() As Oved) As Integer Dim i As Integer Dim high As Integer = x(0).maskoret For i = 0 To x.Length - 1 If x(i).maskoret > high Then high = x(i).maskoret End If Next Return high End FunctionEnd Module

מה יופיע פה?

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

תרגיל – תכנון חנות

ברצוננו לייצר טבלה המכילה את רשימת המוצרים בחנות.•לכל מוצר יש:•

שם•ברקוד•

מחיר•

Structure Shop Dim name As String Dim code As Integer Dim price As DoubleEnd Structure

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

Module Module1 Structure Shop Dim name As String Dim code As Integer Dim price As Double End Structure

Sub Main() Dim makolet(10) As Shop 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 Dim i As Integer For i = 0 To makolet.Length() - 1 Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price) Next End SubEnd Module

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25Product 3 is: , its price is: 0Product 4 is: , its price is: 0Product 5 is: , its price is: 0Product 6 is: , its price is: 0Product 7 is: , its price is: 0Product 8 is: , its price is: 0Product 9 is: , its price is: 0Product 10 is: , its price is: 0

מימוש חנות

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

Module Module1 Structure Shop Dim name As String Dim code As Integer Dim price As Double End Structure Sub Main() Dim makolet(10) As Shop 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 Dim i As Integer For i = 0 To makolet.Length() - 1 If makolet(i).name <> Nothing Then Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, makolet(i).name, makolet(i).price) End If Next End SubEnd Module

בלי הדפסת מוצרים ריקים

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25

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

Module Module1 Structure Shop Dim name As String Dim code As Integer Dim price As Double End Structure Sub Main() Dim makolet(10) As Shop 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) End Sub Sub PrintPrice(ByVal s() As Shop) Dim i As Integer For i = 0 To s.Length() - 1 If s(i).name <> Nothing Then Console.WriteLine("Product {0} is: {1}, its price is: {2}", i, s(i).name, s(i).price) End If Next End SubEnd Module

עם פונקציה

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45Product 2 is: Shoko, its price is: 4.25

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

רוצים יכולת למצוא את שם המוצר הכי זול

נכתוב פונקציה•נרצה להעביר לפונקציה את המערך של החנות•נרצה לקבל מהפונקציה את השם של המוצר•שימו לב – הפונקציה לא מדפיסה שום דבר!!!•

Function GetZol(ByVal s() As Shop) As String Dim i As Integer Dim idx As Integer = 0 Dim min As Double = s(0).price For i = 1 To s.Length() - 1 If s(i).name <> Nothing And s(i).price < min Then min = s(i).price idx = i End If Next Return s(idx).name End Function

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

הדפסת רשימת המוצרים המלאה ואת המוצר הכי זול במכולת

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

נשתמש בפונקציה למציאת שם המוצר, ואז נדפיס•

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

Sub Main() Dim makolet(10) As Shop 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) Dim zol As String zol = GetZol(makolet) Console.WriteLine("The cheapest thing in makolet is " & zol)

End Sub

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is Cheese

:הקוד

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

Sub Main() Dim makolet(10) As Shop 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) As Shop 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) Dim zol As String 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) End Sub

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is CheeseProduct 0 is: roll, its price is: 6.32Product 1 is: pita, its price is: 3.6The cheapest thing in bakery is pita

ואם יש גם מכולת וגם מאפיה?

נשתמש באותה פונקציה שוב...נעביר לה פרמטר אחר, ונקבל ממנה

משהו אחר

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

Sub Main() Dim makolet(10) As Shop 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) As Shop 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) Dim zol As String

Console.WriteLine("The cheapest thing in makolet is " & GetZol(makolet))

PrintPrice(bakery) Console.WriteLine("The cheapest thing in makolet is " & GetZol(bakery)) End Sub

Product 0 is: Cheese, its price is: 8.75Product 1 is: Shnitzel, its price is: 21.45The cheapest thing in makolet is CheeseProduct 0 is: roll, its price is: 6.32Product 1 is: pita, its price is: 3.6The cheapest thing in bakery is pita

אפשר גם לשתול את מה שחוזר מהפונקציה

ישר לתוך פקודת הדפסה

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

Function Kupa(ByVal cart() As String, ByVal s() As Shop) As Single Dim total As Single = 0 For i = 0 To cart.Length - 1 For j = 0 To s.Length() - 1 If cart(i) = s(j).name Then total += s(j).price End If Next Next Return total End Function

פונקציה לחישוב סכום המוצרים בסל

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

Sub Main() Dim super(10) As Shop 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() As String = {"Bread", "Shoko"} Console.WriteLine("Lunch costs " & Kupa(lunch, super) & " shekel.") End Sub

Lunch costs 9.8 shekel.

המשך חישוב סכום המוצרים בסל

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

תרגיל

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