网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Delphi
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.怎样修改windows里的开始按钮的位.
.区分保留字跟关键字.
.使你的窗体保留在桌面的最上面.
.用Delphi实现BP机、手机短讯.
.SQLServer和Oracle的常用函数对比.
.建立、读取、存贮INI文件的方法《.
.tlistview使用--拖放操作.
.Delphi语言优化.
.自动化每日构建(二)用Ant来完成.
.项目迭代开发手记--文件分割存储.
.在delhpi程序中获取网络资源信息.
.让Fastreport3.x支持中文PDF的输.
.可以计算到<<毫秒级>>的控件.
.如何在托盘图标实现漂亮的菜单.
.序列化FastReport.
.Delphi制作带图标的弹出式选单.
.用DELPHI进行NT、2000、2003服务.
.先人的DELPHI基础开发技巧.
.Delphi中关于TApplication类的详.
.怎样通过编程方式管理nt的用户及.

一个计算器的代码,欢迎大家点评

发表日期:2006-2-4


 

例如:
   1. CalcExpr('2*5+1')='11'
   2. 带条件
       CalcExpr('2>1&4<=5 : 2*5')='10'
       CalcExpr('6<2 : 3')='0'
   3. 带函数
       CalcExpr('max(1,2,3,6,4+7,7)')='11'

用法:将untCalc.pas 加入到你的工程里面,然后调用CalcExpr即可。

这里是源代码:

unit untJCalc;

interface

uses
   classes,sysutils;

type
   TJStack=class
      private
         Lines:TStrings;
      public
         constructor Create;
         destructor Destroy;
         procedure init;
         procedure push(s:string);
         function GetTop:String;
         function Pop:String;
      end;
   TJExpr=class
      private
         Expr:String;
         Position:Integer;
         Min,max:Integer;
         Eof:Boolean;
      public
         constructor Create(pExpr:String);
         function read:String;
         procedure GoFirst;
      end;

function CalcExpr(sExpr:String):String;
function CalcExprItem(sOptr,sA,sB:String):String;
function OptrIndex(w:string):Integer;
function GetParamCount(pFunc:String):Integer;
function ExecFunc(pFunc:String;pParam:Array  of string;pParamCount:Integer):string;

implementation

constructor TJStack.Create;
begin
   inherited Create;
   lines:=TStringList.create;
end;

procedure TJStack.init;
begin
   lines.free;
end;

destructor TJStack.Destroy;
begin
   lines.free;
   inherited Destroy;
end;

procedure TJStack.push(s:string);
begin
   lines.add(s);
end;

function TJStack.GetTop:String;
begin
   if Lines.count>0 then
      Result:=lines[lines.count-1]
      else
      Result:='';
end;

function TJStack.Pop:String;
begin
   if Lines.Count>0 then
   begin
      Result:=GetTop;
      lines.delete(lines.count-1);
   end
   else
      Result:='';
end;

//////////////////////TJExpr////////////////

constructor TJExpr.Create(pExpr:String);
begin
   Expr:=lowercase(pExpr)+'#';
   Min:=1;
   Max:=length(Expr);
   Position:=1;
   Eof:=false;
end;

function TJExpr.read:String;
   function SameType(s1,s2:string):boolean;
   var
      c1,c2:string;
   begin
      c1:='';c2:='';
      if length(s1)>0 then c1:=s1[length(s1)];
      if length(s2)>0 then c2:=s2[Length(s2)];
      if ((pos(c1,'0123456789.')>0) and (pos(c2,'0123456789.')>0))
         then
         begin
            result:=true;
         end
         else
         begin
            Result:=false;
         end;
      if (c1='-')and(c2='-') then Result:=false;
      if s1+s2='>=' then Result:=true;
      if s1+s2='<=' then Result:=true;
      if s1+s2='<>' then Result:=true;
      if pos(s1+s2,'max(')>0 then Result:=true;
      if pos('-',s1+s2)>1 then Result:=false;
      if (s1='')or(s2='') then result:=true;
   end;
begin
   if Position<=Max then
   begin
      Result:=trim(Expr[Position]);
      Inc(Position);
      while Position<=Max do
      begin
         if SameType(Result,Expr[Position]) then
         begin
            Result:=Result+trim(Expr[Position]);
            Inc(Position);
         end
         else
         begin
            exit;
         end;
      end;
   end
   else
   begin
      Result:='';
      Eof:=true;
   end;
end;

procedure  TJExpr.GoFirst;
begin
   Position:=1;
   Eof:=false;
end;

/////////////////////////////////////////

