unit MainForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Math,
StdCtrls, ComCtrls, Menus;
type
{ TMainForm }
TMainForm =
class(TForm)
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
Stack: TListBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button0: TButton;
ButtonPoint: TButton;
ButtonEnter: TButton;
ButtonAdd: TButton;
ButtonSub: TButton;
ButtonMul: TButton;
ButtonDiv: TButton;
edtCalc: TEdit;
ButtonPower: TButton;
ButtonPosNeg: TButton;
ButtonRoot: TButton;
ButtonLog: TButton;
ButtonRound: TButton;
ButtonPi: TButton;
ButtonFak: TButton;
Button1_2: TButton;
Button10: TButton;
Button11: TButton;
ButtonProm: TButton;
ButtonProz: TButton;
procedure Button0Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button1_2Click(Sender: TObject);
procedure ButtonPowerClick(Sender: TObject);
procedure ButtonAddClick(Sender: TObject);
procedure ButtonDivClick(Sender: TObject);
procedure ButtonEnterClick(Sender: TObject);
procedure ButtonFakClick(Sender: TObject);
procedure ButtonLogClick(Sender: TObject);
procedure ButtonMulClick(Sender: TObject);
procedure ButtonPiClick(Sender: TObject);
procedure ButtonPointClick(Sender: TObject);
procedure ButtonPosNegClick(Sender: TObject);
procedure ButtonPromClick(Sender: TObject);
procedure ButtonProzClick(Sender: TObject);
procedure ButtonRootClick(Sender: TObject);
procedure ButtonRoundClick(Sender: TObject);
procedure ButtonSubClick(Sender: TObject);
procedure edtCalcClick(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure StackDblClick(Sender: TObject);
private
{ private declarations }
function Pop: Real;
procedure Push(R: Real);
public
{ public declarations }
end;
var
frmMainForm: TMainForm;
implementation
function TMainForm.Pop: Real;
begin
Result := StrToFloat(Stack.Items[Stack.Items.Count - 1]);
Stack.Items.Delete(Stack.Items.Count - 1);
end;
procedure TMainForm.Push(R: Real);
begin
Stack.Items.Add(FloatToStr(R));
end;
procedure TMainForm.Button0Click(Sender: TObject);
begin
edtCalc.Text := edtCalc.Text + (Sender
as TButton).Caption;
end;
procedure TMainForm.Button10Click(Sender: TObject);
begin
Push(1/4);
end;
procedure TMainForm.Button11Click(Sender: TObject);
begin
Push(3/4);
end;
procedure TMainForm.Button1_2Click(Sender: TObject);
begin
Push(1/2);
end;
procedure TMainForm.ButtonPowerClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Power(Num2, Num1));
end;
end;
procedure TMainForm.ButtonAddClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Num2 + Num1);
end;
end;
procedure TMainForm.ButtonDivClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Num2 / Num1);
end;
end;
procedure TMainForm.ButtonEnterClick(Sender: TObject);
begin
if edtCalc.Text <> '
'
then
begin
Stack.Items.Add(edtCalc.Text);
edtCalc.Text := '
';
end;
end;
procedure TMainForm.ButtonFakClick(Sender: TObject);
var
Num: Real;
I, J: Integer;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
J := 1;
for I := 1
to Round(Num)
do
J := J * I;
Push(Round(J));
end;
end;
procedure TMainForm.ButtonLogClick(Sender: TObject);
var
Num: Real;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
Push(Log10(Num));
end;
end;
procedure TMainForm.ButtonMulClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Num2 * Num1);
end;
end;
procedure TMainForm.ButtonPiClick(Sender: TObject);
begin
Stack.Items.Add(FloatToStr(PI));
end;
procedure TMainForm.ButtonPointClick(Sender: TObject);
begin
if Pos('
.', edtCalc.Text) = 0
then
edtCalc.Text := edtCalc.Text + '
.';
end;
procedure TMainForm.ButtonPosNegClick(Sender: TObject);
var
Num: Real;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
Push(Num * -1);
end;
end;
procedure TMainForm.ButtonPromClick(Sender: TObject);
var
Num: Real;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
Push(Num / 1000);
end;
end;
procedure TMainForm.ButtonProzClick(Sender: TObject);
var
Num: Real;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
Push(Num / 100);
end;
end;
procedure TMainForm.ButtonRootClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Power(Num2, (1 / Num1)));
end;
end;
procedure TMainForm.ButtonRoundClick(Sender: TObject);
var
Num: Real;
begin
if Stack.Items.Count > 0
then
begin
Num := Pop;
Push(Round(Num));
end;
end;
procedure TMainForm.ButtonSubClick(Sender: TObject);
var
Num1, Num2: Real;
begin
if Stack.Items.Count > 1
then
begin
Num1 := Pop;
Num2 := Pop;
Push(Num2 - Num1);
end;
end;
procedure TMainForm.edtCalcClick(Sender: TObject);
begin
edtCalc.Text := Copy(edtCalc.Text, 1, Length(edtCalc.Text) - 1);
end;
procedure TMainForm.MenuItem1Click(Sender: TObject);
begin
ShowMessage('
(C) 2007 by Paddy Lerner [PaddyLerner@gmail.com]');
end;
procedure TMainForm.StackDblClick(Sender: TObject);
begin
if not Stack.ItemIndex = -1
then
Stack.Items.Delete(Stack.ItemIndex);
end;
initialization
{$I MainForm.lrs}
end.