pascal计算器怎么编?? 波兰式

来源:百度知道 编辑:UC知道 时间:2024/06/01 06:23:44
mq_miqing

包括+、-、*、/、(、)
好像可以用树做(波兰式)
5*(9-5)-2*3

-
/ \
* *
/ \ / \
5 - 2 3
/ \
9 5

怎么实现?最好有标程
如图

program p3_3;
const
n0=30;
var
s1:array[1..n0]of integer;
s2:array[1..n0]of char;
t1,t2:integer;

procedure calcu;
var
x1,x2,x:integer;
p:char;
begin
p:=s2[t2];t2:=t2-1;
x2:=s1[t1];t1:=t1-1;
x1:=s1[t1];t1:=t1-1;
case p of
'+':x:=x1+x2;
'-':x:=x1-x2;
'*':x:=x1*x2;
'/':x:=x1 div x2;
end;
t1:=t1+1;s1[t1]:=x;
end;

procedure calculator;
var
c:char;
v:integer;
begin
t1:=0;t2:=0;
read(c);
while c<>';' do
case c of
'+','-':
begin
while (t2>0)and(s2[t2]<>'(') do calcu;
t2:=t2+1;s2[t2]:=c;
read(c);
end;
'*','/':
begin
if (t2>0)and((s2[t2]='*')or(s2[t2]='/')) then calcu;
t2:=t2+1;s2[t2]:=c;
read(c);
end;
'(&