转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
一个计算器的代码,欢迎大家点评         ★★★★

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

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

例如:
   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 ,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Index(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);
           &nb

[1] [2] [3]  下一页


没有相关教程
教程录入: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……
    咸宁网络警察报警平台