原文︰http://student.puiching.edu.hk/viewtopic.php?topic=22167&forum=3&3

第一課 : type and simple program

重點:
1. 記著pascal是一種對type非常嚴格的程式語言, 講求門當戶對, 例如real number 4是不可以放在integer的variable之內的。每一種data type都有它自已的限制範圍, 例如 turbo pascal的integer type variable只可以處理-32768至32767之間的整數。

*** 一則關於資料型態故事的中文文章 ***

「1996 年 6 月,法國雅利安五號火箭才升空不到一分鐘,就自動銷毀了,直接與間接造成幾十億元的金錢損失,以及使雅利安計畫停滯達數月之久。對失敗的原因,調查委員會 這樣描述:「在主引擎點火後 37 秒,導向與高度的資訊完全喪失。」原因是:「內部參考系統軟體的規格與設計錯誤。」最後發現錯誤的地方出在某一行程式,要把 64 位元的數字裝填到 16 位元的位置 (應該是把一個屬於 double 型態的數值,存入一個 short 型態的變數裡面;譬如說 short n=exp(12); 就會發生這種情況),使得電腦溢流 (overflow) 爆掉了。」

2. 留心理解所有red alert紅色error提示句。 有不明白的可以上載到討論區查詢。 當pascal compiler (turbo pascal) 不明白你給出的指令的時候, 它會發出「紅色警號」。大家要學習好好和電腦「溝通」, 要注意有時候電腦沒有說你有錯, 你的問題可能還要大! No news IS NOT good news!

3. 要熟習純keyboard操作, 以下是必須用到的commands:
- CTRL + F9 (RUN)
- ALT + F9 (COMPILE)
- ALT + F5 (CHECK OUTPUT SCREEN)
- CRTL + F1 (ASK FOR DETAILS)
- F3 (OPEN FILE)
- F2 (SAVE FILE)

Class Demo : Finding the sum of integers 1 to 1000.

Code:

program test;
var
  sum, i : longint;
begin
   sum := 0;
   for i := 1 to 1000 do
      sum := sum + i;
   writeln(sum);
end.

Classwork : Finding the sum of of all multiples of 7 from 1 to 1000.

若果你對這一條題目有問題, 我們鼓勵大家上pascal learning web多做一些淺的題目:

Exercise 2-1 Rectangle
Exercise 2-2 Sell Disks
Exercise 2-3 Letter
Exercise 2-4 Circle
Exercise 2-5 Age


有同學說中三時沒有學過 for loop, 我現在把上面的program轉為while loop版本:

Code:

program test;
var
  sum, i : longint;
begin
   sum := 0;
   i := 1;
   while  i <= 1000 do
      begin
         sum := sum + i;
         i := i + 1;
      end;
   writeln(sum);
end.
 

你們喜歡while loop的話便用while loop好了, 我比較喜歡for loop。


*** 若果你想在家中用熟學校的TP版面, 你可以去BORLAND下載FREE DOWNLOAD的TP 55。 ***
詳細介紹: Antique Software: Turbo Pascal v5.5 – by David Intersimone。 [立即下載TP55]第二課 : good programming style重點:
1. 如何由頭到尾打一個program。 永遠由”program +var+begin+end.”開始。
2. 永遠要用正確的indentation去打program, 要整整齊齊…
3. 要熟習純keyboard操作, 以下是今天提到的commands:
- CTRL + break (STOP A RUNNING PROGRAM)
- CTRL + K I (INDENT BLOCK)
- CTRL + K U (UNINDENT BLOCK)
4. 什麼是ASCII TABLE? 下面的小程式可以列出所有ASCII CODE… (*** 一篇關於ASCII編碼的中文文章 ***)

program print_ASCII_table;
uses
 crt;
var
 i : integer;
begin
 clrscr;
 for i := 0 to 255 do
  begin
   if (i>=32) then
    write(' ', i:3, ':', chr(i),' ');
  end;
 writeln;
end.

小貼士: 若你想用keyboard直接打入一個ascii code, 可以按 ALT+該碼的對應ASCII VALUE (必須以NUMERIC KEYPAD輸入), 例如ALT-248是度數的符號、ALT-253是平方的符號。

家課 : chapter 2 的2題堂課 + 3題家課

Advertisement