转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
计算出用字符串表示的数学表达式的值         ★★★★

计算出用字符串表示的数学表达式的值

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1527 更新时间:2009/4/23 18:39:37

// built by Liu Yang 2002.1.8

library Expression;

uses Dialogs, Math, SysUtils;

Const
  Symbol_Mod=''''M'''';  Symbol_Div=''''D'''';
  Symbol_Shl=''''L'''';  Symbol_Shr=''''R'''';
  Symbol_Or=''''O'''';   Symbol_Xor=''''X'''';
  Symbol_And=''''A'''';

function ConvertExpression(ExpressionString:PChar):PChar; stdcall;
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; stdcall;
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(''''Unknown expression  : ''''+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
  if snv<>'''''''' then
     if snv[Length(snv)]=''''B''''
        then nv:=BinToInt(Copy(snv,1,Length(snv)-1))
        else if nextch=''''H'''' then begin nv:=StrToInt(''''$''''+snv); readnextch; end
                           else nv:=StrToInt(snv);
  if nextch=''''.'''' then
                     begin
                       radix:=10;
                       readnextch;
                       while nextch in [''''0''''..''''9''''] do
                         begin
                           nv:=nv+(ord(nextch)-ord(''''0''''))/radix;
                           radix:=radix*10;
                           readnextch;
                         end;
                      end;
end;
procedure factor(var fv:extended);
Var Symbol:string;
  function CalcN(Value:integer):extended;
  var i:integer;
  begin
    Result:=1;
    if Value=0 then Exit
       else for i:=1 to Value do
              Result:=Result*i;
  end;
  function ParseFunction(var FunctionSymbol:string):boolean;
  begin
    FunctionSymbol:='''''''';
    while not (nextch in [''''0''''..''''9'''',''''.'''',''''('''','''')'''',''''+'''',''''-'''',''''*'''',''''/'''',''''='''']) do
      begin
        FunctionSymbol:=FunctionSymbol+nextch;
        readnextch;
      end;
    if FunctionSymbol=''''ABS'''' then Result:=true else
    if FunctionSymbol=''''SIN'''' then Result:=true else
    if FunctionSymbol=''''COS'''' then Result:=true else
    if FunctionSymbol=''''TG'''' then Result:=true else
    if FunctionSymbol=''''TAN'''' then Result:=true else
    if FunctionSymbol=''''ARCSIN'''' then Result:=true else
    if FunctionSymbol=''''ARCCOS'''' then Result:=true else
    if FunctionSymbol=''''ARCTG'''' then Result:=true else
    if FunctionSymbol=''''ARCTAN'''' then Result:=true else
    if FunctionSymbol=''''LN'''' then Result:=true else
    if FunctionSymbol=''''LG'''' then Result:=true else
    if FunctionSymbol=''''EXP'''' then Result:=true else
    if FunctionSymbol=''''SQR'''' then Result:=true else
    if FunctionSymbol=''''SQRT'''' then Result:=true else
    if FunctionSymbol=''''PI'''' then Result:=true else
    if FunctionSymbol=''''NOT'''' then Result:=true else
    if FunctionSymbol=''''N!'''' then Result:=true else
    if FunctionSymbol=''''E'''' then Result:=true else
       Result:=false;
  end;
begin
  Case nextch of
    ''''0''''..''''9'''' : number(fv);
    ''''('''' : begin
            readnextch;
            expression(fv);
            if nextch='''')''''
               then readnextch else error(nextch);
          end
    else if ParseFunction(Symbol) then
            if nextch=''''('''' then
               begin
                 readnextch;
                 expression(fv);
                 if Symbol=''''ABS'''' then fv:=abs(fv) else
                 if Symbol=''''SIN'''' then fv:=sin(fv) else
                 if Symbol=''''COS'''' then fv:=cos(fv) else
                 if Symbol=''''TG'''' then fv:=tan(fv) else
                 if Symbol=''''TAN'''' then fv:=tan(fv) else
                 if Symbol=''''ARCSIN''

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台