字符串变量如何转换为计算表达式?
是否有人知道,麻烦一下:) 问题点数:50、回复次数:9Top
1 楼noflybird(东方不亮西方亮)回复于 2002-05-29 15:28:57 得分 0
计算表达式 是什么意思?Top
2 楼lanbada(lanbada)回复于 2002-05-29 15:31:00 得分 0
不行Top
3 楼lanbada(lanbada)回复于 2002-05-29 15:34:28 得分 0
不行Top
4 楼Lion_sj(一棵葱)回复于 2002-05-29 15:45:01 得分 0
比如3+4+5/2是一个字符串,现在要求把它转换成计算表达式
我也认为不可以,是别人问我的,我不会,只好来请高手了:)Top
5 楼tygh2000(峰)回复于 2002-05-29 15:53:53 得分 0
呵呵,这个还好了,如果有括号就麻烦了???Top
6 楼plainsong(短歌)()回复于 2002-05-29 15:53:55 得分 10
用两个栈,操作符入操作符栈,操作数入操作数栈.
遇到比当前操作符(栈顶)级别低的操作符时,先操作符出栈,两个操作数入栈,运算结果入操作数栈.
结束符是级别最低的操作符.
遇到结束符时一定会运算到操作符栈空.这时操作数栈顶就是运算结果.Top
7 楼happyboy(happyboy)回复于 2002-05-29 15:54:32 得分 40
什么是计算表达式呀?
你不在要四则运算的函数吧?
我有个函数自己写的,
给你吧。
function sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;
Top
8 楼noflybird(东方不亮西方亮)回复于 2002-05-29 15:55:52 得分 0
对字符串进行分析,类似于编译原理中的字符解析..Top
9 楼happyboy(happyboy)回复于 2002-05-29 15:57:20 得分 0
S为你要计算的字符串
用Edit1.Text:=sum(S);
Edit1.Text就是计算结果Top
10 楼windindance(风舞轻扬·白首为功名)回复于 2002-05-29 15:58:29 得分 0
提问前请先搜索。
回复人: li_zhifu(东北人) ( ) 信誉:100 2002-2-16 20:02:45 得分:0
唉,你们都是怎么了,这个问题M$已经有了一个解决方案了。在Win2K下在Delphi中Import ActiveX Control,选Microsoft Script Control 1.0,安装,在应用程序中
ScriptControl1.Language:='JavaScript';
ShowMessage(ScriptControl1.Eval('2*3+5'));
就可以了。
在Win98中可以把Win2K下的msscript.ocx拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。Top
11 楼li_zhifu(东北人)回复于 2002-05-29 16:24:51 得分 0
谢谢 windindance(风舞轻扬),
http://www.csdn.net/expert/topic/711/711837.xml?temp=.7109644Top




