Skip to content

Commit d50a4f1

Browse files
committed
Added more example programs
1 parent 6ab5dbb commit d50a4f1

File tree

8 files changed

+157
-11
lines changed

8 files changed

+157
-11
lines changed

progs/chars.psc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# Output all ASCII 7-bit (UTF-7) characters from 32 to 127, in a table
2+
13
FOR page <- 32 TO 112 STEP 16
2-
s <- ''
3-
FOR char <- 0 TO 15
4-
s <- s + CODE_TO_CHAR(page + char) + ' '
5-
ENDFOR
6-
OUTPUT s
4+
s <- ''
5+
FOR char <- 0 TO 15
6+
s <- s + CODE_TO_CHAR(page + char) + ' '
7+
ENDFOR
8+
OUTPUT s
79
ENDFOR

progs/circle.psc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
CONSTANT PI <- 3.14
1+
# Calculate circumference and area of a circle using formulas
2+
3+
CONSTANT PI <- 3.1416
24

35
SUBROUTINE circumference(radius : Real)
4-
RETURN 2.0 * PI * radius
6+
RETURN 2.0 * PI * radius
57
ENDSUBROUTINE
68

79
SUBROUTINE area(radius : Real)
8-
RETURN PI * radius * radius
10+
RETURN PI * radius * radius
911
ENDSUBROUTINE
1012

11-
FOR n <- 1 TO 4
12-
OUTPUT 'Please enter the radius:'
13+
OUTPUT 'Please enter the radius of the circle:'
1314
r <- STRING_TO_REAL(USERINPUT)
1415
OUTPUT 'Circumference is: ' + REAL_TO_STRING(circumference(r))
1516
OUTPUT 'Area is: ' + REAL_TO_STRING(area(r))
16-
ENDFOR

progs/compare.psc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Compare two Integers with all the boolean tests supported
2+
3+
SUBROUTINE BOOL_TO_STRING(condition : Boolean)
4+
IF condition THEN
5+
str <- 'True'
6+
ELSE
7+
str <- 'False'
8+
ENDIF
9+
RETURN str
10+
ENDSUBROUTINE
11+
12+
OUTPUT 'Please enter an Integer:'
13+
a <- STRING_TO_INT(USERINPUT)
14+
OUTPUT 'Please enter one more Integer:'
15+
b <- STRING_TO_INT(USERINPUT)
16+
OUTPUT 'Sum: ' + INT_TO_STRING(a + b)
17+
difference <- a - b
18+
IF difference < 0 THEN
19+
difference <- -difference
20+
ENDIF
21+
OUTPUT 'Difference: ' + INT_TO_STRING(difference)
22+
OUTPUT 'Product: ' + INT_TO_STRING(a * b)
23+
OUTPUT 'Result of division: ' + INT_TO_STRING(a DIV b) + ', remainder: ' + INT_TO_STRING(a MOD b)
24+
25+
OUTPUT INT_TO_STRING(a) + ' = ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a = b)
26+
OUTPUT INT_TO_STRING(a) + ' /= ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a /= b)
27+
OUTPUT INT_TO_STRING(a) + ' < ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a < b)
28+
OUTPUT INT_TO_STRING(a) + ' <= ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a <= b)
29+
OUTPUT INT_TO_STRING(a) + ' >= ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a >= b)
30+
OUTPUT INT_TO_STRING(a) + ' > ' + INT_TO_STRING(b) + ': ' + BOOL_TO_STRING(a > b)

progs/find.psc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Program which finds tries to find the letter "a" in a string
2+
3+
OUTPUT 'Please enter a word containing the letter "a":'
4+
word <- USERINPUT
5+
OUTPUT 'Word has ' + INT_TO_STRING(LEN(word)) + ' letters'
6+
position <- POSITION(word, 'a')
7+
IF position > 0 THEN
8+
OUTPUT 'Found "a" at position: ' + INT_TO_STRING(position)
9+
ELSE
10+
OUTPUT 'Word does not contain the letter "a"'
11+
ENDIF

