Fórum Calculadora Delphi #603516
08/07/2019
0
Olá galera, estou fazendo uma calculadora básica no Delphi, igual a calculadora padrão do windows e estou com dois erros que não faço ideia de como resolver:
1 - Ao clicar em qualquer operador ele continua fazendo o calculo e não faz isso na calculadora do windows.
2 - Ao clicar no operador de adição (+), depois no de subtração (-) ele não troca os operadores da conta, mas continua a fazer o calculo na ordem que clico nos operadores.
Segue código completo que fiz:
unit UnitCalculadora;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TFormCalculadora = class(TForm)
PN_FUNDO: TPanel;
ED_VISOR: TEdit;
BT_UM: TButton;
BT_DOIS: TButton;
BT_TRES: TButton;
BT_QUATRO: TButton;
BT_CINCO: TButton;
BT_SEIS: TButton;
BT_SETE: TButton;
BT_OITO: TButton;
BT_NOVE: TButton;
BT_ZERO: TButton;
BT_MAIS: TButton;
BT_MENOS: TButton;
BT_MULTIPLICA: TButton;
BT_DIVIDE: TButton;
BT_LIMPATUDO: TButton;
BT_IGUAL: TButton;
BT_VIRGULA: TButton;
BT_LIMPACASA: TButton;
BT_APAGA: TButton;
LB_MOSTRACONTA: TLabel;
procedure BT_MAISClick(Sender: TObject);
procedure BT_MENOSClick(Sender: TObject);
procedure BT_MULTIPLICAClick(Sender: TObject);
procedure BT_DIVIDEClick(Sender: TObject);
procedure BT_LIMPATUDOClick(Sender: TObject);
procedure BT_IGUALClick(Sender: TObject);
procedure BT_VIRGULAClick(Sender: TObject);
procedure BT_APAGAClick(Sender: TObject);
procedure BT_LIMPACASAClick(Sender: TObject);
procedure BT_ALLNUMBERSClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure AcertarValores(Valor: String);
procedure Operadores(Operador: String);
procedure ValoresIniciais;
private
{ Private declarations }
Concatenar: Boolean;
Numero, Valor2: Double;
Operacao: String;
public
{ Public declarations }
end;
var
FormCalculadora: TFormCalculadora;
implementation
{$R *.dfm}
procedure TFormCalculadora.BT_ALLNUMBERSClick(Sender: TObject);
begin
if LB_MOSTRACONTA.Caption = '''''''' then
ED_VISOR.Clear;
if Valor2 = 0 then
ED_VISOR.Text := TButton(Sender).Caption
else
ED_VISOR.Text := ED_VISOR.Text + TButton(Sender).Caption;
Valor2 := StrToFloat(ED_VISOR.Text);
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_VIRGULAClick(Sender: TObject);
begin
if pos('''','''', ED_VISOR.Text) >= 1 then
ED_VISOR.Text := ED_VISOR.Text
else
ED_VISOR.Text := ED_VISOR.Text + TButton(Sender).Caption;
if pos('''','''', LB_MOSTRACONTA.Caption) >= 1 then
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption
else
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MAISClick(Sender: TObject);
begin
Operadores('''''''');
Operadores(''''+'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MENOSClick(Sender: TObject);
begin
Operadores(''''-'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MULTIPLICAClick(Sender: TObject);
begin
Operadores(''''*'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_DIVIDEClick(Sender: TObject);
begin
Operadores(''''/'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_LIMPACASAClick(Sender: TObject);
begin
ED_VISOR.Clear;
if LB_MOSTRACONTA.Caption = '''''''' then
LB_MOSTRACONTA.Caption := ''''''''
else
begin
while LB_MOSTRACONTA.Caption <> FloatToStr(Numero) do
LB_MOSTRACONTA.Caption := copy(LB_MOSTRACONTA.Caption, 0, length(LB_MOSTRACONTA.Caption)-1);
end;
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + Operacao;
end;
procedure TFormCalculadora.BT_LIMPATUDOClick(Sender: TObject);
begin
LB_MOSTRACONTA.Caption := '''''''';
ED_VISOR.Text := ''''0'''';
Operacao := '''''''';
Numero := 0;
Valor2 := 0;
end;
procedure TFormCalculadora.BT_APAGAClick(Sender: TObject);
begin
ED_VISOR.Text := copy(ED_VISOR.Text, 1, length(ED_VISOR.Text) -1);
LB_MOSTRACONTA.Caption := copy(LB_MOSTRACONTA.Caption, 1, length(LB_MOSTRACONTA.Caption) -1);
end;
procedure TFormCalculadora.BT_IGUALClick(Sender: TObject);
begin
if Operacao <> ('''''''') then
begin
if Operacao = ''''+'''' then
ED_VISOR.Text := FloatToStr(Numero+StrToFloat(ED_VISOR.Text))
else if Operacao = ''''-'''' then
ED_VISOR.Text := FloatToStr(Numero-StrToFloat(ED_VISOR.Text))
else if Operacao = ''''*'''' then
ED_VISOR.Text := FloatToStr(Numero*StrToFloat(ED_VISOR.Text))
else if Operacao = ''''/'''' then
begin
if StrToFloat(ED_VISOR.Text) = 0 then
ED_VISOR.Text := ''''Não é possível dividir por zero''''
else
ED_VISOR.Text := FloatToStr(Numero/StrToFloat(ED_VISOR.Text));
end;
end;
LB_MOSTRACONTA.Caption := '''''''';
end;
procedure TFormCalculadora.Operadores(Operador: String);
begin
if Operacao = ''''+'''' then
ED_VISOR.Text := FloatToStr(Numero+StrToFloat(ED_VISOR.Text))
else if Operacao = ''''-'''' then
ED_VISOR.Text := FloatToStr(Numero-StrToFloat(ED_VISOR.Text))
else if Operacao = ''''*'''' then
ED_VISOR.Text := FloatToStr(Numero*StrToFloat(ED_VISOR.Text))
else if Operacao = ''''/'''' then
ED_VISOR.Text := FloatToStr(Numero/StrToFloat(ED_VISOR.Text));
Operacao := Operador;
Numero := StrToFloat(ED_VISOR.Text);
Concatenar := False;
Valor2 := 0;
end;
procedure TFormCalculadora.ValoresIniciais;
begin
Concatenar := true;
Operacao := '''''''';
Numero := 0;
end;
procedure TFormCalculadora.AcertarValores(Valor: String);
begin
if ED_VISOR.Text = ''''0'''' then
ED_VISOR.Text := '''''''';
if Concatenar then
ED_VISOR.Text := ED_VISOR.Text + Valor
else
ED_VISOR.Text := Valor;
Concatenar := True;
end;
procedure TFormCalculadora.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ADD then
BT_MAIS.Click;
if Key = VK_SUBTRACT then
BT_MENOS.Click;
if Key = VK_MULTIPLY then
BT_MULTIPLICA.Click;
if Key = VK_DIVIDE then
BT_DIVIDE.Click;
if Key = VK_BACK then
BT_APAGA.Click;
if Key = VK_RETURN then
BT_IGUAL.click;
if Key = VK_NUMPAD0 then
BT_ZERO.Click;
if Key = VK_NUMPAD1 then
BT_UM.Click;
if Key = VK_NUMPAD2 then
BT_DOIS.Click;
if Key = VK_NUMPAD3 then
BT_TRES.Click;
if Key = VK_NUMPAD4 then
BT_QUATRO.Click;
if Key = VK_NUMPAD5 then
BT_CINCO.Click;
if Key = VK_NUMPAD6 then
BT_SEIS.Click;
if Key = VK_NUMPAD7 then
BT_SETE.Click;
if Key = VK_NUMPAD8 then
BT_OITO.Click;
if Key = VK_NUMPAD9 then
BT_NOVE.Click;
if Key = VK_DECIMAL then
BT_VIRGULA.Click;
if Key = VK_DELETE then
BT_LIMPACASA.Click;
end;
end.
1 - Ao clicar em qualquer operador ele continua fazendo o calculo e não faz isso na calculadora do windows.
2 - Ao clicar no operador de adição (+), depois no de subtração (-) ele não troca os operadores da conta, mas continua a fazer o calculo na ordem que clico nos operadores.
Segue código completo que fiz:
unit UnitCalculadora;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TFormCalculadora = class(TForm)
PN_FUNDO: TPanel;
ED_VISOR: TEdit;
BT_UM: TButton;
BT_DOIS: TButton;
BT_TRES: TButton;
BT_QUATRO: TButton;
BT_CINCO: TButton;
BT_SEIS: TButton;
BT_SETE: TButton;
BT_OITO: TButton;
BT_NOVE: TButton;
BT_ZERO: TButton;
BT_MAIS: TButton;
BT_MENOS: TButton;
BT_MULTIPLICA: TButton;
BT_DIVIDE: TButton;
BT_LIMPATUDO: TButton;
BT_IGUAL: TButton;
BT_VIRGULA: TButton;
BT_LIMPACASA: TButton;
BT_APAGA: TButton;
LB_MOSTRACONTA: TLabel;
procedure BT_MAISClick(Sender: TObject);
procedure BT_MENOSClick(Sender: TObject);
procedure BT_MULTIPLICAClick(Sender: TObject);
procedure BT_DIVIDEClick(Sender: TObject);
procedure BT_LIMPATUDOClick(Sender: TObject);
procedure BT_IGUALClick(Sender: TObject);
procedure BT_VIRGULAClick(Sender: TObject);
procedure BT_APAGAClick(Sender: TObject);
procedure BT_LIMPACASAClick(Sender: TObject);
procedure BT_ALLNUMBERSClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure AcertarValores(Valor: String);
procedure Operadores(Operador: String);
procedure ValoresIniciais;
private
{ Private declarations }
Concatenar: Boolean;
Numero, Valor2: Double;
Operacao: String;
public
{ Public declarations }
end;
var
FormCalculadora: TFormCalculadora;
implementation
{$R *.dfm}
procedure TFormCalculadora.BT_ALLNUMBERSClick(Sender: TObject);
begin
if LB_MOSTRACONTA.Caption = '''''''' then
ED_VISOR.Clear;
if Valor2 = 0 then
ED_VISOR.Text := TButton(Sender).Caption
else
ED_VISOR.Text := ED_VISOR.Text + TButton(Sender).Caption;
Valor2 := StrToFloat(ED_VISOR.Text);
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_VIRGULAClick(Sender: TObject);
begin
if pos('''','''', ED_VISOR.Text) >= 1 then
ED_VISOR.Text := ED_VISOR.Text
else
ED_VISOR.Text := ED_VISOR.Text + TButton(Sender).Caption;
if pos('''','''', LB_MOSTRACONTA.Caption) >= 1 then
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption
else
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MAISClick(Sender: TObject);
begin
Operadores('''''''');
Operadores(''''+'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MENOSClick(Sender: TObject);
begin
Operadores(''''-'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_MULTIPLICAClick(Sender: TObject);
begin
Operadores(''''*'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_DIVIDEClick(Sender: TObject);
begin
Operadores(''''/'''');
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + TButton(Sender).Caption;
end;
procedure TFormCalculadora.BT_LIMPACASAClick(Sender: TObject);
begin
ED_VISOR.Clear;
if LB_MOSTRACONTA.Caption = '''''''' then
LB_MOSTRACONTA.Caption := ''''''''
else
begin
while LB_MOSTRACONTA.Caption <> FloatToStr(Numero) do
LB_MOSTRACONTA.Caption := copy(LB_MOSTRACONTA.Caption, 0, length(LB_MOSTRACONTA.Caption)-1);
end;
LB_MOSTRACONTA.Caption := LB_MOSTRACONTA.Caption + Operacao;
end;
procedure TFormCalculadora.BT_LIMPATUDOClick(Sender: TObject);
begin
LB_MOSTRACONTA.Caption := '''''''';
ED_VISOR.Text := ''''0'''';
Operacao := '''''''';
Numero := 0;
Valor2 := 0;
end;
procedure TFormCalculadora.BT_APAGAClick(Sender: TObject);
begin
ED_VISOR.Text := copy(ED_VISOR.Text, 1, length(ED_VISOR.Text) -1);
LB_MOSTRACONTA.Caption := copy(LB_MOSTRACONTA.Caption, 1, length(LB_MOSTRACONTA.Caption) -1);
end;
procedure TFormCalculadora.BT_IGUALClick(Sender: TObject);
begin
if Operacao <> ('''''''') then
begin
if Operacao = ''''+'''' then
ED_VISOR.Text := FloatToStr(Numero+StrToFloat(ED_VISOR.Text))
else if Operacao = ''''-'''' then
ED_VISOR.Text := FloatToStr(Numero-StrToFloat(ED_VISOR.Text))
else if Operacao = ''''*'''' then
ED_VISOR.Text := FloatToStr(Numero*StrToFloat(ED_VISOR.Text))
else if Operacao = ''''/'''' then
begin
if StrToFloat(ED_VISOR.Text) = 0 then
ED_VISOR.Text := ''''Não é possível dividir por zero''''
else
ED_VISOR.Text := FloatToStr(Numero/StrToFloat(ED_VISOR.Text));
end;
end;
LB_MOSTRACONTA.Caption := '''''''';
end;
procedure TFormCalculadora.Operadores(Operador: String);
begin
if Operacao = ''''+'''' then
ED_VISOR.Text := FloatToStr(Numero+StrToFloat(ED_VISOR.Text))
else if Operacao = ''''-'''' then
ED_VISOR.Text := FloatToStr(Numero-StrToFloat(ED_VISOR.Text))
else if Operacao = ''''*'''' then
ED_VISOR.Text := FloatToStr(Numero*StrToFloat(ED_VISOR.Text))
else if Operacao = ''''/'''' then
ED_VISOR.Text := FloatToStr(Numero/StrToFloat(ED_VISOR.Text));
Operacao := Operador;
Numero := StrToFloat(ED_VISOR.Text);
Concatenar := False;
Valor2 := 0;
end;
procedure TFormCalculadora.ValoresIniciais;
begin
Concatenar := true;
Operacao := '''''''';
Numero := 0;
end;
procedure TFormCalculadora.AcertarValores(Valor: String);
begin
if ED_VISOR.Text = ''''0'''' then
ED_VISOR.Text := '''''''';
if Concatenar then
ED_VISOR.Text := ED_VISOR.Text + Valor
else
ED_VISOR.Text := Valor;
Concatenar := True;
end;
procedure TFormCalculadora.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ADD then
BT_MAIS.Click;
if Key = VK_SUBTRACT then
BT_MENOS.Click;
if Key = VK_MULTIPLY then
BT_MULTIPLICA.Click;
if Key = VK_DIVIDE then
BT_DIVIDE.Click;
if Key = VK_BACK then
BT_APAGA.Click;
if Key = VK_RETURN then
BT_IGUAL.click;
if Key = VK_NUMPAD0 then
BT_ZERO.Click;
if Key = VK_NUMPAD1 then
BT_UM.Click;
if Key = VK_NUMPAD2 then
BT_DOIS.Click;
if Key = VK_NUMPAD3 then
BT_TRES.Click;
if Key = VK_NUMPAD4 then
BT_QUATRO.Click;
if Key = VK_NUMPAD5 then
BT_CINCO.Click;
if Key = VK_NUMPAD6 then
BT_SEIS.Click;
if Key = VK_NUMPAD7 then
BT_SETE.Click;
if Key = VK_NUMPAD8 then
BT_OITO.Click;
if Key = VK_NUMPAD9 then
BT_NOVE.Click;
if Key = VK_DECIMAL then
BT_VIRGULA.Click;
if Key = VK_DELETE then
BT_LIMPACASA.Click;
end;
end.
Samuel Pedroso
Curtir tópico
+ 1
Responder
Post mais votado
08/07/2019
Cara, Umas dicas rapidemente pra te ajudar, pra códigos muito extensos, opte por usar a Formatação de pra facilitar a leitura ou use um Pastebin
Segundo, não li o código inteiro por esse motivo, tente focar em achar o seu exato problema, ou indicar onde possa estar.
Terceiro, agora envolvendo o seu problema, tente envolver um padrão de estados para a calculadora, Onde Ela possua por exemplo ,
Estado 1 = Inserir algum valor ,
Estado 2 é ativado ao clicar em algum operador, nesse estado 2 ele consegue alterar esse operador atual, e ao clicar em algum outro numero ele entra de novo pro estado 1 onde pode inserir números,
E o = seria o estado 3 onde resultaria um valor que voltaria pro Estado 1 automaticamente, dei um exemplo só do que pode fazer, mas é com você !
if HasCodeTag Then
Print("Legivel");
Segundo, não li o código inteiro por esse motivo, tente focar em achar o seu exato problema, ou indicar onde possa estar.
Terceiro, agora envolvendo o seu problema, tente envolver um padrão de estados para a calculadora, Onde Ela possua por exemplo ,
Estado 1 = Inserir algum valor ,
Estado 2 é ativado ao clicar em algum operador, nesse estado 2 ele consegue alterar esse operador atual, e ao clicar em algum outro numero ele entra de novo pro estado 1 onde pode inserir números,
E o = seria o estado 3 onde resultaria um valor que voltaria pro Estado 1 automaticamente, dei um exemplo só do que pode fazer, mas é com você !
Gabriel Arcasa
Responder
Gostei + 1
Clique aqui para fazer login e interagir na Comunidade :)