[300]求任意个数的数字的四则运算的结果 ( 积分: 300 )

  • 主题发起人 liugaohui
  • 开始时间
Y

yuzk2005

Unregistered / Unconfirmed
GUEST, unregistred user!
任意个数, 即使只有加减, 这种组合恐怕也是天闻数字
 
L

liugaohui

Unregistered / Unconfirmed
GUEST, unregistred user!
to 勇者:因为我不懂,所以没有试;qq:82660360 msn:liugaohui@msn.com
to yuzk2005,10几个数应该没有问题的
 

传说中的虫

Unregistered / Unconfirmed
GUEST, unregistred user!
个人认为:
最终结果是 集合 A 与 集合 B 的四则运算。
那么 集合 A 与 集合 B 的结果是由更小的 集合 Ai 与 集合 Bi 组成,(不知道是不是使用递归),最后分结分最小的原子 An 和 Bn,那么就好了。
这样是排列 C1 的情况,再使用排列,那么排列完毕,就所有集合都有了。。
优化问题,还没有想。。
 
A

AI_Player

Unregistered / Unconfirmed
GUEST, unregistred user!
如果是运算结果都是整数,可以利用位来表示集合,例如一个4字节的32为整数,可以表示0-31这32的数是否是运算结果。
如果结果有浮点数,利用有序链表来表示集合。
先把第一个数放入集合,然后依次把接下来的每个数与集合里的每个数进行四则运算,结果并入集合。
对数字进行全排列,每个排列求出这样一个集合,并与以前的结果集合合并。最后的集合即为所求。
 
L

lovezyp

Unregistered / Unconfirmed
GUEST, unregistred user!
function calculateexpress(strexpress:string):string;
VAR
strord:string;
i:integer;
c:char;
ctemp:char;
strnew:string;
Operators:array [0..99] of char;
Operands:array [0..99] ofdo
uble;
Operand:string;
top:integer;
x, y, v:double;
begin
strord:=trim(strexpress);
//判断STRORD只有数字或运算符号
if not isnum(strord) then
begin
calculateexpress:='Invalid Input!';
exit;
end;
//----------------------------
strnew:='';
top:=-1;
for i:=1 to length(strord)do
begin
c:=strord;
case c of
' ': ;
'+','-':begin
while (top>=0)do
begin
ctemp:=Operators[top];top:=top-1;
if ctemp='(' then
begin
top:=top+1;
Operators[top]:=ctemp;
break;//跳出这个while循环
end else
strnew:=strnew+ctemp;
end;
top:=top+1;
Operators[top]:=c;
strnew:=strnew+' ';
end;
'*','/': begin
while (top>=0)do
begin
ctemp:=Operators[top];
top:=top-1;
if ctemp='(' then
begin
top:=top+1;
Operators[top]:=ctemp;
break;//跳出这个while循环
end
else
begin
if (ctemp='+') or (ctemp='-') then
begin
top:=top+1;
Operators[top]:=ctemp;
break;//跳出这个while循环
end
else
strnew:=strnew+ctemp;
end;
end;
top:=top+1;
Operators[top]:=c;
strnew:=strnew+' ';
end;
'(': begin
top:=top+1;
Operators[top]:=c;
strnew:=strnew+' ';
end;
')': begin
while (top>=0)do
begin
ctemp:=Operators[top];
top:=top-1;
if ctemp='(' then
begin
break;//跳出这个while循环
end
else
strnew:=strnew+ctemp;
end;
strnew:=strnew+' ';
end;
else
strnew:=strnew+c ;
end;
end;
//for end
//////////操作符出栈
while (top>=0)do
begin
strnew:=strnew+Operators[top];
top:=top-1;
end;
//////////
/////后缀表达式计算完毕:其结果保存在 strnew 中,接下来,就到其求值
form1.sb.Panels[0].Text:='后缀表达式为: '+strnew ;
//////////计算后缀表达式:开始
top:=-1;
Operand:='';
for i:=1 to length(strnew)do
begin
c:=strnew;
if ((c>='0') and (c<='9')) or (c='.') then
begin
Operand:=Operand+c;
end;
if ((c=' ') or (i=length(strnew))) and (Operand<>'') then
begin
top:=top+1;
Operands[Top]:=strtofloat(Operand);
Operand:='';
end;
if (c='+') or (c='-') or (c='*' ) or (c='/') then
begin
if (Operand<>'') then
begin
top:=top+1;
Operands[Top]:=strtofloat(Operand);
Operand:='';
end;
y:=Operands[Top];//pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
top:=top-1;
x:=Operands[Top];
//pop 双目运算符的第一操作数
top:=top-1;
case c of
'+': v := x + y;
'-': v := x - y;
'*': v := x * y;
'/': begin
if y<>0 then
begin
v := x / y;
// 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
end else
begin
calculateexpress:='Dividend By Zero' ;exit;
end;
end;
else
v := 0;
end;
//end case
top:=top+1;
Operands[Top] := v;//push 中间结果再次入栈
end;
end;
//end for
v := Operands[Top];
//pop 最终结果
top:=top-1;
// showmessage('计算结果为: '+floattostr(v));
//////////计算后缀表达式:结束
result:=floattostr(v);
end;

转一个别人的,传入string的四则表达式,直接出结果!!
 
顶部