作业帮 > 综合 > 作业

用pascal求后缀表达式的值.

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/04/27 15:28:56
用pascal求后缀表达式的值.
【问题描述】
根据后缀算术表达式计算其值,注意相邻的数值型数据间用空格隔开,数据与运算符间也用空格隔开,表达式以@结束.计算的值保留两位小数,并对第三位四舍五入.
如35 6 +@其值输出为41.
【输入】
输入一行以@结束的表达式
【输出】
结果
【样例】
输入
36 57 /@
输出
0.63
用pascal求后缀表达式的值.
program track;
var t1:array[1..10000] of longint;
top,n,i:integer;
rd:char;
f:boolean;
begin
rd:=' ';
top:=0;
f:=false;
while rd'@' do
begin
read(rd);
n:=0;
while (rd in ['0'..'9']) do
begin
f:=true;
n:=n*10+ord(rd)-ord('0');
read(rd);
end;
if f then begin
inc(top);
t1[top]:=n;
f:=false;end;
if rd in ['+','-','*','/'] then
begin
case rd of
'+':t1[top-1]:=t1[top-1]+t1[top];
'-':t1[top-1]:=t1[top-1]-t1[top];
'*':t1[top-1]:=t1[top-1]*t1[top];
'/':t1[top-1]:=t1[top-1] div t1[top];
end;
dec(top);
end;
end;
write(t1[1]);
end.