progs/guess.psc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Guess a number between 1 and 100 in seven or fewer attempts
2+
3+
CONSTANT MIN <- 1
4+
CONSTANT MAX <- 100
5+
str <- USERINPUT
6+
IF str /= '' THEN
7+
number_to_find <- STRING_TO_INT(str)
8+
ELSE
9+
number_to_find <- RANDOM_INT(MIN,MAX)
10+
ENDIF
11+
OUTPUT 'Try to find: ' + INT_TO_STRING(number_to_find)
12+
13+
guess <- 0
14+
attempts <- 0
15+
low <- MIN
16+
high <- MAX
17+
WHILE guess /= number_to_find
18+
guess <- low + (high - low) DIV 2
19+
attempts <- attempts + 1
20+
OUTPUT 'Guessed: ' + INT_TO_STRING(guess)
21+
IF guess > number_to_find THEN
22+
OUTPUT 'Too high.'
23+
high <- guess - 1
24+
ELSE IF guess < number_to_find THEN
25+
OUTPUT 'Too low.'
26+
low <- guess + 1
27+
ELSE
28+
OUTPUT 'Got it in ' + INT_TO_STRING(attempts) + ' attempts!'
29+
ENDIF
30+
ENDWHILE

progs/hello.psc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Simple "Hello world" program which accepts input
2+
13
OUTPUT 'Please enter your name:'
24
name <- USERINPUT
35
OUTPUT 'Hi, ' + name + '! How are you today?'

progs/matrix.psc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Transforms an (x,y) co-ordinate using 2-by-2 matrix multiplication
2+
3+
OUTPUT 'Please enter x co-ordinate:'
4+
x <- STRING_TO_REAL(USERINPUT)
5+
OUTPUT 'Please enter y co-ordinate:'
6+
y <- STRING_TO_REAL(USERINPUT)
7+
OUTPUT 'Please choose from:'
8+
OUTPUT ' 1) Reflection in x-axis'
9+
OUTPUT ' 2) Reflection in y-axis'
10+
OUTPUT ' 3) Scale by factor 2'
11+
OUTPUT ' 4) Rotation 90 degrees (anti-clockwise)'
12+
transform <- STRING_TO_INT(USERINPUT)
13+
14+
matrix <- [[0.0, 0.0], [0.0, 0.0]]
15+
16+
IF transform = 1 THEN
17+
OUTPUT 'Reflection in x-axis'
18+
matrix[0][0] <- 1.0
19+
matrix[1][1] <- -1.0
20+
ELSE IF transform = 2 THEN
21+
OUTPUT 'Reflection in y-axis'
22+
matrix[0][0] <- -1.0
23+
matrix[1][1] <- 1.0
24+
ELSE IF transform = 3 THEN
25+
OUTPUT 'Scale by factor 2'
26+
matrix[0][0] <- 2.0
27+
matrix[1][1] <- 2.0
28+
ELSE IF transform = 4 THEN
29+
OUTPUT 'Rotation by 90 degrees (anti-clockwise)'
30+
matrix[0][1] <- -1.0
31+
matrix[1][0] <- 1.0
32+
ELSE
33+
OUTPUT 'Bad option'
34+
ENDIF
35+
36+
x_t <- x * matrix[0][0] + y * matrix[0][1]
37+
y_t <- x * matrix[1][0] + y * matrix[1][1]
38+
39+
OUTPUT 'Transformed: (' + REAL_TO_STRING(x_t) + ',' + REAL_TO_STRING(y_t) + ')'

progs/primes.psc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Find the sum of the first N prime numbers
2+
3+
OUTPUT 'Please enter the number of primes to find:'
4+
str <- USERINPUT
5+
IF str /= '' THEN
6+
primes <- STRING_TO_INT(str)
7+
ELSE
8+
OUTPUT 'Okay, I chose 100'
9+
primes <- 100
10+
ENDIF
11+
total <- 0
12+
printout <- ''
13+
n <- 2
14+
WHILE primes > 0
15+
is_prime <- True
16+
p <- 2
17+
WHILE p * p <= n
18+
IF n MOD p = 0 THEN
19+
is_prime <- False
20+
ENDIF
21+
p <- p + 1
22+
ENDWHILE
23+
IF is_prime THEN
24+
printout <- printout + INT_TO_STRING(n) + ' '
25+
total <- total + n
26+
primes <- primes - 1
27+
ENDIF
28+
n <- n + 1
29+
ENDWHILE
30+
31+
OUTPUT 'Sum: ' + INT_TO_STRING(total)
32+
OUTPUT printout

0 commit comments

Comments
 (0)