Advertisement
ipilot

Lab Pascal

Oct 29th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.59 KB | None | 0 0
  1. label lab2;
  2.  
  3. procedure fullsearch();
  4. var i,k,n:integer;
  5.     s:string;
  6.     f:text;
  7.     m:array[1..15] of string;
  8.  
  9. begin
  10.   writeln('Selected method: Full search. Processing...');
  11.   reset(input,'test.txt');
  12.   rewrite(f,'result_fullsearch.txt');
  13.   i:=1;
  14.   readln(s);
  15.   writeln(f,'Для строки "',s,'" - 0 сравнений');
  16.   m[i]:=s;
  17.   while (i<15) do
  18.   begin
  19.     readln(s);
  20.     k:=1;
  21.     n:=0;
  22.     while (k <= i) and (s <> m[k]) do
  23.     begin
  24.       inc(k);
  25.       inc(n);
  26.     end;
  27.     if (k > i) then
  28.     begin
  29.       inc(i);
  30.       m[i]:=s;
  31.     end
  32.     else inc(n);
  33.     writeln(f,'Для строки "',s,'" - ',n,' сравнений');
  34.   end;
  35.   close(input);
  36.   close(f);
  37. end;
  38.  
  39. procedure binsearch();
  40. var s:string;
  41.     m:array[1..15] of string;
  42.     i,k,n,l,r,x:integer;
  43.     f:boolean;
  44.     t:text;
  45.  
  46. begin
  47.   writeln('Selected method: Binary search. Processing...');
  48.   reset(input,'test.txt');
  49.   rewrite(t,'result_binsearch.txt');
  50.   i:=1;
  51.   readln(s);
  52.   writeln(t,'Для строки "',s,'" - 0 сравнений');
  53.   m[i]:=s;
  54.   while (i < 15) do
  55.   begin
  56.     readln(s);
  57.     n:=0;
  58.     l:=1;
  59.     r:=i;
  60.     f:=false;
  61.     while (l <= r) do
  62.     begin
  63.       x:=(l+r) div 2;
  64.       if (m[x] = s) then
  65.       begin
  66.         l:=r+1;
  67.         f:=true;
  68.       end
  69.       else
  70.         if (m[x] > s) then r:=x-1
  71.         else l:=x+1;
  72.       inc(n);
  73.     end;
  74.     if not f then
  75.     begin
  76.       k:=i;
  77.       while (k > 0) and (m[k] > s) do
  78.       begin
  79.         m[k+1]:=m[k];
  80.         dec(k);
  81.       end;
  82.       m[k+1]:=s;
  83.       inc(i);
  84.     end;
  85.     writeln(t,'Для строки "',s,'" - ',n,' сравнений');
  86.   end;
  87.   close(input);
  88.   close(t);
  89. end;
  90.  
  91. procedure hash();
  92. var s:string;
  93.     hash:array[0..14] of boolean;
  94.     m:array[0..14] of string;
  95.     i,k,n,l,h,u:integer;
  96.     f:boolean;
  97.     t:text;
  98.  
  99. begin
  100.   writeln('Selected method: Hashing. Processing...');
  101.   reset(input,'test.txt');
  102.   rewrite(t,'result_hash.txt');
  103.   i:=0;
  104.   fillchar(hash,sizeof(hash),0);
  105.   while (i < 15) do
  106.   begin
  107.     readln(s);
  108.     n:=0;
  109.     l:=length(s);
  110.     h:=0;
  111.     for u := 1 to l do
  112.        h :=(h + ord(s[u])) mod 15;
  113.     if (not hash[h]) then
  114.     begin
  115.       hash[h]:=true;
  116.       m[h]:=s;
  117.       inc(i);
  118.     end
  119.     else
  120.     begin
  121.       inc(n);
  122.       f:=false;
  123.       k:=1;
  124.       if (m[h] <> s) then
  125.       while not f do
  126.       begin
  127.         if ((h - k) >= 0) then
  128.           if (not hash[h-k]) then
  129.           begin
  130.             hash[h-k]:=true;
  131.             m[h-k]:=s;
  132.             inc(i);
  133.             f:=true;
  134.           end
  135.           else
  136.           begin
  137.             inc(n);
  138.             if (m[h-k] = s) then f:=true;
  139.           end;
  140.         if (not f) then
  141.           if ((h + k) <= 14) then
  142.             if (not hash[h+k]) then
  143.             begin
  144.               hash[h+k]:=true;
  145.               m[h+k]:=s;
  146.               inc(i);
  147.               f:=true;
  148.             end
  149.             else
  150.             begin
  151.               inc(n);
  152.               if (m[h+k] = s) then f:=true;
  153.             end;
  154.         inc(k);
  155.       end;
  156.     end;
  157.     writeln(t,'Для строки "',s,'" - ',n,' сравнений');
  158.   end;
  159.   close(input);
  160.   close(t);
  161. end;
  162.  
  163. var k:char;
  164. begin
  165.   writeln('Select the method of data processing:');
  166.   writeln('1. Full search');
  167.   writeln('2. Binary search');
  168.   writeln('3. Hashing');
  169.   lab2:
  170.   readln(k);
  171.   case k of
  172.     '1': fullsearch();
  173.     '2': binsearch();
  174.     '3': hash();
  175.     else
  176.     begin
  177.       writeln('Error! Please type the number from 1 to 3!');
  178.       goto lab2;
  179.     end;
  180.   end;
  181.   writeln('All data were processed.');
  182. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement