⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fccalculator.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 5 页
字号:
        teststr := CalcEdit.Text;
        digits := 0;
        for index := 1 to length(teststr) do
        begin
           case teststr[index] of
           '0'..'9':inc(digits);
           end;
        end;
        if digits >= CalcPrecision then begin
          beep;
          exit;
        end;


        if (not FDecimalEntered) and //(not DecimalInStr(CalcEdit.Text)) and
          ((CalcEdit.Text = '0'+decimalseparator) or (CalcEdit.Text = '0') or (FCurrentValue = 0)) then
           CalcEdit.Text := '';

        teststr := DecimalSeparator;
        if Copy(CalcEdit.Text,Length(CalcEdit.Text),1)=teststr then
        if not FDecimalEntered then //DecimalInStr(CalcEdit.Text) then
           CalcEdit.Text := Copy(CalcEdit.Text,1,Length(CalcEdit.Text)-1);

        FBackSpaceValid := True;

        CalcEdit.Text := CalcEdit.Text + Btns[ButtonType].Caption;
        FCurrentValue := fcStrToFloat(CalcEdit.Text);

        if not (DecimalInStr(CalcEdit.Text) and (Btns[ButtonType].Caption = '0')) then
           CalcEdit.Text := GetformattedText(FCurrentValue);

        CalcEdit.SelStart := Length(CalcEdit.Text);
        if (FStatusLabel <> nil) then
           FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
        FLastButtonType := ButtonType;
     end;

    btDecimal:
     begin
        if FClearOnNextKey then begin
           if FLastOperatorEquals then Reset
           else CalcEdit.Text := '0';
        end;

        FClearOnNextKey := false;
        if (CalcEdit.Text = '0'+decimalseparator) or (CalcEdit.Text = '0') or (FCurrentValue = 0) then
           CalcEdit.Text := '0';

        If not FDecimalEntered then begin//DecimalinStr(CalcEdit.Text) then begin
           FCurrentValue := fcStrToFloat(CalcEdit.Text);
           teststr := DecimalSeparator;
           if Copy(CalcEdit.Text,Length(CalcEdit.Text),1)<>teststr then
              CalcEdit.Text := CalcEdit.Text + Btns[ButtonType].Caption;
           if (FStatusLabel <> nil) then
              FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
        end
        else if FLastButtonType=btDecimal then Beep;
        FDecimalEntered := True;
        FLastButtonType := ButtonType;
     end;

    btPlusMinus:
     begin
      FLastButtonType := ButtonType;

      FCurrentValue := fcStrToFloat(CalcEdit.Text) * -1;

      CalcEdit.Text := FloatToStr(FCurrentValue);
      CalcEdit.SelStart := Length(CalcEdit.Text);

      if FStatusLabel <> nil then begin
         if FClearOnNextKey then
           FStatusLabel.Caption := '0'
         else
           FStatusLabel.Caption := FLastStatus+CalcEdit.Text;
      end;
     end;

    btClearAll:
     begin
      Reset;
     end;

    btClear:
     begin
      FCurrentValue := 0;
      CalcEdit.Text := '0';
      if FStatusLabel <> nil then begin
         if FClearOnNextKey then
           FStatusLabel.Caption := '0'
         else
           FStatusLabel.Caption := FLastStatus;
      end;
      FDecimalEntered := False;
     end;

    btPercent,btAdd,btSubtract,btDivide,btMultiply,btEquals:
     begin
       TempValue := fcStrToFloat(CalcEdit.Text);

       if (Length(CalcEdit.Text) > 0) and (ButtonType <> btEquals) then begin
          if (FLastOperatorEquals) then begin
              FLastOperand := TempValue;
              FLastOp := ButtonType;
              FLastOperatorEquals := False;
              if FStatusLabel <> nil then
                 FStatusLabel.Caption:=FLastStatus+OpToChar(ButtonType);
              FLastStatus := FStatusLabel.Caption;
              FCurrentValue := 0;
              FClearOnNextKey := True;
          end
          else if not(FLastOp in [btMultiply,btDivide,btAdd,btSubtract,btPercent]) then
          begin
             FLastOperand := TempValue;
             FNextToLastOp := FLastOp;
             FLastOp := ButtonType;
             FResultValue:=tempvalue;
             FClearOnNextKey := True;
             if FStatusLabel <> nil then begin
                if FStatusLabel.Caption = '' then FStatusLabel.Caption := '0';
                FLastStatus := FStatusLabel.Caption;
                FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
             end;
             FCurrentValue := 0;
          end
          else if (FLastOp in [btMultiply,btDivide,btAdd,btSubtract]) then begin
             if ButtonType = btPercent then begin
                fLastOperand := fResultValue * (TempValue/100.0);
             end
             else begin
               if FLastButtonType in [btMultiply,btDivide,btAdd,btSubtract] then exit; //10/05/2001-Shouldn't do anything if hit key more than once.
               FClearOnNextKey := True;
               case FLastOp of
                 btMultiply: fResultValue := fResultValue * TempValue;
                 btDivide: begin
                  try
                    fResultValue := fResultValue / TempValue;
                  except
                    on E: Exception do begin   //**** Need Translation for Strings like below
                       if TempValue = 0.0 then
                          raise EfcCalcException.Create('Cannot divide by zero')
                       else raise;
                    end;
                  end;
                 end;
                 btAdd: fResultValue := fResultValue + TempValue;
                 btSubtract: fResultValue := fResultValue - TempValue;
               end;
               FLastOperand := TempValue;
             end;

             if (FStatusLabel <> nil) then begin
                If (ButtonType=btPercent) then
                   FStatusLabel.Caption:='('+FStatusLabel.Caption+OpToChar(ButtonType)+')'
                else if (((ButtonType = btMultiply) or (ButtonType=btDivide))
                  and ((FLastOp = btAdd) or (FLastOp=btSubtract))) then
                      FStatusLabel.Caption:='('+FStatusLabel.Caption+')'
                else begin
                   FStatusLabel.Caption := FLastStatus+OpToChar(ButtonType)+FloatToStr(TempValue);
                end;

