1 / 30

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

הרצאה 6. מבוא למדעי המחשב לתעשייה וניהול. מפעל השעווה – לולאות. עד עכשיו טיפלנו בייצור נרות מסוג אחד, במחיר אחיד למדנו להתמודד עם טיפול במקרים שונים (הנחות, מחירים שונים לצבעים שונים וכד') למדנו לחשב כמה נרות צריך (עבור קונה יחיד) לכל החג. נרצה לתת אפשרות לבצע מספר הזמנות ברצף

wayde
Télécharger la présentation

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

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. הרצאה 6 מבוא למדעי המחשב לתעשייה וניהול

  2. מפעל השעווה – לולאות • עד עכשיו • טיפלנו בייצור נרות מסוג אחד, במחיר אחיד • למדנו להתמודד עם טיפול במקרים שונים (הנחות, מחירים שונים לצבעים שונים וכד') • למדנו לחשב כמה נרות צריך (עבור קונה יחיד) לכל החג. • נרצה לתת אפשרות לבצע מספר הזמנות ברצף • כאשר מספר ההזמנות ידוע מראש • כאשר המשתמש יחליט כמה הזמנות לבצע ?

  3. פתרון בעזרת for, כאשר מספר ההזמנות ידוע ConstWaxPerCandleAsInteger = 10 DimstorageWaxAsInteger = 1000 DimtotalWax, buyWax, numCandlesAsInteger totalWax= 0 ForiAsInteger = 1 To3 Console.WriteLine("Enter number of candles for order " & i) numCandles = Console.ReadLine() totalWax += WaxPerCandle * numCandles Next buyWax= totalWax - storageWax Console.WriteLine("Amount of wax to buy: " & buyWax)

  4. While

  5. while • אם התנאי conditionהוא 'אמת' אז ההצהרה statementמתבצעת • אח"כ התנאי נבדק שובאם ערכו עדיין 'אמת' ההצהרה מבוצעת שוב • ההצהרה מבוצעת שוב ושוב עד שהתנאי נהיה 'שקר' • ההצהרה יכולה להכיל מספר פקודות, ותקרא גוף הלולאה While condition statements End While

  6. condition evaluated הביטוי נבדק false הלוגיקה של פקודת While true statement הצהרה מבוצעת

  7. פתרון בעזרת while, כאשר מספר ההזמנות אינו ידוע ConstWaxPerCandleAsInteger = 10 DimstorageWaxAsInteger = 1000 DimtotalWax, buyWax, numCandlesAsInteger totalWax= 0 Console.WriteLine("Enter number of candles for order ") numCandles= Console.ReadLine() WhilenumCandles <> 0 totalWax += WaxPerCandle * numCandles Console.WriteLine("Enter number of candles for order ") numCandles = Console.ReadLine() EndWhile buyWax= totalWax - storageWax Console.WriteLine("Amount of wax to buy: " & buyWax)

  8. דוגמא לשימוש בלולאה – חישוב ממוצע • הבעיה: • נקלוט מספרים ממשתמש, נחשב את ממוצע המספרים. • לא נקבע מראש כמה מספרים לקלוט • פתרון: • נקלוט מספר חדש • נוסיף אותו לסכום הביניים + נעדכן את המונה • כשנחליט לעצור • נחלק את סכום הביניים במונה • איך נדע לעצור? • נבחר ערך שמייצג רצון לעצור

  9. קודלחישוב ממוצע – חלק 1 Dimsum AsInteger = 0, count AsInteger = 0 Dimvalue, average AsSingle Console.WriteLine("Enter an integer (0 to quit): ") value = Console.ReadLine Whilevalue <> 0 count += 1 sum += value Console.WriteLine("Enter an integer (0 to quit): ") value = Console.ReadLine EndWhile המשך בשקף הבא

  10. קוד לחישוב ממוצע – חלק 2 המשך משקף קודם... Ifcount = 0 Then Console.WriteLine("No values were entered.") Else average = sum / count EndIf Console.WriteLine("The average is " & average)

  11. דוגמא לשימוש בלולאה – בדיקת קלט • הבעיה: • רוצים לקלוט מספרים מהמשתמש • דרוש לקלוט מספרים מסוג מסוים • פתרון: • נשתמש בלולאה • כל עוד לא קיבלנו קלט מתאים • נבקש ונקלוט קלט נוסף

  12. קוד- בדיקת קלט Dim value AsSingle Console.WriteLine("Enter a number between 1 to 10") value = Console.ReadLine Whilevalue < 1 Or value > 10 Console.WriteLine("Bad input. Enter number between 1 to 10") value = Console.ReadLine EndWhile Console.WriteLine("You entered {0}. Thank you!", value)

  13. יציאה מ while • יציאה מתבצעת כאשר התנאי כבר לא מתקיים • ניתן להתערב בהתנהלות של הלולאה (כמו ב for) בצורות הבאות: • פקודת Continueגורמת ללולאה לדלג לסיבוב הבא • פקודת Exit גורמת ללולאה להסתיים לחלוטין Dimx AsInteger = 0 Whilex < 10 If x = 3 Then Exit While EndIf Console.WriteLine("the number is: " & x) x += 1 EndWhile

  14. מה קורה? Dim x AsInteger = 0 Whilex < 10 Console.WriteLine("the number is: " & x) If x = 5 Then Continue While EndIf x += 1 EndWhile Dim x AsInteger = 0 Whilex > 10 Console.WriteLine("the number is: " & x) x += 1 EndWhile

  15. מה עושה הקוד? Dim x AsInteger x = Console.Read() While x <> AscW(vbCr) If x >= AscW(0) And x =< AscW(9) Then Console.Write(ChrW(x)) EndIf x = Console.Read() EndWhile

  16. Do … loop

  17. Do Until … Loop Dim counter AsInteger = 0 DoUntil (counter = 10) Console.WriteLine("What is this " & counter) counter = counter + 1 Loop Console.ReadKey()

  18. Do … loop Do statements Loop While condition Do While condition statements Loop Do Until condition statements Loop Do statements Loop Until condition While – כל עוד Until – עד ש

  19. condition evaluated הביטוי נבדק condition evaluated הביטוי נבדק false false statement הצהרה מבוצעת השוואה בין גרסאות do loop Do While\Until …. Loop Do …. Loop While\Until true true statement הצהרה מבוצעת

  20. השוואה בין do ל while Dimlow AsInteger = 0 Dimhigh AsInteger = 10 Do Console.WriteLine(low) low += 1 LoopWhile (low < high) Dim low AsInteger = 0 Dimhigh AsInteger = 10 DoWhile (low < high) Console.WriteLine(low) low += 1 Loop Dimlow AsInteger = 0 Dimhigh AsInteger = 10 Do Console.WriteLine(low) low += 1 LoopWhile (high < low) Dim low AsInteger = 0 Dimhigh AsInteger = 10 DoWhile (high < low) Console.WriteLine(low) low += 1 Loop

  21. שימוש ב while – לולאה עד אירוע Dim x AsChar Dimcounter AsInteger = 0 x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 EndIf While (x <> ".") x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 EndIf EndWhile Console.WriteLine("The counter is " & counter)

  22. שיפור ע"י Do while Dim x AsChar Dimcounter AsInteger = 0 Do x = ChrW(Console.Read()) If (x <> ".") Then counter = counter + 1 EndIf LoopWhile (x <> ".") Console.WriteLine("The counter is " & counter)

  23. מה השתנה? Dimx AsChar Dimcounter AsInteger = 0 x = ChrW(Console.Read()) DoUntil (x = ".") x = ChrW(Console.Read()) counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

  24. ומה עכשיו ? Dim x AsChar Dimcounter AsInteger = 0 x = Console.ReadLine() DoUntil (x = ".") x = Console.ReadLine() counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

  25. ועוד גיוון... Dim x AsString Dimcounter AsInteger = 0 DoUntil (x = "stop") Console.WriteLine("Please enter a word") x = Console.ReadLine() counter = counter + 1 Loop Console.WriteLine("The counter is " & counter)

  26. עבודה עם קבצים ולולאות

  27. קריאה מתוך קובץ - הרחבה • ראינו שמאתחלים את שם הקובץ כך: writer =My.Computer.FileSystem.OpenTextFileWriter("שם קובץ") • ניתן גם להשתמש ב : • writer =File.OpenText("שם קובץ")

  28. לולאה בקבצים עד אירוע DimInput AsInteger Dimsum = 0, count = 0 DimobjStreamReaderAsStreamReader objStreamReader= File.OpenText("z:\NumTest.txt") DoUntil Input = -1 Input = objStreamReader.ReadLine() If (Input <> -1) Then sum += Input count += 1 EndIf Loop Console.WriteLine("The Average is " & sum / count)

  29. אפשר לוותר על ה if, ע"י השינוי הבא: DimInput AsInteger = 0 Dimsum = 0, count = 0 DimobjStreamReaderAsStreamReader objStreamReader= File.OpenText("z:\NumTest.txt") DoUntil Input = -1 sum += Input count += 1 Input = objStreamReader.ReadLine() Loop Console.WriteLine("The Average is " & sum / (count - 1))

  30. לולאה עד סוף הקובץ DimInput AsString = "" DimobjStreamReaderAsStreamReader objStreamReader= File.OpenText("z:\NumTest.txt") Do Input = objStreamReader.ReadLine() Console.WriteLine("I read " & Input) LoopUntil Input IsNothing

More Related