Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 13.33 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids,
  8.   Vcl.Imaging.jpeg, Vcl.ExtCtrls;
  9.  
  10.   type
  11.   plist   = ^element;
  12.   TSerial  = record
  13.     tytul    : String[50];
  14.     ocena    : Integer;
  15.     odcinki  : Integer;
  16.     sezony   : Integer;
  17.   end;
  18.   element = record
  19.     key : TSerial;
  20.     wsk : plist;
  21.   end;
  22.  
  23. type
  24.   TForm1 = class(TForm)
  25.     Edit1: TEdit;
  26.     Edit2: TEdit;
  27.     Edit3: TEdit;
  28.     Label1: TLabel;
  29.     Label2: TLabel;
  30.     Label3: TLabel;
  31.     Button1: TButton;
  32.     Button5: TButton;
  33.     Edit4: TEdit;
  34.     Label4: TLabel;
  35.     StringGrid1: TStringGrid;
  36.     Button2: TButton;
  37.     Button3: TButton;
  38.     Edit5: TEdit;
  39.     Label5: TLabel;
  40.     Button4: TButton;
  41.     Image1: TImage;
  42.     procedure Button1Click(Sender: TObject);
  43.     procedure FormCreate(Sender: TObject);
  44.     procedure Button5Click(Sender: TObject);
  45.     procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  46.       Shift: TShiftState; X, Y: Integer);
  47.     procedure Button2Click(Sender: TObject);
  48.     procedure Button3Click(Sender: TObject);
  49.     procedure Button4Click(Sender: TObject);
  50.  
  51.   private
  52.     { Private declarations }
  53.   public
  54.     { Public declarations }
  55.   end;
  56.  
  57. var
  58.   Form1: TForm1;
  59.   first: plist;
  60.   list_length: Integer = 0;
  61.   I: Integer;
  62.   a : TSerial;
  63.   Kolumna : Integer = -1;
  64.   Wiersz : Integer = -1;
  65.   counter:Integer;
  66.   prev, cur: plist;
  67.   plik : file of TSerial;
  68.   elem : TSerial;
  69.   szukanyTytul : string[50];               //sprawdza jaki tytuł chcemy wyszukać
  70.   wskaznikSortowania : Integer;            //sprawdza wg. czego chcemy posortować
  71.   id : Integer = 1;                        //nadaje id
  72.   kluczUsuwania : Integer;                 //nie mam pojecia
  73.   dziuba : Integer = 0;                    //do sprawdzania czy edytujemy czy dodajemy
  74.   wskaznikSortowania2: Integer = 1;
  75.   kluczyk: Integer =2;
  76.   zmiennaEdytowana:Integer;
  77.  
  78.  
  79. implementation
  80.  
  81. {$R *.dfm}
  82.  
  83. //---------------------------------------------------------------PROCEDURKI FUNKCYJNE-----------------------------------------------------
  84.  
  85. // ---INSERTNODE---
  86. procedure insertNode(a:TSerial);
  87. begin
  88. cur := first;
  89. if (first = nil) then
  90.   begin
  91.   New(first);
  92.   first^.key := a;
  93.   first^.wsk := nil;
  94.   end
  95. else if (a.ocena) <= (first^.key.ocena) then
  96.   begin
  97.   New(cur);
  98.   cur^.key := a;
  99.   cur^.wsk := first;
  100.   first := cur;
  101.   end
  102. else
  103.   begin
  104.   while (cur^.wsk <> nil) and (a.ocena > cur^.wsk^.key.ocena) do
  105.     cur := cur^.wsk;
  106.   prev := cur;
  107.   New(cur);
  108.   cur^.key := a;
  109.   cur^.wsk := prev^.wsk;
  110.   prev^.wsk := cur;
  111.   end;
  112. end;
  113.  
  114. // --USUWA CAŁĄ BAZĘ--
  115. procedure usunBaze();
  116.  
  117. begin
  118.   first := nil;
  119.   Form1.Edit1.Text := ('');
  120.   Form1.Edit2.Text := ('');
  121.   Form1.Edit3.Text := ('');
  122.   Form1.Edit4.Text := ('');
  123. end;
  124.  
  125. //---ZAPISUJE BAZE DO PLIKU--
  126. procedure zapisz();
  127. begin
  128.   cur := first;
  129.   AssignFile(plik, 'recordy.dat');
  130.   Rewrite(plik);
  131.   if first <> nil then
  132.   begin
  133.   while (cur^.wsk <> nil) do
  134.     begin
  135.     Write(plik, cur^.key);
  136.     cur := cur^.wsk;
  137.     end;
  138.  
  139.   Write(plik, cur^.key);
  140.   end;
  141.  
  142.   CloseFile(plik);
  143. end;
  144.  
  145. //--WCZYTUJE BAZE Z PLIKU---
  146. procedure otworzBaze();
  147.  
  148. begin
  149.   Assignfile(plik, 'recordy.dat');
  150.   if FileExists('recordy.dat') then
  151.     Reset(plik)
  152.   else
  153.     Rewrite(plik);
  154.  
  155.   usunBaze();
  156.   for I := 1 to filesize(plik) do
  157.   begin
  158.   read(plik, elem);
  159.   insertNode(elem);
  160.   end;
  161.   CloseFile(plik);
  162. end;
  163.  
  164. //--WYŚWIETLA BAZE--
  165. procedure wyswietl();
  166.  
  167. begin
  168.   counter:=1;
  169.   cur := first;
  170.   Form1.Edit1.Text := ('');
  171.   Form1.Edit2.Text := ('');
  172.   Form1.Edit3.Text := ('');
  173.   Form1.Edit4.Text := ('');
  174.  
  175.   //sprawdzenie ilości rekordów
  176.     if first <> nil then
  177.     begin
  178.     while cur^.wsk <> nil do
  179.       begin
  180.       cur := cur^.wsk;
  181.       counter:=counter+1;
  182.       end;
  183.     end;
  184.  
  185. //wyświetlanie w StringGridzie
  186.   with Form1.StringGrid1 do
  187.   begin
  188.      RowCount:=counter+1;
  189.   end;
  190.  
  191.   counter:=1;
  192.   cur := first;
  193.   if first <> nil then
  194.     begin
  195.     with Form1.StringGrid1 do
  196.     begin
  197.     while cur^.wsk <> nil do
  198.       begin
  199.       Cells[0,counter]:=cur^.key.tytul;
  200.       Cells[1,counter]:=IntToStr(cur^.key.ocena);
  201.       Cells[2,counter]:=IntToStr(cur^.key.odcinki);
  202.       Cells[3,counter]:=IntToStr(cur^.key.sezony);
  203.       cur := cur^.wsk;
  204.       counter:=counter+1;
  205.       end;
  206.       Cells[0,counter]:=cur^.key.tytul;
  207.       Cells[1,counter]:=IntToStr(cur^.key.ocena);
  208.       Cells[2,counter]:=IntToStr(cur^.key.odcinki);
  209.       Cells[3,counter]:=IntToStr(cur^.key.sezony);
  210.     end;
  211.     end;
  212.     end;
  213.  
  214. //---USUWA SERIAL Z BAZY---
  215. procedure usunSerial();
  216. var licznik : Integer;
  217. begin
  218. new(cur);
  219. licznik:=0;
  220. prev := first;
  221. cur  := prev^.wsk;
  222.  
  223. if kluczUsuwania>2 then
  224. begin
  225.   while licznik<>(kluczUsuwania-3) do
  226.   begin
  227.     prev:=prev^.wsk;
  228.     cur:=prev^.wsk;
  229.     licznik:=licznik+1;
  230.   end;
  231.  prev^.wsk:=cur^.wsk;
  232.  dispose(cur);
  233. end
  234. else if first^.wsk<>nil then  //usuwamy pierwszy ale reszte zostawiamy
  235.      begin
  236.          first:=first^.wsk;
  237.      end
  238. else
  239. begin
  240.   usunBaze();
  241.    with Form1.StringGrid1 do
  242.   begin
  243.      Form1.StringGrid1.rows[1].Clear;
  244.   end;
  245. end;
  246. licznik:=0;
  247. end;
  248.  
  249. //---WYSZUKIWANIE PO TYTULE---
  250. procedure wyszukajPoTytule();
  251. begin
  252.  
  253. with Form1.StringGrid1 do
  254. begin
  255. for I := 1 to counter do
  256.   begin
  257.     if (SameText(Cells[0,I],szukanyTytul)=TRUE) then    //znalazło
  258.     begin
  259.         kluczyk:=1;
  260.         Form1.Edit1.Text:=Cells[0,I];
  261.         Form1.Edit2.Text:=Cells[1,I];
  262.         Form1.Edit3.Text:=Cells[2,I];
  263.         Form1.Edit4.Text:=Cells[3,I];
  264.         zmiennaEdytowana:=I;
  265.      break;
  266.     end;
  267.   end;
  268.  
  269.   if (SameText(Cells[0,I],szukanyTytul)=FALSE) then        //nie znalazło
  270.     begin
  271.     showMessage('Nie udało się odnaleźć tego serialu.');
  272.     kluczyk:=0;
  273.     end;
  274.    end;
  275. end;
  276.  
  277. //----NADAWANIE ID----
  278. procedure nadajId();
  279. begin
  280.   for I := 0 to counter do
  281.     begin
  282.       with Form1.StringGrid1 do
  283.       begin
  284.         Cells[4,I]:= IntToStr(id);
  285.         id:=id+1;
  286.       end;
  287.     end;
  288. end;
  289.  
  290. //--PROCEDURA DO SORTOWANIA--
  291. procedure sortuj();
  292. var
  293. title, mark, episodes, seasons, fbi : string[50];
  294. licznik:Integer;
  295. begin
  296. licznik:=1;
  297. if wskaznikSortowania<>0 then
  298. begin
  299.  
  300.         with Form1.StringGrid1 do
  301.   begin
  302.     while licznik<>0 do
  303.     begin
  304.       licznik:=0;
  305.       for I := 1 to counter-1 do
  306.       begin
  307.         if StrToInt(Cells[wskaznikSortowania,I])>StrToInt(Cells[wskaznikSortowania,I+1]) then       //sortowanie liczbowe
  308.         begin
  309.            title:=Cells[0,I];
  310.            Cells[0,I]:=Cells[0,I+1];
  311.            Cells[0,I+1]:=title;
  312.  
  313.            mark:=Cells[1,I];
  314.            Cells[1,I]:=Cells[1,I+1];
  315.            Cells[1,I+1]:=mark;
  316.  
  317.            episodes:=Cells[2,I];
  318.            Cells[2,I]:=Cells[2,I+1];
  319.            Cells[2,I+1]:=episodes;
  320.  
  321.            seasons:=Cells[3,I];
  322.            Cells[3,I]:=Cells[3,I+1];
  323.            Cells[3,I+1]:=seasons;
  324.  
  325.            fbi:=Cells[4,I];
  326.            Cells[4,I]:=Cells[4,I+1];
  327.            Cells[4,I+1]:=fbi;
  328.  
  329.            licznik:=licznik+1;
  330.         end;
  331.       end;
  332.     end;
  333.   end;
  334. end
  335. else
  336. begin
  337.          with Form1.StringGrid1 do
  338.   begin
  339.     while licznik<>0 do
  340.     begin
  341.       licznik:=0;
  342.       for I := 1 to counter-1 do
  343.       begin
  344.         if Cells[wskaznikSortowania,I]>Cells[wskaznikSortowania,I+1] then     //sorotwanie alfabetyczne
  345.         begin
  346.            title:=Cells[0,I];
  347.            Cells[0,I]:=Cells[0,I+1];
  348.            Cells[0,I+1]:=title;
  349.  
  350.            mark:=Cells[1,I];
  351.            Cells[1,I]:=Cells[1,I+1];
  352.            Cells[1,I+1]:=mark;
  353.  
  354.            episodes:=Cells[2,I];
  355.            Cells[2,I]:=Cells[2,I+1];
  356.            Cells[2,I+1]:=episodes;
  357.  
  358.            seasons:=Cells[3,I];
  359.            Cells[3,I]:=Cells[3,I+1];
  360.            Cells[3,I+1]:=seasons;
  361.  
  362.            fbi:=Cells[4,I];
  363.            Cells[4,I]:=Cells[4,I+1];
  364.            Cells[4,I+1]:=fbi;
  365.  
  366.            licznik:=licznik+1;
  367.         end;
  368.       end;
  369.     end;
  370.   end;
  371. end;
  372. end;
  373.  
  374. //---------------------------------------------------------------PRZYCISKI----------------------------------------------------
  375.  
  376. //---PRZYCISK DO WYSZUKANIA PO TYTULE---
  377. procedure TForm1.Button4Click(Sender: TObject);
  378. begin      //zmienna Edytowana zapamiętuje wiersz danego serialu
  379.   szukanyTytul:=Edit5.Text;
  380.   wyszukajPoTytule();
  381.  
  382.   if kluczyk=1 then
  383.   begin
  384.     with StringGrid1 do
  385.     begin
  386.     kluczUsuwania:=StrToInt(Cells[4,zmiennaEdytowana]);
  387.     end;
  388.    Button3.Show;
  389.    Button2.Show;
  390.    dziuba:=dziuba+1;
  391.    Button1.Caption:='Naciśnij jeśli chcesz dodawać element';
  392.    StringGrid1.Row:=zmiennaEdytowana;
  393.    StringGrid1.Col:=0;
  394.   end;
  395.  
  396.   Edit5.Text:='';
  397.  
  398. end;
  399.  
  400. //---PRZYCISK DO DODANIA REKORDU DO LISTY---
  401. procedure TForm1.Button1Click(Sender: TObject);
  402. var sprawdzacz:Integer;
  403. begin
  404. sprawdzacz:=1;
  405.   if dziuba=0 then //NORMALNE DODAWANIE
  406.   begin
  407.   a.ocena := StrToInt(Edit2.Text);
  408.   a.odcinki := StrToInt(Edit3.Text);
  409.   a.sezony := StrToInt(Edit4.Text);
  410.   if ((a.ocena>10) or (a.ocena<0)) then
  411.   begin
  412.    ShowMessage('Ocena ma się mieścić między 0 a 10');
  413.    Edit2.Text:='';
  414.   end else if a.sezony<1 then
  415.        begin
  416.         ShowMessage('Wprowadź dodatnią liczbę sezonów!');
  417.         Edit4.Text:='';
  418.        end else if a.odcinki<1 then
  419.                 begin
  420.                   ShowMessage('Wprowadź dodatnią liczbę odcinków!');
  421.                   Edit3.Text:='';
  422.                 end else
  423.   begin
  424.   a.tytul := Edit1.Text;
  425.   with StringGrid1 do
  426.   begin
  427.     for I := 1 to id do
  428.       begin
  429.         if (SameText(Cells[0,I],a.tytul)=TRUE) then
  430.         begin
  431.          sprawdzacz:=0;
  432.          break
  433.         end;
  434.       end;
  435.   end;
  436.  
  437.   if sprawdzacz=1 then    //normalne dodawanie
  438.    begin
  439.    wskaznikSortowania:=1;
  440.    id:=1;
  441.    sortuj();
  442.    insertNode(a);
  443.    wyswietl();
  444.    nadajId();
  445.    zapisz();
  446.    wskaznikSortowania:=wskaznikSortowania2;
  447.    sortuj();
  448.   end else           //nie dodaje bo serial jest w bazie
  449.    begin
  450.    showmessage('Dany serial jest już w bazie!');
  451.    StringGrid1.Row:=I;
  452.    Edit1.Text := '';
  453.    Edit2.Text := '';
  454.    Edit3.Text := '';
  455.    Edit4.Text := '';
  456.   end;
  457.   end;
  458.   end else         //Naciśnij jeśli chcesz dodać element
  459.   begin
  460.   Button1.Caption:='Dodaj';
  461.   dziuba:=0;
  462.   Button3.Hide;
  463.   Button2.Hide;
  464.    Edit1.Text := '';
  465.    Edit2.Text := '';
  466.    Edit3.Text := '';
  467.    Edit4.Text := '';
  468.   end;
  469.   end;
  470.  
  471. //---PRZYCISK 2  DO USUNIĘCIA DANEGO SERIALU---
  472. procedure TForm1.Button2Click(Sender: TObject);
  473. begin
  474. usunSerial();
  475. zapisz();
  476. wyswietl();
  477. Button1.Caption:='Dodaj do bazy!';
  478. dziuba:=0;
  479. Button3.Hide;
  480. Button2.Hide;
  481. wskaznikSortowania:=wskaznikSortowania2;
  482. sortuj();
  483. StringGrid1.Row:=0;
  484. end;
  485.  
  486. //--PRZYCISK DO ROZPOCZĘCIA EDYCJI--
  487. procedure TForm1.Button3Click(Sender: TObject);
  488. begin
  489. if (Button1.Caption='Naciśnij jeśli chcesz dodawać element') OR (kluczyk=1) then
  490. begin
  491. a.ocena := StrToInt(Edit2.Text);
  492. a.tytul := Edit1.Text;
  493. a.odcinki := StrToInt(Edit3.Text);
  494. a.sezony := StrToInt(Edit4.Text);
  495. usunSerial();
  496. wskaznikSortowania:=1;
  497. id:=1;
  498. sortuj();
  499. insertNode(a);
  500. wyswietl();
  501. nadajId();
  502. zapisz();
  503. Button1.Caption:='Dodaj do bazy!';
  504. dziuba:=0;
  505. Button3.Hide;
  506. Button2.Hide;
  507. wskaznikSortowania:=wskaznikSortowania2;
  508. sortuj();
  509. end;
  510. end;
  511.  
  512. //--PRZYCISK DO USUNIĘCIA CAŁEJ LISTY---
  513. procedure TForm1.Button5Click(Sender: TObject);
  514. begin
  515.     usunBaze();
  516.     zapisz();
  517.     wyswietl();
  518.     with StringGrid1 do
  519.   begin
  520.      Cells[0,1]:=' ';
  521.      Cells[1,1]:=' ';
  522.      Cells[2,1]:=' ';
  523.      Cells[3,1]:=' ';
  524.   end;
  525. end;
  526.  
  527. //---------------------------------------------------------------INNE PROCEDURKI-----------------------------------------------------
  528.  
  529. //---PROCEDURA DO OTWORZENIA LISTY PRZY WŁĄCZENIU PROGRAMU---
  530. procedure TForm1.FormCreate(Sender: TObject);
  531. begin
  532. otworzBaze();
  533.   with StringGrid1 do
  534.   begin
  535.      Cells[0,0]:='Tytuł:';
  536.      Cells[1,0]:='Ocena:';
  537.      Cells[2,0]:='Odcinki:';
  538.      Cells[3,0]:='Sezony:';
  539.   end;
  540. StringGrid1.ColWidths[0] := 256;
  541. wyswietl();
  542. nadajId();
  543. Button3.Hide;
  544. Button2.Hide;
  545. end;
  546.  
  547. //---UZYSKIWANIE ADRESU KOMÓRKI---
  548. procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  549.   Shift: TShiftState; X, Y: Integer);
  550. begin
  551. StringGrid1.MouseToCell(X,Y,Kolumna,Wiersz);
  552. with StringGrid1 do
  553. begin
  554.   kluczUsuwania:=StrToInt(Cells[4,wiersz]);
  555. end;
  556.  
  557. //--SORTOWANIE--
  558. if ((Kolumna=0) AND (Wiersz=0)) then
  559. begin   //-- ALFABETYCZNE --
  560.    wskaznikSortowania:=0;
  561.    wskaznikSortowania2:=0;
  562.    sortuj();
  563. end
  564. else if ((Kolumna=1) AND (Wiersz=0)) then
  565. begin   //-- OCENAMI --
  566. wskaznikSortowania2:=1;
  567.    wskaznikSortowania:=1;
  568.    sortuj();
  569. end   //-- ODCINKAMI --
  570. else if ((Kolumna=2) AND (Wiersz=0)) then
  571. begin
  572.    wskaznikSortowania:=2;
  573.    wskaznikSortowania2:=2;
  574.    sortuj();
  575. end else if ((Kolumna=3) AND (Wiersz=0)) then
  576. begin   // -- SEZONAMI --
  577. wskaznikSortowania2:=3;
  578.   wskaznikSortowania:=3;
  579.    sortuj();
  580. end;
  581.  
  582. if ((wiersz<>0) AND (wiersz<>-1)) then
  583. begin
  584.  begin                // POKAZAĆ PRZYCISK
  585.  Button3.Show;
  586.  Button2.Show;
  587.  dziuba:=dziuba+1;
  588.  Button1.Caption:='Naciśnij jeśli chcesz dodawać element';
  589.  Edit1.Text := Form1.StringGrid1.Cells[0, wiersz];
  590.  Edit2.Text := Form1.StringGrid1.Cells[1, wiersz];
  591.  Edit3.Text := Form1.StringGrid1.Cells[2, wiersz];
  592.  Edit4.Text := Form1.StringGrid1.Cells[3, wiersz];
  593.  end;
  594. end;
  595.  
  596. end;
  597. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement