Program prueba1;
uses Estructu;
Var Pila1:Pila; Fila1,Completa:Fila;
Begin
Inicfila (Completa);
readpila(Pila1);
readfila(Fila1);
While (not pilavacia(Pila1) and not filavacia(Fila1)) do
begin
if (tope(Pila1) > primero(Fila1)) then
begin
agregar(Completa, desapilar(Pila1))
else
if (tope(Pila1) < primero(Fila1)) then
begin
agregar(Completa, extraer(Fila1))
else
if (tope(Pila1) = primero(Fila1)) then
begin
agregar(Completa, desapilar(Pila1));
agregar(Completa, extraer(Fila1))
end
end
end
end
write('El resultado final de Completa es');
Writefila(Completa);
End.
该程序的目的是在Completa中按从头到尾的顺序组织来自Pila1和Fila1的所有变量。我不知道自己在做什么错,希望能得到帮助
您没有正确使用if ... then ... else
和begin ... end
。
具有block的概念,该概念以begin
开头并以end
结束。任何需要单个statement的地方,都可以放一个block。 if <condition> then <statement> else <statement>
也是这种情况。
因此,此代码有效:
if something() then
stuff
else
stuff;
...是这样:
if something() then
begin
stuff;
moreStuff;
end
else
begin
otherStuff;
moreOtherStuff;
end;
但是,(您正在使用的)不是:
if something() then
begin
stuff // I guess here you omitted the semicolon because you correctly remembered
// that there shouldn't be a semicolon before `else`, but...
else // WRONG, this is in the middle of the block!
otherStuff;
end;
[为什么,让我们修复缩进以匹配此代码的逻辑解释:
if something() then
begin
stuff
else // ????????
otherStuff;
end;
由于begin ... else ... end
不是有效的构造,因此会出现错误。由于在begin
之前有一个end
,但是没有else
,因此您的else
位于then
块的中间,这没有任何意义。
开始end
部分之前,请确保先else
您的块,然后再begin
创建一个新块。