MODULE arithmetic; (*--------------------------------------------------------- File name: arithmetic.pm Author: MM Date: 5/18/05 Problem: This program pefroms basic operations on a chain configuration. *Addition *Substraction *Integer Division *Multiplication *Power *Modulus All operations are performed on each element of the chain. In this example the chain starts with ID Example of Execution: C:\>p3 arithmetic.pm C:\>a Line is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Add 2 to Line is: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Substract 5 from Line is: -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 Multiply by 5 Line is: -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 Integer Divide by 2 Line is: -5 -2 0 2 5 7 10 12 15 17 20 22 25 27 Power 2 Line is: 24 4 0 4 24 49 99 144 224 289 399 483 624 729 Modulus 6 Line is: 0 4 0 4 0 1 3 0 2 1 3 3 0 3 Cycles: 1 addition + 1 substraction + 1 DIV + 1 multiplication + 1 power + 1 MOD. *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR L: chain OF INTEGER; (* Line always "L# *) RightLimit: INTEGER; (*----------------------------------------------------------- MAIN -----------------------------------------------------------*) BEGIN (* initializing data || NOT COUNTED ON CYCLES ||*) L := ID( chain ); (* Index is ID *) RightLimit:=15; IF L < RightLimit THEN (*printing*) WriteString( " Line is: ");WriteInt( L , 2);WriteLn; (* addition*) L:= L + 2; (*printing*) WriteString("Add 2 to Line is: "); WriteInt( L , 2);WriteLn; (* substraction*) L:= L - 5; (*printing*) WriteString("Substract 5 from Line is: "); WriteInt( L , 2);WriteLn; (*multiplication*) L:= L * 5; (*printing*) WriteString("Multiply by 5 Line is: "); WriteInt( L , 2);WriteLn; (*integer division*) L:= L DIV 2; (*printing*) WriteString("Integer Divide by 2 Line is: "); WriteInt( L , 2);WriteLn; (*power*) L:= L**2; (*printing*) WriteString("Power 2 Line is: "); WriteInt( L , 2);WriteLn; (*modulus*) L:= L MOD 6; (*printing*) WriteString("Modulus 6 Line is: "); WriteInt( L , 2);WriteLn; END; (* IF *) END arithmetic.