Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids,
- Vcl.Imaging.jpeg, Vcl.ExtCtrls;
- type
- plist = ^element;
- TSerial = record
- tytul : String[50];
- ocena : Integer;
- odcinki : Integer;
- sezony : Integer;
- end;
- element = record
- key : TSerial;
- wsk : plist;
- end;
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Edit3: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Button1: TButton;
- Button5: TButton;
- Edit4: TEdit;
- Label4: TLabel;
- StringGrid1: TStringGrid;
- Button2: TButton;
- Button3: TButton;
- Edit5: TEdit;
- Label5: TLabel;
- Button4: TButton;
- Image1: TImage;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure Button5Click(Sender: TObject);
- procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- procedure Button4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- first: plist;
- list_length: Integer = 0;
- I: Integer;
- a : TSerial;
- Kolumna : Integer = -1;
- Wiersz : Integer = -1;
- counter:Integer;
- prev, cur: plist;
- plik : file of TSerial;
- elem : TSerial;
- szukanyTytul : string[50]; //sprawdza jaki tytuł chcemy wyszukać
- wskaznikSortowania : Integer; //sprawdza wg. czego chcemy posortować
- id : Integer = 1; //nadaje id
- kluczUsuwania : Integer; //nie mam pojecia
- dziuba : Integer = 0; //do sprawdzania czy edytujemy czy dodajemy
- wskaznikSortowania2: Integer = 1;
- kluczyk: Integer =2;
- zmiennaEdytowana:Integer;
- implementation
- {$R *.dfm}
- //---------------------------------------------------------------PROCEDURKI FUNKCYJNE-----------------------------------------------------
- // ---INSERTNODE---
- procedure insertNode(a:TSerial);
- begin
- cur := first;
- if (first = nil) then
- begin
- New(first);
- first^.key := a;
- first^.wsk := nil;
- end
- else if (a.ocena) <= (first^.key.ocena) then
- begin
- New(cur);
- cur^.key := a;
- cur^.wsk := first;
- first := cur;
- end
- else
- begin
- while (cur^.wsk <> nil) and (a.ocena > cur^.wsk^.key.ocena) do
- cur := cur^.wsk;
- prev := cur;
- New(cur);
- cur^.key := a;
- cur^.wsk := prev^.wsk;
- prev^.wsk := cur;
- end;
- end;
- // --USUWA CAŁĄ BAZĘ--
- procedure usunBaze();
- begin
- first := nil;
- Form1.Edit1.Text := ('');
- Form1.Edit2.Text := ('');
- Form1.Edit3.Text := ('');
- Form1.Edit4.Text := ('');
- end;
- //---ZAPISUJE BAZE DO PLIKU--
- procedure zapisz();
- begin
- cur := first;
- AssignFile(plik, 'recordy.dat');
- Rewrite(plik);
- if first <> nil then
- begin
- while (cur^.wsk <> nil) do
- begin
- Write(plik, cur^.key);
- cur := cur^.wsk;
- end;
- Write(plik, cur^.key);
- end;
- CloseFile(plik);
- end;
- //--WCZYTUJE BAZE Z PLIKU---
- procedure otworzBaze();
- begin
- Assignfile(plik, 'recordy.dat');
- if FileExists('recordy.dat') then
- Reset(plik)
- else
- Rewrite(plik);
- usunBaze();
- for I := 1 to filesize(plik) do
- begin
- read(plik, elem);
- insertNode(elem);
- end;
- CloseFile(plik);
- end;
- //--WYŚWIETLA BAZE--
- procedure wyswietl();
- begin
- counter:=1;
- cur := first;
- Form1.Edit1.Text := ('');
- Form1.Edit2.Text := ('');
- Form1.Edit3.Text := ('');
- Form1.Edit4.Text := ('');
- //sprawdzenie ilości rekordów
- if first <> nil then
- begin
- while cur^.wsk <> nil do
- begin
- cur := cur^.wsk;
- counter:=counter+1;
- end;
- end;
- //wyświetlanie w StringGridzie
- with Form1.StringGrid1 do
- begin
- RowCount:=counter+1;
- end;
- counter:=1;
- cur := first;
- if first <> nil then
- begin
- with Form1.StringGrid1 do
- begin
- while cur^.wsk <> nil do
- begin
- Cells[0,counter]:=cur^.key.tytul;
- Cells[1,counter]:=IntToStr(cur^.key.ocena);
- Cells[2,counter]:=IntToStr(cur^.key.odcinki);
- Cells[3,counter]:=IntToStr(cur^.key.sezony);
- cur := cur^.wsk;
- counter:=counter+1;
- end;
- Cells[0,counter]:=cur^.key.tytul;
- Cells[1,counter]:=IntToStr(cur^.key.ocena);
- Cells[2,counter]:=IntToStr(cur^.key.odcinki);
- Cells[3,counter]:=IntToStr(cur^.key.sezony);
- end;
- end;
- end;
- //---USUWA SERIAL Z BAZY---
- procedure usunSerial();
- var licznik : Integer;
- begin
- new(cur);
- licznik:=0;
- prev := first;
- cur := prev^.wsk;
- if kluczUsuwania>2 then
- begin
- while licznik<>(kluczUsuwania-3) do
- begin
- prev:=prev^.wsk;
- cur:=prev^.wsk;
- licznik:=licznik+1;
- end;
- prev^.wsk:=cur^.wsk;
- dispose(cur);
- end
- else if first^.wsk<>nil then //usuwamy pierwszy ale reszte zostawiamy
- begin
- first:=first^.wsk;
- end
- else
- begin
- usunBaze();
- with Form1.StringGrid1 do
- begin
- Form1.StringGrid1.rows[1].Clear;
- end;
- end;
- licznik:=0;
- end;
- //---WYSZUKIWANIE PO TYTULE---
- procedure wyszukajPoTytule();
- begin
- with Form1.StringGrid1 do
- begin
- for I := 1 to counter do
- begin
- if (SameText(Cells[0,I],szukanyTytul)=TRUE) then //znalazło
- begin
- kluczyk:=1;
- Form1.Edit1.Text:=Cells[0,I];
- Form1.Edit2.Text:=Cells[1,I];
- Form1.Edit3.Text:=Cells[2,I];
- Form1.Edit4.Text:=Cells[3,I];
- zmiennaEdytowana:=I;
- break;
- end;
- end;
- if (SameText(Cells[0,I],szukanyTytul)=FALSE) then //nie znalazło
- begin
- showMessage('Nie udało się odnaleźć tego serialu.');
- kluczyk:=0;
- end;
- end;
- end;
- //----NADAWANIE ID----
- procedure nadajId();
- begin
- for I := 0 to counter do
- begin
- with Form1.StringGrid1 do
- begin
- Cells[4,I]:= IntToStr(id);
- id:=id+1;
- end;
- end;
- end;
- //--PROCEDURA DO SORTOWANIA--
- procedure sortuj();
- var
- title, mark, episodes, seasons, fbi : string[50];
- licznik:Integer;
- begin
- licznik:=1;
- if wskaznikSortowania<>0 then
- begin
- with Form1.StringGrid1 do
- begin
- while licznik<>0 do
- begin
- licznik:=0;
- for I := 1 to counter-1 do
- begin
- if StrToInt(Cells[wskaznikSortowania,I])>StrToInt(Cells[wskaznikSortowania,I+1]) then //sortowanie liczbowe
- begin
- title:=Cells[0,I];
- Cells[0,I]:=Cells[0,I+1];
- Cells[0,I+1]:=title;
- mark:=Cells[1,I];
- Cells[1,I]:=Cells[1,I+1];
- Cells[1,I+1]:=mark;
- episodes:=Cells[2,I];
- Cells[2,I]:=Cells[2,I+1];
- Cells[2,I+1]:=episodes;
- seasons:=Cells[3,I];
- Cells[3,I]:=Cells[3,I+1];
- Cells[3,I+1]:=seasons;
- fbi:=Cells[4,I];
- Cells[4,I]:=Cells[4,I+1];
- Cells[4,I+1]:=fbi;
- licznik:=licznik+1;
- end;
- end;
- end;
- end;
- end
- else
- begin
- with Form1.StringGrid1 do
- begin
- while licznik<>0 do
- begin
- licznik:=0;
- for I := 1 to counter-1 do
- begin
- if Cells[wskaznikSortowania,I]>Cells[wskaznikSortowania,I+1] then //sorotwanie alfabetyczne
- begin
- title:=Cells[0,I];
- Cells[0,I]:=Cells[0,I+1];
- Cells[0,I+1]:=title;
- mark:=Cells[1,I];
- Cells[1,I]:=Cells[1,I+1];
- Cells[1,I+1]:=mark;
- episodes:=Cells[2,I];
- Cells[2,I]:=Cells[2,I+1];
- Cells[2,I+1]:=episodes;
- seasons:=Cells[3,I];
- Cells[3,I]:=Cells[3,I+1];
- Cells[3,I+1]:=seasons;
- fbi:=Cells[4,I];
- Cells[4,I]:=Cells[4,I+1];
- Cells[4,I+1]:=fbi;
- licznik:=licznik+1;
- end;
- end;
- end;
- end;
- end;
- end;
- //---------------------------------------------------------------PRZYCISKI----------------------------------------------------
- //---PRZYCISK DO WYSZUKANIA PO TYTULE---
- procedure TForm1.Button4Click(Sender: TObject);
- begin //zmienna Edytowana zapamiętuje wiersz danego serialu
- szukanyTytul:=Edit5.Text;
- wyszukajPoTytule();
- if kluczyk=1 then
- begin
- with StringGrid1 do
- begin
- kluczUsuwania:=StrToInt(Cells[4,zmiennaEdytowana]);
- end;
- Button3.Show;
- Button2.Show;
- dziuba:=dziuba+1;
- Button1.Caption:='Naciśnij jeśli chcesz dodawać element';
- StringGrid1.Row:=zmiennaEdytowana;
- StringGrid1.Col:=0;
- end;
- Edit5.Text:='';
- end;
- //---PRZYCISK DO DODANIA REKORDU DO LISTY---
- procedure TForm1.Button1Click(Sender: TObject);
- var sprawdzacz:Integer;
- begin
- sprawdzacz:=1;
- if dziuba=0 then //NORMALNE DODAWANIE
- begin
- a.ocena := StrToInt(Edit2.Text);
- a.odcinki := StrToInt(Edit3.Text);
- a.sezony := StrToInt(Edit4.Text);
- if ((a.ocena>10) or (a.ocena<0)) then
- begin
- ShowMessage('Ocena ma się mieścić między 0 a 10');
- Edit2.Text:='';
- end else if a.sezony<1 then
- begin
- ShowMessage('Wprowadź dodatnią liczbę sezonów!');
- Edit4.Text:='';
- end else if a.odcinki<1 then
- begin
- ShowMessage('Wprowadź dodatnią liczbę odcinków!');
- Edit3.Text:='';
- end else
- begin
- a.tytul := Edit1.Text;
- with StringGrid1 do
- begin
- for I := 1 to id do
- begin
- if (SameText(Cells[0,I],a.tytul)=TRUE) then
- begin
- sprawdzacz:=0;
- break
- end;
- end;
- end;
- if sprawdzacz=1 then //normalne dodawanie
- begin
- wskaznikSortowania:=1;
- id:=1;
- sortuj();
- insertNode(a);
- wyswietl();
- nadajId();
- zapisz();
- wskaznikSortowania:=wskaznikSortowania2;
- sortuj();
- end else //nie dodaje bo serial jest w bazie
- begin
- showmessage('Dany serial jest już w bazie!');
- StringGrid1.Row:=I;
- Edit1.Text := '';
- Edit2.Text := '';
- Edit3.Text := '';
- Edit4.Text := '';
- end;
- end;
- end else //Naciśnij jeśli chcesz dodać element
- begin
- Button1.Caption:='Dodaj';
- dziuba:=0;
- Button3.Hide;
- Button2.Hide;
- Edit1.Text := '';
- Edit2.Text := '';
- Edit3.Text := '';
- Edit4.Text := '';
- end;
- end;
- //---PRZYCISK 2 DO USUNIĘCIA DANEGO SERIALU---
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- usunSerial();
- zapisz();
- wyswietl();
- Button1.Caption:='Dodaj do bazy!';
- dziuba:=0;
- Button3.Hide;
- Button2.Hide;
- wskaznikSortowania:=wskaznikSortowania2;
- sortuj();
- StringGrid1.Row:=0;
- end;
- //--PRZYCISK DO ROZPOCZĘCIA EDYCJI--
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- if (Button1.Caption='Naciśnij jeśli chcesz dodawać element') OR (kluczyk=1) then
- begin
- a.ocena := StrToInt(Edit2.Text);
- a.tytul := Edit1.Text;
- a.odcinki := StrToInt(Edit3.Text);
- a.sezony := StrToInt(Edit4.Text);
- usunSerial();
- wskaznikSortowania:=1;
- id:=1;
- sortuj();
- insertNode(a);
- wyswietl();
- nadajId();
- zapisz();
- Button1.Caption:='Dodaj do bazy!';
- dziuba:=0;
- Button3.Hide;
- Button2.Hide;
- wskaznikSortowania:=wskaznikSortowania2;
- sortuj();
- end;
- end;
- //--PRZYCISK DO USUNIĘCIA CAŁEJ LISTY---
- procedure TForm1.Button5Click(Sender: TObject);
- begin
- usunBaze();
- zapisz();
- wyswietl();
- with StringGrid1 do
- begin
- Cells[0,1]:=' ';
- Cells[1,1]:=' ';
- Cells[2,1]:=' ';
- Cells[3,1]:=' ';
- end;
- end;
- //---------------------------------------------------------------INNE PROCEDURKI-----------------------------------------------------
- //---PROCEDURA DO OTWORZENIA LISTY PRZY WŁĄCZENIU PROGRAMU---
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- otworzBaze();
- with StringGrid1 do
- begin
- Cells[0,0]:='Tytuł:';
- Cells[1,0]:='Ocena:';
- Cells[2,0]:='Odcinki:';
- Cells[3,0]:='Sezony:';
- end;
- StringGrid1.ColWidths[0] := 256;
- wyswietl();
- nadajId();
- Button3.Hide;
- Button2.Hide;
- end;
- //---UZYSKIWANIE ADRESU KOMÓRKI---
- procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- StringGrid1.MouseToCell(X,Y,Kolumna,Wiersz);
- with StringGrid1 do
- begin
- kluczUsuwania:=StrToInt(Cells[4,wiersz]);
- end;
- //--SORTOWANIE--
- if ((Kolumna=0) AND (Wiersz=0)) then
- begin //-- ALFABETYCZNE --
- wskaznikSortowania:=0;
- wskaznikSortowania2:=0;
- sortuj();
- end
- else if ((Kolumna=1) AND (Wiersz=0)) then
- begin //-- OCENAMI --
- wskaznikSortowania2:=1;
- wskaznikSortowania:=1;
- sortuj();
- end //-- ODCINKAMI --
- else if ((Kolumna=2) AND (Wiersz=0)) then
- begin
- wskaznikSortowania:=2;
- wskaznikSortowania2:=2;
- sortuj();
- end else if ((Kolumna=3) AND (Wiersz=0)) then
- begin // -- SEZONAMI --
- wskaznikSortowania2:=3;
- wskaznikSortowania:=3;
- sortuj();
- end;
- if ((wiersz<>0) AND (wiersz<>-1)) then
- begin
- begin // POKAZAĆ PRZYCISK
- Button3.Show;
- Button2.Show;
- dziuba:=dziuba+1;
- Button1.Caption:='Naciśnij jeśli chcesz dodawać element';
- Edit1.Text := Form1.StringGrid1.Cells[0, wiersz];
- Edit2.Text := Form1.StringGrid1.Cells[1, wiersz];
- Edit3.Text := Form1.StringGrid1.Cells[2, wiersz];
- Edit4.Text := Form1.StringGrid1.Cells[3, wiersz];
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement