Advertisement
m4ly

Kolos PPK

Jan 27th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.70 KB | None | 0 0
  1. {Autor: Dawid Mocek}
  2.  
  3. Kolos PPK
  4.  
  5. 1)
  6. Przeczytać na wikipedii ;-)
  7. { czyli w skrocie:
  8.  nie.jest to spowodowane tym że komputer posługuje ssystem binarnym 0 i 1 wiec nie jest w stanie dokladnie okreslić liczby typu real. Liczba R1 moze np wynosc 1.3000000 natomiast R2 moze wynosic 1.299999   lub 1.30000001 a one nie są sobie równe
  9. }
  10. 2) procedure wypelnj(var: tablica)
  11. var
  12.     i :integer;
  13.     idx : integer;
  14. begin
  15.      idx := 0;
  16.      for i:=2 to N do begin
  17.        if i mod 2 = 0 then begin
  18.          tablica[idx] := i;
  19.          idx := idx + 1;
  20.        end;
  21.  
  22.      end;
  23. end;
  24.  
  25. 3)
  26. var
  27.   i:integer;
  28.  
  29. begin
  30.  
  31.  
  32.       readln(i);
  33. repeat
  34.      writeln(i);
  35.       i:= i+1;
  36. until(i > 10) ;
  37. readln;
  38. end.
  39.  
  40. { Poprawione: musi zostac dodany warunek if przed petla poniewaz reapet until wykoona petle conajmniej raz }
  41.  
  42. 4)
  43. type
  44.   tPoryRoku = (Wiosna, Lato, Jesien, Zima );
  45.  
  46. 5)
  47. for i:=0 to 100 do begin
  48.     writeln(i);
  49.   end;
  50. { poprawione:
  51. http://borlandpascal.wikia.com/wiki/Script:Is_prime
  52. }
  53. 6)
  54. var  tablica:array[-5..131] of integer;
  55.  
  56. 7)
  57. type
  58.   ptrtablica = ^real;
  59. var
  60.     tablica: ptrtablica;
  61.     rozmiar: integer;
  62. begin
  63.       rozmiar :=10;
  64.  
  65.       getmem(tablica, sizeof(real) * rozmiar);
  66.       freemem(tablica, sizeof(real) *rozmiar);
  67.       tablica := nil;
  68.  
  69.       readln;
  70. end.
  71.  
  72. LUB
  73.  
  74. var
  75.     tablica: array of real;
  76.     rozmiar: integer;
  77. begin
  78. {
  79. http://wiki.freepascal.org/Dynamic_array
  80. Assigning nil to a dynamic array variable automatically frees the memory where the pointer pointed to. It's identical to SetLength(MyVariable, 0).
  81.  
  82. }
  83.       rozmiar :=10;
  84.       SetLength(tablica, rozmiar);
  85.       tablica:=nil;
  86.  
  87. end.
  88.  
  89. 8)
  90. type
  91.   osoba =record
  92.     Imie: string[15];
  93.     Nazwisko: string[25];
  94.  
  95.   end;
  96.  
  97. var
  98.     zm: osoba;
  99.     plik: file;
  100. begin
  101.      zm:= InicjalizujZm();
  102.  
  103.       assign(plik, 'plik.a');
  104.       reset(plik, 1);
  105.       write(plik, zm);
  106.       close(plik);
  107.  
  108. end.
  109. { Poprawione
  110.     zm:= InicjalizujZm();
  111.  
  112.      // to nie jest istotnie wazne jaka rozszerzenie ma plik binarny
  113.       assign(plik, 'D:\plik.a');
  114.       reset(plik, 1);
  115.       BlockWrite(plik, zm.Nazwisko, sizeof(osoba.Nazwisko));
  116.       BlockWrite(plik, zm.Imie, sizeof(osoba.Imie));
  117.       close(plik);
  118.  
  119.       readln;          
  120. }
  121. 9)
  122. var
  123.   a:real;
  124.   b:^real;
  125.  
  126.  
  127. ########################## LISTA ########################################
  128. {
  129.  Author: Dawid Mocek
  130. }
  131.  
  132. type
  133.   PElem = ^TElem;
  134.   TElem = record
  135.     d: integer;
  136.     next: PElem;
  137.   end;
  138.  
  139. {Sprawdza czy element nalezy  do zbioru}
  140. function sprawdz(Z:PElem; V:integer ): Boolean;
  141. var
  142.  wynik : boolean;
  143. begin
  144.      wynik := false;
  145.    while Z <> nil do begin
  146.      if V = Z^.d then begin
  147.      wynik := true;
  148.      end;
  149.     Z := Z^.next;
  150.   end;
  151.  
  152. sprawdz := wynik;
  153.  
  154. end;
  155.  
  156. {
  157. wstawienie elementu do zbioru - ten var tu jest POTRZEBNY - jak mam zmieniać cos
  158. w liscie jak nie dostaje jej adresu tylko beznadziejną kopie(no chyba że to globalna zmienna a nie stworzona na heap`ie) ?
  159. }
  160. procedure wstaw(var Z:PElem; V:Integer);
  161. var
  162.  nowy : PElem;
  163. begin
  164.      // Szukamy elementu V w zbiorze - jesli nie znalezlismy takiego samego dodajemy nowy element
  165.      if sprawdz(Z, V) <> true  then begin
  166.        new(nowy);
  167.        nowy^.d := V;
  168.        nowy^.next:= Z;
  169.        Z := nowy;
  170.      end;
  171. end;
  172.  
  173.  
  174. function suma(Z1:PElem;  Z2: PElem) : PElem;
  175. var
  176.     nowa_lista : PElem;
  177. begin
  178.    nowa_lista := nil;
  179.  
  180.   // iterujemy po pierwszej liscie
  181.   while Z1 <> nil do begin
  182.     // dodajmy do nowej
  183.      wstaw(nowa_lista, Z1^.d);
  184.      Z1 := Z1^.next;
  185.   end;
  186.  
  187.   // iterujemy po drugiej liscie
  188.   while Z2 <> nil do begin
  189.     // dodajemy no nowej
  190.     wstaw(nowa_lista, Z2^.d);
  191.     Z2 := Z2^.next;
  192.   end;
  193.  
  194.   // zwracamy nowa liste
  195.   suma := nowa_lista;
  196.  
  197. end;
  198.  
  199. { czyli z zbioru Z1 wypierdzielamy wszystko co jest takiego smaego w zbiorze Z2 }
  200. function roznica(Z1, Z2: PElem): PElem;
  201. var
  202.     nowa_lista: PElem;
  203. begin
  204.   nowa_lista := nil;
  205.   while (Z1 <> nil)  do begin
  206.         if sprawdz(Z2, Z1^.d) = false then begin
  207.            wstaw(nowa_lista, Z1^.d);
  208.          end
  209.          else begin
  210.               while(Z2 <> nil ) and (Z2^.d <>  Z1^.d) do begin
  211.                   wstaw(nowa_lista, Z2^.d);
  212.                   Z2 := Z2^.next;
  213.               end;
  214.         end;
  215.  
  216.   Z1 := Z1^.next;
  217.   end;
  218.  
  219.   roznica:=nowa_lista;
  220.  
  221. end;
  222.  
  223. { Nie wiem o co chodzi w przecieciu ?
  224. function przeciecie(Z1, Z2: PElem):PElem;
  225. begin
  226. end;
  227. }
  228.  
  229. procedure zniszczliste(var Z:PElem);
  230. var
  231.     current : PElem;
  232. begin
  233.    current := Z;
  234.     while current <> nil  do begin
  235.     current := current^.next;
  236.     Dispose(Z);
  237.     Z := current;
  238.  end;
  239. end;
  240.  
  241. procedure drukujliste(Z:PElem);
  242. begin
  243.   if Z <> nil then begin
  244.      while Z <> nil do begin
  245.          writeln(Z^.d);
  246.          Z := Z^.next;
  247.       end;
  248.    end;
  249. end;
  250.  
  251. var
  252.  elementy1 : PElem;
  253.  elementy2 : PElem;
  254.  elementy12 : PElem;
  255.  roznica_zbiorow : PElem;
  256. begin
  257.      elementy1 := nil;
  258.      elementy2 := nil;
  259.      elementy12 := nil;
  260.      roznica_zbiorow := nil;
  261.  
  262.      wstaw(elementy1, 100);
  263.      wstaw(elementy1, 102);
  264.      wstaw(elementy1, 104);
  265.  
  266.  
  267.      wstaw(elementy2, 104); // to nie powinno  znajdowac w nowym zbiorze - roznicy
  268.      wstaw(elementy2, 253);
  269.      wstaw(elementy2, 254);
  270.  
  271.      writeln('lista 1:');
  272.      drukujliste(elementy1);
  273.  
  274.      writeln('lista 2:');
  275.      drukujliste(elementy2);
  276.  
  277.      elementy12 := suma(elementy1, elementy2);
  278.      writeln('lista 1 i 2:');
  279.      drukujliste(elementy12);
  280.  
  281.      writeln('lista 1 - lista 2:');
  282.      roznica_zbiorow := roznica(elementy1, elementy2);
  283.      drukujliste(roznica_zbiorow);
  284.  
  285.      zniszczliste(elementy1);
  286.      zniszczliste(elementy2);
  287.      zniszczliste(elementy12);
  288.      zniszczliste(roznica_zbiorow);
  289.  
  290. readln;
  291. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement