Вобщем, недавно начал изучать Object Pascal и одним из первых своих творений конечно был простейший калькулятор. Но мне хотелось его усложнить к многоразовому. Вот моя попытка:
Код:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1, ButtonDelete, ButtonReset, CompL, CompR, ButtonT, ButtonL, ButtonI, ButtonX, Button0, ButtonFinal, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9: TButton;
Label1: TLabel;
Memory: TLabel;
procedure Button0Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure ButtonDeleteClick(Sender: TObject);
procedure ButtonFinalClick(Sender: TObject);
procedure ButtonIClick(Sender: TObject);
procedure ButtonLClick(Sender: TObject);
procedure ButtonResetClick(Sender: TObject);
procedure ButtonTClick(Sender: TObject);
procedure ButtonXClick(Sender: TObject);
procedure FormShow(Sender: TObject);
end;
var
Form1: TForm1;
var
i: string;
row: integer;
a, b: integer;
act: char;
answer: real;
implementation
{$R *.lfm}
procedure TForm1.FormShow(Sender: TObject);
begin
a:= 0;
row:= 0
end;
procedure TForm1.Button0Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(0);
if row = 1 then row:= 2;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(1)
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(2)
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(3)
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(4)
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(5)
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(6)
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(7)
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(8)
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
Label1.Caption:= Label1.Caption + Inttostr(9)
end;
{----actions----}
procedure TForm1.ButtonTClick(Sender: TObject);
begin
if row = 0 then
a:= Strtoint(Label1.Caption);
Memory.Caption:= Memory.Caption + Label1.Caption + (' + ');
Label1.Caption:= '';
row:= 1;
act:= '+';
if (row = 2) and (Label1.Caption <> '') then
b:= Strtoint(Label1.Caption);
if act = '+' then answer:= a+b;
if act = '*' then answer:= a*b;
if act = '-' then answer:= a-b;
if act = '/' then answer:= a/b;
act:= '+';
row:= 1;
Memory.Caption:= Memory.Caption + Label1.Caption + (' + ');
Label1.Caption:= Floattostr(answer) + (' + ')
end;
procedure TForm1.ButtonXClick(Sender: TObject);
begin
if row = 0 then
a:= Strtoint(Label1.Caption);
Memory.Caption:= Memory.Caption + Label1.Caption + (' * ');
Label1.Caption:= '';
row:= 1;
act:= '*';
if (row = 2) and (Label1.Caption <> '') then
b:= Strtoint(Label1.Caption);
if act = '+' then answer:= a+b;
if act = '*' then answer:= a*b;
if act = '-' then answer:= a-b;
if act = '/' then answer:= a/b;
act:= '*';
row:= 1;
Memory.Caption:= Memory.Caption + Label1.Caption + (' * ');
Label1.Caption:= Floattostr(answer) + (' * ')
end;
procedure TForm1.ButtonIClick(Sender: TObject);
begin
if row = 0 then
a:= Strtoint(Label1.Caption);
Memory.Caption:= Memory.Caption + Label1.Caption + (' - ');
Label1.Caption:= '';
row:= 1;
act:= '-';
if (row = 2) and (Label1.Caption <> '') then
b:= Strtoint(Label1.Caption);
if act = '+' then answer:= a+b;
if act = '*' then answer:= a*b;
if act = '-' then answer:= a-b;
if act = '/' then answer:= a/b;
act:= '-';
row:= 1;
Memory.Caption:= Memory.Caption + Label1.Caption + (' - ');
Label1.Caption:= Floattostr(answer) + (' - ')
end;
procedure TForm1.ButtonLClick(Sender: TObject);
begin
if row = 0 then
a:= Strtoint(Label1.Caption);
Memory.Caption:= Memory.Caption + Label1.Caption + (' / ');
Label1.Caption:= '';
row:= 1;
act:= '/';
if (row = 2) and (Label1.Caption <> '') then
b:= Strtoint(Label1.Caption);
if act = '+' then answer:= a+b;
if act = '*' then answer:= a*b;
if act = '-' then answer:= a-b;
if act = '/' then answer:= a/b;
act:= '/';
row:= 1;
Memory.Caption:= Memory.Caption + Label1.Caption + (' / ');
Label1.Caption:= Floattostr(answer) + (' / ')
end;
{---- end ----}
procedure TForm1.ButtonDeleteClick(Sender: TObject);
begin
i:= Label1.Caption;
if i <>'' then
delete(i, Length(i),1);
Label1.Caption:=i;
end;
procedure TForm1.ButtonResetClick(Sender: TObject);
begin
Label1.Caption:= ('');
Memory.Caption:= ('');
row:= 0;
a:= 0;
b:= 0;
end;
procedure TForm1.ButtonFinalClick(Sender: TObject);
begin
Memory.Caption:= (Memory.Caption + ' ' + act + ' ' + Label1.Caption + ' = ' + Floattostr(answer));
end;
end.
Да, я знаю что это и близко недоработано (например можно поставить несколько знаков сразу, равно полностью неработоспособно итд), но меня волнует другая проблема. Уже на данной стадии так скажем разработки, проведение какой то функции игнорирует <> '' и true/false а исполняет всё ёё совместимое. Может я просто туплю, об'ясните где.
Заранеё спасибо