//!!!!! Try 3** to see problem.  Also test 3*5%% and 3*5%* to test functionality with percent.
                FLastStatus := FStatusLabel.Caption;

                if ButtonType <> btPercent then
                   FStatusLabel.Caption := FStatusLabel.Caption+OpToChar(ButtonType);

            {    if ButtonType = btPercent then begin  //!!!! Status not right
                   FLastStatus := FStatusLabel.Caption;
                   FStatusLabel.Caption := FlastStatus+OpToChar(btEquals) + FloatToStr(FResultValue);
                end;     }
             end;
             FCurrentValue := 0;
          end;

          FNextToLastOp := FLastOp;
          FLastOp:=ButtonType;
          if buttontype=btPercent then
             CalcEdit.Text := Getformattedtext(FLastOperand)
          else begin
             CalcEdit.Text := Getformattedtext(FResultValue);
          end;
          FCurrentValue := 0;
       end
       else if (Length(CalcEdit.Text) > 0)then begin
          if FLastOperatorEquals or (FLastOp = btPercent) then TempValue := FLastOperand
          else FLastOperand := TempValue;

          case FLastOp of
            btPercent:
                begin
                    case FNextToLastOp of
                       btMultiply: fResultValue := fResultValue * TempValue;
                       btDivide: fResultValue := fResultValue / TempValue;
                       btAdd: fResultValue := fResultValue + TempValue;
                       btSubtract: fResultValue := fResultValue - TempValue;
                    end;
                end;
            btMultiply: fResultValue := fResultValue * TempValue;
            btDivide: begin
                  try
//                     if TempValue <> 0.0 then
                    fResultValue := fResultValue / TempValue;
                  except
                    on E: Exception do begin   //**** Need Translation for Strings like below
                       if TempValue = 0.0 then
                          raise EfcCalcException.Create('Cannot divide by zero')
                       else raise;
                    end;
                  end;
               end;
            btAdd: fResultValue := fResultValue + TempValue;
            btSubtract: fResultValue := fResultValue - TempValue;
            btNone:fResultValue := TempValue;
          end;

