Built-in functions from the insert
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A receipt joins a number to text
- Every Paper 2 has an insert of built-in functions. Anything not in the pseudocode guide is printed there, and an answer may call it.
LEFT(Text, N)copies the firstNcharacters andRIGHT(Text, N)the lastN.MID(Text, Start, N)copiesNcharacters from positionStart. Positions start at 1.NUM_TO_STR(4.5)is the string"4.5". Join it to words with&:&joins strings only.
English
DECLARE Code : STRING
DECLARE Price : REAL
Code ← "LON-2026-B"
OUTPUT "City: " & LEFT(Code, 3)
OUTPUT "Year: " & MID(Code, 5, 4)
OUTPUT "Gate: " & RIGHT(Code, 1)
Price ← 3 * 1.5
OUTPUT "Total: " & NUM_TO_STR(Price)
"Total: " & Priceis wrong:Priceis aREAL, so turn it into text withNUM_TO_STRfirst.
English
Check before you convert
- Text typed by a user, or read from a file, is a
STRING, even when it looks like a number. IS_NUM(Text)isTRUEonly when the whole string is a number:"12a"and""giveFALSE.STR_TO_NUM(Text)turns the string into a number you can calculate with. Call it only afterIS_NUMsays the text is safe.
Text |
IS_NUM(Text) |
STR_TO_NUM(Text) |
|---|---|---|
"42" |
TRUE |
42 |
"3.5" |
TRUE |
3.5 |
"12a" |
FALSE |
do not call it |
English
DECLARE Entry : STRING
DECLARE Index : INTEGER
DECLARE Total : REAL
DECLARE Entries : ARRAY[1:4] OF STRING
Entries[1] ← "12"
Entries[2] ← "seven"
Entries[3] ← "30"
Entries[4] ← "4.5"
Total ← 0
FOR Index ← 1 TO 4
Entry ← Entries[Index]
IF IS_NUM(Entry) THEN
Total ← Total + STR_TO_NUM(Entry)
ELSE
OUTPUT "Skipped: " & Entry
ENDIF
NEXT Index
OUTPUT "Total: " & NUM_TO_STR(Total)
English
ASC and CHR
ASC('A')is the integer code65;'B'is66, and so on.CHR(72)is the character'H'.- Adding to a code moves along the alphabet:
CHR(ASC('C') + 2)is'E'.
English
DECLARE Letter : CHAR
Letter ← 'C'
OUTPUT ASC(Letter)
OUTPUT CHR(ASC(Letter) + 2)
OUTPUT CHR(ASC('a') - 32)
- Lower-case codes are 32 more than upper-case ones (
'a'is 97), so- 32gives the capital.TO_UPPERfrom the insert does this for you.
English
Common mistakes
| Wrong | Right | Why |
|---|---|---|
"Score: " & 87.5 |
"Score: " & NUM_TO_STR(87.5) |
& joins strings only |
STR_TO_NUM(Text) with no check |
IF IS_NUM(Text) THEN … first |
text such as "12a" is not a number |
UCASE("hello") |
TO_UPPER("hello") |
UCASE takes one character |
LEFT(Code, 0) for the first letter |
LEFT(Code, 1) |
the second value is how many characters |
MID(Code, 0, 3) |
MID(Code, 1, 3) |
positions start at 1 |
English
Now you try
- The tasks use
"Computer",87.5and"23". Choose the function, then check that the output has exactly the words and number asked for.
English
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
输出 LEFT("Computer", 3).
点击“运行”查看此处输出。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
输出 Score: 87.5 on one line by joining the word and NUM_TO_STR(87.5) with &.
点击“运行”查看此处输出。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Text is(是) "23". If IS_NUM(Text), output STR_TO_NUM(Text) + 1. Otherwise output bad.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Read a string containing a numeric value or invalid text. If IS_NUM is true, output twice its numeric value; otherwise output invalid.
点击“运行”查看此处输出。