Difference between revisions of "Modules/Console"

From vishap oberon compiler
Jump to navigation Jump to search
 
Line 32: Line 32:
 
These procedures get as argument variable of the corresponding type, and write to the standard output text representation of the object. Second argument of <tt>Int</tt> procedure defines how much space needs to be provided.
 
These procedures get as argument variable of the corresponding type, and write to the standard output text representation of the object. Second argument of <tt>Int</tt> procedure defines how much space needs to be provided.
  
=== Auxiliary procedures ===
+
=== Aŭiliary procedures ===
  
 
<syntaxhighlight lang="oberon2" style="font-size:10pt">
 
<syntaxhighlight lang="oberon2" style="font-size:10pt">
Line 105: Line 105:
 
Fibonacci  40  102334155        493 milliseconds
 
Fibonacci  40  102334155        493 milliseconds
 
</pre>
 
</pre>
 +
 +
 +
[[Category:Modules]]

Latest revision as of 13:54, 24 February 2016

Deutsch (de) | English (en) | հայերեն (hy)

Module Console[edit]

Module Console provides simple input output routines.

Input[edit]

There are two procedures for data input: Read և ReadLine.

Procedure Read reads one character from the standard input. Procedure ReadLine reads one line.

Both procedures return the result by VAR argument.

PROCEDURE Read*(VAR ch: CHAR);
PROCEDURE ReadLine*(VAR line: ARRAY OF CHAR);

Output[edit]

Module Console provides several functions to write to standard output: Char, Int, String, Bool և Hex.

PROCEDURE Char*(ch: CHAR);
PROCEDURE Int*(i, n: LONGINT);
PROCEDURE String*(s: ARRAY OF CHAR);
PROCEDURE Bool*(b: BOOLEAN);
PROCEDURE Hex*(i: LONGINT);

These procedures get as argument variable of the corresponding type, and write to the standard output text representation of the object. Second argument of Int procedure defines how much space needs to be provided.

Aŭiliary procedures[edit]

PROCEDURE Flush*();
PROCEDURE Ln*;

Examples[edit]

MODULE hello;

IMPORT Out := Console;

BEGIN

Out.String("Hey"); Out.Ln;
END hello.

Other example

MODULE Fibonacci;
IMPORT Out := Console, Time := oocTime;
VAR n: INTEGER;

PROCEDURE Fibo(n: INTEGER): LONGINT;
VAR f: LONGINT;
BEGIN
    IF n <= 2 THEN
        f := 1
    ELSE
        f := Fibo(n - 1) + Fibo(n - 2)
    END;
    RETURN f
END Fibo;

PROCEDURE WriteFibo(n: INTEGER);
  VAR start, stop: Time.TimeStamp; 
  interval: Time.Interval;
  f: LONGINT;
BEGIN
    Time.GetTime(start);
    f := Fibo(n);
    Time.GetTime(stop);
    stop.Delta(start, interval);
    Out.String("Fibonacci");
    Out.Int(n, 4);
    Out.Int(f, 12);
    Out.Int(interval.msecInt, 12); Out.String(" milliseconds");
    Out.Ln
END WriteFibo;

BEGIN
    FOR n := 32 TO 40 DO WriteFibo(n) END
END Fibonacci.

This program will output

noch@chinari /tmp $ ./Fibonacci 
Fibonacci  32     2178309          25 milliseconds
Fibonacci  33     3524578          34 milliseconds
Fibonacci  34     5702887          45 milliseconds
Fibonacci  35     9227465          58 milliseconds
Fibonacci  36    14930352          77 milliseconds
Fibonacci  37    24157817         116 milliseconds
Fibonacci  38    39088169         189 milliseconds
Fibonacci  39    63245986         307 milliseconds
Fibonacci  40   102334155         493 milliseconds