表达式求值pas,非c

本文介绍了一种用于解析和计算数学表达式的算法实现。该算法通过读取字符输入,将其转换为中间形式,并最终计算出表达式的数值结果。文章详细展示了如何处理括号、运算符优先级及操作数的读取。
 

const  data:array [1..7] of integer=(-1,0,1,1,2,2,3);
type   stack=array[1..100] of char;
       arr=array[1..100] of char;
       arr1=array[1..100] of real;
var  top,i,J,h:integer;   ch,w:char;    a,s :stack;ss:arr1;
     x:real;
Function   t(p1:char):integer;
var x1:1..7;
begin
    case p1 of
         '#': x1:=1;
         '(': x1:=2;
         '+': x1:=3;
         '-': x1:=4;
         '*': x1:=5;
         '/': x1:=6;
         ')': x1:=7;
    end;    t:=data[x1];
end;
procedure    push(var s:stack; ch:char);
begin
    inc(top);
    s[top]:=ch;
end;

procedure   pop(var s:stack);
begin
    dec(top);
end;

function    readtop(s:stack):char;
begin
    readtop:=s[top];
end;
function comp(a:arr; var s:arr1):real;
   var i,k:integer;      ch:char;
   begin
     i:=1; top:=0;
     ch:=a[ i];
     while ch<>'@' do
      begin
        case ch of
        '0'..'9':begin x:=0;
                    while (ch<>' ')  do
                    begin
                      x:=x*10+ord(ch)-ord('0');
                      i:=i+1;  ch:=a[ i];
                    end;   top:=top+1;
                  end;
'+':begin x:=s[top-1]+s[top];top:=top-1; end;
        '-':begin x:=s[top-1]-s[top];top:=top-1; end;
        '*':begin x:=s[top-1]*s[top];top:=top-1; end;
        '/':begin if s[top]<>0 then
                  begin x:=s[top-1]/s[top];top:=top-1; end
                         else begin                                    writeln('error');;
                                      end;
             end;
    end ; {case}
      s[top]:=x;       i:=i+1;       ch:=a[i];
     end ;{while}
 comp:=s[top];
end;{function}

begin
    push(s,'#'); // 为了识别栈底,与结束符对应
    J:=1;
    read(ch);
    while ch<> '#' do
          begin
               if ch in ['0'..'9'] then
                  begin
                       while ch in ['0'..'9'] do
                             begin
                                  a[J]:=ch;
                                  inc(J);
                                  read(ch);
                             end;
                       a[j]:=' ';inc(J); // 每个操作数后增加一个空格分隔
                  end
else
     begin
       case ch of
       '+','-','*','/':begin
                          w:=s[top];
                           while t(w)>=t(ch) do
                             begin a[J]:=w;
                              J:=J+1;  pop(s);
                               w:=readtop(s);
                              end;
                          if t(w)<t(ch) then  push(s,ch);
                          end;
'(': begin push(s,ch); end;
       ')':begin
                         while s[top]<>'('do
                        begin
                         a[J]:=s[top];J:=J+1;
                         pop(s);
                        end; top:=top-1;
                        end;

                  end;
               read(ch); end;
        end;
w:=s[top];
        while w<>'#' do
         begin  if w<>'(' then  begin a[J]:=w;J:=J+1;end;
         pop(s);w:=readtop(s);
         end;
        a[J]:='#';pop(s);
  h:=1;
  repeat      //3*(2+5)-143# 
  write(a[h]);
  h:=h+1;
  until a[h]='#';
  writeln(a[h]);
  x:=comp(a,ss);
  writeln(x:0:3);  //-128.000
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值