RevolutIIon

Untitled

Oct 25th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.51 KB | None | 0 0
  1. program Test2var2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   SysUtils;
  9.  
  10. type
  11.    TMatrix = array of array of Integer;
  12.    TIntArr = array of Integer;
  13. const
  14.    StartMessage = 'This program places the lines in ascending order of the number of zero elements.';
  15.    Example = '"Your_file_name.txt"';
  16.  
  17. //ввод имени файла
  18. function InputName: string;
  19. var
  20.    FileName: string;
  21.    IsCorrectName: Boolean;
  22. begin
  23.    repeat
  24.       Writeln('Enter name of file in format: ', Example);
  25.       IsCorrectName := False;
  26.       Readln(FileName);
  27.       if FileExists(FileName) then
  28.          IsCorrectName := True
  29.       else
  30.          Writeln('File is not exist. Please, follow instruction and enter right name of file ');
  31.    until IsCorrectName;
  32.    InputName := FileName;
  33. end;
  34.  
  35. //находим размерность, если квадрат
  36. function FindDimensionN(var Input: TextFile): Integer;
  37. var
  38.    i, j, Order, Symbol: Integer;
  39.    IsCorrect: Boolean;
  40. begin
  41.    Reset(Input);
  42.    IsCorrect := True;
  43.    i := 0;
  44.    Order := 0;
  45.    while (not Eof(Input) and (IsCorrect)) do
  46.    begin
  47.       j := 0;
  48.       while ((not EoLn(Input)) and IsCorrect) do
  49.       begin
  50.          try
  51.             Read(Input, Symbol);
  52.          except
  53.             IsCorrect := False;
  54.             Write('Error. Your matrix must consist of integers. Correct your' +
  55.                 ' file. Press enter to continue...');
  56.          end;
  57.          Inc(j);
  58.       end;
  59.       if Order = 0 then
  60.          Order := j;
  61.       if (Order <> j)or (Order < 2) then
  62.       begin
  63.          IsCorrect := False;
  64.          Writeln('Your matrix must be square and order must be greater than 2. Correct your file and start again');
  65.       end;
  66.       Inc(i);
  67.       Readln(Input);
  68.    end;
  69.    Close(Input);
  70.    if (Order <> i) or (not IsCorrect)
  71.     then
  72.       Order := -1;
  73.    FindDimensionN := Order;
  74. end;
  75.  
  76. //ввод матрицы из файла
  77. function EnterMatrix(Order: Integer; var Input: TextFile):TMatrix;
  78. var
  79.    i, j: Integer;
  80.    Matrix: TMatrix;
  81. begin
  82.    Reset(Input);
  83.    SetLength(Matrix, Order, Order);
  84.    Dec(Order);
  85.    for i := 0 to Order do
  86.    begin
  87.       for j:= 0 to Order do
  88.          Read(Input, Matrix[i,j]);
  89.       Readln(Input);
  90.    end;
  91.    Close(Input);
  92.    EnterMatrix := Matrix;
  93. end;
  94.  
  95. //считает ноли в строке матрицы
  96. function CountZero(const MatrixStr: TIntArr): Integer;
  97. var
  98.    Counter, i, MaxIndex: Integer;
  99.  
  100. begin
  101.    Counter := 0;
  102.    MaxIndex := High(MatrixStr);
  103.    for i := 0 to MaxIndex do
  104.    begin
  105.       if MatrixStr[i] = 0 then
  106.          Inc(Counter);
  107.    end;
  108.    CountZero := Counter;
  109. end;
  110.  
  111. function GetCountersArr(const Matrix: TMatrix): TIntArr;
  112. var
  113.    CounterArr,  MatrixStr: TIntArr;
  114.    i, j, MaxIndex: Integer;
  115. begin
  116.    MaxIndex := High(Matrix);
  117.    SetLength(MatrixStr, MaxIndex + 1);
  118.    SetLength(CounterArr, MaxIndex + 1);
  119.    for i := 0 to MaxIndex do
  120.    begin
  121.       for j := 0 to MaxIndex do
  122.          MatrixStr[j] := Matrix [i, j];
  123.       CounterArr[i] := CountZero(MatrixStr);
  124.    end;
  125.    GetCountersArr := CounterArr;
  126. end;
  127.  
  128. //переставляет строки матрицы как в условии
  129. function ChangeMatrix( Matrix: TMatrix; CounterArr: TIntArr): TMatrix;
  130. var
  131.    IsEnd: Boolean;
  132.    i, j, MaxIndex, Optim, TempCounter, TempMatrixEl: Integer;
  133. begin
  134.    MaxIndex := High(Matrix);
  135.    Optim := MaxIndex - 1;
  136.    repeat
  137.       IsEnd := True;
  138.       for i := 0 to Optim do
  139.       begin
  140.          if CounterArr[i] > CounterArr[i + 1] then
  141.          begin
  142.             for j := 0 to MaxIndex do
  143.             begin
  144.                TempMatrixEl := Matrix[i, j];
  145.                Matrix[i, j] := Matrix[i + 1, j];
  146.                Matrix[i + 1, j] := TempMatrixEl;
  147.             end;
  148.             TempCounter := CounterArr[i];
  149.             CounterArr[i] := CounterArr[i + 1];
  150.             CounterArr[i + 1] := TempCounter;
  151.             IsEnd := False;
  152.          end;
  153.       end;
  154.       Dec(Optim);
  155.    until IsEnd;
  156.    ChangeMatrix := Matrix;
  157. end;
  158.  
  159. //вывод матрицы в файл
  160. procedure FileOutput(Matrix: TMatrix);
  161. var
  162.    i, j, MaxIndex: Integer;
  163.    Output: TextFile;
  164.    FileName: string;
  165.  
  166. begin
  167.    Writeln('Enter name of file for output. For example: ', Example);
  168.    Readln(FileName);
  169.    AssignFile(Output, FileName);
  170.    MaxIndex := High(Matrix);
  171.    Rewrite(Output);
  172.    for i := 0 to MaxIndex do
  173.    begin
  174.       for j := 0 to MaxIndex do
  175.          Write(Output, Matrix[i, j], ' ');
  176.       Writeln(Output);
  177.    end;
  178.    CloseFile(Output);
  179. end;
  180.  
  181. procedure ConsoleOutput (Matrix: TMatrix);
  182. var
  183.    i, j, MaxIndex: Integer;
  184. begin
  185.    MaxIndex := High(Matrix);
  186.    for i := 0 to MaxIndex do
  187.    begin
  188.       for j := 0 to MaxIndex do
  189.          Write(Matrix[i, j],' ');
  190.       Writeln;
  191.    end;
  192. end;
  193.  
  194. procedure Main;
  195. var
  196.    Order: Integer;
  197.    FileName: string;
  198.    Matrix: TMatrix;
  199.    Input: TextFile;
  200.    IsValidInput: Boolean;
  201.    CounterArr: TIntArr;
  202.  
  203. begin
  204.    repeat
  205.       Writeln(StartMessage);
  206.       FileName := InputName;
  207.       Assign(Input, FileName);
  208.       IsValidInput := True;
  209.       Order := FindDimensionN(Input);
  210.       if Order = -1 then
  211.          IsValidInput:= False;
  212.    until IsValidInput;
  213.    Matrix := EnterMatrix(Order, Input);
  214.    Writeln('Your start martix: ');
  215.    ConsoleOutput(Matrix);
  216.    CounterArr := GetCountersArr(Matrix);
  217.    Matrix := ChangeMatrix(Matrix, CounterArr);
  218.    FileOutput(Matrix);
  219.    Writeln('Your final martix: ');
  220.    ConsoleOutput(Matrix);
  221.    Writeln;
  222.    Writeln('Press enter to exit...');
  223.    Readln;
  224. end;
  225.  
  226. begin
  227.    Main;
  228. end.
Add Comment
Please, Sign In to add comment