传入一个字符串如(10+(3*4+8)*6)/(7+3),求其运算结果,没思路!(再现等待)

iceriver521 2002-09-18 03:19:19
传入一个字符串如(10+(3*4+8)*6)/(7+3),求其运算结果,如=13
...全文
22 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cowboy1999 2002-09-19
  • 打赏
  • 举报
回复
看看数据结构,逆波兰表达试。
匿名用户12345 2002-09-19
  • 打赏
  • 举报
回复
琢木鸟说的是个好方法啊,不过不知道在机器中是怎样实现的。

是delphi自动调用它自带的一个函数还是调用别的文件处理的。希望高手指教
goease 2002-09-19
  • 打赏
  • 举报
回复
看看编译原理就知道了,呵呵,
windindance 2002-09-19
  • 打赏
  • 举报
回复
回复人: 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拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。
noall 2002-09-19
  • 打赏
  • 举报
回复
有个控件

parse.pas吧

查这个安装为一个控件。


rlongriver 2002-09-19
  • 打赏
  • 举报
回复
我前几天刚问了这样的问题,看这个贴子:
http://www.csdn.net/expert/topic/1019/1019667.xml?temp=.646435

我的一个同学要的,我没细研究里面贴子的内容,不过据他说,那个贴子的算法取的结果不是很正确,他又改了不少,不过能给你个思路。能解决你的问题
prosectinfo 2002-09-19
  • 打赏
  • 举报
回复
下面是一个单元文件
使用时调用Do_Calculate

unit calc;
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('工资项目的计算公式不正确,请检查!');
Abort;
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.
iceriver521 2002-09-19
  • 打赏
  • 举报
回复
floattostr((10+(3*4+8)*6)/(7+3))

var
s:string;
begin
s:=(10+(3*4+8)*6)/(7+3);
edit1.text:=floattostr(s);
end;
是两回事!!
doni 2002-09-18
  • 打赏
  • 举报
回复
真奇怪,为什么floattostr((10+(3*4+8)*6)/(7+3));
这样也可以
bamboo_flute 2002-09-18
  • 打赏
  • 举报
回复
找清华大学出的一本数据结构书(pascal)上面专有一个输入字符串算结果的,大学时编过,早就不见了!
王集鹄 2002-09-18
  • 打赏
  • 举报
回复
http://zswang.51.net/function/zsfunc0k.htm
allkeys 2002-09-18
  • 打赏
  • 举报
回复
使用栈
iceriver521 2002-09-18
  • 打赏
  • 举报
回复
(10+(3*4+8)*6)/(7+3)是字符串的,不行啊
neilwq 2002-09-18
  • 打赏
  • 举报
回复
最简单的办法
try
floattostr((10+(3*4+8)*6)/(7+3));
except
//errormessage
end
drizzt123 2002-09-18
  • 打赏
  • 举报
回复
看数据结构书关于栈,后缀表达式那几节

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