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

📄 mainmath.pas

📁 四则运算的代码,有兴趣的可以研究下下的呵呵.
💻 PAS
字号:
unit mainmath;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, math;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function   Calculate(Expression:   string):   string;
var   vText:   string;
  function   Reckon(mText:   string):   string;
  procedure   Bracket(mText:   string;   var   nLStr,   nCStr,   nRStr:   string);
  var  i,   CountLeft,   CountRight:   Integer;
       Bool:   Boolean;
          begin
              nLStr   :=   '';
              nCStr   :=   '';
              nRStr   :=   '';
              CountLeft   :=   0;
              CountRight   :=   0;
              Bool   :=   true;
              for   i   :=   1   to   Length(mText)   do
                  if   Bool   then
                  begin
                      if   mText[i]   =   '('   then   Inc(CountLeft)
                      else   if   mText[i]   =   ')'   then   Inc(CountRight);
                      if   CountLeft   =   0   then   nLStr   :=   nLStr   +   mText[i]
                      else   if   CountLeft   >   CountRight   then   nCStr   :=   nCStr   +   mText[i]
                      else   Bool   :=   false;
                  end   else   nRStr   :=   nRStr   +   mText[i];
              Delete(nCStr,   1,   1);
          end;
      var   vLStr,   vCStr,   vRStr:   string;
              i,   j,   k,   mTextLength:   Integer;
      begin
          mTextLength   :=   Length(mText);
          if   Pos('(',   mText)   >   0
          then   begin
                        Bracket(mText,   vLStr,   vCStr,   vRStr);
                        Result   :=   Reckon(vLStr   +   Reckon(vCStr)   +   vRStr);
                    end
          else   if   (Pos('+',   mText)   >   0)   or   (Pos('-',   mText)   >   0)
          then   begin
                        i   :=   Pos('+',   mText);
                        j   :=   Pos('-',   mText);
                        if   i   =   0   then   i   :=   mTextLength;
                        if   j   =   0   then   j   :=   mTextLength;
                        k   :=   Min(i,   j);
                        vLStr   :=   Copy(mText,   1,   Pred(k));
                        vRStr   :=   Copy(mText,   Succ(k),   mTextLength);
                        if   vLStr   =   ''   then   vLStr   :=   '0';
                        if   vRStr   =   ''   then   vRStr   :=   '0';
                        if   i   =   k
                        then   Result   :=   FloatToStr(StrToFloat(Reckon(vLStr))   +   StrToFloat(Reckon(vRStr)))
                        else   Result   :=   FloatToStr(StrToFloat(Reckon(vLStr))   -   StrToFloat(Reckon(vRStr)))
                    end
          else   if   (Pos('*',   mText)   >   0)   or   (Pos('/',   mText)   >   0)
          then   begin
                        i   :=   Pos('*',   mText);
                        j   :=   Pos('/',   mText);
                        if   i   =   0   then   i   :=   mTextLength;
                        if   j   =   0   then   j   :=   mTextLength;
                        k   :=   Min(i,   j);
                        vLStr   :=   Copy(mText,   1,   Pred(k));
                        vRStr   :=   Copy(mText,   Succ(k),   mTextLength);
                        if   vLStr   =   ''   then   vLStr   :=   '0';
                        if   vRStr   =   ''   then   vRStr   :=   '0';
                        if   i   =   k
                        then   Result   :=   FloatToStr(StrToFloat(Reckon(vLStr))   *   StrToFloat(Reckon(vRStr)))
                        else   Result   :=   FloatToStr(StrToFloat(Reckon(vLStr))   /   StrToFloat(Reckon(vRStr)))
                    end
          else   if   Pos('_',   mText)   =   1
          then   Result   :=   FloatToStr(-StrToFloat(Reckon(Copy(mText,   2,   mTextLength))))
          else   Result   :=   FloatToStr(StrToFloat(mText));
      end;

  var   i,   ExpLength:   integer;
  begin
      vText   :=   '';
      ExpLength   :=   Length(Expression);

      for   i   :=   1   to   ExpLength   do
          if   (Expression[i]   =   '-')   and   (i   <   ExpLength)   and   (not   (Expression[Succ(i)]   in   ['+',   '-',   '(',   ')']))
          then   if   (i   =   1)   or   ((i   >   1)   and   (Expression[Pred(i)]   in   ['*',   '/']))
                    then   vText   :=   vText   +   '_'
                    else   if   ((i   >   1)   and   (Expression[Pred(i)]   in   ['+',   '-']))   or   ((i   <   ExpLength)   and   (not   (Expression[Succ(i)]   in   ['+',   '-',   '(',   ')'])))
                    then   vText   :=   vText   +   '+_'
                    else   vText   :=   vText   +   Expression[i]
          else   vText   :=   vText   +   Expression[i];
      Result   :=   Reckon(vText);
  end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage(Format('%s=%s',[edit1.text,Calculate(Edit1.text)]));
end;

end.

⌨️ 快捷键说明

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