Hola Juampi,
Con esto puedes avanzar un poco.
Esto al principio
Código Delphi
[-]
const
WVK_BACKDELETE = #8;
WVK_SUM = #43;
WVK_REST = #45;
WVK_EQUAL = #61;
WVK_NULL = #0;
WVK_EMPTY = '';
type
TCalculatorReg = record
NumberStr: String;
KeyPred: Char;
KeyNext: Char;
SubTotal: Integer;
end;
Y esto en los eventos correspondientes.
Código Delphi
[-]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9', WVK_SUM, WVK_REST, WVK_EQUAL, WVK_BACKDELETE]) then
Key := WVK_NULL;
end;
procedure TForm1.Button1Click(Sender: TObject);
function IsNumeric(S: Char): Boolean;
begin
Result := (S in ['0'..'9']);
end;
function IsSum(S: Char): Boolean;
begin
Result := (S = '+');
end;
function IsRest(S: Char): Boolean;
begin
Result := (S = '-');
end;
var
Calc: TCalculatorReg;
nI: Integer;
S: String;
Key: Char;
begin
FillChar(Calc, SizeOf(Calc), #0);
Key := WVK_NULL;
S := Edit1.Text;
for nI := 1 to Length(S) do
begin
if IsNumeric(S[nI]) then
Calc.NumberStr := Calc.NumberStr + S[nI]
else
if IsSum(S[nI]) then
Key := WVK_SUM
else
if IsRest(S[nI]) then
Key := WVK_REST
else
Key := WVK_EQUAL;
if (Key <> WVK_EMPTY) and (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
Calc.KeyNext := Key;
if (Key <> WVK_EMPTY) and (Calc.KeyPred = WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
Calc.KeyPred := Key;
Key := WVK_NULL;
if (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
if (Calc.SubTotal = 0) then
begin
Calc.SubTotal := StrToInt(Calc.NumberStr);
Calc.NumberStr := WVK_EMPTY;
end;
if (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext <> WVK_EMPTY) then
begin
if (Calc.KeyPred = WVK_SUM) then
Calc.SubTotal := Calc.SubTotal + StrToInt(Calc.NumberStr);
if (Calc.KeyPred = WVK_REST) then
Calc.SubTotal := Calc.SubTotal - StrToInt(Calc.NumberStr);
Calc.NumberStr := WVK_EMPTY;
Calc.KeyPred := Calc.KeyNext;
Calc.KeyNext := WVK_NULL;
end;
end;
Label2.Caption := IntToStr(Calc.SubTotal);
end;
Un Saludo.
p.d.: Sólo funciona con enteros y al final tienes que poner un = para que lo sume todo, ya lo modificas como quieras.