Board index » delphi » Program to sort real values in a text file

Program to sort real values in a text file

Can anyone give me some advise.

I have to write a program to sort real values in a text file by means of a
Table ADT. I have the ADT. The program must simply read the values one by
one from the text file (one number per line) and insert them into a table.
Once all the values have been inserted, the contents of the file must be
written back to the text file. The numbers should be written back to the
file with two digits after the decimal place.

Thanks, Johan

 

Re:Program to sort real values in a text file


On Sun, 16 Apr 2000 11:48:25 +0100, "Johan Schwab"

Quote
<j.sch...@{*word*269}.net> wrote:
>Can anyone give me some advise.

>I have to write a program to sort real values in a text file by means of a
>Table ADT. I have the ADT. The program must simply read the values one by
>one from the text file (one number per line) and insert them into a table.
>Once all the values have been inserted, the contents of the file must be
>written back to the text file. The numbers should be written back to the
>file with two digits after the decimal place.

>Thanks, Johan

Not sure what is troubling you but maybe this simple demo will get you
started.

I used this textfile as a test:
-123.456
98.765
87.123
3.14159
.707
-2345.7
54321.87

Program ExtractingReals;
{One real-formatted string per line.
Decimal fraction is rounded to 2 places.
The 8 "fields" can be 5 digits + the decimal + 2 decimal places OR
       a minus sign + 4 digits + the decimal + 2 decimal places }

VAR
f:Text;
r:real;
s:String;
errCode:Integer;

Begin
     Assign(f, 'MYNUMS.TXT');
     reset(f);
     While not eof(f) Do
     Begin
          Readln(f, s);
          Val(s, r, errCode);
          If errCode = 0 then Writeln(r:8:2);
     End;
Close(f);
readln;
End.

Other Threads