delphi中memo的一个小问题(高分!)

在delphi中拖入两个组件,分别是memo1和memo2,我想实现这个功能,在memo1中输入下面引号中的内容(不包含引号和逗号)“A+B=”,然后memo2就会显示该... 在delphi中拖入两个组件,分别是memo1和memo2,我想实现这个功能,在memo1中输入下面引号中的内容(不包含引号和逗号)“A+B=”,然后memo2就会显示该内容数学算法的结果。上面的A和B可以换成任意数字。比如一个例子:在memo1中输入1+2=,在memo2中就会自动显示3。该功能的实现语句麻烦请写在一个按钮控件button1中。谢谢!好的会追加分数!在线等! 展开
 我来答
syht2000
高粉答主

2013-05-22 · 关注我不会让你失望
知道大有可为答主
回答量:3万
采纳率:79%
帮助的人:1.4亿
展开全部

这有个现成的函数,使用方法在button1中你一看就知道就不多说了,可以输入比如(1+2)*(4-6)这种公式或者更复杂的也行


procedure Eval(Formula: string; {   要计算的表达式   }
  var Value: Real; {   返回数值   }
  var ErrPos: Integer); {   错误信息   }
const
  Digit: set of Char = ['0'..'9'];
var
  Posn: Integer; {   算式当前位置   }
  CurrChar: Char; {   算式当前字符   }
  procedure ParseNext;
  begin
    repeat
      Posn := Posn + 1;
      if Posn <= Length(Formula) then
        CurrChar := Formula[Posn]
      else
        CurrChar := ^M;
    until CurrChar <> '   ';
  end {   ParseNext   };
  function add_subt: Real;
  var
    E: Real;
    Opr: Char;
    function mult_DIV: Real;
    var
      S: Real;
      Opr: Char;
      function Power: Real;
      var
        T: Real;
        function SignedOp: Real;
          function UnsignedOp: Real;
          type
            StdFunc = (fabs, fsqrt, fsqr, fsin, fcos,
              farctan, fln, flog, fexp, ffact);
            StdFuncList = array[StdFunc] of string[6];
          const
            StdFuncName: StdFuncList =
            ('ABS', 'SQRT', 'SQR', 'SIN', 'COS',
              'ARCTAN', 'LN', 'LOG', 'EXP', 'FACT');
          var
            E, L, Start: Integer;
            Funnet: Boolean;
            F: Real;
            Sf: StdFunc;
            function Fact(I: Integer): Real;
            begin
              if I > 0 then
              begin
                Fact := I * Fact(I - 1);
              end
              else
                Fact := 1;
            end {   Fact   };
          begin
            if CurrChar in Digit then
            begin
              Start := Posn;
              repeat ParseNext until not (CurrChar in Digit);
              if CurrChar = '.' then
                repeat ParseNext until not (CurrChar in Digit);
              if CurrChar = 'E' then
              begin
                ParseNext;
                repeat ParseNext until not (CurrChar in Digit);
              end;
              Val(Copy(Formula, Start, Posn - Start), F, ErrPos);
            end
            else if CurrChar = '(' then
            begin
              ParseNext;
              F := add_subt;
              if CurrChar = ')' then
                ParseNext
              else
                ErrPos := Posn;
            end
            else
            begin
              Funnet := False;
              for sf := fabs to ffact do
                if not Funnet then
                begin
                  l := Length(StdFuncName[sf]);
                  if Copy(Formula, Posn, l) = StdFuncName[sf] then
                  begin
                    Posn := Posn + l - 1;
                    ParseNext;
                    f := UnsignedOp;
                    case sf of
                      fabs: f := abs(f);
                      fsqrt: f := SqrT(f);
                      fsqr: f := Sqr(f);
                      fsin: f := Sin(f);
                      fcos: f := Cos(f);
                      farctan: f := ArcTan(f);
                      fln: f := LN(f);
                      flog: f := LN(f) / LN(10);
                      fexp: f := EXP(f);
                      ffact: f := fact(Trunc(f));
                    end;
                    Funnet := True;
                  end;
                end;
              if not Funnet then
              begin
                ErrPos := Posn;
                f := 0;
              end;
            end;
            UnsignedOp := F;
          end {   UnsignedOp};
        begin {   SignedOp   }
          if CurrChar = '-' then
          begin
            ParseNext;
            SignedOp := -UnsignedOp;
          end
          else
            SignedOp := UnsignedOp;
        end {   SignedOp   };
      begin {   Power   }
        T := SignedOp;
        while CurrChar = '^' do
        begin
          ParseNext;
          if t <> 0 then
            t := EXP(LN(abs(t)) * SignedOp)
          else
            t := 0;
        end;
        Power := t;
      end {   Power   };
    begin
      s := Power;
      while CurrChar in ['*', '/'] do
      begin
        Opr := CurrChar;
        ParseNext;
        case Opr of
          '*': s := s * Power;
          '/': s := s / Power;
        end;
      end;
      mult_DIV := s;
    end;
  begin
    E := mult_DIV;
    while CurrChar in ['+', '-'] do
    begin
      Opr := CurrChar;
      ParseNext;
      case Opr of
        '+': e := e + mult_DIV;
        '-': e := e - mult_DIV;
      end;
    end;
    add_subt := E;
  end;
