Показать сообщение отдельно
Старый 16.06.2010, 10:40   #1 (permalink)
ДенисКО
Member
 
Регистрация: 02.11.2009
Сообщений: 19
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
По умолчанию Делфи, работа с файлами

Пусть дан файл целых чисел f1. Создайте два новых файла f2 и f3 из положительных и отрицательных чисел соответственно.

unit lab1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var f:TextFile;
a:integer;
begin
AssignFile(f,'l1_in.txt');
Reset(f);
while not eof(f) do
begin
Read(f,a);
ListBox1.Items.Add(Inttostr(a));
end;
closefile(f);
for a:=0 to listbox1.Count-1 do
if Strtoint(ListBox1.Items.Strings[a])>=0 then
listbox2.Items.Add(ListBox1.Items.Strings[a]) else listbox3.Items.Add(ListBox1.Items.Strings[a]);
if Listbox2.Count=0 then listbox2.Items.Add('Нет положительных');
if Listbox3.Count=0 then listbox3.Items.Add('Нет отрицательных');
ListBox2.Items.SaveToFile('l1_max_out.txt');
ListBox3.Items.SaveToFile('l1_min_out.txt');
end;
end.



Заменить блок:
AssignFile(f,'l1_in.txt');
Reset(f);
while not eof(f) do
begin
Read(f,a);
ListBox1.Items.Add(Inttostr(a));
end;
closefile(f);
for a:=0 to listbox1.Count-1 do
if Strtoint(ListBox1.Items.Strings[a])>=0 then
listbox2.Items.Add(ListBox1.Items.Strings[a]) else listbox3.Items.Add(ListBox1.Items.Strings[a]);
if Listbox2.Count=0 then listbox2.Items.Add('Нет положительных');
if Listbox3.Count=0 then listbox3.Items.Add('Нет отрицательных');
ListBox2.Items.SaveToFile('l1_max_out.txt');
ListBox3.Items.SaveToFile('l1_min_out.txt');

так чтобы запись результатов шла сразу в файлы
ДенисКО вне форума   Ответить с цитированием
Ads

Яндекс

Member
 
Регистрация: 31.10.2006
Сообщений: 40200
Записей в дневнике: 0
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 55070