//!!!!
          if (StatusLabel <> nil) then
             if (FLastOp<>btNone) then begin
                 if FLastOperatorEquals then begin
                    FStatusLabel.Caption := FLastStatus;
                    FStatusLabel.Caption := FStatusLabel.Caption + OpToChar(FLastOp) + FloatToStr(FLastOperand);
                    FStatusLabel.Caption:='('+FStatusLabel.Caption+')';
                 end
                 else if FLastOp = btPercent then begin
                    FStatusLabel.Caption := FLastStatus;
                 end
                 else begin
                    FStatusLabel.Caption := FLastStatus;
                    FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(FLastOp)+FloatToStr(TempValue);
                    FStatusLabel.Caption:='('+FStatusLabel.Caption+')';
                 end;
                 FLastStatus := FStatusLabel.Caption;
                 FStatusLabel.Caption := FStatusLabel.Caption+OpToChar(ButtonType);
                 FStatusLabel.Caption := FStatusLabel.Caption + FloatToStr(FResultValue);
             end
             else FLastStatus:=FStatusLabel.Caption;

          FLastOperatorEquals := True;

          CalcEdit.Text := Getformattedtext(FResultValue,True);
          if cboSelectOnEquals in Options then begin
             CalcEdit.SelectAll;
             selectallflag := True;
          end
          else CalcEdit.SelStart := Length(CalcEdit.Text);
          FClearOnNextKey := True;
          FBackSpaceValid := False;
          FCurrentValue := 0;
       end
       else Beep;
       FLastButtonType := ButtonType;
    end;

    btBackSpace:
     if FBackSpaceValid then begin
      Temp := CalcEdit.Text;
      if True or ((not (cboShowDecimal in Options)) and (Temp[Length(Temp)] in ['0'..'9'])) or
         ((cboShowDecimal in Options) and ((Length(Temp)-1) >= 0) and (Temp[Length(Temp)-1] in ['0'..'9'])) then
      begin
        if (Length(Temp)-1>=0) and (Temp[Length(Temp)]=DecimalSeparator) then
        begin
           Delete(Temp, Length(Temp)-1,2);
           if FStatusLabel <> nil then begin
              Temp := FStatusLabel.Caption;
              Delete(Temp, Length(Temp),1);
              FStatusLabel.Caption := Temp;
           end;
           FDecimalEntered := False;
        end
        else Delete(Temp, Length(Temp),1);
        {        if cboShowDecimal in Options then
          Delete(Temp, Length(Temp)-1,1)
        else
        Delete(Temp, Length(Temp),1);}

        if not DecimalInStr(Temp) then FDecimalEntered := False;

        if Temp = '' then begin
          FBackSpaceValid := False;
          Temp := '0';
        end;

        if (AnsiPos(DecimalSeparator,Temp)> 0) and
           (AnsiPos(DecimalSeparator,Temp)< CalcEdit.selstart) then CalcEdit.Text := Temp
        else begin
          FCurrentValue := fcStrToFloat(Temp);
          CalcEdit.Text := getFormattedText(FCurrentValue);//FloatToStr(FCurrentValue);
        end;

        if FStatusLabel <> nil then begin
           Temp := FStatusLabel.Caption;
           Delete(Temp, Length(Temp),1);
           FStatusLabel.Caption := Temp;
        end;
      end
      else begin
        if FStatusLabel <> nil then begin
           Temp := FStatusLabel.Caption;
           if Temp[Length(Temp)] = DecimalSeparator then begin
              FDecimalEntered := False;
              Delete(Temp, Length(Temp),1);
              FStatusLabel.Caption := Temp;
           end
           else FBackSpaceValid := False;
        end;
      end;
     end
     else Beep;

    btInverse:
     begin
      try
         if cboDigitGrouping in Options then FCurrentValue := 1 / fcStrToFloat(CalcEdit.Text)
         else FCurrentValue := 1 / fcStrToFloat(CalcEdit.Text);
      except
         on E: Exception do begin   //**** Need Translation for Strings like below
            if fcStrToFloat(CalcEdit.Text) = 0.0 then
               raise EfcCalcException.Create('Cannot divide by zero')
            else raise;
         end;
      end;

      CalcEdit.Text := getformattedText(FCurrentValue);//FloatToStr(FCurrentValue);
      CalcEdit.SelStart := Length(CalcEdit.Text);

      if FStatusLabel <> nil then begin
         if FClearOnNextKey then
           FStatusLabel.Caption := '0'
         else
           FStatusLabel.Caption := FLastStatus+CalcEdit.Text;
      end;
      FCurrentValue := 0;
      FClearOnNextKey := True;
      FBackSpaceValid := False;
      FClearStatus := True;
      FLastButtonType := ButtonType;
    end;

    btSqrt:
     begin
      FCurrentValue := Sqrt(fcStrToFloat(CalcEdit.Text));
      CalcEdit.Text := getformattedText(FCurrentValue);//FloatToStr(FCurrentValue);
      CalcEdit.SelStart := Length(CalcEdit.Text);

      if FStatusLabel <> nil then begin
         if FClearOnNextKey then
           FStatusLabel.Caption := '0'
         else
           FStatusLabel.Caption := FLastStatus+CalcEdit.Text;
      end;
      FC

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -