procedure TForm1.btn_0tobtn9(Sender: TObject); begin if jl_jjccEX = true then begin edit1.Text := '; jl_jjccEX := false; end; if edit1.Text = '0' then edit1.Text := '; edit1.Text := edit1.Text + (Sender as TButton).Caption; //1TO9的公共事件
end;
procedure TForm1.btn_DotClick(Sender: TObject); begin if jld = 1 then //如果小数点以用则结束 exit; if length(edit1.Text) < 1 then begin edit1.Text := '0' + '.'; inc(jld); //记录小数点次数 end else begin edit1.Text := edit1.text + '.'; inc(jld); //记录小数的次数 end; end;
procedure TForm1.btn_0Click(Sender: TObject); begin if jl_jjccEX = true then begin edit1.Text := '; jl_jjccEX := false; end; if edit1.Text = '0' then //如果为0按0则为0 edit1.Text := '0' else //如果不为的情况 edit1.Text := edit1.Text + '0'; if jl_jjccfh = '/' then begin MessageBox(Handle, '除数为0无意义', '简易计算器提示', MB_OK + MB_ICONEXCLAMATION); jl_jjccfh := '; edit1.Text := '0'; end;
procedure TForm1.btn_tgClick(Sender: TObject); begin {判断格到一位时候为0} if (Length(edit1.Text) = 2) and (Copy(edit1.Text,1,1) = '-') then edit1.Text := '0'; //如果为负数则退到2位时再退为0 if edit1.Text = '0' then begin edit1.Text := '0'; jld := 0; //如每次退格到0则,给记录小数点的变量重新赋值 jlzf:= 0; //如每次退格到0则,给记录+/-的变量重新赋值 end else if Length(edit1.Text) = 1 then begin edit1.Text := '0'; jld:= 0; //如每次退格到0则,给记录小数点的变量重新赋值 jlzf:= 0; //如每次退格到0则,给记录+/-的变量重新赋值 end else //取edit1.text第二位起始edit1.text - 1的文本 edit1.Text := Copy(edit1.Text,1,Length(edit1.Text) - 1);
end;
procedure TForm1.btn_zfClick(Sender: TObject); {处理+/-的事件} var zf: string; //声明局部变量储存插入+/- begin Inc(jlzf); zf := edit1.Text; if jlzf mod 2 <> 0 then if edit1.Text <> '0' then begin Insert('-',zf,1); edit1.Text := zf; end; if jlzf mod 2 = 0 then if Copy(edit1.Text,1,1) = '-' then edit1.Text := Copy(edit1.Text,2,Length(edit1.Text) - 1);
end;
procedure TForm1.jisuan(Sender: TObject); begin jl_jjccEX := true; Chucuen := StrToFloat(edit1.Text); if (Sender as TButton).Caption = '+' then jl_jjccfh := '+'; //记录+号 if (Sender as TButton).Caption = '-' then jl_jjccfh := '-'; //记录-号 if (Sender as TButton).Caption = '*' then jl_jjccfh := '*'; //记录*号 if (Sender as TButton).Caption = '/' then jl_jjccfh := '/'; //记录/号
end; {执行计算的过程} procedure TForm1.btn_dyClick(Sender: TObject); begin try //除0的错误避免 if jl_jjccfh = '+' then //得出+的结果 edit1.Text := FloatToStr(Chucuen + StrToFloat(edit1.Text)); if jl_jjccfh = '-' then //得出-的结果 edit1.Text := FloatToStr(Chucuen - StrToFloat(edit1.Text)); if jl_jjccfh = '*' then //得出*的结果 edit1.Text := FloatToStr(Chucuen * StrToFloat(edit1.Text)); if jl_jjccfh = '/' then //得出/的结果 edit1.Text := FloatToStr(Chucuen / StrToFloat(edit1.Text)); except MessageBox(Handle, '发生异常错误' + #13#10 + '可能被除数是0'+ #13#10 + '或除数是0', '起点简易计算器Error', MB_OK + MB_ICONERROR); end;
end;
procedure TForm1.Timer1Timer(Sender: TObject); begin Inc(jl_Change); if jl_Change > 8 then jl_Change := 0; case jl_Change of 1: label1.Font.Color := clRed; 2: label1.Font.Color := clBlue; 3: label1.Font.Color := clTeal; 4: label1.Font.Color := clLime; 5: label1.Font.Color := clYellow; 6: label1.Font.Color := clAqua; 7: label1.Font.Color := clWhite; 8: label1.Font.Color := clGreen; end; end;