Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
otchet__fortunovoy_moy.doc
Скачиваний:
1
Добавлен:
01.05.2025
Размер:
11 Мб
Скачать
☆

Практическая работа №10.Создание проекта «бд студенты»

Цель работы: Разработать программу для работы с «БД студенты».

Краткие теоритические сведения: На данном этапе нужно написать программу, работающую с текстовым файлом.

Формулировка задания: Разработать программу с помощью компонентов Edit(строка ввода), Button(командная кнопка), OpenDialog(выбор файла с целью последующего открытия),MainMenu(главное меню программы),StringGrin(ввод массива),SaveDialog, PageControl.

Выполнение задания:

Задание(1)

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids, Menus, ComCtrls;

type

TForm1 = class(TForm)

MainMenu1: TMainMenu;

OpenDialog1: TOpenDialog;

SaveDialog1: TSaveDialog;

PageControl1: TPageControl;

TabSheet1: TTabSheet;

TabSheet2: TTabSheet;

N1: TMenuItem;

StringGrid1: TStringGrid;

ComboBox1: TComboBox;

Label1: TLabel;

Edit1: TEdit;

GroupBox1: TGroupBox;

StringGrid2: TStringGrid;

Label2: TLabel;

Label3: TLabel;

Button1: TButton;

Button2: TButton;

Button3: TButton;

Button4: TButton;

N2: TMenuItem;

N3: TMenuItem;

N7: TMenuItem;

N4: TMenuItem;

procedure NewComboBox;

procedure N2Click(Sender: TObject);

procedure N3Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure NewEdit(Sender: TObject);

procedure N4Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

type

info=record {тип записей}

fio,gr,faq,predmet,ocenka: string[20];

end;

var

f:file of info;{Файл записей}

r:info;{Одна запись}

N:integer;

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.NewComboBox;

var i,j,k: integer;

key: boolean;

begin

{Запоминаем активную позицию комбинированного списка}

j:=ComboBox1.ItemIndex;

ComboBox1.Clear;{очищаем список}

key:=true;

for i:=1 to n do

begin

for k:=0 to ComboBox1.Items.Count-1 do

if ComboBox1.Items[k]=StringGrid1.Cells[0,i] then

key:=false;

if key=true then ComboBox1.Items.Add(StringGrid1.Cells[0,i]);

key:=true;

end;

ComboBox1.ItemIndex:=j{переходим на позицию j}

end;

procedure TForm1.N2Click(Sender: TObject);

var i:integer;

begin

if OpenDialog1.Execute then

begin

AssignFile(F,OpenDialog1.FileName);

Reset(F);

N:=0;

while not eof(f) do

begin

read(F,r);

StringGrid1.Cells[0,N+1]:=r.fio;

StringGrid1.Cells[1,N+1]:=r.gr;

StringGrid1.Cells[2,N+1]:=r.faq;

StringGrid1.Cells[3,N+1]:=r.predmet;

StringGrid1.Cells[4,N+1]:=r.ocenka;

N:=N+1;{увеличение числа записей на 1}

end;

closefile(F);

NewCombobox;{процедура заполняет комбинированный список второй страницы}

//чтобы можно было выводить данные с помощью клика нужно Events/OnChange/newEdit

end;

end;

procedure TForm1.N3Click(Sender: TObject);

var i: integer;

begin

if SaveDialog1.Execute then

begin

AssignFile(F,SaveDialog1.FileName);

Rewrite(F);

i:=1;

while StringGrid1.Cells[3,i]<>''do

begin

r.fio:= StringGrid1.Cells[0,i];

r.gr:=StringGrid1.Cells[1,i];

r.faq:=StringGrid1.Cells[2,i];

r.predmet:=StringGrid1.Cells[3,i];

r.ocenka:=StringGrid1.Cells[4,i];

write(F,r);

i:=i+1;

end;

closefile(F);

end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

StringGrid1.Cells[0,0]:='ФИО';

