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

📄 ucalculator.pas

📁 EXE+代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit uCalculator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ValEdit,
  StrUtils, ComCtrls, ExPars, ExFuncs, ExClass, Vars, parstype, Menus,
  Buttons, ExtCtrls,
  sndkey32,
  IniFiles;

type
  TForm1 = class(TForm)
    sg: TStringGrid;
    Button2: TButton;
    sb: TStatusBar;
    Button3: TButton;
    ExprParser1: TExParser;
    EPUserLib1: TEPUserLib;
    EPVars1: TEPVars;
    EPVars1VAR1: TVariable;
    EPRegFuncs1: TEPRegFuncs;
    EPRegFuncs1MYPROD1: TEPEventFunc;
    EPVCLClasses1: TEPVCLClasses;
    Panel1: TPanel;
    Button4: TButton;
    Button5: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    Button20: TButton;
    Button21: TButton;
    Button22: TButton;
    Button23: TButton;
    Button26: TButton;
    Button28: TButton;
    Button24: TButton;
    Timer1: TTimer;
    Button1: TButton;
    pm: TPopupMenu;
    N4: TMenuItem;
    N5: TMenuItem;
    N9: TMenuItem;
    Sin1: TMenuItem;
    Cos1: TMenuItem;
    an1: TMenuItem;
    N11: TMenuItem;
    Asin1: TMenuItem;
    Acos1: TMenuItem;
    Atan1: TMenuItem;
    Ln1: TMenuItem;
    Lg1: TMenuItem;
    Log1: TMenuItem;
    N12: TMenuItem;
    Exp1: TMenuItem;
    Power1: TMenuItem;
    N15: TMenuItem;
    SaveToFile1: TMenuItem;
    PI1: TMenuItem;
    Button6: TButton;
    Help1: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure Button2Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure sgKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Button3Click(Sender: TObject);
    function ExprParser1Function(Sender: TObject; const FuncName: String;
      const FuncParams: array of Variant): Variant;
    procedure Button22Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Sin1Click(Sender: TObject);
    procedure SaveToFile1Click(Sender: TObject);
    procedure Button28Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Help1Click(Sender: TObject);
  private
    Ipm:integer;
    iniF:TIniFile;
    procedure ufOnClick(sender:Tobject);
    { Private declarations }
    
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
        if win32platform = ver_platform_win32_nt then
        begin
                setprocessworkingsetsize(getcurrentprocess, $ffffffff, $ffffffff);
                application.processmessages;
        end;
end;

function formatStr(s:string):string;
begin
   if pos('(',s)<>0 then
      s:=leftStr(s,pos('(',s));
   s:=stringreplace(s,'+','+=',[rfReplaceAll]);
   s:=stringreplace(s,'(','+9',[rfReplaceAll]);
   s:=stringreplace(s,')','+0',[rfReplaceAll]);
   s:=stringreplace(s,'^','+6',[rfReplaceAll]);
   s:=stringreplace(s,'&','',[rfReplaceAll]);

   formatstr:=s;
end;


function isnum(str:string):boolean;
var
  i:integer;
begin
  for i:=1 to length(str) do
    if (not (str[i] in ['0'..'9'])) and
       (not (str[i] in ['%','+','-','*','/','(',')'])) then
    begin
      result:=false;
      exit;
    end;
  result:=true;
end;

function calculateexpress(strexpress:string):string;
   VAR
   strord:string;
   i:integer;
   c:char;
   ctemp:char;
   strnew:string;
   Operators:array [0..99] of char;
   Operands:array [0..99] of double;
   Operand:string;
   top:integer;
   x, y, v:double;
   begin
    strord:=trim(strexpress);
    //判断STRORD只有数字或运算符号
    if not isnum(strord) then
          begin
          calculateexpress:='Invalid Input!';
          exit;
          end;
    //----------------------------
    strnew:='';
    top:=-1;
    for i:=1 to length(strord) do
     begin
        c:=strord[i];
        case c of
          ' ': ;
          '+','-':begin
              while (top>=0) do
                 begin
                     ctemp:=Operators[top];top:=top-1;
                     if ctemp='(' then
                      begin top:=top+1; Operators[top]:=ctemp;
                       break;//跳出这个while循环
                      end else strnew:=strnew+ctemp;
                 end;
                  top:=top+1; Operators[top]:=c; strnew:=strnew+' ';
              end;
          '*','/': begin
          while (top>=0) do
          begin
          ctemp:=Operators[top];
          top:=top-1;
          if ctemp='(' then
          begin
          top:=top+1;
          Operators[top]:=ctemp;
          break;//跳出这个while循环
          end
          else
          begin
          if (ctemp='+') or (ctemp='-') then
          begin
          top:=top+1;
          Operators[top]:=ctemp;
          break;//跳出这个while循环
          end
          else
          strnew:=strnew+ctemp;
          end;
          end;
          top:=top+1;
          Operators[top]:=c;
          strnew:=strnew+' ';
          end;
          '(': begin
          top:=top+1;
          Operators[top]:=c;
          strnew:=strnew+' ';
          end;
          ')': begin
          while (top>=0) do
          begin
          ctemp:=Operators[top];
          top:=top-1;
          if ctemp='(' then
          begin
          break;//跳出这个while循环
          end
          else
          strnew:=strnew+ctemp;
          end;
          strnew:=strnew+' ';
          end;
        else  strnew:=strnew+c   ;
        end;
     end; //for end
         //////////操作符出栈
          while (top>=0) do
          begin
          strnew:=strnew+Operators[top];
          top:=top-1;
          end;
        //////////
/////后缀表达式计算完毕:其结果保存在 strnew 中,接下来,就到其求值
form1.sb.Panels[0].Text:='后缀表达式为:    '+strnew ;
//////////计算后缀表达式:开始
      top:=-1;
      Operand:='';
      for i:=1 to length(strnew) do
       begin
          c:=strnew[i];
          if ((c>='0') and (c<='9')) or (c='.') then
          begin
          Operand:=Operand+c;
          end;
          if  ((c=' ') or (i=length(strnew))) and (Operand<>'') then
          begin
          top:=top+1;
          Operands[Top]:=strtofloat(Operand);
          Operand:='';
          end;
          if (c='+') or (c='-') or (c='*' ) or (c='/') then
          begin
          if (Operand<>'') then
          begin
          top:=top+1;
          Operands[Top]:=strtofloat(Operand);
          Operand:='';
          end;
          y:=Operands[Top];//pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
          top:=top-1;
          x:=Operands[Top]; //pop 双目运算符的第一操作数

⌨️ 快捷键说明

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