Delphi APP 開發入門(三)簡易計算機

閲讀次數:6823 發表時間:2014/05/20

上週大家學會了設定Android/iOS編譯環境以及寫出第一個Hello World之後,我們今天要來寫第一隻APP簡易計算機。

新增一個空白的APP專案後,我們在畫面上新增一個Edit元件,並且將Align的屬性設為Top(置頂)。

接著更改Edit的TextSettings內的HorzAlign為Trailing,讓文字是靠右對齊。

接著開始佈置計算機的按鈕,我們使用Button元件,更改Text屬性並且拉到適當位置。

都佈置好了之後,我們就可以開始撰寫程式了。程式大概有幾個部份~

  • 數字按鈕處理(按下0~9及小數點)
  • 運算元處理(按下+、-、*、/)
  • 計算結果處理(按下=)
  • 其他部份(按C歸零、按+/-、與%)

我們來一一處理它。

數字按鈕處理

我們先在程式裡宣告三個變數,分別是num1、num2 第一、二次輸入的數字,R運算結果,類型則為Extended。另外是一個iOperator的Integer,這是用來記錄運算元的(1表+、2表-、3表*、4表/)。

var
Form1: TForm1;
num1,num2,R:Extended;
ioperator:integer;

接著我們處理數字及小數點按下的事件,在按鈕0雙擊二下,輸入以下程式碼。如果顯示的是0或是不是按小數點的話,就直接顯示輸入的數字,如果不成立的話,就將螢幕上顯示的數字累加。

procedure TForm1.Button1Click(Sender: TObject);
begin
if (Edit1.Text = '0') And (TButton(Sender).Text<>'.') then
Edit1.Text := TButton(Sender).Text
else Edit1.Text := Edit1.Text + TButton(Sender).Text;
end;

寫完後,我們將其它1-9以及小數點的按鈕的OnClick事件都指令到Button1Click就可以了。因為在我們剛才寫的事件中,是透過TButton(Sender).Text去判斷按鈕值,而不同按鈕傳入的Sender不同就可以做各自判斷了!

運算元處理

接著處理加減乘除的部份,我們也在+的按鈕點二下,輸入以下的程式碼。在按下時先將目前螢幕上顯示的值記錄到num1,接著判斷按鈕是+-*/而給予iOperator不同的值,最後將螢幕上清為0。

procedure TForm1.Button5Click(Sender: TObject);
begin
num1 := StrToFloat(Edit1.Text);
if TButton(Sender).Text = '+' then
iOperator := 1
else if TButton(Sender).Text = '-' then
iOperator := 2
else if TButton(Sender).Text = 'X' then
iOperator := 3
else if TButton(Sender).Text = '/' then
iOperator := 4
else iOperator := 0;
Edit1.Text := '0';
end;

寫完後一樣如同上一個步驟,將-*/的按鈕指定到Button5Click即可。

計算結果處理

最後處理結果=的按鈕,在=按鈕雙擊二下後,輸入以下的程式碼。

procedure TForm1.Button7Click(Sender: TObject);
begin
if iOperator >0 then begin
num2 := StrToFloat(Edit1.Text);
case iOperator of
1: R := num1 + num2;
2: R := num1 - num2;
3: R := num1 * num2;
4: R := num1 / num2;
end;
Edit1.Text := FloatToStr(R);
end;
end;

其他部份

  • 歸零 – 把螢幕清空為零
  • 正負值 – 將數字乘以負1
  • 百分比 – 將數字除以100

結果

最後執行後,我們可以看到程式的執行結果囉!完成了我們第一支同時支援Android、iOS、Wind32的計算機APP了。

原始碼下載  https://github.com/superlevin/XE6calculator

Delphi APP 開發入門(三)簡易計算機的更多相关文章

  1. Delphi APP 開發入門(四)簡易手電筒

    Delphi APP 開發入門(四)簡易手電筒 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:32 ...

  2. Delphi APP 開發入門(五)GPS 定位功能

    Delphi APP 開發入門(五)GPS 定位功能 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數 ...

  3. Delphi APP 開發入門(八)SQLite資料庫

    Delphi APP 開發入門(八)SQLite資料庫 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次 ...

  4. Delphi APP 開發入門(七)通知與雲端推播

    Delphi APP 開發入門(七)通知與雲端推播 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數: ...

  5. Delphi APP 開發入門(一)重生的 Delphi

    Delphi APP 開發入門(一)重生的 Delphi 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀 ...

  6. Delphi APP 開發入門(六)Object Pascal 語法初探

    Delphi APP 開發入門(六)Object Pascal 語法初探 分享: Share on facebookShare on twitterShare on google_plusone_sh ...

  7. Delphi APP 開發入門(十)REST Client 開發

    Delphi APP 開發入門(十)REST Client 開發 分享: Share on facebookShare on twitterShare on google_plusone_share ...

  8. Delphi APP 開發入門(九)拍照與分享

    Delphi APP 開發入門(九)拍照與分享 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:30 ...

  9. Delphi APP 開發入門(二)Android/iOS設定,Hello World

    Delphi APP 開發入門(二)Android/iOS設定,Hello World 分享: Share on facebookShare on twitterShare on google_plu ...

随机推荐

  1. VBS 移除excel数据公式,只保留值

    如果将excel数据公式移除,只保留计算之后的值,将大大减少excel文件. 因为有上篇移除excel外部数据链接的经验,进行excel数据公式移除将快的多,方法如下. 首先我们得明白怎么手动移除ex ...

  2. print多重打印

    遇见有趣的问题必须记录下来,当时的想法思路也要记下来 以下两行代码打印出来的结果会是什么 print('2 * 3 = %d' % (2 * 3)) print('2 * 3 = %d' % 2 * ...

  3. Juicer——a fast template engine

    https://blog.csdn.net/yutao_struggle/article/details/79201688 当前最新版本: 0.6.8-stable Juicer 是一个高效.轻量的前 ...

  4. A Great List of Windows Tools

    Windows is an extremely effective and a an efficient operating system. Like any other operating syst ...

  5. netty接收大文件的方法

    参考:http://blog.csdn.net/linuu/article/details/51371595 https://www.jianshu.com/p/a0a51fd79f62 netty默 ...

  6. java -jar后台启动

    nohup  java -jar XX.jar >logs.log &

  7. Python全栈day21(作业针对一个文件进行查询修改删除的操作练习)

    需求,有一个配置文件test.conf内容如下 backend www1 server 1 server 2 backend www2 server 3 server 4 add [{'backend ...

  8. poj3585 Accumulation Degree【树形DP】【最大流】

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:3151   Accepted: 783 ...

  9. tcpdump linux抓http请求头

    sudo tcpdump -i eth0 port 80 -s 1024 -l -A

  10. web.xml 中以编码方式添加filter并设置初始化参数AbstractAnnotationConfigDispatchServletInitializer

    web.xml中配置filter <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...