Operators, strings and subroutines
הוצג
13:08
תכנות מחשב
הנה תוכנית קצרה. היא קוראת חמש ציוני מבחן והדפיסה את הסכום הכולל. החמש ציונים הם שמונה, תשע, שבע, שש ועשר, ולכן הסכום הכולל אמור להיות ארבעים. הצפו…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| program/ˈprəʊɡræm/ | A set of instructions specifying operations for a computer to carry out. |
| values/ˈvæljuːz/ | |
| operators/ˈɒpəreɪtəz/ | אופרטורים |
| strings/strɪŋz/ | LENGTH, SUBSTRING , UCASE, LCASE. |
| code/kəʊd/ | |
| comments/ˈkɒments/ | Explanatory text in source code or markup that does not execute as a program instruction. |
| Arithmetic/əˈrɪθmətɪk/ | |
| power/ˈpaʊə/ | |
| library routines/ˈlaɪbrəri ruːˈtiːnz/ | Reusable functions or procedures supplied in a collection for programs to call. |
| remainder/rɪˈmeɪndə/ | For integer division here, the amount left after taking whole groups of the divisor. |
| Relational/rɪˈleɪʃənl/ | |
| Boolean/ˈbuːlɪən/ | A logical value that is either true or false. |
| Logical/ˈlɒdʒɪkl/ | |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| LENGTH/leŋθ/ | |
| SUBSTRING/ˈsʌbstrɪŋ/ | |
| UCASE | |
| LCASE | |
| procedure/prəˈsiːdʒə/ | פרוצדורה |
| function/ˈfʌŋkʃn/ | פונקציה |
| RETURN/rɪˈtɜːn/ | |
| parameter/pəˈræmɪtə/ | פרמטר |
| local variable/ˈləʊkl ˈveərɪəbl/ | משתנה מקומי |
| subroutine/ˈsʌbruːtiːn/ | |
| global/ˈɡləʊbl/ | גלובלי |
| CALL/kɔːl/ |
הכלים של תוכנית
- תוכניות משלבות ערכים עם אופרטורים ועובדות עם מחרוזות.
- כדי למנוע חזרה על כתיבת קוד, אנו משתמשים ב-פרוצדורות וב-פונקציות.
- שמירה טובה והערות מתאימות הופכות תוכנה לתחזוקה.
אופרטורים
- אריטימטי:
+ - * /,^(חזקה),MOD(שארית),DIV(חלק שלם). לדוגמה:17 MOD 5 = 2,17 DIV 5 = 3. - יחסים (מחזירים בוליאן):
= < <= > >= <>(לא שווה). - לוגיים:
AND(שניהם נכונים),OR(לפחות אחד),NOT(הפיכת ערך).

לולאת תנאי מקדים (WHILE) בודקת לפני הגוף, ולכן ייתכן שתפעיל אפס פעמים; לולאת תנאי סוף (REPEAT) בודקת לאחר, ולכן תפעיל לפחות פעם אחת.

What is 17 MOD 5?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2.
What is 17 DIV 5?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded).
עיבוד מחרוזות
- מחרוזת מורכבת מאותיות. פעולות שימושיות:
- LENGTH (מספר האותיות), SUBSTRING (חלק ממנה), UCASE (אותיות גדולות), LCASE (אותיות קטנות).
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"

משתנה הוא אחסון בעל שם שערכו יכול להשתנות
SUBSTRING: take part of a string
The first character is at position 1. SUBSTRING(Word, Start, Length) takes Length characters beginning at Start — drag the two handles and read the call it builds.
What does LENGTH("Computer") return?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
"Computer" has 8 characters.
Match each operator or string function to what it gives.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings.
פרוצדורות, פונקציות וסגנון טוב
- פרוצדורה מבצעת משימה (מתבצעת עם
CALL); פונקציה מחזירה ערך. - פרמטר הוא ערך המועבר לתוך הפונקציה (עד שלושה).
- משתנה מקומית קיימת רק בתוך התת-תוכנית (בטוח יותר); גלובלית נראית בכל מקום.
- שגרת ספרייה:
MOD,DIV,ROUND,RANDOM. - תוכנית תחזוקה משתמשת בשמות משמעותיים, תגיות הסבר ותת-תוכניות.

הסוגים הבסיסיים לנתונים: שלם, ממשי, קרואקטר, מחרוזת ובוליאני
A local variable:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Locals are safer because other parts of the program cannot change them by mistake.
A function returns a value (used in an expression) while a procedure just performs a task, and a local variable can only be used inside the subroutine where it is declared.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain.
הכרטיס הזה חדש, לכן הוא מוצג באנגלית עד שהתרגום שלו יהיה מוכן.
Worked example: minutes to hours
A film lasts 135 minutes. Use DIV and MOD to calculate its length in hours and minutes, then write a function that returns 返回 the whole number of hours.
DIV(135, 60)= 2 (the whole number of hours) andMOD(135, 60)= 15 (the remainder), so the film is 2 hours 15 minutes.- As a function with a parameter, it returns a value to the program that called 调用 it:
FUNCTION Hours(Mins : INTEGER) RETURNS INTEGER
RETURN DIV(Mins, 60)
ENDFUNCTION

What is 23 MOD 4?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
23 = 5 × 4 + 3, so the remainder is 3.
Name ← "Shanghai". What does SUBSTRING(Name, 1, 5) return?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
It takes 5 characters starting at position 1.
הבנתך מושלמת
MOD= שארית,DIV= חלק שלם; לוגיAND/OR/NOT- מחרוזות: אורך, תת-מחרוזת, אות גדולה, אות קטנה
- תת-תוכנית מבצעת משימה; פונקציה מחזירה ערך; פרמטרים מעבירים ערכים פנימה
- משתנים מקומיים בטוחים יותר מאשר גלובליים; תחזוקה = שמות משמעותיים + תגיות הסבר + תת-תוכניות
מה השתבש? רק צוות האתר רואה זאת.