이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.
- Write a program which gets a number from user and displays its cube.
| x |
x := stdin nextLine.
x := x asNumber
(x * x * x * x) printNl
- Write a program which gets two numbers from the user separated by a space and displays their arithmetic average. (Hint: You will get an input like
3 4
from the user which is aString
holding two numbers. Usetokenize: aString
message on this string to separate the numbers from each other.tokenize:
message separates a string into pieces, which are called tokens, wherever it finds its argument inside the receiver and returns anArray
which consists of all the tokens.)
| line x y tokens average |
line := stdin nextLine.
tokens := line tokenize: ' '.
x := (tokens at: 1) asNumber.
y := (tokens at: 2) asNumber.
average := (x + y) / 2.
average printNl.
- Write a program which holds definitions of some concepts we saw in Chapter 1 in a
Dictionary
object. The program should ask the user to enter a word then display the corresponding definition.
aDictionary := Dictionary new.
aDictionary at: 'apple' put: 'I like it!'; at: 'banana' put: 'yummy!'.
userInput := stdin nextLine.
(aDictionary at: userInput) printNl