| 1 |
% This software is licensed to you under the terms of a BSD-style license, |
| 2 |
% see bsdlic.txt for details. |
| 3 |
% This file demonstrates usage of the dotnot macros. |
| 4 |
\input dotnot |
| 5 |
\headline{\bf \title} |
| 6 |
\def\title{\centerline{Usage of dotnot}} |
| 7 |
\def\out{\hbox{\it out:\/}} |
| 8 |
|
| 9 |
The greatest common denominator: |
| 10 |
\bblock |
| 11 |
\cofl{m > 0 \land n > 0} |
| 12 |
\[\glocon m, n; \virvar gcd |
| 13 |
; \privar i, j |
| 14 |
; i \vir int, j \vir int \:= m, n |
| 15 |
; \do i \not= j \->\IF i > j \-> i \:= i - j |
| 16 |
\| j > i \-> j \:= j - i |
| 17 |
\FI |
| 18 |
\od |
| 19 |
; gcd \vir int \:= i |
| 20 |
\] \co{gcd = \gcd.(n, m)} |
| 21 |
\eblock |
| 22 |
This is the outcome of p0: |
| 23 |
\bblock |
| 24 |
\out\;\[\glocon n, q; \virvar d |
| 25 |
\cofl{0 \le n < q} |
| 26 |
; \privar u, v |
| 27 |
; d \vir int \array, u \vir int, v \vir int \:= (1), 10, n*10 |
| 28 |
; \do u \div 2 < v \mand u \le q \->\&% |
| 29 |
d:hiext.\bigl((v + u \div 2 - 1) \div q\bigr) |
| 30 |
; u, v \:= 10*u, (v - d.high*q)*10 \decind |
| 31 |
\od |
| 32 |
; \IF u \div 2 < v \-> d:hiext.\bigl((v + q \div 2) \div q\bigr)% |
| 33 |
\| v \le u \div 2 \-> skip % use `%' to combine several input lines |
| 34 |
% into one output line |
| 35 |
\FI |
| 36 |
\] \co{\hbox{$d$ contains the decimal digits of $n/q$}} |
| 37 |
\eblock |
| 38 |
An alternative program to compute the greatest common denominator: |
| 39 |
\bblock |
| 40 |
\[\glocon m, n; \virvar gcd |
| 41 |
;\privar i, j |
| 42 |
;i \vir int, j \vir int \:= m, n |
| 43 |
;\do i > j \-> i \:= i - j |
| 44 |
\| i < j \-> j \:= j - i |
| 45 |
\od |
| 46 |
;gcd \vir int \:= (i + j) \div 2 |
| 47 |
\] |
| 48 |
\eblock |
| 49 |
This program computes the next higher permutation of c. |
| 50 |
\bblock |
| 51 |
\[ \glovar c; \privar i,j |
| 52 |
; i\vir int \:= c.hib -1;\;\do c.i \ge c.(i+1) \-> i \:= i - 1\od |
| 53 |
; j\vir int \:= c.hib;\;\do c.j \le c.i \-> j \:= j-1 \od |
| 54 |
; c:swap.(i,j) |
| 55 |
; i \:= i+1; j \:= c.hib; |
| 56 |
; \do i < j \-> c:swap.(i,j); i,j \:= i+1,j-1 \od |
| 57 |
\] |
| 58 |
\eblock |
| 59 |
And this is the famous Dutch flag program: |
| 60 |
\bblock |
| 61 |
\[ \glovar buck; \glocon n; \privar r, w, b |
| 62 |
; r \vir int, w \vir int, b\vir int \:= 1, n,n |
| 63 |
; \do w \ge r \-> \[ \glovar buck, r, w, b; \pricon col |
| 64 |
; col \vir colour \:= buck.w |
| 65 |
; \IF col = red \-> buck:swap.(r,w); r := r+1 |
| 66 |
\| col = white \-> w \:= w-1 |
| 67 |
\| col = blue \-> buck:swap.(w,b); w,b \:= w-1, b-1 |
| 68 |
\FI |
| 69 |
\] |
| 70 |
\od |
| 71 |
\] |
| 72 |
\eblock |
| 73 |
\bye |