编写pascal(delphi程序)计算数学算式s是一个数学算式,里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}要求按照正常的数学计算顺序

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 15:07:30
编写pascal(delphi程序)计算数学算式s是一个数学算式,里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}要求按照正常的数学计算顺序

编写pascal(delphi程序)计算数学算式s是一个数学算式,里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}要求按照正常的数学计算顺序
编写pascal(delphi程序)计算数学算式
s是一个数学算式,
里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号
也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}
要求按照正常的数学计算顺序进行计算
比如s:='53+6*[31-(5^3*6)/40]'
求程序.

编写pascal(delphi程序)计算数学算式s是一个数学算式,里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}要求按照正常的数学计算顺序

procedure TForm1.btn1Click(Sender: TObject);

begin

  edt2.Text := untJCalc.CalcExpr(edt1.Text);

end;

确定合你的意喽.

unit untJCalc;

interface

uses

   classes,sysutils, Math;

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..18,1..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 ,2 ,2 ,0 ,0),

      {&}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,2 ,2 ,2 ,0 ,0),

      {:}(0 ,0 ,0 ,0 ,0 ,2 ,2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,2 ,2 ,0 ,0),

      {,}(0 ,0 ,0 ,0 ,0 ,1 ,2 ,0 ,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 ,0),

      {^}(2 ,2 ,2 ,2 ,0 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,0 ,2)     

      );

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, stExpr:String;

   i,nParamCount:integer;

begin

   stExpr := sExpr;

   stExpr := StringReplace(stExpr, '[', '(', [rfReplaceAll]);

   stExpr := StringReplace(stExpr, ']', ')', [rfReplaceAll]);  

   stExpr := StringReplace(stExpr, '{', '(', [rfReplaceAll]);

   stExpr := StringReplace(stExpr, '}', ')', [rfReplaceAll]);

   jexpr:=TjExpr.Create(stExpr);

   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:=&

编写pascal(delphi程序)计算数学算式s是一个数学算式,里面包括数字,加号,减号,乘号,除号,乘方号,小括号,中括号和大括号也就是包括0-9 ,+ ,- ,* ,/ ,^ ,( ,) ,[ ,] ,{ ,}要求按照正常的数学计算顺序 编写程序,计算S 用Delphi设计一个程序,从键盘输入a,b,c3个整数,将它们按照从大到小的次序输出请用Delphi编写,运行成功后请截下图. 用 free pascal 编写程序,输出一列图形(字母金字塔) a a b a b c …… 用 Pascal 编写一个程序将十进制整数n转换为二进制数.用 Pascal 编写加说明 软件是指用程序设计语言(如PASCAL ,C,VISUAL BASIC 等)编写的程序,软件开发实际上就是编写程序代码这句话对吗? 汇编语言程序设计编写计算n!的程序(利用循环程序结构编写),谢谢! 用pascal编写程序 输入十个整数,统计大于0的数的个数.用pascal编写程序输入十个整数,统计大于0的数的个数. PASCAL程序 全1数字全1 数字请用PASCAL编写,输入一个奇数P(P C 语言 编写程序,计算分段函数: 用pascal输出语句编写下列图形程序 1 121 12321 12343211121123211234321 pascal程序怎样随机产生一个三位数?并解释为什么要这样编写? 用PASCAL语言编写一个求1+2+3+...+N的程序 pascal金字塔编写一个程序,输入两个整数N和K(N,K pascal编写一个程序均分计算计算比赛的得分,求去掉最高分和最低分后的平均分.输出格式 分两行,第一行,一个正整数n(n 写 Delphi 程序 输入一个整数n,计算1~n之间的平方和,立方和,偶数和 用pascal编写程序 求1000内的素数(素数就是只能被1和它自己本身整除,素数比如:2、3、5、7……)用pascal编写程序 求1000内的素数(素数就是只能被1和它自己本身整除,素数比如:2、3、5、7… 编写程序,输入正整数n,计算它的阶乘n!(n!=n×(n-1)×…×3×2×1).请大家用PASCAL语言,不要用c语言啊!