StringGrid1.Cells[1,0]:='Группа';

StringGrid1.Cells[2,0]:='Факультет';

StringGrid1.Cells[3,0]:='Предмет';

StringGrid1.Cells[4,0]:='Оценка';

StringGrid2.Cells[0,0]:='ФИО';

StringGrid2.Cells[1,0]:='Группа';

StringGrid2.Cells[2,0]:='Факультет';

StringGrid2.Cells[3,0]:='Предмет';

StringGrid2.Cells[4,0]:='Оценка';

ComboBox1.Items.Add('<'); //Создаем пустую строку в комбинированном списке

ComboBox1.ItemIndex:=0; //Активизируем эту строку на случай создания нового справочника

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

if ComboBox1.ItemIndex=0 then

ComboBox1.ItemIndex:=N-1

else ComboBox1.ItemIndex:=ComboBox1.ItemIndex-1;

NewEdit(ComboBox1) //Обновляем панель "Найдена запись"

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if ComboBox1.ItemIndex=N-1 then

ComboBox1.ItemIndex:=0

else ComboBox1.ItemIndex:=ComboBox1.ItemIndex+1;

NewEdit(ComboBox1)//Обновляем панель "Найдена запись"

end;

procedure TForm1.Button3Click(Sender: TObject);

var i,j,k:integer;//номер строки данных в таблице

begin

if Edit1.Text='' then

begin

showmessage('Введите строку для поиска');

exit;

end;

k:=0;

for i:=0 to StringGrid2.RowCount-1 do

for j:=1 to StringGrid2.ColCount-1 do

StringGrid2.Cells[i,j]:='';

for i:=1 to N do

begin

if StringGrid1.Cells[0,i]=Edit1.Text then

begin

k:=k+1;

StringGrid2.Cells[0,k]:=StringGrid1.Cells[0,i];

StringGrid2.Cells[1,k]:=StringGrid1.Cells[1,i];

StringGrid2.Cells[2,k]:=StringGrid1.Cells[2,i];

StringGrid2.Cells[3,k]:=StringGrid1.Cells[3,i];

StringGrid2.Cells[4,k]:=StringGrid1.Cells[4,i];

combobox1.ItemIndex:=i-1;

end;

end;

i:=1;

k:=0;

While StringGrid2.Cells[0,i]<>''do

begin

k:=k+StrToInt(StringGrid2.Cells[4,i]);

i:=i+1;

end;

if i<>1 then

i:=i-1;

label3.Caption:=FloatToStr(k/(i));

if StringGrid2.Cells[0,1]='' then Showmessage('Ничего не найдено');

end;

procedure TForm1.Button4Click(Sender: TObject);

var i,j: integer;

s: string;

begin

s:=ComboBox1.Text;

for i:=0 to StringGrid2.RowCount-1 do

for j:=1 to StringGrid2.ColCount-1 do

StringGrid2.Cells[i,j]:='';

for i:=0 to Combobox1.Items.Count do

begin

if ComboBox1.Items[i]=s then ComboBox1.DeleteSelected

end;

Button2Click(Sender);

end;

procedure TForm1.NewEdit(Sender: TObject);

var i,j,k:integer;//номер строки данных в таблице

begin

k:=0;

for i:=0 to StringGrid2.RowCount-1 do

for j:=1 to StringGrid2.ColCount-1 do

StringGrid2.Cells[i,j]:='';

for i:=1 to N do

begin

if StringGrid1.Cells[0,i]=ComboBox1.Items[ComboBox1.ItemIndex] then

begin

k:=k+1;

StringGrid2.Cells[0,k]:=StringGrid1.Cells[0,i];

StringGrid2.Cells[1,k]:=StringGrid1.Cells[1,i];

StringGrid2.Cells[2,k]:=StringGrid1.Cells[2,i];

StringGrid2.Cells[3,k]:=StringGrid1.Cells[3,i];

StringGrid2.Cells[4,k]:=StringGrid1.Cells[4,i];

