سؤال

Here is the textfile that the program must read, and put every num. in a different variable. The first num., in this case 3, is the n, and tells the procedure the program how many times to be done. Between the nums., there is a space.

The text file f is like that

3 2
2 1
1 5
4 2

When it runs the code the following thing keeps being writen

->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->
->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->

Why do that happeen?

Can anyone please help me with this program?

The code is the following one.

Program thefinalp;

Uses SysUtils;

Var
  f: Text;
  m, d: Integer;
  n: Char;
  c: String[1];

  a, e: array of Integer;
  LowArr: Integer;
  HighArr: Integer;
  ArrayLen: Integer;
  i: Integer;

begin
  Assign(f, 'd:\tempfiles\finalp.txt');
  Reset(f);

  repeat
    Readln(f, n);
    Write(n);
  until (n = ' ');

  Read(f, c);
  Write(c);

  while not SeekEoln(f) do
  begin
    read(f, d);
    Write(d);
  End;
  Readln;
  Writeln;

  StrToIntDef(n, m);

  setlength(a, m);
  LowArr := Low(a);
  HighArr := High(a);
  ArrayLen := Length(a);

  setlength(e, m);
  LowArr := Low(e);
  HighArr := High(e);
  ArrayLen := Length(e);

  for i := LowArr to HighArr do

  begin

    repeat
      Read(f, a[i]);
      Write(a[i]);
    until (n = ' ');

    Read(f, c);
    Write(c);

    while not SeekEoln(f) do
    begin
      read(f, e[i]);
      Write(e[i]);
    End;
    Readln;
    Writeln;

  End;
  Readln;

End.
هل كانت مفيدة؟

المحلول

In your first repeat until, you are readlning into a char. The first character will appear in n and the remainder of the characters will be skipped entirely until the newline has been read. AT that point, your file-pointer will be at 2 on the second line of data.

Since your test is for n=' ' then the readln will again be executed, this time delivering 2 into n and pushing the file-pointer to the 1 on the third line.

When eventually end-of-file is reached, a Control-Z character is 'read' from the file. This is the character you are seeing. Since it isn't Space, the loop continues forever.

Change the readln here to a read and one character will be read. (then it works, and you can go on to the next problem...)

Remember, readln reads until it has read a newline. Read reads into the variable - if it's a char, it reads one char. If it's a string, it reads a string - but not the newline.


Program thefinalp;

Uses SysUtils;

Var
f:Text;
m,d:Integer;
n:string;
n2:string;
c:String[1];

a,e:array of integer;//dynamic array//
LowArr:Integer;
HighArr:Integer;
ArrayLen:Integer;
i:Integer;
  ch : char;

function readinteger : string;
var
  st : string;
begin
  st := '';
//  read up to first digit
  repeat
    read(f,ch);
    write(ch);
  until ch in ['0'..'9'];
//accumulate digits
  repeat
    st := st + ch;
    read(f,ch);
    write(ch);
  until not (ch in ['0'..'9']);
  readinteger := st;
end;


begin
  Assign(f,'q21366050.txt');
  Reset(f);
// read first integer
  n:= readinteger;
// read second integer
  n2:= readinteger;

  m := StrToInt(n); //puts a string into an integer//

  setlength(a,m);
  LowArr:=Low(a);
  HighArr:=High(a);
  ArrayLen:=Length(a);

  setlength(e,m);
  LowArr:=Low(e);
  HighArr:=High(e);
  ArrayLen:=Length(e);

  for i:= LowArr to HighArr do
  begin
// read first integer
    n:= readinteger;
// read second integer
    n2:= readinteger;
    a[i]:=StrToInt(n); //puts a string into an integer//
    e[i]:=StrToInt(n2); //puts a string into an integer//
  End;
  Writeln;
  writeln('Results');
  for i:= LowArr to HighArr do
    writeln(inttostr(i),'=',inttostr(a[i]),',',inttostr(e[i]));
// pause to read results
  Readln;
End.

Unfortunately, it's a little difficult to figure out just exactly what you want to do. This routine will read the first line and then put the remaining lines into a[?] and e[?].

Using descriptive variablenames would perform some of the documentation so you can follow what is happening. Since I don't actually know, I'm having to make assumptions and make a few things up to fill in the gaps.

Looking at the main routine, first you assign a filename (I used q21366050.txt for my convenience) and open the file with a reset.

Next job is to read the first number in from the file. Now you have only shown single-digit numbers, but it's easy to set the routine to cope with a sequence.

n:=readinteger;

assigns the result of the function readinteger to the string n

readinteger works this way: first clear the string st which is a "local variable" - only available to this routine. Then keep reading characters into ch until the character read is in the range '0'..'9' - so it skips until it reads a digit. Then it adds the digit read to the string st and continues to read characters and accumulate them until the character found is not a digit. (That character, should we need it, is in ch) We then assign the accumulated string of digits to the resut of the function.

Hence, n will get the first string of digits in the file; the next character has been read,and we know it isn't a digit (otherwise it would have been appended to the string returned).

We then repeat the process with n2. All of the remaining characters before then next digit are skipped, the digit sequence returned and the following character placed in ch

Then we assign the resullt of converting the string n to an integer to m. You haven't described what the other number may be used for, so it's there - but unused.

Set up the two arays, a and e.

Then use the same routine to read the next integer. It doesn't matter that there are CRLF characters - we skip to the next numeric and return it. and repeat that for the second number in the line.

Convert the two numbers and put them into their respective arrays.

Do this m times.

inally, write a new line to the display, then another reporting Results and then repeat m times write a line containing the iteration number i and the values of the two arrays, a and e, all as integers-converted-to-strings and with = and , characters to show that we're not just repeating the dat read from the input.

Finally, wait for an input from the keyboard (since the readln has no explicit filevar) which holds the program open until we can see the results.

Now - nominally, of course, you should also close the file before terminating...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top