Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.    adapted to Oberon-07 by 0CodErr, KolibriOS team
  3.                                                    *)
  4. MODULE SierpinskiCarpet;
  5.  
  6. IMPORT In, Out, Console;
  7.  
  8.  
  9. VAR
  10.     order: INTEGER;
  11.  
  12.  
  13. PROCEDURE pow(b, n: INTEGER): INTEGER;
  14. VAR
  15.     i, res: INTEGER;
  16.  
  17. BEGIN
  18.     res := 1;
  19.     FOR i := 1 TO n DO
  20.         res := res * b
  21.     END
  22.  
  23.     RETURN res
  24. END pow;
  25.  
  26.  
  27. PROCEDURE in_carpet(x, y: INTEGER): BOOLEAN;
  28. VAR
  29.     res, exit: BOOLEAN;
  30.  
  31. BEGIN
  32.     exit := FALSE;
  33.     res := TRUE;
  34.     WHILE (x > 0) & (y > 0) & (exit = FALSE) DO
  35.         IF (x MOD 3 = 1) & (y MOD 3 = 1) THEN
  36.             res := FALSE;
  37.             exit := TRUE
  38.         END;
  39.         y := y DIV 3;
  40.         x := x DIV 3
  41.     END
  42.  
  43.     RETURN res
  44. END in_carpet;
  45.  
  46.  
  47. PROCEDURE PrintSierpinski(n: INTEGER);
  48. VAR
  49.     i, j, l: INTEGER;
  50.  
  51. BEGIN
  52.     l := pow(3, n) - 1;
  53.     FOR i := 0 TO l DO
  54.         FOR j := 0 TO l DO
  55.             IF in_carpet(i, j) THEN
  56.                 Out.Char("#")
  57.             ELSE
  58.                 Out.Char(" ")
  59.             END
  60.         END;
  61.         Out.Ln
  62.     END
  63. END PrintSierpinski;
  64.  
  65.  
  66. BEGIN
  67.     Console.open;
  68.  
  69.     Out.String("Input carpet order(0..3):");
  70.     In.Int(order);
  71.     PrintSierpinski(order);
  72.     In.Ln;
  73.  
  74.     Console.exit(TRUE)
  75. END SierpinskiCarpet.