如果单纯是解你这道题,那么 procedure TForm1.Button1Click(Sender: TObject); var v1,v2,v3:variant; _Pos:integer; begin v1:=4; v2:='4*5'; _Pos:=Pos('*',v2); v3:=strtoint(v1)+(strtoint(copy(v2,_Pos-1,1))*strtoint(copy(v2,_Pos+1,1))); showmessage(inttostr(v3)); end;
在VFP中用宏可以搞定,但在DELPHI中较困难。
凑了一上午,终于凑出一个差不多万能的表达式求值: 楼主一定要给满一百分哦. //说明一个:下面例子在窗体上加两个Edit,一个Button,Edit1中为输入的表达式,支持所有运算符,包括"()",Edit2为输出结果 unit compute;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Math;type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } s: string; function GetNumber(): real; function comp(a, b: char): char; function cal(a, b: real; p: char): real; function expr(): real; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var r: real; begin s := edit1.Text; //edit1中为要求值的表达式 r := expr(); // 调用求值函数 edit2.Text := floattostr(r); //将求值结果写到eidt2中 end;function TForm1.cal(a, b: real; p: char): real; begin case p of '+': result := a + b; '-': result := a - b; '*': result := a * b; '/': begin if b = 0 then begin showmessage('除数为0'); result := 0; end else result := a / b; end; end; end;function TForm1.comp(a, b: char): char; begin if not (a in ['+', '-', '*', '/', '(', ')', '#']) or not (b in ['+', '-', '*', '/', '(', ')', '#']) then result := 'e' else case a of '+', '-': case b of '*', '/', '(': result := '<'; else result := '>'; end; '*', '/': if b = '(' then result := '<' else result := '>'; '(': if b = ')' then result := '=' else if b = '#' then result := 'e' else result := '<'; ')': if b = '(' then result := 'e' else result := '>'; '#': if b = ')' then result := 'e' else if b = '#' then result := 'R' else result := '<'; end; end; function TForm1.expr(): real; var res, ch: char; oprTop, numTop: integer; opr: array[1..100] of char; num: array[1..100] of real;begin opr[1] := '#'; s := s + '#'; oprTop := 1; numTop := 0; res := ' '; repeat if s[1] in ['0'..'9'] then begin inc(numTop); num[numTop] := GetNumber(); end else begin ch := s[1]; delete(s, 1, 1); repeat res := comp(opr[oprTop], ch); case res of '>': begin num[numTop - 1] := cal(num[numTop - 1], num[numtop], opr[oprTop]); dec(numTop); dec(oprTop); end; '<': begin inc(oprTop); opr[oprTop] := ch; end; '=': begin dec(oprTop); end; 'e': begin showmessage('表达式出错!'); result := 0; exit; end; 'R': begin result := num[numTop]; end; end; until res <> '>'; end; until res = 'R'; end; function TForm1.GetNumber(): real; var i, code: integer; begin i := 1; while (i <= length(s)) and (s[i] in ['0'..'9', '.']) do inc(i); val(copy(s, 1, i - 1), result, code); delete(s, 1, i - 1); end; end.
procedure TForm1.Button1Click(Sender: TObject);
var
v1,v2,v3:variant;
_Pos:integer;
begin
v1:=4;
v2:='4*5';
_Pos:=Pos('*',v2);
v3:=strtoint(v1)+(strtoint(copy(v2,_Pos-1,1))*strtoint(copy(v2,_Pos+1,1)));
showmessage(inttostr(v3));
end;
楼主一定要给满一百分哦.
//说明一个:下面例子在窗体上加两个Edit,一个Button,Edit1中为输入的表达式,支持所有运算符,包括"()",Edit2为输出结果
unit compute;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject); private
{ Private declarations }
s: string;
function GetNumber(): real;
function comp(a, b: char): char;
function cal(a, b: real; p: char): real;
function expr(): real;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var r: real;
begin
s := edit1.Text; //edit1中为要求值的表达式
r := expr(); // 调用求值函数
edit2.Text := floattostr(r); //将求值结果写到eidt2中
end;function TForm1.cal(a, b: real; p: char): real;
begin
case p of
'+': result := a + b;
'-': result := a - b;
'*': result := a * b;
'/':
begin
if b = 0 then
begin
showmessage('除数为0');
result := 0;
end
else
result := a / b;
end;
end;
end;function TForm1.comp(a, b: char): char;
begin
if not (a in ['+', '-', '*', '/', '(', ')', '#']) or not (b in ['+', '-', '*', '/', '(', ')', '#']) then result := 'e'
else
case a of
'+', '-': case b of
'*', '/', '(': result := '<';
else result := '>';
end;
'*', '/': if b = '(' then result := '<' else result := '>';
'(': if b = ')' then result := '=' else if b = '#' then result := 'e' else result := '<';
')': if b = '(' then result := 'e' else result := '>';
'#': if b = ')' then result := 'e' else if b = '#' then result := 'R' else result := '<';
end;
end;
function TForm1.expr(): real;
var
res, ch: char;
oprTop, numTop: integer;
opr: array[1..100] of char;
num: array[1..100] of real;begin opr[1] := '#';
s := s + '#';
oprTop := 1;
numTop := 0;
res := ' ';
repeat
if s[1] in ['0'..'9'] then
begin
inc(numTop);
num[numTop] := GetNumber();
end
else
begin
ch := s[1];
delete(s, 1, 1);
repeat
res := comp(opr[oprTop], ch);
case res of
'>': begin
num[numTop - 1] := cal(num[numTop - 1], num[numtop], opr[oprTop]);
dec(numTop);
dec(oprTop);
end;
'<': begin
inc(oprTop);
opr[oprTop] := ch;
end;
'=': begin
dec(oprTop);
end;
'e': begin
showmessage('表达式出错!');
result := 0;
exit;
end;
'R': begin
result := num[numTop];
end;
end;
until res <> '>';
end;
until res = 'R';
end;
function TForm1.GetNumber(): real;
var
i, code: integer;
begin
i := 1;
while (i <= length(s)) and (s[i] in ['0'..'9', '.']) do inc(i);
val(copy(s, 1, i - 1), result, code);
delete(s, 1, i - 1);
end;
end.