end;

end;

i:=1;

k:=0;

while StringGrid2.Cells[0,i]<>'' do

begin

k:=k+StrToInt(StringGrid2.Cells[4,i]);

i:=i+1;

end;

if i<>1 then

i:=i-1;

Label3.Caption:=FloatToStr(k/i);

end;

procedure TForm1.N4Click(Sender: TObject);

begin

close;

end;

end.

Задание (2). Напишите собственную программу по заданию,выданного варианта. (2вариант)

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Menus, Grids, ComCtrls;

type

info=record {тип записей}

nomer,fio,kredit,procent,sum:string[20];

end;

TForm1 = class(TForm)

PageControl1: TPageControl;

TabSheet1: TTabSheet;

TabSheet2: TTabSheet;

StringGrid1: TStringGrid;

StringGrid2: TStringGrid;

GroupBox1: TGroupBox;

ComboBox1: TComboBox;

Edit1: TEdit;

OpenDialog1: TOpenDialog;

SaveDialog1: TSaveDialog;

MainMenu1: TMainMenu;

N1: TMenuItem;

N2: TMenuItem;

N3: TMenuItem;

N4: TMenuItem;

N5: TMenuItem;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Button1: TButton;

Button2: TButton;

Button3: TButton;

procedure N2Click(Sender: TObject);

procedure N3Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure N5Click(Sender: TObject);

procedure NewComboBox;

