pwxiaolongren 2015-08-18 03:44 采纳率: 50%
浏览 1306
已结题

pascal 问题 计算式子的值 求大神指点

描述 Description

给定一个表达式串,计算其最后结果

输入格式 Input Format

一个表达式串(只包函+-*/()等运算符,且是整除;表达式长度小于255个字符)

输出格式 Output Format
最后结果(一个整数)
样例输入
1+2*3+(1+2)
样例输出

10

program p1654;
var tot:ansistring;
function f(st:ansistring):longint;
var i,len,khw,ans,n,m:longint;
fh:boolean;
begin
if st='' then exit (0);
n:=520;
len:=length(st);
fh:=false;
ans:=0;
for i:=1 to len do
if st[i]in['+','-','*','/'] then fh:=true;
if not fh then begin val(st,ans);exit(ans);end;
khw:=0;
for i:=len downto 1 do
begin
if st[i] =')' then inc(khw);
if st[i] ='(' then dec(khw);
if khw=0 then
begin
if st[i]in['+','-','*','/'] then
begin
case st[i] of
'+':begin n:=1;m:=i;end;
'-':if n<>1 then begin n:=2;m:=i;end;
'*':if not(n in[1,2]) then begin n:=3;m:=i;end;
'/':if not(n in[1,2,3]) then begin n:=4;m:=i;end;
end;
case n of
1:exit(f(copy(st,1,m-1))+f(copy(st,m+1,length(st)-m)));
2:exit(f(copy(st,1,m-1))-f(copy(st,m+1,length(st)-m)));
3:exit(f(copy(st,1,m-1))*f(copy(st,m+1,length(st)-m)));
4:exit(f(copy(st,1,m-1))div f(copy(st,m+1,length(st)-m)));
end;
end;
end;
end;
if n=520 then exit (f(copy(st,2,len-2)));
end;

begin
readln(tot);
writeln(f(tot));
// readln(tot);
end.

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-08-18 05:14
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器