var
  total:Real;
  Number:Real;
  complete:boolean;
  operator:char;

{字符串到实数转换函数}
function ValFn(S:string):Real;
var
  code:integer;
  L:real;
begin
  Val(S,L,code);
  result:=L;
end;

{实数到字符串转换函数,详细内容请参考前面的转换步骤}
function StrFn(L:Real):string;
var
  Sc,Scientific,Converted,Digit:string;
  I,Exponent:integer;
  Negate:boolean;
begin
  str(L,Scientific);
  sc:=Scientific;
  //Scientific格式:3.00000000000000E+0000
  if Copy(Scientific,1,1)='-'then Negate:=true
    else Negate:=false;
  Delete(scientific,1,1);
  //删除Scientific小数点后的多余0
  I:=pos('E',Scientific);
  dec(I);
  while(Copy(Scientific,I,1)='0')do
  begin
    Delete(Scientific,i,1);
    dec(I);
  end;
  //将E前面的数字转换到converted中
  converted:='';
  I:= 1;
  repeat
    Digit:= Copy(Scientific,I,1);
    if (Digit<>'.') and (Digit<>'E') then
      Converted:= converted + Digit;
    Inc(I);
  until (Digit='E');
  //
  Val(Copy(Scientific,I,255),Exponent,i);
  i:=exponent+2;
  while i>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 Length(Converted)>17 then result:=Sc
  else result:=Converted;
end;

{计算过程}
procedure calculate(Nextop:char);
begin
  Number:=ValFn(GetProperty(0,'Item0'));
  if (Nextop='s') or (Nextop='c') or (Nextop='r')
    or (Nextop='v') or (Nextop='i') or (Nextop='o') or (Nextop='±') or (Nextop='%') then
  begin//其它特殊运算
    case Nextop of
      's': Total:=number*number;       //
      'c': Total:=number*number*number;
      'r': Total:=Sqrt(number);//√ 开方
      'v': Total:=1/number;    //1/x
      'i': Total:=Sin(number); //sin
      'o': Total:=cos(number); //cos
      '±': Total:=0-Number;
      '%': Total:=Number/100;
      //'=': Total:=Number;
    end;
    
    //lblResult.caption:=StrFn(Total);
    SetProperty(0,'Item0',StrFn(Total));
    complete:=false;
    operator:='n';
  end
  else//加减乘除
  begin
    //showtrayhint('ca',floattostr(total)+operator+floattostr(number)+nextop);
    case  operator of
      '+' : Total:=Total+number; 
      '-' : Total:=Total-number;
      'x' : Total:=Total*number;
      'd' : if number<>0 then Total:=Total/number;
      '=' : Total:=Number;
      {'m' :Total:=Total mod number;}
      //'n':
    end;
    //lblResult.caption:=StrFn(Total);
    SetProperty(0,'Item0',StrFn(Total));
      
    complete:= true;
    operator:=Nextop;
  end;
end;
 

procedure KeyPress(aKey:string);
var
  r:real;
  s:string;
begin
  //showmessage('dfafa');
  s:=GetProperty(0,'Item0');
  //showmessage(s);
  case aKey of
    '0','1','2','3','4','5','6','7','8','9','.':
      begin
        if complete then
        begin
          s:='';
          complete:=false;
        end;
        s:=s+aKey;
        //lblResult.caption:=s;
        SetProperty(0,'Item0',s);
        //showmessage(s);
      end;
    '+','-': calculate(aKey); 
    '×': calculate('x'); 
    '÷': calculate('d'); 
    '=': calculate('=');
    '√': calculate('r');
    '%': calculate('%');
    '1/x': calculate('v');
    '±': calculate('±');
    //"c"键是彻底清除键，也就是之前所有操作清除。 
    //"ce"键是本次操作清除键。如果输入数据错误可以用"ce"键清除本次输入的数据，如果不小心用了"c"键就是把以前的运算全部取消了。
    'CE':
      begin
        //lblResult.caption:='';
        SetProperty(0,'Item0','');
      end;
    'C':
      begin
        s:='0';
        total:=0;
        complete:=true;
        operator:='+';
        //lblResult.caption:=s;
        SetProperty(0,'Item0',s);
      end;
    'DEL':
      begin
        //lblResult.caption:=copy(s,1,length(s)-1);
        s:=copy(s,1,length(s)-1);
        SetProperty(0,'Item0',s);
        Total:=ValFn(s);
      end;
  end;
  if GetProperty(0,'Item0')='' then complete:=false;
  
  RefreshSkin(false);
end;

begin
  Total:=0.0;
  Number:=0.0;
  complete:=true;
  operator:='+';
  //MiniMode:=false;
  
end.

