回 帖 发 新 帖 刷新版面

主题:表达式树的建立

哪儿出错了(FP运行:TYPE IDENTIFIER EXPECTED)
有表达式建树更好方法的,赶快发贴
program  biaodashi;
    type btree=^point;
         point=record
           n:string;
           r,l:btree;
                end;
    var ps,st:string;
           f:text;
          s:array[1..1000]of btree;
     p,q:btree;
     a:array[#1..#225]of integer;
     b:array[1..7,1..7]of char;
       i,j,k:integer;
procedure lastorder(p:btree);
   begin
      if p^.l<>nil then lastorder(p^.l);
      if p^.r<>nil then lastorder(p^.r);
      if (p^.r=nil)and(p^.l=nil) then begin write(p^.n);dispose(p);end;
     end;
     begin
     a['+']:=1;a['-']:=2;
     a['*']:=3;a['/']:=4;
     a['(']:=5;a[')']:=6;
     a[' ']:=7;
     assign(f,'bds.in');
     reset(f);
     for i:=1 to 7 do
       begin
       for j:=1 to 7 do
         read(f,b[i,j]);
         readln(f);
       end;
     readln(st);st:='('+st+')';
     s[1]^.n:='(';i:=2;k:=1;
     while i<=length(st)  do
 begin       if st[i] in ['0'..'9'] then begin
             ps:=' ';
          while st[i] in ['0'..'9'] do
             begin ps:=ps+st[i];
                   inc(i);
                   end;
          new(p);p^.n:=ps;inc(k);s[k]:=p;
                                  end
        else begin new(p);
        p^.n:=st[i];inc(k);s[k]:=p;end;
   if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='='then k:=k-2
else if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='>'then begin
                  s[k]^.l:=s[k-1];s[k-1]:=s[k];k:=k-1;
                                                 end
else begin
          while b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='<'  do
              begin
                 s[k-2]^.r:=s[k-1];s[k-1]:=s[k];dec(k);q:=s[k-2];
               end;
      end;
  end;
  lastorder(q);
end.

回复列表 (共8个回复)

沙发

>><<<>>
>><<<>>
>>>><>>
>>>><>>
<<<<<=  
>>>>>>< 
<<<<      
改了一点点
 program  biaodashi;
    type btree=^point;
         point=record
           n:string;
           r,l:btree;
                end;
    var ps,st:string;
           f:text;
          s:array[1..1000]of btree;
     p,q:btree;
     a:array[#1..#225]of integer;
     b:array[1..7,1..7]of char;
       i,j,k:integer;
procedure lastorder(p:btree);
   begin
      if p^.l<>nil then lastorder(p^.l);
      if p^.r<>nil then lastorder(p^.r);
      if (p^.r=nil)and(p^.l=nil) then begin write(p^.n);dispose(p);end;
     end;
     begin
     a['+']:=1;a['-']:=2;
     a['*']:=3;a['/']:=4;
     a['(']:=5;a[')']:=6;
     a[' ']:=7;
     assign(f,'d:\hdrgo\lbds.in');
     reset(f);
     for i:=1 to 7 do
       begin
       for j:=1 to 7 do
         read(f,b[i,j]);
         readln(f);
       end;
       close(f);
     readln(st);st:='('+st+')';
     s[1]^.n:='(';i:=2;k:=1;
     while i<=length(st)  do
 begin       if st[i] in ['0'..'9'] then begin
             ps:=' ';
          while st[i] in ['0'..'9'] do
             begin ps:=ps+st[i];
                   inc(i);
                   end;
          new(p);p^.n:=ps;inc(k);s[k]:=p;
                                  end
        else begin new(p);
        p^.n:=st[i];inc(k);s[k]:=p;end;
   if s[k]^.n<>'(' then begin
   if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='='then k:=k-2
else if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='>'then begin
                  s[k]^.l:=s[k-1];s[k-1]:=s[k];k:=k-1;
                                                 end
else begin
          while b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='<'  do
              begin
                 s[k-2]^.r:=s[k-1];s[k-1]:=s[k];dec(k);q:=s[k-2];
               end;
      end;
                       end;
  end;
  lastorder(q);
end.

板凳

我最近比较忙,Mato来解决一下吧

3 楼

你这个程序没出问题啊?

楼上的:
    我现在也比较忙,冲刺今年的NOIP,要做好多题。

4 楼

NOIP的普及组比较简单,基本上就是:枚举、搜索、DP、高精度、模拟,连图论都很少出到

拿满分还是比较容易的

5 楼

是吗?你们发点题过来呀!
再帮我看一下
输入3+6
    运行后 有一行出现(***+****
                      ^^
                      (***6***6****)***
问题是第一个6应该是+呀,原程序运行到##所在地就出了问题,K:=K+1后竟然会改变原K的指向的变量的值
原程序在附件里

6 楼

好了,总算完成了。总结几点教训:
1。对变量要有深刻的理解;
2。测试程序要分段测试;
3。对各种概念及用法要熟悉
原来的程序错在很多细节之上,有心人可以去体会
ANGWUY发点你们做的题目上来呀。。。。。。。

7 楼

源程序:
有更好的发上来
program biaodashi;
    type btree=^point;
         point=record
            n:string;
            r,l:btree;
                end;
    var p,q:btree;s:array[1..1000]of btree;
        a:array[#1..#225]of integer;
        b:array[1..6,1..7]of char;
        i,j,k,n:integer;
        sp,st:string;f:text;
     procedure   preorder(var root:btree);
        begin
          if root<>nil then begin
                write(root^.n);
                preorder(root^.l);
                preorder(root^.r);
                             end;
        end;
    procedure lastorder(var root:btree);
         begin
         if root<>nil then begin
               lastorder(root^.l);
               lastorder(root^.r);
               write(root^.n);
                         end;
            end;
     begin
       a['+']:=1;a['-']:=2;
       a['*']:=3;a['/']:=4;
       a['(']:=5;a[')']:=6;
       a[' ']:=7;
       assign(f,'d:\hdrgo\bds.in');
       reset(f);
       for i:=1 to 6 do
         begin
          for j:=1 to 7 do
             read(f,b[i,j]);
             readln(f);
           end;
           close(f);
       writeln(' input ');
       readln(st);
       st:=st+')';k:=1;new(s[1]);s[1]^.n:='(';i:=1;
       while i<=length(st) do
         begin
           while st[i]='(' do
             begin
               inc(k);new(s[k]);s[k]^.n:='(';inc(i);
              end;
        if st[i]<>')' then begin
         sp:=' ';while st[i] in ['0'..'9'] do begin sp:=sp+st[i];inc(i);end;
         inc(k);new(s[k]);s[k]^.n:=sp;
         inc(k);new(s[k]);s[k]^.n:=st[i];inc(i);
                            end
        else  begin inc(k);new(s[k]);s[k]^.n:=')';inc(i);end;
         if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='>' then begin
              s[k]^.l:=s[k-1];new(s[k-1]);s[k-1]:=s[k];k:=k-1;
              writeln(s[k]^.n,'left',s[k]^.l^.n);
                                                      end
    else if b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='<' then begin
                               while (b[a[s[k]^.n[1]],a[s[k-1]^.n[1]]]='<')and(s[k-2]^.n<>'(')  do
                                      begin
                                           s[k-2]^.r:=s[k-1];
                                           writeln(s[k-2]^.n,'right',s[k-2]^.r^.n);
                                           new(s[k-1]);s[k-1]:=s[k];k:=k-1;
                                        end;
                                 if b[a[s[k]^.n[1]],a[s[k-2]^.n[1]]]='=' then begin
                                      new(s[k-2]);s[k-2]:=s[k-1];k:=k-2;
                                                                             end;
                                                     end;
            end;
      {finish setting up the tree}
       writeln('hou zhui biao da shi');
       lastorder(s[1]);
       writeln;
       writeln('qian zhui biao da shi ');
       preorder(s[1]);
     end.

8 楼

bds.in
>><<  >
>><<  >
>>>>  >
>>>>  >
     = 
<<<<= <

我来回复

您尚未登录,请登录后再回复。点此登录或注册