Advertisement
Domerk

Типизированные файлы 2

Oct 20th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.48 KB | None | 0 0
  1. {Программа. Типизированный файл, содержащий произвольные числа. Найти и удалить наибольшее.}
  2.  
  3. program tip;
  4. uses crt;
  5.  
  6. var
  7. f: file of integer;
  8. a1, n, i, amax, nmax, na : integer;
  9. begin
  10. clrscr;
  11.  
  12. writeln ('Vvod kolichestva chisel');
  13. readln (n);
  14. assign (f, 'file.int');
  15. rewrite (f);
  16. randomize;
  17.  
  18. for i := 1 to n do begin
  19. a1:=Random (100);
  20. write (f, a1);
  21. end;
  22.  
  23. reset (f);
  24. clrscr;
  25.  
  26. for i :=1 to n do begin
  27. read (f, a1);
  28. writeln (a1);
  29. end;
  30.  
  31. reset (f);
  32. amax:=-1;
  33.  
  34. while not eof(f) do begin
  35. read (f, a1);
  36.     if a1 > amax then begin
  37.     amax:=a1;
  38.     nmax:=filepos (f);
  39.     end; end;
  40.  
  41. writeln ('max = ', amax, ' nmax = ', nmax);
  42.  
  43. if (n-nmax)>2 then begin
  44.     seek (f, nmax-1);
  45.     read (f, a1);
  46.     seek (f, n);
  47.     write (f, a1);
  48.  
  49.     i:=n+1;
  50.     na:=nmax;
  51.  
  52.     reset (f);
  53.  
  54.     while (i>0) do begin
  55.  
  56.         i:=i-1;
  57.  
  58.         seek (f, na-1);
  59.         read (f, a1);
  60.         seek (f, na-2);
  61.         write (f, a1);
  62.  
  63.         na:=na+2;
  64.  
  65.     end;
  66.     seek (f, n-1);
  67.     truncate (f);
  68.  
  69. end  else begin
  70.  
  71.      if nmax=n-1 then begin
  72.      seek (f, nmax);
  73.      truncate (f);
  74.      end else begin
  75.      seek (f, n-1);
  76.      read (f, a1);
  77.      write (f, amax);
  78.      seek (f, amax-1);
  79.      write (f, a1);
  80.      seek (f, n-1);
  81.      truncate (f);
  82.      end;
  83. end;
  84.  
  85. reset (f);
  86.  
  87. while not eof(f) do begin
  88.     read (f, a1);
  89.     writeln (a1);
  90. end;
  91.  
  92. close (f);
  93. readln;
  94. clrscr;
  95. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement