6.PAS 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const
  2. name_len = 11;
  3. phone_len = 9;
  4. type
  5. TPE = record
  6. Name : string[name_len];
  7. Phone : string[phone_len];
  8. end;
  9. TPEa = array [1..20] of TPE;
  10. Procedure Exchange(var a,b:TPE);
  11. var
  12. t:tpe;
  13. begin
  14. t:=a;a:=b;b:=t;
  15. end;{Exchange}
  16. {----------------------------------------}
  17. Procedure QSort(var aA:TPEa;L,R:word);
  18. var
  19. i,j : word;
  20. cen : string[phone_len];
  21. begin
  22. if L<>R then
  23. begin
  24. cen:=aA[(l+r) div 2].Phone;
  25. i:=l;
  26. j:=r;
  27. while i<=j do
  28. begin
  29. while (i<r) and (aa[i].Phone > cen) do inc(i);
  30. while (j>l) and (aa[j].Phone < cen) do dec(j);
  31. if i<=j then
  32. begin
  33. Exchange(aA[i],aA[j]);
  34. Inc(i);
  35. Dec(j);
  36. end;
  37. end;
  38. if j > L then QSort(Aa,L,j);
  39. if i < r then QSort(aA,i,r);
  40. end;
  41. end;{SortNum}
  42. {----------------------------------------}
  43. var
  44. Ar : TPEa;
  45. INP : text;
  46. i,cnt : integer;
  47. begin
  48. Assign(INP,'phones.txt');
  49. Assign(Output,'out6.txt');
  50. Rewrite(Output);
  51. {$I-}
  52. Reset(INP);
  53. If IOResult <> 0 then
  54. begin
  55. WriteLn('File ''phones.txt'' not found');
  56. Halt(255);
  57. end;
  58. {$I+}
  59. cnt:=1;
  60. While not EOF(INP) do
  61. begin
  62. Read(INP,Ar[cnt].Name);
  63. ReadLn(INP,Ar[cnt].Phone);
  64. WriteLn(Ar[cnt].Name,Ar[cnt].Phone);
  65. inc(cnt);
  66. end;
  67. Close(INP);
  68. WriteLn('Result:');
  69. Dec(cnt);
  70. QSort(Ar,1,cnt);
  71. for i := 1 to cnt do WriteLn(Ar[i].Name,Ar[i].Phone);
  72. WriteLn('End.');
  73. end.