Advertisement
DimaT1

DZ4

Sep 15th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.66 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     Memo2: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. const
  35.   MAXN = 10000;
  36.  
  37. type
  38.   TSym = record
  39.     a :integer;
  40.     p :integer;
  41.   end;
  42.  
  43.   Tmas = array[1 .. MAXN] of TSym;
  44.  
  45. procedure swap(var a, b :TSym);
  46. var
  47.   c :integer;
  48. begin
  49.   c:= a.a;
  50.   a.a:= b.a;
  51.   b.a:= c;
  52. end;
  53.  
  54. procedure qSort(var v :Tmas; l,r :longint);
  55. var i,j:longint;
  56.     q :integer;
  57. begin
  58.   i := l; j := r;
  59.   q := v[(l+r) div 2].a;
  60.   repeat
  61.     while (v[i].a < q) do inc(i);
  62.     while (q < v[j].a) do dec(j);
  63.     if (i <= j) then
  64.     begin
  65.       swap(v[i], v[j]);
  66.       inc(i); dec(j);
  67.     end;
  68.   until (i > j);
  69.   if (l < j) then qSort(v, l,j);
  70.   if (i < r) then qSort(v, i,r);
  71. end;
  72.  
  73. function work(s :string) :string;
  74. var
  75.     n, i :integer;
  76.     arr :Tmas;
  77. begin
  78.      n:= 0;
  79.      for i:= 1 to length(s) do
  80.        if(pos(s[i], '0123456789') <> 0) then
  81.        begin
  82.          inc(n);
  83.          arr[n].a:= pos(s[i], '0123456789') - 1;
  84.          arr[n].p:= i;
  85.        end;
  86.  
  87.      if(n <> 0) then
  88.           qSort(arr, 1, n);
  89.  
  90.      for i:= 1 to n do
  91.      begin
  92.        delete(s, arr[i].p, 1);
  93.        insert(inttostr(arr[i].a), s, arr[i].p);
  94.      end;
  95.      work:= s;
  96. end;
  97.  
  98. procedure TForm1.Button1Click(Sender: TObject);
  99. begin
  100.      Memo2.append(work(Memo1.Lines[0]));
  101. end;
  102.  
  103. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement