Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.82 KB | None | 0 0
  1.  
  2. program begin12;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7.   SysUtils;
  8.  
  9. const n = 15;
  10.  
  11. type tzap = Record
  12.    p1:Integer;
  13.    p2:string[15];
  14.    p3:Boolean;
  15. end;
  16.  
  17. type tmas = array [1..n] of tzap;
  18.  
  19.  
  20.  
  21. procedure input_mas(var a:tmas);
  22.  var i:integer; k:string;
  23. begin
  24.   Randomize;
  25.   for i:=1 to n do
  26.    with a[i] do
  27.     begin
  28.        p1:=Random(201);
  29.        Str(i,k);
  30.        p2:='my_test' + k;
  31.        p3:=false;
  32.     end;
  33. end;
  34.  
  35. procedure output_mas(var a:tmas);
  36.  var i:integer;
  37. begin
  38.   for i:=1 to n do
  39.   with a[i] do
  40.    begin
  41.     write(p1:5,' ');
  42.     write(p2:18,' ');
  43.     write(p3,' ');
  44.     writeln;
  45.    end;
  46. end;
  47.  
  48. procedure sortpsecond (var a:tmas);
  49. var i,j:integer; tmp:tzap;
  50.  
  51. begin
  52.   for i:=1 to n-1 do
  53.    begin
  54.     for j:=1 to n-i do
  55.      begin
  56.        if a[j].p2>a[j+1].p2 then
  57.         begin
  58.           tmp:=a[j];
  59.           a[j]:=a[j+1];
  60.           a[j+1]:=tmp;
  61.         end;
  62.      end;
  63.    end;
  64. end;
  65.  
  66. procedure binarysearch (var a:tmas);
  67. var s1:string; i,m,j,r:integer;
  68. begin
  69.  readln(s1);
  70.  m:=n div 2;
  71.  i:=1;
  72.  j:=m;
  73.  r:=n;
  74.   if a[j].p2=s1 then
  75.       a[j].p3:=True;
  76.  while (s1<>a[j].p2)    do
  77.   begin
  78.     a[j].p3:=True;
  79.     if a[j].p2<s1 then
  80.      begin
  81.       i:=j+1;
  82.       j:=(i+r) div 2 ;
  83.      end;
  84.     if a[j].p2>s1 then
  85.      begin
  86.       r:=j-1;
  87.       j:=(i+r) div 2;
  88.      end;
  89.     if a[j].p2=s1 then
  90.      a[j].p3:=true;
  91.   end;
  92. end;
  93.  
  94. procedure truel (var a:tmas);
  95. var j,count:integer;
  96. begin
  97.   for j:=1 to n do
  98.   if a[j].p3=True then
  99.    inc(count);
  100.   Writeln('Number of true elements = ' , count);
  101. end;
  102.  
  103. procedure falseel(var a:tmas);
  104. var j:integer;
  105. begin
  106.   for j:=1 to n do
  107.    a[j].p3:=false;
  108. end;
  109.  
  110. procedure binarysearch2 (var a:tmas);
  111. var  i,m,j,num,r,s:integer;
  112. begin
  113.  read(num);
  114.  m:=n div 2;
  115.  i:=1;
  116.  j:=m;
  117.  r:=n;
  118.  if a[j].p1=num then
  119.       a[j].p3:=True;
  120.  while (num<>a[j].p1)  do
  121.   begin
  122.     a[j].p3:=true;
  123.     if num>a[j].p1 then
  124.      begin
  125.        i:=j+1;
  126.        j:=(i+r) div 2;
  127.      end;
  128.     if num<a[j].p1 then
  129.      begin
  130.        r:=j-1;
  131.        j:=(i+r) div 2;
  132.      end;
  133.     if num=a[j].p1 then
  134.      a[j].p3:=true;
  135.   end;
  136.  s:=j;
  137.  while a[s].p1=a[s-1].p1 do
  138.  begin
  139.   Dec(s);
  140.   a[s].p3:=true;
  141.  end;
  142.  while a[j].p1=a[j+1].p1 do
  143.  begin
  144.   Inc(j);
  145.   a[j].p3:=true;
  146.  end;
  147. end;
  148.  
  149. procedure sortpione(var a:tmas);
  150. var i,j:integer; tmp:tzap;
  151. begin
  152.  for i:=1 to n-1 do
  153.   begin
  154.      for j:=1 to n-i do
  155.       begin
  156.        if a[j].p1>a[j+1].p1 then
  157.         begin
  158.           tmp:=a[j];
  159.           a[j]:=a[j+1];
  160.           a[j+1]:=tmp;
  161.         end;
  162.       end;
  163.   end;
  164. end;
  165. var a:tmas;
  166.  
  167. begin
  168.  input_mas(a);
  169.  output_mas(a);
  170.  sortpsecond(a);
  171.  writeln;
  172.  binarysearch(a);
  173.  output_mas(a);
  174.  Writeln;
  175.  truel(a);
  176.  sortpione(a);
  177.  falseel(a);
  178.  Writeln;
  179.  output_mas(a);
  180.  Writeln;
  181.  binarysearch2(a);
  182.  output_mas(a);
  183.  truel(a);
  184.  readln;
  185.  readln;
  186. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement