25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 32<br />

Nc = 128; (*size of consumer block*)<br />

N = 1024; (*buffer size, common multiple of Np <strong>and</strong> Nc*)<br />

VAR ne, nf: INTEGER;<br />

in, out: INTEGER;<br />

nonfull: Signals.Signal; (*ne >= 0*)<br />

nonempty: Signals.Signal; (*nf >= 0*)<br />

buf: ARRAY N OF CHAR;<br />

PROCEDURE deposit (VAR x: ARRAY OF CHAR);<br />

BEGIN<br />

ne := ne - Np;<br />

IF ne < 0 THEN Signals.Wait(nonfull) END;<br />

FOR i := 0 TO Np-1 DO buf[in] := x[i]; INC(in) END;<br />

IF in = N THEN in := 0 END;<br />

nf := nf + Np;<br />

IF nf >= 0 THEN Signals.Send(nonempty) END<br />

END deposit;<br />

PROCEDURE fetch (VAR x: ARRAY OF CHAR);<br />

BEGIN<br />

nf := nf - Nc;<br />

IF nf < 0 THEN Signals.Wait(nonempty) END;<br />

FOR i := 0 TO Nc-1 DO x[i] := buf[out]; INC(out) END;<br />

IF out = N THEN out := 0 END;<br />

ne := ne + Nc;<br />

IF ne >= 0 THEN Signals.Send(nonfull) END<br />

END fetch;<br />

BEGIN<br />

ne := N; nf := 0; in := 0; out := 0;<br />

Signals.Init(nonfull); Signals.Init(nonempty)<br />

END Buffer.<br />

1.7.4 Textual Input <strong>and</strong> Output<br />

By st<strong>and</strong>ard input <strong>and</strong> output we underst<strong>and</strong> the transfer of data to (from) a computer system from (to)<br />

genuinely external agents, in particular its human operator. Input may typically originate at a keyboard <strong>and</strong><br />

output may sink into a display screen. In any case, its characteristic is that it is readable, <strong>and</strong> it typically<br />

consists of a sequence of characters. It is a text. This readability condition is responsible for yet another<br />

complication incurred in most genuine input <strong>and</strong> output operations. Apart from the actual data transfer, they<br />

also involve a transformation of representation. For example, numbers, usually considered as atomic units<br />

<strong>and</strong> represented in binary form, need be transformed into readable, decimal notation. <strong>Structures</strong> need to be<br />

represented in a suitable layout, whose generation is called formatting.<br />

Whatever the transformation may be, the concept of the sequence is once again instrumental for a<br />

considerable simplification of the task. The key is the observation that, if the data set can be considered as<br />

a sequence of characters, the transformation of the sequence can be implemented as a sequence of<br />

(identical) transformations of elements.<br />

T() = <br />

We shall briefly investigate the necessary operations for transforming representations of natural numbers<br />

for input <strong>and</strong> output. The basis is that a number x represented by the sequence of decimal digits d = has the value<br />

x = Si: i = 0 .. n-1: d i * 10 i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!