原文︰http://student.puiching.edu.hk/viewtopic.php?topic=22167&forum=3&3
第三課 : Arithmetic in Pascal
重點:
1. 現實世界的數學符號不一定在pascal compiler內有相同的效果。 例如 x號 需要變成 *號; 大括號{}和中括號[]要變回小括號(); 除號分為 (1) 實數除real division / 和 (2) 整數除integer division div等等
2.數學函數的使用和規範 (argument type和function type的認識)。
3. 考考你: 要清楚明白pascal compiler對type的理解, 請你指出下列那一句pascal statement包含syntax error?
program type_mismatch_errors_demo;
{check which statement(s) has/have syntax error?}
var
a, b : integer;
c, d : real;
e, f : string;
g, h : char;
i, j : boolean;
begin
(* group one : numbers manipulation *)
a := 1 + 2 - 4;
b := 1 + 4 / 2;
c := 1 + 2 - 4;
d := 1 + 4 / 2;
(* group two : text manipulation *)
e := 'CHUN Wai Tung';
f := 'Mr.' + e;
g := 'a';
h := f;
f := g;
h := 'Chungsir';
f := 'a';
(* group three : boolean manipulation *)
i := TRUE;
j := i and FALSE;
i := (4>3) and j or ('a'>'e');
j := 1 or 4;
(* group four : mix type data manipulation *)
a := c;
d := b;
a := e;
d := h;
j := a;
end.
堂上小玩意: pascal的bitwise and, or, not operator …
堂上問過大家的IQ題:
若果pascal定義:
1 AND 4 = 0
1 AND 7 = 1
3 AND 7 = 3
11 AND 12 = 8
那麼…
6 AND 4 = ?
23 AND 70 = ?
100 AND 200 = ?
若果pascal定義:
1 OR 4 = 5
1 OR 8 = 9
1 OR 7 = 7
3 OR 9 = 11
那麼…
6 OR 4 = ?
23 OR 70 = ?
100 OR 200 = ?
若果pascal定義:
NOT 4 = -5
NOT 120 = -121
那麼…
NOT 2000 = ?
NOT -1 = ?
無獎遊戲… ![]()
不准打入機run, 要玩推理…
=========================================
Real number formatting exercise
冒號對各種類的值格式化 (format) 有什麼效果?可下載這個 program 試試。
| Code: |
var
x: real;
a: integer;
s: string;
begin
a := 352;
x := 42.35454;
s := 'Pui Ching';
writeln('----|----1----|----2----|----3----|----4----|----5----|----6');
writeln(a:5);
writeln(a:2);
writeln;
writeln('----|----1----|----2----|----3----|----4----|----5----|----6');
writeln(x);
writeln(x:5);
writeln(x:10);
writeln(x:5:3);
writeln(x:10:3);
writeln(x:5:6);
writeln;
writeln('----|----1----|----2----|----3----|----4----|----5----|----6');
writeln(s);
writeln(s:15)
end.
|
試試改動冒號後的數字,看看有什麼效果。
家課:Pascal programming Chapter 3(共二題)