delphi计算表达式的值

时间:2010-01-29 08:55:55  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:sWindows, Messages, Math, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;constSymbol_Mod = 'M';Symbol_Div = 'D';Symbol_Shl = 'L';Symbol_Shr = 'R';Symbol_Or = 'O';Symbol_Xor = 'X';Symbol_And = 'A';typeTForm1 = class(TForm)Button1: TButton;Edit1: TEdit;Edi...
  正文:支持常用函数的表达式计算函数:


unit m;

interface

uses
Windows, Messages, Math, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
Symbol_Mod = 'M';
Symbol_Div = 'D';
Symbol_Shl = 'L';
Symbol_Shr = 'R';
Symbol_Or = 'O';
Symbol_Xor = 'X';
Symbol_And = 'A';
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function ConvertExpression(ExpressionString: PChar): PChar;
var
inputexp: string;
begin
  inputexp := ExpressionString;
  //convert input expression to recognize expression
   if pos('=', inputexp) = 0 then
  inputexp := inputexp + '='
  else
  inputexp := Copy(inputexp, 1, Pos('=', inputexp));
  inputexp := UpperCase(inputexp);
  inputexp := StringReplace(inputexp, ' ', '', [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'MOD', Symbol_Mod, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'DIV', Symbol_Div, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'AND', Symbol_And, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'XOR', Symbol_Xor, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'OR', Symbol_Or, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'SHL', Symbol_Shl, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, 'SHR', Symbol_Shr, [rfReplaceAll]);
  inputexp := StringReplace(inputexp, '(-', '(0-', [rfReplaceAll]);
  if pos('-', inputexp) = 1 then inputexp := '0' + inputexp;
  Result := PChar(inputexp);
end;

function ParseExpression(ExpressionString: PChar): extended;
var
nextch: char;
nextchpos, position: word;
inputexp: string;
procedure expression(var ev: extended); forward;
procedure readnextch;
begin
  repeat
  if inputexp[position] = '=' then
  nextch := '='
  else
  begin
    inc(nextchpos);
    inc(position);
    nextch := inputexp[position];
  end;
  until (nextch <> ' ') or eoln;
end;
procedure error(ErrorString: string);
begin
  MessageDlg('无法识别的语法  : ' + ErrorString, mterror, [mbok], 0);
  exit;
end;
procedure number(var nv: extended);
var
radix: longint;
snv: string;
function BinToInt(value: string): integer;
var
i, size: integer;
begin // convert binary number to integer
         result := 0;
  size := length(value);
  for i := size downto 1 do
  if copy(value, i, 1) = '1'
  then result := result + (1 shl (size - i));
end;
begin
  nv := 0;
  snv := '';
  while nextch in ['0'..'9', 'A'..'F'] do
  begin
    //      nv:=10*nv+ord(nextch)-ord('0');
         snv := snv + nextch;
    readnextch;
  end;
  // parse Hex, Bin

"delphi计算表达式的值"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12568.html

 1/4    1 2 3 4 下一页 尾页


关键字:

关于《delphi计算表达式的值》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td