数组索引中的 Ada 类型冲突
Ada type conflict in array indices
这是在 Java 中运行的代码和算法,但在 Ada 中它仍然会引发 CONSTRAINT_ERROR。在 Ada 中,我们从 1 开始对数组进行索引,而不是像大多数其他语言那样从 0 开始。出于某种原因,我仍在对数组进行索引。
procedure spiral is
M : Matrix := ((11,22,33,44,55),(1,8,3,8,9),(10,10,20,30,1));
lastcol, firstcol, lastrow : Integer := 1;
rowsize, colsize : Integer := 0;
procedure Print ( M: in Matrix ) is
begin
rowsize := M'Length(1);
colsize := M'Length(2);
while lastrow<=rowsize loop
for I in Index range Index(firstcol)..Index(colsize) loop
Put( Elem'Image(M(Index(lastrow),Index(I))));
Put( Ascii.HT );
end loop;
lastrow := lastrow + 1;
if (lastrow>=rowsize) then
return;
end if;
for J in Index range Index(lastrow)..Index(rowsize) loop
Put( Elem'Image(M(Index(J),Index(colsize))));
Put( Ascii.HT );
end loop;
colsize := colsize - 1;
for I in reverse Index range Index(colsize)..Index(lastcol) loop
Put( Elem'Image(M(Index(rowsize), Index(I))));
Put( Ascii.HT );
end loop;
rowsize := rowsize- 1;
lastcol := lastcol+ 1;
for I in reverse Index range Index(rowsize)..Index(lastrow) loop
Put( Elem'Image(M(Index(I), Index(firstcol))));
Put( Ascii.HT );
end loop;
firstcol := firstcol + 1;
end loop;
end Print;
begin
--Put(rowDown);
Print(M);
end spiral;
矩阵包定义为:
package Matrix_pack is
type Index is new Integer;
type Elem is new Integer;
type Matrix is array (Index range <>, Index range <>) of Elem;
end Matrix_pack;
解决方法可以分两步完成:
- 首先:使循环中使用的索引类型明确:
for I in Index range colLeft .. colRight loop
Put (Elem'Image (M (rowUp, I)));
end loop;
- 其次:修复
colLeft
和colRight
的声明:
colLeft, colRight : Index;
这是在 Java 中运行的代码和算法,但在 Ada 中它仍然会引发 CONSTRAINT_ERROR。在 Ada 中,我们从 1 开始对数组进行索引,而不是像大多数其他语言那样从 0 开始。出于某种原因,我仍在对数组进行索引。
procedure spiral is
M : Matrix := ((11,22,33,44,55),(1,8,3,8,9),(10,10,20,30,1));
lastcol, firstcol, lastrow : Integer := 1;
rowsize, colsize : Integer := 0;
procedure Print ( M: in Matrix ) is
begin
rowsize := M'Length(1);
colsize := M'Length(2);
while lastrow<=rowsize loop
for I in Index range Index(firstcol)..Index(colsize) loop
Put( Elem'Image(M(Index(lastrow),Index(I))));
Put( Ascii.HT );
end loop;
lastrow := lastrow + 1;
if (lastrow>=rowsize) then
return;
end if;
for J in Index range Index(lastrow)..Index(rowsize) loop
Put( Elem'Image(M(Index(J),Index(colsize))));
Put( Ascii.HT );
end loop;
colsize := colsize - 1;
for I in reverse Index range Index(colsize)..Index(lastcol) loop
Put( Elem'Image(M(Index(rowsize), Index(I))));
Put( Ascii.HT );
end loop;
rowsize := rowsize- 1;
lastcol := lastcol+ 1;
for I in reverse Index range Index(rowsize)..Index(lastrow) loop
Put( Elem'Image(M(Index(I), Index(firstcol))));
Put( Ascii.HT );
end loop;
firstcol := firstcol + 1;
end loop;
end Print;
begin
--Put(rowDown);
Print(M);
end spiral;
矩阵包定义为:
package Matrix_pack is
type Index is new Integer;
type Elem is new Integer;
type Matrix is array (Index range <>, Index range <>) of Elem;
end Matrix_pack;
解决方法可以分两步完成:
- 首先:使循环中使用的索引类型明确:
for I in Index range colLeft .. colRight loop
Put (Elem'Image (M (rowUp, I)));
end loop;
- 其次:修复
colLeft
和colRight
的声明:
colLeft, colRight : Index;