procedure NewEdit(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

//PagesDlg: TPagesDlg; {Строка есть в заголовке модуля}

f: file of info; {Файл записей}

r: info; {Одна запись}

N: integer; {Количество записей}

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.NewComboBox;

var i,j,k: integer; key:boolean;

begin

{Запоминаем активную позицию комбинированного списка}

j:=ComboBox1.ItemIndex;

ComboBox1.Clear; {очищаем список}

key:=true;

for i:=1 to n do

begin

for k:=0 to combobox1.Items.Count-1 do

if ComboBox1.Items[k]=StringGrid1.Cells[0,i] then

key:=false;

if key=true then ComboBox1.Items.Add(StringGrid1.Cells[0,i]);

key:=true;

end;

ComboBox1.ItemIndex:=j {Переходим на позицию j}

end;

procedure TForm1.N2Click(Sender: TObject);

var i: integer;

begin

if OpenDialog1.Execute then

begin

AssignFile(F,OpenDialog1.FileName);

Reset(F);

N:=0;

while not eof(f) do

begin

read(F,r);

stringGrid1.Cells[0,N+1]:=r.nomer;

stringGrid1.Cells[1,N+1]:=r.fio;

stringGrid1.Cells[2,N+1]:=r.kredit;

stringGrid1.Cells[3,N+1]:=r.procent;

stringGrid1.Cells[4,N+1]:=r.sum;

N:=N+1 {увеличение числа записей на 1}

end;

closeFile(F);

NewCombobox{процедура заполняет комбинированный список второй страницы};

end;

end;

procedure TForm1.N3Click(Sender: TObject);

var i: integer;

begin

if SaveDialog1.Execute then

begin

AssignFile(F,SaveDialog1.FileName);

Rewrite(F);

i:=1;

while stringGrid1.Cells[3,i]<>'' do

begin

r.nomer:=stringGrid1.Cells[0,i];

r.fio:=stringGrid1.Cells[1,i];

r.kredit:=stringGrid1.Cells[2,i];

r.procent:=stringGrid1.Cells[3,i];

r.sum:=stringGrid1.Cells[4,i];

write(F,r);

i:=i+1

end;

closeFile(F);

end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

stringGrid1.Cells[0,0]:='№ п/п';

stringGrid1.Cells[1,0]:='ФИО';

stringGrid1.Cells[2,0]:='Название книги';

stringGrid1.Cells[3,0]:='Дата выдачи';

stringGrid1.Cells[4,0]:='Дата сдачи';

stringGrid2.Cells[0,0]:='№ п/п';

stringGrid2.Cells[1,0]:='ФИО';

stringGrid2.Cells[2,0]:='Название книги';

stringGrid2.Cells[3,0]:='Дата выдачи';

stringGrid2.Cells[4,0]:='Дата сдачи';

ComboBox1.Items.Add('<'); {Создаем пустую строку в комбинированном списке}

ComboBox1.ItemIndex:=0; {Активизируем эту строку на случай создания нового справочника}

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

if ComboBox1.ItemIndex=0

then ComboBox1.ItemIndex:=N-1

else ComboBox1.ItemIndex:=ComboBox1.ItemIndex-1;

NewEdit(ComboBox1) {Обновляем панель «Найдена запись»};

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if ComboBox1.ItemIndex=N-1

then ComboBox1.ItemIndex:=0

else ComboBox1.ItemIndex:=ComboBox1.ItemIndex+1;

NewEdit(ComboBox1) {Обновляем панель «Найдена запись»}

end;

procedure TForm1.Button3Click(Sender: TObject);

var i,j,k:integer;

begin

if Edit1.Text='' then begin showmessage('Введите строку для поиска');exit;end;

k:=0;

for i:=0 to stringgrid2.RowCount-1 do

for j:=1 to stringgrid2.ColCount-1 do

stringgrid2.Cells[i,j]:='';

for i:=1 to N do

begin

if StringGrid1.Cells[0,i]=Edit1.Text

then

begin

k:=k+1;

stringgrid2.Cells[0,k]:=stringgrid1.Cells[0,i];

stringgrid2.Cells[1,k]:=stringgrid1.Cells[1,i];

stringgrid2.Cells[2,k]:=stringgrid1.Cells[2,i];

stringgrid2.Cells[3,k]:=stringgrid1.Cells[3,i];

stringgrid2.Cells[4,k]:=stringgrid1.Cells[4,i];

combobox1.ItemIndex:=i-1;

end;

end;

i:=1; k:=0;

While Stringgrid2.Cells[0,i]<>'' do

begin

k:=k+strtoint(Stringgrid2.Cells[4,i]);

i:=i+1;

end;

if i<>1 then

i:=i-1;

label3.Caption:= floattostr(k/(i));

if stringgrid2.Cells[0,1]='' then Showmessage('Ничего не найдено');

end;

procedure TForm1.N5Click(Sender: TObject);

begin

Close;

end;

procedure TForm1.NewEdit(Sender: TObject);

var i,j,k:integer;

begin

k:=0;

for i:=0 to stringgrid2.RowCount-1 do

for j:=1 to stringgrid2.ColCount-1 do

stringgrid2.Cells[i,j]:='';

for i:=1 to N do

begin

if StringGrid1.Cells[0,i]=ComboBox1.Items[ComboBox1.ItemIndex]

then

begin

k:=k+1;

stringgrid2.Cells[0,k]:=stringgrid1.Cells[0,i];

stringgrid2.Cells[1,k]:=stringgrid1.Cells[1,i];

stringgrid2.Cells[2,k]:=stringgrid1.Cells[2,i];

stringgrid2.Cells[3,k]:=stringgrid1.Cells[3,i];

stringgrid2.Cells[4,k]:=stringgrid1.Cells[4,i];

end;

end;

i:=1; k:=0;

While Stringgrid2.Cells[0,i]<>'' do

begin

k:=k+strtoint(Stringgrid2.Cells[4,i]);

i:=i+1;

end;

if i<>1 then

i:=i-1;

label3.Caption:= floattostr(k/i);

end;

end.

Практическая работа №1

procedure TForm1.N2Click(Sender: TObject)

procedure TForm1.N4Click(Sender: TObject)

procedure TForm1.N5Click(Sender: TObject)

procedure TForm1.N6Click(Sender: TObject)

procedure TForm1.N7Click(Sender: TObject)

procedure TForm1.N8Click(Sender: TObject)

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]