1、比如有变量i,j,一个edit,内容为'j*2+3',
如何实现i:=j*2+3?就是让i执行edit.text中的赋值?
2、比如有变量i,j,一个edit,内容为'i:=j*2+3',
如何实现i:=j*2+3?就是如何执行edit.text中的语句?

解决方案 »

  1.   

    1.首先检讨你的编程思路,除非是做界面,给用户操作,否则你的程序应该避免这类的用法。2.可以利用sql的存储过程来完成表达式的解析,或者利用window里面自己带的Javascript的ocx控件,只要是javascript的语法都能支持。
      

  2.   

    http://www.playicq.com/dispdoc.php?t=&id=1505
      

  3.   

    对,有人提过,
    好象是:
    FloatToStr等类型转换的应用吧。忘记了。
      

  4.   

    远不是FloatToStr这么简单,应该是做一个逆波兰分析,ehom朋友说得对
      

  5.   

    1.找一个控件
    2.可以利用sql的存储过程来完成表达式的解析
      

  6.   

    interface
    uses SysUtils;
    var
       Ca_err:boolean;Function Do_Calculate(s:string):Extended;implementation
    var
       Ca_token:char;
       Ca_exp:string;
       Ca_CurPos,Ca_Len:integer;function term:Extended;forward;
    function factor:Extended;forward;procedure error;
    begin
     Ca_err:=true;
     raise Exception.Create('错误的表达式!');
    end;function GetNextChar:char;
    begin
      if Ca_CurPos=Ca_Len then Result:=#0
      else
         begin
           inc(Ca_CurPos);
           Result:=Ca_exp[Ca_CurPos];
         end;
    end;procedure match(expectedToken:Char);
    begin
      if Ca_token=expectedToken then Ca_token := GetNextChar
      else Ca_err := true;
    end;function exp:Extended ;
    var temp:Extended;
    begin
    if not Ca_err then
     begin
      temp:=term;
      while (Ca_token='+') or (Ca_token='-') do
         case (Ca_token) of
            '+':begin
                match('+');
                temp:=temp+term;
                end;
            '-':begin
                 match('-');
                 temp:=temp-term;
                end;
         end; //case
      Result:=temp;
     end;
    end;function term:Extended;
    var temp:Extended;
    begin
     if not Ca_err then
      begin
      temp:=factor;
      while (Ca_token='*') or (Ca_token='/') do
         case (Ca_token) of
            '*':begin
                   match('*');
                   temp:=temp*factor;
                end;
            '/':begin
                 match('/');
                 temp:=temp/factor;
                end;
         end; //case
      result:=temp;
      end;
    end;function FindNum:Extended;
    var s:string;
    begin
    if not Ca_err then
     begin
      s:=Ca_token;
      Ca_token := GetNextChar;
      while Ca_token in ['0'..'9'] do
       begin
        s:=s+Ca_token;
        Ca_token := GetNextChar;
       end;
      if Ca_token='.' then
         begin
            s:=s+'.';
            Ca_token := GetNextChar;
            while Ca_token in ['0'..'9'] do
             begin
              s:=s+Ca_token;
              Ca_token := GetNextChar;
             end;
         end;
      Result:=StrToFloat(s);
      if Ca_token='%' then
         begin
           match('%');
           Result:=Result/100;
           Ca_token := GetNextChar;
         end;
     end;
    end;function factor:Extended ;
    var temp:Extended;
    begin
     if not Ca_err then
     if Ca_token='(' then
        begin
          match('(');
          temp := exp;
          match(')');
        end
     else if (Ca_token in ['0'..'9']) then
        begin
          temp:=FindNum;
        end
     else error;
     Result:=temp;
    end;Function Do_Calculate(s:string):Extended;
    begin
      Ca_CurPos:=0;
      Ca_err:=false;
      Ca_exp:=s;
      Ca_Len:=length(s);
      Ca_token:=GetNextChar;
      Result:=exp;
      if Ca_CurPos<>Ca_Len then error;
    end;
    end.用法:
    Edit2.Text:=FloatToStr(Do_Calculate(Edit1.Text)); 把这个函数加上就行了;
    快给分