这个很简单的啊,好多入门的书上有过程的啊。对不起大家了,我复制一下。
科学计算器程序代码如下:
unit CalScien;
interface
uses
SysUtils,WinTypes,WinProcs,Messages,Classes,Graphics,Controls,
Forms,Dialogs,StdCtrls,math;
type
Tcalfm = class(TForm)
Entry:TEdit;
Seven:TButton;
Eight:TButton;
Nine:TButton;
plus:TButton;
clear:TButton;
four:TButton;
five:TButton;
six:TButton;
minus:TButton;
Allclear:TButton;
one:TButton;
two:TButton;
three:TButton;
multiply:TButton;
zero:TButton;
equal:TButton;
devide:TButton;
point:TButton;
squa:TButton;
cube:TButton;
reverse:TButton;
sqrt1:TButton;
sin1:TButton;
Cos1:TButton;
procedure zeroClick(Sender:TObject);
procedure twoClick(Sender:TObject);
procedure oneClick(Sender:TObject);
procedure threeClick(Sender:TObject);
procedure fourClick(Sender:TObject);
procedure fiveClick(Sender:TObject);
procedure sixClick(Sender:TObject);
procedure SevenClick(Sender:TObject);
procedure EightClick(Sender:TObject);
procedure NineClick(Sender:TObject);
procedure plusClick(Sender:TObject);
procedure minusClick(Sender:TObject);
procedure multiplyClick(Sender:TObject);
procedure devideClick(Sender:TObject);
procedure pointClick(Sender:TObject);
procedure clearClick(Sender:TObject);
procedure AllclearClick(Sender:TObject);
procedure equalClick(Sender:TObject);
procedure EntryClick(Sender:TObject);
procedure squaClick(Sender:TObject);
procedure cubeClick(Sender:TObject);
procedure sqrt1Click(Sender:TObject);
procedure reverseClick(Sender:TObject);
procedure Cos1Click(Sender:TObject);
procedure sin1Click(Sender:TObject);
private
{Private declarations}
public
procedure calculate(Number:real;Nextop:char);
end;
var
calfm:Tcalfm;
{全局变量}
Total:Real;
complete:boolean;
operator:char;
implementation
{$R*.DFM}
{字符串到实数转换函数}
function ValFn(S:string):Real;
var
code:integer;
L:real;
begin
Val(S,L,code);
ValFn:=L;
end;
{实数到字符串转换函数,详细内容请参考前面的转换步骤}
function StrFn(L:Real):string;
var
Sc,Scientific,Converted,Digit:string;
I,Exponent:integer;
Negate:boolean;
begin
str(L,Scientific);
sc:=Scientific;
if Copy(Scientific,1,1)=’-’then Negate:=true
else Negate:=false;
Delete(scientific,1,1);
I:=pos(’E’,Scientific);
dec(I);
while(Copy(Scientific,I,1)=’0’)do
begin
Delete(Scientific,I,1):
dec(I);
end;
converted:=’’;
I:= 1;
repeat
Digit:= Copy(Scientifc,I,1);
if(Digit< >’.’)and(Digit< >’E’)then
Converted:= converted + Digit;
Inc(I);
until (Digit=’E’);
Val(Copy(Scientific,I,255),Exponent,Ⅰ);
Ⅰ:=exponent+2;
while Ⅰ>length(converted)+1 do converted:=converted十’0’;
while I<1 do
begin
Inc(I);
converted:=’0’十converted;
end;
if I<=Length(Converted)then Insert(’.’,Converted,I);
if Negate then Converted:=’-’十Converted;
if Lengtg(Converted)>17 then StrFn:=Sc
else StrFn:=Converted;
end;
{计算过程}
procedure Tcalfm.calculate(Number:Real;Nextop:char);
var textl:string;
begin
if(not complete or(Total=0.0))then
begin
if(Nextop=’s’)or(Nextop=’c’)or(Nextop=’r’)
or(Nextop=’v’)or(Nextop=’i’)or(Nextop=’o’)then
begin
case Nextop of
’s’:text1:=StrFn(number*number);
’c’:text1:=StrFn(number*number*number);
’r’:Text1=SrtFn(Sqrt(number));
’v’ :Text1=SrtFn(1/number);
’i’ :Text1=SrtFn(Sin(number));
’o’ :Text1=SrtFn(cos(number));
end;
Entry.text:=text1;
complete:false;
operator:=’n’;
end
else
begin
case operator of
’+’:Total:=Total+number;
’-’ :Total:=Total-number;
’x’ :Total:=Total*number;
’D’ :Total:=Total/number;
{’m’ :Total:=Total mod number;}
’n’:
end;
complete:= true;
operator:=Nextop;
end;
end;
end;
{以下过程完成数值输入}
procedure Tcalfm.zeroClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’0’;
end;
procedure Tcalfm.twoClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’2’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’1’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’3’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’4’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’5’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’6’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’7’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’8’;
end;
procedure Tcalfm.oneClick(Sender:T0bject);
begin
if complete then entry.text:=’’;
Entry.text:=entry.text +’9’;
end;
{以下为运算符输入及运算过程}
procedure Tcalfm.plusClick(Sender:TObject);
begin
calculate(ValFn(entry.text),’+’);
Entry.text:=StrFn(Total);
end;
procedure Tcalfm.plusClick(Sender:TObject);
begin
calculate(ValFn(entry.text),’-’);
Entry.text:=StrFn(Total);
end;
procedure Tcalfm.plusClick(Sender:TObject);
begin
calculate(ValFn(entry.text),’x’);
Entry.text:=StrFn(Total);
end;
procedure Tcalfm.plusClick(Sender:TObject);
begin
calculate(ValFn(entry.text),’D’);
Entry.text:=StrFn(Total);
end;
{小数点输入过程}
procedure Tcalfm.pointClick(Sender:TObject);
begin
if complete then Entry.Text:=’’;
if Pos(’.’,Entry.Text)=0 then
Entry.text:= Entry.text +’.’;
end;
{等号完成最终结果}
procedure Tcalfm.equalClick(Sender:T0bject);
begin
calculate(ValFn(entry.text),operator);
Entry.text:= StrFn(Total);
end;
{清零过程}
procedure Tcalfm.clearClick(Sender:T0bject);
begin
Entry.text:=’0’;
operator:=’+’;
complete:=true;
end;
{复位过程}
procedure Tcaclfm.AllclearClick(Sender:T0bject);
begin
Entry.text:=’0’;
Total:=0;
complete:=true;
operator:=’+’;
end;
procedure Tcalfm.EntryChange(Sender:T0bject);
begin
if Entry.text=’’then complete:= false;
end;
{平方运算过程}
procedure Tcalfm.squaClick(Sender:T0bject);
begin
if complete = true then complete:= false;
calculate(ValFn(entry.text),’s’);
end;
{立方运算过程}
procedure Tcalfm.cubeClick(Sender:T0bject);
begin
if complete = true then complete:= false;
calculate(ValFn(entry.text),’c’);
end;
{方根运算过程}
procedure Tcalfm.sqrt1Click(Sender:Tobject);
begin
if complete = true then complete:=false;
calculate(ValFn(entry.text),’r’);
total:= ValFn(Entry.text);
end;
{倒数运算过程}
procedure Tcalfm.reverseClick(Sender:T0bject);
begin
if complete = true then complete:= false;
calculate(ValFn(entry.text),’v’);
end;
{余弦运算过程}
procedure Tcalfm.Cos1Click(Sender:T0bject);
begin
if complete = true then complete:= false;
calculate(ValFn(entry.text),’o’);
end;
{正弦运算过程}
procedure Tcalfm.sin1Click(Sender:T0bject);
begin
if complete = true then complete:= false;
calculate(ValFn(entry.text),’i’);
end;
end.