50分求这个语法的讲解!关于函数实参的加Var与不加Var!!
比如
Type pos=^integer;procedure(var bt:pos);与procedure(bt:pos);有什么区别?
那procedure(bt:integer)与procedure(var bt:integer)呢?谁能讲解,并给出样例?最好能给出个C或C++的等价的表达式来告诉我//加100分无所谓!!
比如
Type pos=^integer;procedure(var bt:pos);与procedure(bt:pos);有什么区别?
那procedure(bt:integer)与procedure(var bt:integer)呢?谁能讲解,并给出样例?最好能给出个C或C++的等价的表达式来告诉我//加100分无所谓!!
Var是函数内的修改结果可带回
没Var的就不返回具体看书或Google,基础知识来的http://lysoft.7u7.net
为什么crt_tree函数加了Var就就完整输出前序便利44,而如果不加Var只输出第一个4?
能实际讲1下我的程序么?
Type
BinaryTree=^bnode;
bnode=record
val:integer;
rchild,lchild:BinaryTree;
end;
var
Bt,Root:binarytree;
i,EmptyTag:integer;procedure Crt_Tree(bt:BinaryTree);
var
val:integer;
begin
read(val);
if(val=0) then begin
bt:=nil;
end
else begin
new(bt);
if(EmptyTag=0) then root:=bt;
EmptyTag:=1;
bt^.val:=val;
Crt_Tree(bt^.lchild);
Crt_Tree(bt^.rchild);
end;
end;procedure GetPostOrder(bt:BinaryTree);
begin
if(bt<>nil) then begin
write(bt^.val);
GetPostOrder(bt^.lchild);
GetPostOrder(bt^.rchild);
end;
end;begin
Crt_Tree(Bt);
bt:=root;
GetPostOrder(Bt);
end.
我以前学过C++的,弄了弄Pascal,不清楚这个
procedure(var bt:pos);与procedure(bt:pos);应该没有区别,都是指针
procedure(bt:integer)与procedure(var bt:integer)区别在于在调用前后bt值可能发生变化(得看你得procedure中是否为bt赋值了)
这个程序,二叉树建立,输出前序遍历的程序,输入4 0 4 0 0
为什么crt_tree函数加了Var就就完整输出前序便利44,而如果不加Var只输出第一个4?
回答以下就可以了,我已经知道Var的问题了
read(val)是什么意思啊?我在程序中这样写的话,会报出I/O错误
这个函数读出的是哪里的数据啊?
Read的格式要空格,比如4 0 4 0 0.
就是从控制台读入阿
A
/ \
B C
/ \ / \
D E F G
/ \
H I
}
program preorder_bt;
type notetype=record
ch:char;
parent,lch,rch:integer;
end;
var bt:array[1..9] of notetype;
procedure visit(p:integer);
begin
if bt[p].ch<>'' then
begin
if bt[p].lch<>0 then visit(bt[p].lch);
write(bt[p].ch:2);
if bt[p].rch<>0 then visit(bt[p].rch);
end;
end;
begin
fillchar(bt,sizeof(bt),0);
bt[1].ch:='A'; bt[1].lch:=2; bt[1].rch:=3; bt[1].parent:=0;
bt[2].ch:='B'; bt[2].lch:=4; bt[2].rch:=5; bt[2].parent:=1;
bt[3].ch:='C'; bt[3].lch:=6; bt[3].rch:=7; bt[3].parent:=1;
bt[4].ch:='D'; bt[4].lch:=0; bt[4].rch:=0; bt[4].parent:=2;
bt[5].ch:='E'; bt[5].lch:=8; bt[5].rch:=9; bt[5].parent:=2;
bt[6].ch:='F'; bt[6].lch:=0; bt[6].rch:=0; bt[6].parent:=3;
bt[7].ch:='G'; bt[7].lch:=0; bt[7].rch:=0; bt[7].parent:=3;
bt[8].ch:='H'; bt[8].lch:=0; bt[8].rch:=0; bt[8].parent:=5;
bt[9].ch:='I'; bt[9].lch:=0; bt[9].rch:=0; bt[9].parent:=5;
visit(1);
readln;
end.
A
/ \
B C
/ \ / \
D E F G
/ \
H I
}program preorder_bt;
type notetype=record
ch:char;
parent,lch,rch:integer;
end;
var bt:array[1..9] of notetype;
procedure visit(p:integer);
begin
if bt[p].ch<>'' then
begin
write(bt[p].ch:2);
if bt[p].lch<>0 then visit(bt[p].lch);
if bt[p].rch<>0 then visit(bt[p].rch);
end;
end;
begin
fillchar(bt,sizeof(bt),0);
bt[1].ch:='A'; bt[1].lch:=2; bt[1].rch:=3; bt[1].parent:=0;
bt[2].ch:='B'; bt[2].lch:=4; bt[2].rch:=5; bt[2].parent:=1;
bt[3].ch:='C'; bt[3].lch:=6; bt[3].rch:=7; bt[3].parent:=1;
bt[4].ch:='D'; bt[4].lch:=0; bt[4].rch:=0; bt[4].parent:=2;
bt[5].ch:='E'; bt[5].lch:=8; bt[5].rch:=9; bt[5].parent:=2;
bt[6].ch:='F'; bt[6].lch:=0; bt[6].rch:=0; bt[6].parent:=3;
bt[7].ch:='G'; bt[7].lch:=0; bt[7].rch:=0; bt[7].parent:=3;
bt[8].ch:='H'; bt[8].lch:=0; bt[8].rch:=0; bt[8].parent:=5;
bt[9].ch:='I'; bt[9].lch:=0; bt[9].rch:=0; bt[9].parent:=5;
visit(1);
readln;
end.
当传递参数是对象类型时,情况复杂一点,因为对象都是引用类型.
这个用数组,不一样的,我用二叉链表
Crt_Tree(bt^.lchild);
Crt_Tree(bt^.rchild);
构造子树时,这个指针只是副本?不指向真正的左右子树?