function Cal(Str:string):single;
var
i,k,lenghtOfString:integer;
C_num:integer; //乘法和除法的总个数
C_position:integer; //乘法和除法的位置
J_num:integer; //加法和减法的总个数
J_position:integer; //加法和减法的位置
befor_C_position,after_C_position:integer; //乘法和除法前面、后面的数字的位置
befor_J_position,after_J_position:integer; //加法和减法前面、后面的数字的位置
tem1,tem2:string;
str1,str2,str3:string;
begin
lenghtOfString:=length(str);
C_num:=0;
J_num:=0;
for i:=1 to lenghtOfString do //求乘法和除法的总个数
begin
if str[i] in ['*','/'] then
C_num:=C_num+1;
end;
if C_num<>0 then //如果有乘法和除法
begin
for k:=1 to C_num do
begin
for i:=1 to lenghtOfString do //找到乘法和除法的位置
begin
if Str[i] in ['*','/'] then
begin
C_position:=i;
break;
end;
end;
for i:=(C_position-1) downto 1 do //找到*/号前面的数字的首的位置
begin
befor_C_position:=1;
if str[i] in ['+','-','*','/'] then
begin
befor_C_position:=i+1;
break;
end;
end;
for i:=(C_position+1) to lenghtOfString do //找到*/号后面的数字的尾位置
begin
after_C_position:=lenghtOfString;
if str[i] in ['+','-','*','/'] then
begin
after_C_position:=i-1;
break;
end;
end;
tem1:='';
for i:=befor_C_position to C_position-1 do
tem1:=tem1+str[i];
tem2:='';
for i:=C_position+1 to after_c_position do
tem2:=tem2+str[i];
case str[C_position] of
'*': str2:=floattostr(strtofloat(tem1)*strtofloat(tem2));
'/': str2:=floattostr(strtofloat(tem1)/strtofloat(tem2));
end;
str1:='';
if befor_C_position<>1 then
begin
for i:=1 to befor_C_position-1 do
str1:=str1+str[i];
end;
if befor_C_position=1 then
begin
str1:='';
end;
str3:='';
if after_C_position<>lenghtOfString then
begin
for i:=C_position+1 to after_C_position+1 do
str1:=str1+str[i];
end;
if after_C_position=lenghtOfString then
begin
str3:='';
end;
for i:=1 to lenghtOfString do //加法和减法的总个数
begin
if str[i] in ['+','-'] then
J_num:=J_num+1;
end;
if J_num<>0 then //如果有加法和减法
begin
for k:=1 to J_num do
begin
for i:=1 to lenghtOfString do //找到加法和减法的位置
begin
if str[i] in ['+','-'] then
begin
J_position:=i;
break;
end;
end;
for i:=(J_position-1) downto 1 do //找到+-号前面的数字的首的位置
begin
befor_J_position:=1;
if str[i] in ['+','-','*','/'] then
begin
befor_J_position:=i+1;
break;
end;
end;
for i:=(J_position+1) to lenghtOfString do //找到+-号后面的数字的尾的位置
begin
after_J_position:=lenghtOfString;
if str[i] in ['+','-','*','/'] then
begin
after_J_position:=i-1;
break;
end;
end;
tem1:='';
for i:=befor_J_position to J_position-1 do
tem1:=tem1+str[i];
tem2:='';
for i:=J_position+1 to after_J_position do
tem2:=tem2+str[i];
case str[J_position] of
'+': str2:=floattostr(strtofloat(tem1)+strtofloat(tem2));
'-': str2:=floattostr(strtofloat(tem1)-strtofloat(tem2));
end;
str1:='';
if befor_j_position<>1 then
begin
for i:=1 to befor_J_position-1 do
str1:=str1+str[i];
end;
if befor_j_position=1 then
str1:='';
str3:='';
if after_j_position<>lenghtOfString then
begin
for i:=after_J_position+1 to lenghtOfString do
str3:=str3+str[i];
end;
if after_j_position=lenghtOfString then
str3:='';
function MainCal(Str:string):single;
var
i,k,lengthOfString:integer;
end_position,begin_position:integer;
str1,str2,str3:string;
check_bracket:integer;
left_bracket,right_bracket:integer;
begin
lengthOfString:=length(str);
str1:='';
str2:='';
str3:='';
left_bracket:=0;
right_bracket:=0;
for i:=1 to lengthOfString do
begin
if str[i]='(' then left_bracket:=left_bracket+1;
if str[i]=')' then right_bracket:=right_bracket+1;
end;
if left_bracket<>right_bracket then
begin
result:=0; //出错
exit;
end;
if left_bracket<>0 then
begin
for k:=1 to left_bracket do
begin
for i:=1 to lengthOfString do //找到)
begin
if str[i] in [')'] then
begin
end_position:=i;
break;
end;
end;
for i:=end_position downto 1 do //找到对应的(
begin
if str[i] in ['('] then
begin
begin_position:=i;
break;
end;
end;
for i:=1 to begin_position-1 do
str1:=str1+str[i];
for i:= end_position+1 to lengthOfString do
str3:=str3+str[i];
for i:= begin_position+1 to end_position-1 do
str2:=str2+str[i];
str:=str1+floattostr(cal(str2))+str3;
str1:='';
str2:='';
str3:='';
lengthOfString:=length(str);
end;
end;
result:=cal(str);
function cal(str:String):single;
var
//计算形如"a*b+c-d" 的计算式
opercount:integer;
i,j,k:integer;
preposition:integer;//保存上一字符位置
precal:array[0..254] of single;
operand:string;
begin
operand:='';
str:='*'+str+'*';
for i:=0 to 254 do
begin
precal[i]:=0; //表示没有要算的东西
end;
k:=0;
opercount:=0;
j:=length(str);
preposition:=1;
for i:=1 to j do
begin
if str[i] in ['+','-','*','/'] then
begin
operand:=operand+str[i];
opercount:=opercount+1; //i当前操作符的位置
if k<>0 then
precal[k]:=strtofloat(copy(str,preposition+1,i-preposition-1));
//ShowMessage(floattostr(precal[k]));
k:=k+1;
preposition:=i; //前一操作符的位置
end;
end;
operand:=copy(operand,2,length(operand)-2); //运算符拿到
//获得了运算次数
for i:=1 to opercount-2 do
begin
if operand[i]='*' then
begin
precal[i+1]:=precal[i]*precal[i+1];
precal[i]:=0;
operand[i]:='+';
end
else if operand[i]='/' then
begin
precal[i+1]:=precal[i]/precal[i+1];
precal[i]:=0;
operand[i]:='+';
end;
end;
//showmessage(operand);
//下面计算结果
result:=precal[1];
for i:=1 to opercount-2 do
begin
if operand[i]='+' then
result:=result+precal[i+1]
else
result:=result-precal[i+1]
end;
end;