主要應用了:
1. 手動建立控制項及釋放;
2. 繫結控制項事件;
**如下:
unit uformmain;
inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls, extctrls, comctrls;
const calccount = 5;
type
tformmain = class(tform)
edtexpression: tedit;
pnlnum: tpanel;
btnclose: tbutton;
pnlcalc: tpanel;
btnclear: tbutton;
stat1: tstatusbar;
btnbackspace: tbutton;
procedure formcreate(sender: tobject);
procedure formdestroy(sender: tobject);
procedure btnnumclick(sender: tobject);
procedure btncalcclick(sender: tobject);
procedure btnclearclick(sender: tobject);
procedure btncloseclick(sender: tobject);
procedure edtexpressionkeypress(sender: tobject; var key: char);
procedure btnbackspaceclick(sender: tobject);
private
public
btnnum: array[0..9] of tbutton;
btncalc: array[0..calccount - 1] of tbutton;
end;
const
calcopts: array[0..calccount - 1] of char = ('+', '-', '*', '/', '=');
var formmain: tformmain;
result: integer = 0;
opt: char; // perior operator
implementation
procedure tformmain.btncalcclick(sender: tobject);
var c: char;
begin
if edtexpression.text ='' then exit;
:= edtexpression.text + (sender as tbutton).caption;
c := (sender as tbutton).caption[1];
if opt <> '' then
begin
case opt of
'+':
begin
result := result + strtoint(edtexpression.text);
end;
'-':
begin
result := result - strtoint(edtexpression.text);
end;
'*':
begin
result := result * strtoint(edtexpression.text);
end;
'/':
begin
if strtoint(edtexpression.text) = 0 then exit;
result := result div strtoint(edtexpression.text);
end;
'=':
begin
end;
end;
endelse
result := strtoint(edtexpression.text);
if (c <> '=') then
begin
edtexpression.text := '0';
endelse
edtexpression.text := inttostr(result);
opt := c;
stat1.******text := inttostr(result);
end;
procedure tformmain.btnnumclick(sender: tobject);
begin
edtexpression.text := inttostr(strtoint(edtexpression.text) * 10
+ strtoint((sender as tbutton).caption));
end;
procedure tformmain.formcreate(sender: tobject);
var i: integer;
begin
for i := low(btnnum) to high(btnnum) do
begin
btnnum[i] := tbutton.create(self);
with btnnum[i] do
begin
parent := pnlnum;
left := 20 + (i mod 3) * 50;
top := 10 + ( i div 3) * 40;
width := 30;
height := 30;
caption := inttostr((i+1) mod 10);
onclick := btnnumclick;
end;
end;
// 初始化運算子鍵
for i := 0 to calccount - 1 do
begin
btncalc[i] := tbutton.create(self);
with btncalc[i] do
begin
parent := pnlcalc;
left := 10;
top := 5 + i * 35;
width := 30;
height := 30;
caption := calcopts[i];
font.size := 12;
font.style := font.style + [fsbold];
onclick := btncalcclick;
end;
end;
stat1.******panel := true;
edtexpression.text := '0';
end;
procedure tformmain.formdestroy(sender: tobject);
var i: integer;
begin
for i := low(btnnum) to high(btnnum) do
begin
if assigned(btnnum[i]) then
freeandnil(btnnum);
end;
for i := low(btncalc) to high(btncalc) do
begin
if assigned(btncalc[i]) then
freeandnil(btncalc);
end;
end;
procedure tformmain.btnclearclick(sender: tobject);
begin
edtexpression.text := '0';
result := 0;
stat1.******text := inttostr(result);
end;
procedure tformmain.btncloseclick(sender: tobject);
begin
close;
end;
procedure tformmain.edtexpressionkeypress(sender: tobject; var key: char);
begin
if not (key in ['0' .. '9']) then key := #0;
end;
procedure tformmain.btnbackspaceclick(sender: tobject);
begin
edtexpression.text := inttostr(strtoint(edtexpression.text) div 10);
end;
end.
字串應用(計算器)
給定乙個包含正整數 加 減 乘 除 的算數表示式 括號除外 計算其結果。表示式僅包含非負整數,四種運算子和空格。整數除法僅保留整數部分。示例 輸入 3 2 2 輸出 7 答案 思路 1.碰到數字直接入棧 2.碰到空格,直接下一步 3.碰到 碰到 下乙個數字直接入棧 碰到 num入棧 num num ...
c 資料結構 棧的應用 計算器
新年第一次發,這幾天在複習期末考試。但是好像已經複習完了但是還有十天多才考試感覺挺無聊的就寫個計算器。但是 太難看。裡一堆的if else。我還是學好catch throw吧!先貼出來。看看再修修補補,好好弄弄結構。買了明晚星戰的票。那今晚要不要出去high呢。pragma once ifndef ...
棧的應用 計算器 加,減,乘,除,括號
最初接觸此類的題目,是只涉及到的加減乘除四則運算,沒有符號的操作,因為在一次筆試中遇到了有括號的情況,抱著遇到問題一定要去解決的態度,筆試結束後嘗試將這道題目進行還原。這裡的思想主要是用到了棧,分為兩個棧,乙個棧存放數字,另外乙個棧存放運算子和括號 include include using nam...