showcase11.mod 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. MODULE Showcase11;
  2. (* Standard-library tour: SysIO, TextIO, WholeIO, Strings, Math,
  3. Files. Reads one CARDINAL from stdin (e.g. 42).
  4. Prints: Modula-2 / 8 / 42 / 12 / 42 ; exit 80. *)
  5. IMPORT Files;
  6. FROM Files IMPORT File;
  7. FROM Math IMPORT sqrt, Pi;
  8. FROM Strings IMPORT Assign, Concat, Length;
  9. FROM SysIO IMPORT Write, WriteInt, WriteLn;
  10. FROM TextIO IMPORT WriteCard;
  11. FROM WholeIO IMPORT ReadCard;
  12. VAR ExitCode : INTEGER;
  13. VAR s, t, u : ARRAY [0..31] OF CHAR;
  14. VAR r : REAL;
  15. VAR f : File;
  16. VAR n : INTEGER;
  17. VAR card : CARDINAL;
  18. BEGIN
  19. (* Strings: build "Modula-2" and report its length. *)
  20. Assign("Modula", s);
  21. Assign("-2", t);
  22. Concat(s, t, u);
  23. Write(u);
  24. WriteLn;
  25. WriteInt(Length(u)); (* 8 *)
  26. WriteLn;
  27. (* WholeIO input + TextIO output: echo the read card. *)
  28. ReadCard(card);
  29. WriteCard(card); (* 42 *)
  30. WriteLn;
  31. (* Math: sqrt(144) = 12. *)
  32. r := sqrt(144.0) + Pi - Pi;
  33. IF r > 11.9 THEN n := 12 ELSE n := 0 END;
  34. WriteInt(n); (* 12 *)
  35. WriteLn;
  36. (* Files: write two chars, read the integer back. *)
  37. f := Files.OpenWrite("gen_ssa/_showcase11.txt");
  38. Files.WriteChar(f, '4');
  39. Files.WriteChar(f, '2');
  40. Files.Close(f);
  41. f := Files.OpenRead("gen_ssa/_showcase11.txt");
  42. n := Files.ReadInt(f);
  43. Files.Close(f);
  44. WriteInt(n); (* 42 *)
  45. WriteLn;
  46. ExitCode := n - 12 + Length(u) + card
  47. END Showcase11.