Тема: Калькулятор. Не правильно виконує дію.
Наприклад, 1+1 рахує правильно, а 1+1+1 - не правильно. І так для всіх операцій. Як виправити?
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Math, Graphics, Controls, Forms,
Dialogs, StdCtrls, sSkinManager;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Edit1: TEdit;
sSkinManager1: TsSkinManager;
Button17: TButton;
Button18: TButton;
Button19: TButton;
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Button18Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button19Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
kod:char;
x,y,z:real;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'1'
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'2'
end;
procedure TForm2.Button5Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'3'
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'4'
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'5'
end;
procedure TForm2.Button7Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'6'
end;
procedure TForm2.Button9Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'7'
end;
procedure TForm2.Button11Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'8'
end;
procedure TForm2.Button10Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'9'
end;
procedure TForm2.Button12Click(Sender: TObject);
begin
edit1.Text:=edit1.Text+'0'
end;
procedure TForm2.Button16Click(Sender: TObject);
begin
edit1.Clear;
end;
procedure TForm2.Button6Click(Sender: TObject);
begin
if edit1.Text<>'' then x:=strtofloat(edit1.text);
kod := '*';
edit1.Clear;
end;
procedure TForm2.Button8Click(Sender: TObject);
begin
if edit1.Text<>'' then x:=strtofloat(edit1.text);
kod := '/';
edit1.Clear;
end;
procedure TForm2.Button15Click(Sender: TObject);
begin
if edit1.Text<>'' then x:=strtofloat(edit1.text);
kod := '+';
edit1.Clear;
end;
procedure TForm2.Button14Click(Sender: TObject);
begin
if edit1.Text<>'' then x:=strtofloat(edit1.text);
kod := '-';
edit1.Clear;
end;
procedure TForm2.Button13Click(Sender: TObject);
begin
if edit1.Text<>'' then y:=strtofloat(edit1.text);
case kod of
'+': z:=x+y;
'-': z:=x-y;
'*': z:=x*y;
'/': if y=0 then ShowMessage ('На нуль ділити не можна!') else z:=x/y;
else ShowMessage ('Виконайте дію');
end;
edit1.Text:=FloatToStr(Z);
end;
procedure TForm2.Button17Click(Sender: TObject);
begin
x:=StrToFloat(Edit1.Text);
Edit1.Clear;
z:=sqrt(x);
Edit1.Text:=FloatToStr(z);
end;
procedure TForm2.Button18Click(Sender: TObject);
begin
x:=StrToFloat(Edit1.Text);
Edit1.Clear;
z:=x*x;
Edit1.Text:=FloatToStr(z);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
Application.Title:= 'Калькулятор';
end;
procedure TForm2.Button19Click(Sender: TObject);
begin
if Edit1.Text = '0' then
begin
Edit1.Text := '0';
Z := 1;
end;
if Pos('.',Edit1.Text) = 0 then
Edit1.Text := Edit1.Text + ',';
end;
end.