1 / 29

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

הרצאה 4. מבוא למדעי המחשב לתעשייה וניהול. Select Case. Select Case – לבחירה בין מספר ערכים. Dim letter As Char Console .WriteLine ( "Please enter a letter:" ) letter = Console .ReadLine () Select Case letter Case "a" Console .WriteLine ( "you selected the first letter" ) Case "b"

osborn
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. הרצאה 4 מבוא למדעי המחשב לתעשייה וניהול

  2. Select Case

  3. Select Case – לבחירה בין מספר ערכים Dimletter AsChar Console.WriteLine("Please enter a letter:") letter = Console.ReadLine() SelectCase letter Case"a" Console.WriteLine("you selected the first letter") Case"b" Console.WriteLine("you selected the second letter") Case"c" Console.WriteLine("you selected the third letter") Case"z" Console.WriteLine("you selected the last letter") Case Else Console.WriteLine("you selected a regular letter") EndSelect

  4. Select Case – דוגמא נוספת Dim letter AsInteger Console.WriteLine("Please enter a letter:") letter = Console.ReadLine() SelectCase letter Case 1 Console.WriteLine("you selected the first letter") Case 2 Console.WriteLine("you selected the second letter") Case 3 Console.WriteLine("you selected the third letter") Case 4 Console.WriteLine("you selected the last letter") CaseElse Console.WriteLine("you selected a regular letter") EndSelect

  5. Select Case – ריבוי אפשרויות Dim number AsInteger = 8 SelectCase number Case 1 To 5 Console.WriteLine("Between 1 and 5, inclusive") Case6, 7, 8 Console.WriteLine("Between 6 and 8, inclusive") Case9 To 10 Console.WriteLine("Equal to 9 or 10") Case Else Console.WriteLine("Not between 1 and 10, inclusive") EndSelect

  6. החלפת ערכים בין 2 משתנים

  7. החלפת ערכים • תוכנית מחשב למשחק קלפים "טאקי" עוקבת אחר מספר הקלפים שיש לכל שחקן. כאשר לשחקן יש 0 קלפים התוכנית תכריז עליו כמנצח. • בעיה: • נניח שלאלי יש 8 קלפים ואילו לבניה יש 2 קלפים. אלי מניח קלף מסוג "החלף קלפים", ועלינו להחליף את מספר הקלפים שיש לכל שחקן. מהו האלגוריתם המתאים לפתרון הבעיה?

  8. החלפת ערכיהם של 2 משתנים 'program to swap a and z Dima, b AsInteger a = 8 b = 2 Console.WriteLine("a is {0} and b is {1} ", a, b) Dim temp AsInteger temp = a a = b b = temp Console.WriteLine("a is {0} and b is {1} ", a, b)

  9. ערכים עוקבים( + שימוש בערך בוליאני)

  10. בדיקת ערכים עוקבים – פלט בוליאני • בעיה: • בהינתן שני ערכים x1, x2, יש לבדוק אם x2 הוא מספר עוקב (consecutive ) של x1, או להיפך • הפלט יהיה בוליאני (אמת או שקר) • מהו האלגוריתם המתאים לפתרון הבעיה? (בהינתן x1 x2) • אם x2 עוקב לx1או x1 עוקב לx2 • הצג true • אחרת • הצג false

  11. בדיקת ערכים עוקבים – פתרון 1 (שימוש ב if) Dim x1, x2 AsInteger Console.WriteLine("please enter two numbers") x1 = Console.ReadLine() x2 = Console.ReadLine() If x2 = x1 + 1 Or x1 = x2 + 1 Then Console.WriteLine("true") Else Console.WriteLine("false") EndIf

  12. בדיקת ערכים עוקבים – פתרון 2 (עם משתנה בוליאני) Dim x1, x2 AsInteger Console.WriteLine("please enter two numbers") x1 = Console.ReadLine() x2 = Console.ReadLine() DimconsecAsBoolean consec = (x2 = x1 + 1 Or x1 = x2 + 1) Console.WriteLine(consec)

  13. מספרים אקראיים

  14. ייצור של מספר אקראי • ייצור מספר אקראי מתבצע ע"י שימוש ב Rnd Dim a As Single = Rnd() • Rnd מחזיר ערך בין 0 ל1 (לא כולל 1) • אם רוצים לקבל מספר אקראי שלם בין 1 ל 100 Dim a As Single a = Int((100 * Rnd()) + 1) • המספרים במחשב אינם באמת אקראיים • הם פסיאודו אקראיים • כדי שלא נקבל תמיד אותו מספר נקרא קודם ל Randomize

  15. דוגמא לייצור מספר אקראי DimrandNumAsInteger Randomize() randNum = Int((10 * Rnd()) + 1) Console.WriteLine("My random number is " & randNum)

  16. זוגיות מספר

  17. בדיקת זוגיות של מספר • בעיה: • בהינתן מספר X בדוק האם הוא זוגי או אי-זוגי • מהו האלגוריתם המתאים לפתרון הבעיה? (בהינתן x) • אם x מתחלק ב 2 ללא שארית • המספר זוגי • אחרת • המספר אי-זוגי

  18. בדיקת זוגיות של מספר Dimx AsInteger Console.WriteLine("please enter number") x = Console.ReadLine() If x Mod 2 = 0 Then Console.WriteLine("zugi") Else Console.WriteLine("izugi") EndIf

  19. תרגיל כיתה • נכתוב משחק ניחוש • התוכנית תבחר מספר אקראי בין 1 ל 3 והמשתמש יצטרך לנחש, נודיע לו אם ניחש נכון.

  20. עבודה עם תו בודד

  21. 72 105 44 32 72 101 97 116 104 101 114 46 ייצוג טקסט באופן דיגיטלי • כל תו נשמר כמספר • כולל רווחים, פיסוק וספרות. • אותיות גדולות וקטנות הן בעלות ערכים שונים. H i , H e a t h e r .

  22. char • הטיפוס char מכיל תו בודד • תו יכול להיות אות ,ספרה, סימן אחר • לכל תו יש ערך מספרי משלו במחשב • הגדרה של תו Dim c AsChar • ניתן להמיר String ל char ע"י CChar() • רק התו הראשון במחרוזת מומר! • על מנת לקבל ערך מספרי של תו DimiAsInteger i = AscW(c) • על מנת להמיר מערך מספרי לסימן ChrW(i) • קריאה של תו Console.Read()

  23. טבלת ASCII (חלקית)

  24. דוגמא למשחקי תווים DimstAsChar, iAsInteger Console.WriteLine("Enter a number with one digit") 'convert first character of string to char st= CChar(Console.ReadLine()) 'convert char to integer according to ASCII table i= AscW(st) Console.WriteLine(i) i= i + 10 Console.WriteLine(i) Console.WriteLine(st) 'convert integer to char according to ASCII table st = ChrW(i) Console.WriteLine(st)

  25. משחקי תווים 1 Dimdelta AsInteger, cap AsInteger, chAsChar delta = Asc("a") - Asc("A") Console.WriteLine("Enter a capital") cap = Console.Read() IfNot (cap < Asc("A") Or cap > Asc("Z")) Then cap = cap + delta ch = ChrW(cap) Console.WriteLine((ch)) Else Console.WriteLine("You didn’t enter a capital") EndIf

  26. משחקי תווים 2 Dim delta AsInteger, aLetterAsInteger, chAsChar delta = Asc("a") - Asc("A") Console.WriteLine("Please enter a word with 3 non-capital letters") aLetter= Console.Read() If(aLetter>= Asc("a") AndaLetter<= Asc("z")) Then aLetter = aLetter - delta ch = ChrW(aLetter) Console.Write((ch)) aLetter = Console.Read() ch = ChrW(aLetter) Console.Write((ch)) aLetter = Console.Read() ch = ChrW(aLetter) Console.WriteLine((ch)) Else Console.WriteLine(“First letter is non capital") EndIf

  27. משחקי תווים 3 – חלק א Dimdelta AsInteger, aLetterAsInteger, chAsChar delta = Asc("a") - Asc("A") Console.WriteLine("Please enter 2 letters") aLetter= Console.Read() If(aLetter >= Asc("a") AndaLetter <= Asc("z")) Then aLetter = aLetter - delta ch = ChrW(aLetter) Console.Write((ch)) ElseIf(aLetter >= Asc("A") AndaLetter <= Asc("Z")) Then aLetter = aLetter + delta ch = ChrW(aLetter) Console.Write((ch)) EndIf המשך בשקף הבא:

  28. משחקי תווים 3 – חלק ב המשך משקף קודם: aLetter= Console.Read() If(aLetter >= Asc("a") AndaLetter <= Asc("z")) Then aLetter = aLetter - delta ch = ChrW(aLetter) Console.Write((ch)) ElseIf(aLetter >= Asc("A") AndaLetter <= Asc("Z")) Then aLetter = aLetter + delta ch = ChrW(aLetter) Console.Write((ch)) EndIf

  29. משחקי תווים 3 – חלק ג בדוגמא הקודמת היו לנו רק 2 אותיות ואם יהיו 3 אותיות? או 100? פתרונות בשיעור הבא...

More Related