/programs/develop/oberon07/Samples/Windows/Console/Doors.ob07 |
---|
0,0 → 1,58 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* |
There are 100 doors in a row that are all initially closed. |
You make 100 passes by the doors. |
The first time through, visit every door and toggle the door (if the door is closed, open it; if it is open, close it). |
The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it. |
The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door. |
What state are the doors in after the last pass? Which are open, which are closed? |
*) |
MODULE Doors; |
IMPORT In, Out, Console; |
CONST |
CLOSED = FALSE; |
OPEN = TRUE; |
TYPE |
List = ARRAY 101 OF BOOLEAN; |
VAR |
Doors: List; |
I, J: INTEGER; |
BEGIN |
Console.open; |
FOR I := 1 TO 100 DO |
FOR J := 1 TO 100 DO |
IF J MOD I = 0 THEN |
IF Doors[J] = CLOSED THEN |
Doors[J] := OPEN |
ELSE |
Doors[J] := CLOSED |
END |
END |
END |
END; |
FOR I := 1 TO 100 DO |
Out.Int(I, 3); |
Out.String(" is "); |
IF Doors[I] = CLOSED THEN |
Out.String("Closed.") |
ELSE |
Out.String("Open.") |
END; |
Out.Ln |
END; |
In.Ln; |
Console.exit(TRUE) |
END Doors. |
/programs/develop/oberon07/Samples/Windows/Console/HeapSort.ob07 |
---|
0,0 → 1,101 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* ********* Zonnon online collection *********** |
* Sorting: Heap Sort (Chapter 2, Example 2.8) |
* |
* This example is a part of Prof. Nikalus Wirth's book |
* www.zonnon.ethz.ch/usergroup |
* (c) ETH Zurich |
*) |
MODULE HeapSort; |
IMPORT In, Out, Console; |
CONST |
MAX_SIZE = 20; |
TYPE |
DefaultArray = ARRAY MAX_SIZE OF INTEGER; |
VAR |
MyArray: DefaultArray; |
(***** Implementation *****) |
PROCEDURE sift(VAR a: DefaultArray; L,R:INTEGER); |
VAR |
i, j, x: INTEGER; |
BEGIN |
i := L; j:= 2 * L; x:= a[L]; |
IF (j < R) & (a[j] < a[j + 1]) THEN j := j + 1 END; |
WHILE (j <= R) & (x < a[j]) DO |
a[i] := a[j]; i := j; j := 2 * j; |
IF (j < R) & (a[j] < a[j + 1]) THEN j := j + 1 END |
END; |
a[i] := x |
END sift; |
PROCEDURE HeapSort(VAR a: DefaultArray; n: INTEGER); |
VAR |
L, R, x: INTEGER; |
BEGIN |
L := (n DIV 2); R := n - 1; |
WHILE L > 0 DO L := L - 1; sift(a, L, R) END; |
WHILE R > 0 DO |
x := a[0]; a[0] := a[R]; a[R]:= x; |
R := R - 1; sift(a, L, R) |
END |
END HeapSort; |
(***** Example support *****) |
PROCEDURE FillTheArray; |
VAR |
i: INTEGER; |
BEGIN |
FOR i := 0 TO MAX_SIZE - 1 DO |
MyArray[i] := ABS(10 - i) |
END |
END FillTheArray; |
PROCEDURE PrintTheArray; |
VAR |
i: INTEGER; |
BEGIN |
Out.String("Array:"); Out.Ln; |
FOR i := 0 TO MAX_SIZE - 1 DO |
Out.Int(MyArray[i], 2); Out.String(", ") |
END; |
Out.Ln |
END PrintTheArray; |
PROCEDURE Execute; |
BEGIN |
HeapSort(MyArray, MAX_SIZE) |
END Execute; |
BEGIN |
Console.open; |
Out.String("Example 2.8 (Heap sort)"); Out.Ln; |
FillTheArray; |
PrintTheArray; |
Execute; |
PrintTheArray; |
Out.String("Press Enter to continue"); In.Ln; |
Console.exit(TRUE) |
END HeapSort. |
/programs/develop/oberon07/Samples/Windows/Console/Hello.ob07 |
---|
0,0 → 1,13 |
MODULE Hello; |
IMPORT Console, In, Out; |
BEGIN |
Console.open; |
Out.String("Hello, world!"); |
In.Ln; |
Console.exit(TRUE) |
END Hello. |
/programs/develop/oberon07/Samples/Windows/Console/HelloRus.ob07 |
---|
0,0 → 1,26 |
MODULE HelloRus; |
IMPORT Console, In, Out; |
PROCEDURE main; |
VAR |
str: ARRAY 10 OF WCHAR; |
BEGIN |
str := "Привет!"; |
Out.StringW(str); Out.Ln; |
str[2] := "е"; |
str[5] := "д"; |
Out.StringW(str) |
END main; |
BEGIN |
Console.open; |
main; |
In.Ln; |
Console.exit(TRUE) |
END HelloRus. |
/programs/develop/oberon07/Samples/Windows/Console/MagicSquares.ob07 |
---|
0,0 → 1,48 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* ********* Zonnon online collection *********** |
* Magic Squares |
* |
* This example is a part of Prof. Nikalus Wirth's book |
* www.zonnon.ethz.ch/usergroup |
* (c) ETH Zurich |
*) |
MODULE MagicSquares; (*NW 11.8.97*) |
IMPORT In, Out, Console; |
PROCEDURE Generate; (*magic square of order 3, 5, 7, ... *) |
VAR |
i, j, x, nx, nsq, n: INTEGER; |
M: ARRAY 13, 13 OF INTEGER; |
BEGIN |
Out.String("Enter magic square order(3, 5, 7, ..., 13): "); In.Int(n); nsq := n * n; x := 0; |
i := n DIV 2; j := n - 1; |
WHILE x < nsq DO |
nx := n + x; j := (j - 1) MOD n; INC(x); |
Out.Int(i, 1); Out.Char(9X); |
Out.Int(j, 1); Out.Ln; |
M[i, j] := x; |
WHILE x < nx DO |
i := (i + 1) MOD n; j := (j + 1) MOD n; |
INC(x); M[i, j] := x |
END |
END; |
FOR i := 0 TO n - 1 DO |
FOR j := 0 TO n - 1 DO Out.Int(M[i, j], 6) END; |
Out.Ln |
END |
END Generate; |
BEGIN |
Console.open; |
Generate; |
Out.String("Press Enter to continue"); In.Ln; |
Console.exit(TRUE) |
END MagicSquares. |
/programs/develop/oberon07/Samples/Windows/Console/MultiplicationTables.ob07 |
---|
0,0 → 1,52 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* |
Produce a formatted NxN multiplication table |
Only print the top half triangle of products |
*) |
MODULE MultiplicationTables; |
IMPORT In, Out, Console; |
CONST |
N = 18; |
VAR |
I, J: INTEGER; |
BEGIN |
Console.open; |
FOR J := 1 TO N - 1 DO |
Out.Int(J, 3); |
Out.String(" ") |
END; |
Out.Int(N, 3); |
Out.Ln; |
FOR J := 0 TO N - 1 DO |
Out.String("----") |
END; |
Out.String("+"); |
Out.Ln; |
FOR I := 1 TO N DO |
FOR J := 1 TO N DO |
IF J < I THEN |
Out.String(" ") |
ELSE |
Out.Int(I * J, 3); |
Out.String(" ") |
END |
END; |
Out.String("| "); |
Out.Int(I, 2); |
Out.Ln |
END; |
In.Ln; |
Console.exit(TRUE) |
END MultiplicationTables. |
/programs/develop/oberon07/Samples/Windows/Console/SierpinskiCarpet.ob07 |
---|
0,0 → 1,75 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
MODULE SierpinskiCarpet; |
IMPORT In, Out, Console; |
VAR |
order: INTEGER; |
PROCEDURE pow(b, n: INTEGER): INTEGER; |
VAR |
i, res: INTEGER; |
BEGIN |
res := 1; |
FOR i := 1 TO n DO |
res := res * b |
END |
RETURN res |
END pow; |
PROCEDURE in_carpet(x, y: INTEGER): BOOLEAN; |
VAR |
res, exit: BOOLEAN; |
BEGIN |
exit := FALSE; |
res := TRUE; |
WHILE (x > 0) & (y > 0) & (exit = FALSE) DO |
IF (x MOD 3 = 1) & (y MOD 3 = 1) THEN |
res := FALSE; |
exit := TRUE |
END; |
y := y DIV 3; |
x := x DIV 3 |
END |
RETURN res |
END in_carpet; |
PROCEDURE PrintSierpinski(n: INTEGER); |
VAR |
i, j, l: INTEGER; |
BEGIN |
l := pow(3, n) - 1; |
FOR i := 0 TO l DO |
FOR j := 0 TO l DO |
IF in_carpet(i, j) THEN |
Out.Char("#") |
ELSE |
Out.Char(" ") |
END |
END; |
Out.Ln |
END |
END PrintSierpinski; |
BEGIN |
Console.open; |
Out.String("Input carpet order(0..3):"); |
In.Int(order); |
PrintSierpinski(order); |
In.Ln; |
Console.exit(TRUE) |
END SierpinskiCarpet. |
/programs/develop/oberon07/Samples/Windows/Console/SierpinskiTriangle.ob07 |
---|
0,0 → 1,44 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
MODULE SierpinskiTriangle; |
IMPORT In, Out, Console; |
VAR |
order: INTEGER; |
PROCEDURE PrintSierpinski(order: INTEGER); |
VAR |
x, y, k, size: INTEGER; |
BEGIN |
size := LSL(1, order) - 1; |
FOR y := size TO 0 BY -1 DO |
FOR k := 1 TO y DO |
Out.Char(" ") |
END; |
FOR x := 0 TO size - y DO |
IF BITS(x) * BITS(y) = {} THEN |
Out.String("* ") |
ELSE |
Out.String(" ") |
END |
END; |
Out.Ln |
END |
END PrintSierpinski; |
BEGIN |
Console.open; |
Out.String("Input triangle order(0..5):"); |
In.Int(order); |
PrintSierpinski(order); |
In.Ln; |
Console.exit(TRUE) |
END SierpinskiTriangle. |
/programs/develop/oberon07/Samples/Windows/Console/Sieve.ob07 |
---|
0,0 → 1,51 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* This was taken from the CRITICAL MASS MODULA-3 examples *) |
(* The "Sieve" program demonstrates the use of arrays, |
loops and conditionals. *) |
MODULE Sieve; |
IMPORT In, Out, Console; |
(* Search in interval 2 to 1000 for prime numbers. *) |
CONST |
LastNum = 1000; |
(* "prime" is an array of booleans ranging from 2 to "LastNum". *) |
VAR |
prime: ARRAY LastNum + 2 OF BOOLEAN; |
i, j: INTEGER; |
BEGIN |
Console.open; |
Out.String("Primes in range 2.."); Out.Int(LastNum, 1); Out.Char(":"); Out.Ln; |
(* Initialize all elements of the array to "TRUE". |
(Note that we could have initialized the array during |
the assignment.) *) |
FOR i := 2 TO LastNum DO |
prime[i] := TRUE |
END; |
(* Loop through all integers between 2 and "LastNum". Print each prime |
number, starting from 2 and mark all numbers that are divisible by |
that prime number to "FALSE". Repeat the step until we've exhausted |
all the numbers in the interval.*) |
FOR i := 2 TO LastNum DO |
IF prime[i] THEN |
Out.Int(i, 3); |
Out.Char(" "); |
FOR j := i TO LastNum DO |
IF j MOD i = 0 THEN |
prime[j] := FALSE |
END |
END |
END |
END; |
Out.Ln; In.Ln; |
Console.exit(TRUE) |
END Sieve. |
/programs/develop/oberon07/Samples/Windows/Console/SpiralMatrix.ob07 |
---|
0,0 → 1,56 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* |
Produce a spiral array. |
A spiral array is a square arrangement of the first (Width * Height) natural numbers, |
where the numbers increase sequentially as you go around the edges of the array spiraling inwards. |
*) |
MODULE SpiralMatrix; |
IMPORT In, Out, Console; |
VAR |
Width, Height: INTEGER; |
PROCEDURE spiral(w, h, x, y: INTEGER): INTEGER; |
VAR |
res: INTEGER; |
BEGIN |
IF y # 0 THEN |
res := w + spiral(h - 1, w, y - 1, w - x - 1) |
ELSE |
res := x |
END |
RETURN res |
END spiral; |
PROCEDURE print_spiral(w, h: INTEGER); |
VAR |
i, j: INTEGER; |
BEGIN |
FOR i := 0 TO h - 1 DO |
FOR j := 0 TO w - 1 DO |
Out.Int(spiral(w, h, j, i), 4) |
END; |
Out.Ln |
END |
END print_spiral; |
BEGIN |
Console.open; |
Out.String("Input width of matrix(1, 2, 3, ...):"); In.Int(Width); |
Out.String("Input height of matrix:(1, 2, 3, ...)"); In.Int(Height); |
print_spiral(Width, Height); |
In.Ln; |
Console.exit(TRUE) |
END SpiralMatrix. |
/programs/develop/oberon07/Samples/Windows/Console/TempConv.ob07 |
---|
0,0 → 1,44 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* This program is a good example of proper formatting, it is *) |
(* easy to read and very easy to understand. It should be a *) |
(* snap to update a program that is well written like this. You *) |
(* should begin to develop good formatting practice early in *) |
(* your programming career. *) |
MODULE TempConv; |
IMPORT In, Out, Console; |
VAR |
Count : INTEGER; (* a variable used for counting *) |
Centigrade : INTEGER; (* the temperature in centigrade *) |
Farenheit : INTEGER; (* the temperature in farenheit *) |
BEGIN |
Console.open; |
Out.String("Farenheit to Centigrade temperature table"); |
Out.Ln; |
Out.Ln; |
FOR Count := -2 TO 12 DO |
Centigrade := 10 * Count; |
Farenheit := 32 + Centigrade * 9 DIV 5; |
Out.String(" C ="); |
Out.Int(Centigrade, 5); |
Out.String(" F ="); |
Out.Int(Farenheit, 5); |
IF Centigrade = 0 THEN |
Out.String(" Freezing point of water"); |
END; |
IF Centigrade = 100 THEN |
Out.String(" Boiling point of water"); |
END; |
Out.Ln; |
END; (* of main loop *) |
In.Ln; |
Console.exit(TRUE) |
END TempConv. |
/programs/develop/oberon07/Samples/Windows/Console/exp.ob07 |
---|
0,0 → 1,117 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* Print first 'PRINT' digits of 'e'. |
* |
* Originally written in Pascal by Scott Hemphill |
* Rewritten in Modula-2 and modified by Andrew Cadach |
* |
*) |
MODULE exp; |
IMPORT In, Out, Console; |
CONST |
PRINT = 1024; |
DIGITS = PRINT + (PRINT + 31) DIV 32; |
TYPE |
number = ARRAY DIGITS + 1 OF INTEGER; |
VAR |
s, x: number; |
xs, i: INTEGER; |
PROCEDURE init (VAR x: number; n: INTEGER); |
VAR |
i: INTEGER; |
BEGIN |
x[0] := n; |
FOR i := 1 TO DIGITS DO x[i] := 0 END |
END init; |
PROCEDURE divide (VAR x: number; xs, n: INTEGER; |
VAR y: number; VAR ys: INTEGER); |
VAR |
i, c: INTEGER; |
BEGIN |
c := 0; |
FOR i := xs TO DIGITS DO |
c := 10 * c + x[i]; |
y[i] := c DIV n; |
c := c MOD n |
END; |
ys := xs; |
WHILE (ys <= DIGITS) & (y[ys] = 0) DO INC(ys) END |
END divide; |
PROCEDURE add (VAR s, x: number; xs: INTEGER); |
VAR |
i, c: INTEGER; |
BEGIN |
c := 0; |
FOR i := DIGITS TO xs BY -1 DO |
c := c + s[i] + x[i]; |
IF c >= 10 THEN |
s[i] := c - 10; |
c := 1 |
ELSE |
s[i] := c; |
c := 0 |
END |
END; |
i := xs; |
WHILE c # 0 DO |
DEC(i); |
c := c + s[i]; |
IF c >= 10 THEN |
s[i] := c - 10; |
c := 1 |
ELSE |
s[i] := c; |
c := 0 |
END |
END |
END add; |
BEGIN |
Console.open; |
init(s, 0); |
init(x, 1); |
xs := 0; |
add(s, x, xs); |
i := 0; |
REPEAT |
INC(i); |
divide(x, xs, i, x, xs); |
add(s, x, xs); |
UNTIL xs > DIGITS; |
Out.Ln; |
Out.String (" e = "); |
Out.Char (CHR(s[0] + ORD("0"))); |
Out.Char ("."); |
FOR i := 1 TO PRINT DO |
Out.Char (CHR(s[i] + ORD("0"))); |
IF i MOD 64 = 0 THEN |
Out.Ln; |
Out.Int (i, 5); |
Out.String (" ") |
END |
END; |
Out.Ln; |
Out.Ln; |
In.Ln; |
Console.exit(TRUE) |
END exp. |
/programs/develop/oberon07/Samples/Windows/Console/fact.ob07 |
---|
0,0 → 1,59 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* |
* Written by Andrew Cadach |
* |
* Recursive (extremely uneficient:-) implementation of factorial |
* |
* n * (n-1)!, n <> 0 |
* By definition, n! = |
* 1, n = 0 |
* |
*) |
MODULE fact; |
IMPORT In, Out, Console; |
CONST |
MAX_INTEGER = ROR(-2, 1); |
VAR |
i, r: INTEGER; |
PROCEDURE f (n: INTEGER): INTEGER; |
VAR |
Res: INTEGER; |
BEGIN |
IF n = 0 THEN |
Res := 1 |
ELSE |
Res := n * f (n - 1) |
END |
RETURN Res |
END f; |
BEGIN |
Console.open; |
i := 0; |
REPEAT |
r := f(i); |
Out.String ("The factorial of "); |
Out.Int (i, 2); |
Out.String (" is "); |
Out.Int (r, 0); |
Out.Ln; |
INC(i) |
UNTIL r >= MAX_INTEGER DIV i; |
In.Ln; |
Console.exit(TRUE) |
END fact. |
/programs/develop/oberon07/Samples/Windows/Console/hailst.ob07 |
---|
0,0 → 1,117 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* |
The Hailstone sequence of numbers can be generated |
from a starting positive integer, n by: |
IF n is 1 THEN the sequence ends. |
IF n is even THEN the next n of the sequence = n / 2 |
IF n is odd THEN the next n of the sequence = (3 * n) + 1 |
The (unproven) Collatz conjecture is that the hailstone sequence |
for any starting number always terminates. |
*) |
MODULE hailst; |
IMPORT In, Out, API, Console; |
CONST |
maxCard = ROR(-2, 1) DIV 3; |
List = 1; |
Count = 2; |
Max = 3; |
VAR |
a: INTEGER; |
PROCEDURE HALT(code: INTEGER); |
BEGIN |
In.Ln; Console.exit(TRUE); API.exit(code) |
END HALT; |
PROCEDURE HailStone(start, _type: INTEGER): INTEGER; |
VAR |
n, max, count, res: INTEGER; |
exit: BOOLEAN; |
BEGIN |
count := 1; |
n := start; |
max := n; |
exit := FALSE; |
WHILE exit # TRUE DO |
IF _type = List THEN |
Out.Int (n, 12); |
IF count MOD 6 = 0 THEN Out.Ln END |
END; |
IF n # 1 THEN |
IF ODD(n) THEN |
IF n < maxCard THEN |
n := 3 * n + 1; |
IF n > max THEN max := n END |
ELSE |
Out.String("Exceeding max value for type INTEGER at:"); |
Out.Ln; |
Out.String("n = "); Out.Int(start, 1); |
Out.String(", count = "); Out.Int(count, 1); |
Out.String(", intermediate value "); |
Out.Int(n, 1); |
Out.String(". Aborting."); |
Out.Ln; |
HALT(2) |
END |
ELSE |
n := n DIV 2 |
END; |
INC(count) |
ELSE |
exit := TRUE |
END |
END; |
IF _type = Max THEN res := max ELSE res := count END |
RETURN res |
END HailStone; |
PROCEDURE FindMax(num: INTEGER); |
VAR |
val, maxCount, maxVal, cnt: INTEGER; |
BEGIN |
maxCount := 0; |
maxVal := 0; |
FOR val := 2 TO num DO |
cnt := HailStone(val, Count); |
IF cnt > maxCount THEN |
maxVal := val; |
maxCount := cnt |
END |
END; |
Out.String("Longest sequence below "); Out.Int(num, 1); |
Out.String(" is "); Out.Int(HailStone(maxVal, Count), 1); |
Out.String(" for n = "); Out.Int(maxVal, 1); |
Out.String(" with an intermediate maximum of "); |
Out.Int(HailStone(maxVal, Max), 1); |
Out.Ln |
END FindMax; |
BEGIN |
Console.open; |
a := HailStone(27, List); |
Out.Ln; |
Out.String("Iterations total = "); Out.Int(HailStone(27, Count), 1); |
Out.String(" max value = "); Out.Int(HailStone(27, Max), 1); |
Out.Ln; |
FindMax(100000); |
Out.String("Done."); |
Out.Ln; In.Ln; |
Console.exit(TRUE) |
END hailst. |
/programs/develop/oberon07/Samples/Windows/Console/postfix.ob07 |
---|
0,0 → 1,123 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* Example program from Programming In Modula-2, N. Wirth., pg. 56, *) |
(* - no WINDOWS in this example *) |
(* this program translates a small language into postfix form |
* the language is |
* |
* expression = term { [ "+" | "-" ] term } |
* |
* term = factor { [ "*" | "/" ] factor } |
* |
* factor = letter | "(" expression ")" |
* |
* letter = "a" | 'b" | … | "z" |
* |
* try as input |
* a+b |
* a*b+c |
* a+b*c |
* a*(b/(c-d)) |
*) |
MODULE postfix; |
IMPORT In, Out, Console; |
CONST |
OUT_LINE_SIZE = 80; |
IN_LINE_SIZE = 80; |
VAR |
ch : CHAR; |
i, index : INTEGER; |
out_line : ARRAY OUT_LINE_SIZE OF CHAR; |
in_line : ARRAY IN_LINE_SIZE OF CHAR; |
cur_ch : INTEGER; |
PROCEDURE NextChar(): CHAR; |
BEGIN |
INC(cur_ch) |
RETURN in_line[cur_ch - 1] |
END NextChar; |
PROCEDURE expression; |
VAR |
addop :CHAR; |
PROCEDURE term; |
VAR |
mulop :CHAR; |
PROCEDURE factor; |
BEGIN (* factor *) |
IF ch = "(" THEN |
ch := NextChar(); |
expression; |
WHILE ch # ")" DO |
ch := NextChar() |
END (* WHILE *) |
ELSE |
WHILE (ch < "a") OR (ch > "z") DO |
ch := NextChar() |
END; (* WHILE *) |
out_line[index] := ch; |
index := index + 1 |
END; (* IF *) |
ch := NextChar() |
END factor; |
BEGIN (* term *) |
factor; |
WHILE (ch = "*") OR (ch = "/") DO |
mulop := ch; |
ch := NextChar(); |
factor; |
out_line[index] := mulop; |
index := index + 1 |
END (* WHILE *) |
END term; |
BEGIN (* expression *) |
term; |
WHILE (ch = "+") OR (ch = "-") DO |
addop := ch; |
ch := NextChar(); |
term; |
out_line[index] := addop; |
index := index + 1 |
END (* WHILE *) |
END expression; |
BEGIN (* Postfix *) |
Console.open; |
index := 1; cur_ch := 0; |
Out.String("Enter expression:"); |
In.String(in_line); |
ch := NextChar(); |
WHILE ch > " " DO |
expression; |
FOR i := 1 TO index - 1 DO |
Out.Char(out_line[i]) |
END; (* FOR *) |
Out.Ln; |
index := 1; cur_ch := 0; |
Out.String("Enter expression:"); |
In.String(in_line); |
ch := NextChar() |
END; (* WHILE *) |
Console.exit(TRUE) |
END postfix. |
/programs/develop/oberon07/Samples/Windows/Console/sequence012.ob07 |
---|
0,0 → 1,79 |
(* |
adapted to Oberon-07 by 0CodErr, KolibriOS team |
*) |
(* Find sequence of digits 0, 1, 2 and of lengths 1 ... 90, such |
that they contain no two adjacent subsequences that are equal *) |
MODULE sequence012; |
IMPORT In, Out, Console; |
CONST |
maxlength = 75; |
VAR |
n: INTEGER; |
good: BOOLEAN; |
s: ARRAY maxlength OF INTEGER; |
PROCEDURE printsequence; |
VAR |
k: INTEGER; |
BEGIN |
Out.Char(" "); |
FOR k := 1 TO n DO Out.Int(s[k], 1) END; |
Out.Ln |
END printsequence; |
PROCEDURE changesequence; |
BEGIN |
IF s[n] = 3 THEN |
DEC(n); |
changesequence |
ELSE |
s[n] := s[n] + 1 |
END |
END changesequence; |
PROCEDURE try; |
VAR |
i, l, nhalf: INTEGER; |
BEGIN |
IF n <= 1 THEN |
good := TRUE |
ELSE |
l := 0; nhalf := n DIV 2; |
REPEAT |
INC(l); i := 0; |
REPEAT |
good := s[n - i] # s[n - l - i]; |
INC(i) |
UNTIL good OR (i = l) |
UNTIL ~good OR (l >= nhalf) |
END |
END try; |
BEGIN |
Console.open; |
n := 0; |
REPEAT |
INC(n); |
s[n] := 1; try; |
WHILE ~good DO |
changesequence; |
try |
END; |
printsequence |
UNTIL n >= maxlength - 1; |
In.Ln; |
Console.exit(TRUE) |
END sequence012. |