Lazarus 使用 Free Pascal 的編譯器,支持 Object Pascal 語言,與 Delphi 高度兼容,並看做后者的自由軟件替代品。
Lazarus 下載與安裝
我們先去 Lazarus 官網下載 http://www.lazarus-ide.org/ Windows (64 Bits) 版本的安裝程序,我用的電腦是Win10 64位,下載后開始安裝即可,安裝界面如下:
一路 Next 即可安裝完畢,安裝完在桌面上就有了 Lazarus 圖標,一個豹子的圖標。
雙擊 Lazarus 圖標后,先出來 Lazarus IDE Configure 窗口,如下圖:
保持默認設置即可,直接點右下角的 Start IDE 按鈕,啟動 IDE。
Hello world
界面是不是很熟悉呀,Delphi 7的感覺又回來了。下面我們試着寫寫 Hello world 吧。
1、在 Form1 窗口上放置一個 TButton 按鈕
2、雙擊 Button1 按鈕,在 TFrom1.Button1Click 事件中添加如下代碼:
procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('Hello world'); end;
3、按 F9 鍵,開始運行,點擊 Button1 按鈕后彈出 Hello world 對話框。
通過以上示例,我們對 Lazarus IDE 有了初步的熟悉,和 Delphi 保持高度的兼容,快捷鍵呀,界面呀,很容易上手。
A Text File Converter 示例
下面我們參考 Marco Cantù 《Essential Pascal》第四版中第 131 頁,編寫一個 A Text File Converter(文本轉化控制台程序),程序功能是通過參數-U –R –C 來實現將文本文件轉化為大寫,句子首字母大寫等。
1、在 Lazarus 菜單中選擇 【File –> New…】
2、在彈出的 New… 對話框選擇 【Project – Console application】,創建一個控制台程序。
3、彈出 New console application 對話框,我們只創建最簡單的控制台程序,直接點 Cancel 按鈕關閉就行。
4、這樣我就得到了最簡單的控制台程序 project1.dpr 代碼如下:
5、點擊 【File – Save All】,將程序保存,文件結構如下:
6、在 project1.lpr 中添加如下代碼:
program project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Convert { you can add units after this }; var I: Integer; Flag: char; inputFile, outputFile: string; begin // command line processing for I := 1 to ParamCount do begin if ParamStr(i) [1] = '-' then Flag := ParamStr(i) [2] else if inputFile = '' then inputFile := ParamStr(i) else outputFile := ParamStr(i); end; // check we have the two files and a flag if (inputFile = '') or (outputFile = '') or not (Flag in ['U', 'R', 'C']) then begin writeln ('Missing or wrong parameters'); readln; Exit; end; // process the files DoConvert (inputFile, outputFile, Flag); readln; end.
7、新建一個 Unit 文件,並保存為 Convert.pas 文件,其中代碼如下:
unit Convert; {$mode objfpc}{$H+} interface procedure DoConvert (const inputfile, outputfile: string; flag: Char); procedure ConvUpper; procedure ConvCapitalize; procedure ConvSymbols; implementation uses SysUtils; var FileIn, FileOut: TextFile; FileLength: LongInt; procedure DoConvert (const inputfile, outputfile: string; flag: Char); var F: file of Byte; begin // compute the input file length AssignFile (F, inputfile); Reset (F); FileLength := FileSize (F); CloseFile (F); // open the text files AssignFile (FileIn, inputfile); Reset (FileIn); AssignFile (FileOut, outputfile); Rewrite (FileOut); // conversion...} // check the input flag case Flag of 'U': ConvUpper; 'C': ConvCapitalize; 'R': ConvSymbols; end; // close the files CloseFile (FileOut); CloseFile (FileIn); end; procedure ConvUpper; var Ch: Char; Position: LongInt; begin Position := 0; while not Eof (FileIn) do begin Read (FileIn, Ch); Ch := UpCase (Ch); Write (FileOut, Ch); Inc (Position); end; end; function LowCase (C: Char): Char; begin if C in ['A'..'Z'] then LowCase := Chr (Ord (C) - Ord ('A') + Ord ('a')) else LowCase := C; end; procedure ConvCapitalize; var Ch: Char; Period: Boolean; Position: LongInt; begin Period := True; Position := 0; while not Eof (FileIn) do begin Read (FileIn, Ch); case Ch of 'A'..'Z': if Period then begin Write (FileOut, Ch); Period := False; end else begin Ch := LowCase (Ch); Write (FileOut, Ch); Period := False; end; 'a'..'z': if Period then begin Ch := UpCase (ch); Write (FileOut, Ch); Period := False; end else begin Write (FileOut, Ch); Period := False; end; '.', '?', '!': begin Period := True; Write (FileOut, Ch); end; else Write (FileOut, Ch); end; // case Inc (Position); end; // while end; procedure ConvSymbols; var Ch: Char; Position: LongInt; begin Position := 0; while not Eof (FileIn) do begin Read (FileIn, Ch); if Ch < Chr (127) then Write (FileOut, Ch); Inc (Position); end; end; end.
8、按 Ctrl + F9 編譯通過,在 Run 菜單下選擇 Build File,生成 project1.exe 文件,最終目錄如下:
9、在 project1.exe 目錄下我們新建一個 input.txt 文件和 output.txt 文件,其中 input.txt 文件內容下:
10、在 cmd 窗口中輸入下面的命令,用到 –U 參數,將文本轉化為大寫
11、回車執行命令后,打開 output.txt 我們發現,文本已經是大寫了。
12、我們繼續測試 –C 參數,目的是將每個句子首字母變成大寫。
13、回車執行后,打開 output.txt 文本中每個句子首字母已經是大寫字母了。
以上代碼在 Lazarus IDE v1.8.0 中測試通過,示例代碼請下載