begin
  if Formula[1] = '.' then
    Formula := '0' + Formula;
  if Formula[1] = '+' then
    Delete(Formula, 1, 1);
  for Posn := 1 to Length(Formula) do
    Formula[Posn] := Upcase(Formula[Posn]);
  Posn := 0;
  ParseNext;
  Value := add_subt;
  if CurrChar = ^M then
    ErrPos := 0
  else
    ErrPos := Posn;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
value:real;
errno:integer;
begin
str:=memo1.Text;
eval(str,value,errno);
//showmessage(floattostr(value));
memo2.Text:=floattostr(value);
end;
追问
1、这个第一行是不是少了什么?比如括号之类?
2、然后,我具体应该怎么调用你写的这个呢?
如果解释清楚我会采纳你,谢谢!
追答
1、这就是完整的一个过程,没少东西。
2、调用方法其实已经已经写的很清楚了,我假定界面上就两个memo,一个button,双击button的时候事件就是
procedureTForm1.Button1Click(Sender: TObject);
var
str:string;
value:real;
errno:integer;
begin
str:=memo1.Text;
eval(str,value,errno);//这里就是调用上面那个过程
//showmessage(floattostr(value));
memo2.Text:=floattostr(value);
end;
匿名用户
2013-05-22
展开全部
procedure TForm1.btn1Click(Sender: TObject);
var
strtemp,strA,strB,strresult : string;
itemp,tempresult : integer;
begin
for itemp :=0 to mmo1.Lines.Count-1 do
begin
strtemp :=StringReplace(mmo1.Lines.Strings[itemp],' ','',[rfReplaceAll]);
strA := Copy(strtemp,1,Pos('+',strtemp)-1);
strB := Copy(strtemp,Pos('+',strtemp)+1,Pos('=',strtemp)-Pos('+',strtemp)-1);
tempresult := StrToInt(strA)+ StrToInt(strB);
strresult := IntToStr(tempresult);
mmo2.Lines.Add(strresult);
end;
end;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
阿布home
2013-05-22 · 超过10用户采纳过TA的回答
知道答主
回答量:67
采纳率:0%
帮助的人:40.5万
展开全部
用两个TEDIT不成吗,用memo1还得把符号抠出来在计算,不过也简单
追问
谢谢,不过edit只要是个初学者就会的好吧。。。
我之所以用memo,因为我要写好多行类似A+B=的格式,然后在memo2里面显示结果,这个edit就办不到了
追答
我想的简单了
哇噢,楼上syht2000曾经也为这问题烦恼过吧,竟然整理出这么完善的一段代码
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式