function DiffOptr(a,b:string):Integer;
const
   sa:array [1..17,1..17] of
      integer=(
      //  +  -  *  /  (  )  #  >  < >= <=  = <> &  :  ,   max(
      {+}(2 ,2 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {-}(2 ,2 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {*}(2 ,2 ,2 ,2 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {/}(2 ,2 ,2 ,2 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {(}(0 ,0 ,0 ,0 ,0 ,1 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0),
      {)}(2 ,2 ,2 ,2 ,1 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,1),
      {#}(0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0),
      {>}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {<}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
     {>=}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
     {<=}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {=}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
     {<>}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0),
      {&}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,0),
      {:}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,2 ,2 ,0),
      {,}(0 ,0 ,0 ,0 ,0 ,1 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0),
   {max(}(0 ,0 ,0 ,0 ,0 ,1 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0)
      );
var
   aIndex,bIndex:integer;
begin
   aIndex:=OptrIndex(a);
   bIndex:=OptrIndex(b);
   if (aIndex>0)and(bIndex>0) then
      Result:=sa[aIndex,bIndex]-1
      else
      Result:=1;
end;

function CalcExpr(sExpr:String):String;
var
   optr,opnd:TJStack;
   w,theta,a,b:string;
   position:integer;
   jexpr:TJExpr;
   sParam:array[1..20] of string;
   sFunc:String;
   i,nParamCount:integer;
begin
   jexpr:=TjExpr.Create(sExpr);
   optr:=TJStack.create;
   opnd:=TJStack.create;
   optr.push('#');
   w:=jexpr.read;
   while (not ((w='#')and(optr.GetTop='#'))) and (jexpr.Eof =false) do
   begin
      if OptrIndex(w)<0 then
      begin
         opnd.push(w);
         w:=jexpr.read;
      end
      else
      begin
         Case DiffOptr(optr.GetTop,w) of
            -1://<
              begin
                 optr.push(w);
                 w:=jexpr.read;
              end;
            0://=
              begin
                 sFunc:=optr.pop;
                 if sFunc<>'(' then
                 begin
                    nParamCount:=1;
                    while sFunc=',' do
                    begin
                       Inc(nParamCount);
                       sFunc:=optr.pop;
                    end;
                    if GetParamCount(sFunc)=0 then nParamCount:=0;
                    for i:=1 to nParamCount do sParam[i]:=opnd.Pop;
                    opnd.push(ExecFunc(sFunc,sParam,nParamCount));
                 end;
                 w:=jexpr.read;
              end;
            1://>
              begin
                 theta:=optr.pop;
                 b:=opnd.pop;
                 a:=opnd.pop;
                 opnd.push(CalcExprItem(theta,a,b));
              end;
         end;
      end;
   end;
   Result:=opnd.GetTop;
   opnd.free;
   optr.free;
end;

function CalcExprItem(sOptr,sA,sB:String):String;
begin
   if sOptr='+' then
   begin
      if (sA<>'')and(sB<>'') then
      begin
         Result:=floattostr(strtofloat(sA)+strtofloat(sB));
      end
      else
      begin
         Result:=sA+sB;
         if Result='' then Result:='0';
      end;
      exit;
   end;
   if sOptr='-' then
   begin
      if sA='' then
         Result:=floattostr(-strtofloat(sB))
         else
         Result:=floattostr(strtofloat(sA)-strtofloat(sB));
      exit;
   end;
   if sOptr='*' then
   begin
      Result:=floattostr(strtofloat(sA)*strtofloat(sB));
      exit;
   end;
   if sOptr='/' then
   begin
      Result:=floattostr(strtofloat(sA)/strtofloat(sB));
      exit;
   end;
   if sOptr='>' then
   begin
      if strtofloat(sA)>strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='<' then
   begin
      if strtofloat(sA)<strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='>=' then
   begin
      if strtofloat(sA)>=strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='<=' then
   begin
      if strtofloat(sA)<=strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='=' then
   begin
      if strtofloat(sA)=strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='<>' then
   begin
      if strtofloat(sA)<>strtofloat(sB) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr='&' then
   begin
      if (strtofloat(sA)<>0)and(strtofloat(sB)<>0) then
         Result:='1'
         else
         Result:='0';
      exit;
   end;
   if sOptr=':' then
   begin
      if strtofloat(sA)=0 then
         Result:='0'
         else
         Result:=sB;
      exit;
   end;
end;

function GetParamCount(pFunc:String):Integer;
begin
   if pFunc='max(' then result:=2;
end;

function OptrIndex(w:string):Integer;
begin
   if w='+' then begin result:=1; exit; end;
   if w='-' then begin result:=2; exit; end;
   if w='*' then begin result:=3; exit; end;
   if w='/' then begin result:=4; exit; end;
   if w='(' then begin result:=5; exit; end;
   if w=')' then begin result:=6; exit; end;
   if w='#' then begin result:=7; exit; end;
   if w='>' then begin result:=8; exit; end;
   if w='<' then begin result:=9; exit; end;
   if w='>=' then begin result:=10; exit; end;
   if w='<=' then begin result:=11; exit; end;
   if w='=' then begin result:=12; exit; end;
   if w='<>' then begin result:=13; exit; end;
   if w='&' then begin result:=14; exit; end;
   if w=':' then begin result:=15; exit; end;
   if w=',' then begin result:=16; exit; end;
   if w='max(' then begin Result:=17; exit; end;
   result:=-1;
end;

function ExecFunc(pFunc:String;pParam:Array of string;pParamCount:Integer):string;
var
   tmpFloat:real;
   i:integer;
begin
   //
   if pFunc='max(' then
   begin
      tmpFloat:=strtofloat(pParam[0]);
      for i:=1 to pParamCount-1 do
      begin
         if tmpFloat<strtofloat(pParam[i]) then
            tmpFloat:=strtofloat(pParam[i]);
      end;
      Result:=floattostr(tmpFloat);
   end;
end;

end.

上一篇:计算出用字符串表示的数学表达式的值 人气:4831
下一篇:鼓励,很多的,Delphi高手突破,外加冷水一瓢 人气:3678
浏览全部Delphi的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