这是我的程序,没有编译器消息,但在运行时退出,有人可以帮助我吗?什么似乎是错的? 问题在我创建文件t之后开始,所以也许那里有我无法看到的。提前致谢。 折叠|复制代码
program MyProgr;
var
F: text;
t: Textfile;
a, count: array of Integer;
b: Integer;
i, int: Integer;
countnums: Integer;
n, m: String;
lin, nums: Integer;
Small, Big: Integer;
procedure DoWhatEver(S: string);
begin
val(S, int);
Write(S, ' ');
for i := Small to Big do
if (a[i] = int) then
count[i] := count[i] + 1;
end;
procedure FilltheArray;
begin
for i := Small to Big do
a[i] := i + 1;
end;
procedure ProcessString;
var
Strng, S: string;
Last, P: Integer;
begin
readln(F, Strng);
Last := 0;
while Last < length(Strng) do
begin
P := Last + 1;
while (P <= length(Strng)) and (Strng[P] <> ' ') do
inc(P);
S := copy(Strng, Last + 1, (P - Last - 1));
DoWhatEver(S);
Last := P;
end
end;
procedure ProcessStringA;
var
Strng: string;
Last, P: Integer;
begin
readln(F, Strng);
Last := 0;
while Last < length(Strng) do
begin
P := Last + 1;
while (P <= length(Strng)) and (Strng[P] <> ' ') do
inc(P);
n := copy(Strng, Last + 1, (P - Last - 1));
val(n, nums);
Last := P;
end
end;
procedure ProcessStringB;
var
Strng: string;
Last, P: Integer;
begin
readln(F, Strng);
Last := 0;
while Last < length(Strng) do
begin
P := Last + 1;
while (P <= length(Strng)) and (Strng[P] <> ' ') do
inc(P);
m := copy(Strng, Last + 1, (P - Last - 1));
val(m, lin);
Last := P;
end
end;
begin
assign(F, 'myfile.txt');
reset(F);
ProcessStringA;
Writeln(nums);
ProcessStringB;
Writeln(lin);
setlength(a, nums);
Small := Low(a);
Big := High(a);
for i := Small to Big do
count[i] := 0;
FilltheArray;
while not eof(F) do
ProcessString;
for i := Small to Big do
begin
if count[i] = 2 then
countnums := countnums + 1;
end;
Close(F);
assign(t, 'fileout.txt');
Rewrite(t);
Writeln(t, countnums);
Close(t);
end.
答案 0 :(得分:1)
问题在于您已声明了两个动态数组(count
和a
)。
a, count: array of Integer;
此时他们都没有分配内存。
然后为a
分配内存,并获取a
的低和高索引:
setlength(a, nums);
Small := Low(a);
Big := High(a);
然后循环遍历那些尚未分配内存的count
数组中的索引(您在SetLength
上调用了a
):
for i := Small to Big do
count[i] := 0;
访问尚未分配的内存会生成Runtime error 216
,这是一种访问冲突(在Delphi中,如果启用了例外则会引发EAccessViolation
)或一般保护错误(在FreePascal)。