Advertisement
Eugene0091

Laba 2.4 Delphi

Nov 5th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.67 KB | None | 0 0
  1. program Project3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. const
  11.    MISTAKE_NOT_FOUND_MESSAGE = 'Error! File name can not be found. Please try again.';
  12.    MISTAKE_NOT_OPEN_MESSAGE = 'Error! Unable to open this file. Please check the file and try again.';
  13.  
  14. type
  15.    ToWorkArr = array of array of Integer;
  16.    TInput = (ChooseKeyboard, ChooseFile);
  17.    TOutput = (Yes, No);
  18.  
  19. function ChooseInput(): TInput;
  20.  
  21. var
  22.    Symbol: Char;
  23.    Input: TInput;
  24.    IsCorrect: Boolean;
  25.  
  26. begin
  27.    repeat
  28.       WriteLn('If you want to fill the array from the keyboard, please enter "K", if you want to read ' + #10#13 + 'an array from a file, please enter "F":');
  29.       ReadLn(Symbol);
  30.       Symbol := UpCase(Symbol);
  31.       if (Symbol <> 'K') and (Symbol <> 'F') then
  32.           WriteLn('Attention, an error occurred while entering. Please try again.');
  33.    until Symbol in ['K', 'F'];
  34.    case Symbol of
  35.       'K':  ChooseInput := ChooseKeyboard;
  36.       'F':  ChooseInput := ChooseFile;
  37.    end;
  38. end;
  39.  
  40. procedure InputMatrixLength(var LengthOfMatrix: Integer);
  41.  
  42. const
  43.    MIN_MATRIX_LENGTH = 1;
  44.    MAX_MATRIX_LENGTH = 20;
  45.    CHAR_MISTAKE_MESSAGE = 'Error! Enter a numeric value.';
  46.  
  47. var
  48.    CorrectCheck: Boolean;
  49.  
  50. begin
  51.    repeat
  52.       CorrectCheck := False;
  53.       Write('Please enter matrix order (value from ', MIN_MATRIX_LENGTH, ' to ', MAX_MATRIX_LENGTH, '): ');
  54.       try
  55.          ReadLn(LengthOfMatrix);
  56.          CorrectCheck := True;
  57.       except
  58.          WriteLn(CHAR_MISTAKE_MESSAGE);
  59.       end;
  60.       if CorrectCheck and ((LengthOfMatrix < MIN_MATRIX_LENGTH) or (LengthOfMatrix > MAX_MATRIX_LENGTH)) then
  61.       begin
  62.          CorrectCheck := False;
  63.          WriteLn('Error! The number does not satisfy the range of values from ', MIN_MATRIX_LENGTH, ' to ', MAX_MATRIX_LENGTH, '.');
  64.       end;
  65.    until (CorrectCheck);
  66. end;
  67.  
  68. procedure FillMatrixFromKeyboard(var MethodMatrix: ToWorkArr; var LengthOfMatrix: Integer);
  69.  
  70. const
  71.    MIN_INT = - MaxInt - 1;
  72.  
  73. var
  74.    i, j: Integer;
  75.    CorrectFilling: Boolean;
  76.  
  77. begin
  78.    InputMatrixLength(LengthOfMatrix);
  79.    SetLength(MethodMatrix, LengthOfMatrix, LengthOfMatrix);
  80.    LengthOfMatrix := LengthOfMatrix - 1;
  81.    WriteLn('Please enter array elements (values from ', MIN_INT, ' to ', MaxInt, '):');
  82.    for i := 0 to LengthOfMatrix do
  83.       for j := 0 to LengthOfMatrix do
  84.       begin
  85.          repeat
  86.             CorrectFilling := False;
  87.             Write('Array[', i, ', ', j, '] = ');
  88.             try
  89.                ReadLn(MethodMatrix[i, j]);
  90.                CorrectFilling := True;
  91.             except
  92.                WriteLn('Error! Please enter a numerical value with a range from ', MIN_INT, ' to ', MaxInt, '.');
  93.             end;
  94.          until (CorrectFilling);
  95.       end;
  96. end;
  97.  
  98. procedure ConsoleOutput(Matrix: ToWorkArr; MatrixLength: Integer);
  99.  
  100. var
  101.    i, j: Integer;
  102.  
  103. begin
  104.    for i := 0 to MatrixLength do
  105.    begin
  106.       for j := 0 to MatrixLength do
  107.          Write(Matrix[i, j]:5, '  ');
  108.       Writeln;
  109.    end;
  110. end;
  111.  
  112. procedure InputMatrixFromFile(var MethodMatrix: ToWorkArr; var LengthOfMatrix: Integer);
  113.  
  114. const
  115.    INPUT_MESSAGE = 'Please enter the name of the file from which the data will be read.' + #13#10 + 'For example, "C:\Users\Eugene\Desktop\Name.txt".';
  116.    MIN_MATRIX_LENGTH = 1;
  117.    MAX_MATRIX_LENGTH = 20;
  118.    MIN_INT = - MaxInt - 1;
  119.  
  120. var
  121.    IsInvalidInput: Boolean;
  122.    UserFile: TextFile;
  123.    FileName: String;
  124.    i, j, Count, FullLengthOfMatrix: Integer;
  125.  
  126. begin
  127.    repeat
  128.       IsInvalidInput := True;
  129.       WriteLn(INPUT_MESSAGE);
  130.       ReadLn(FileName);
  131.       if FileExists(FileName) then
  132.       begin
  133.          try
  134.             Assign(UserFile, FileName);
  135.             Reset(UserFile);
  136.          except
  137.             WriteLn(MISTAKE_NOT_OPEN_MESSAGE);
  138.             IsInvalidInput := False;
  139.          end;
  140.          if IsInvalidInput then
  141.          begin
  142.             while not EOF(UserFile) do
  143.             begin
  144.                try
  145.                   ReadLn(UserFile, LengthOfMatrix);
  146.                except
  147.                   WriteLn('Error! The file contains invalid data. Please check the file and try again.');
  148.                   IsInvalidInput := False;
  149.                end;
  150.                if (IsInvalidInput) and ((LengthOfMatrix < MIN_MATRIX_LENGTH) or (LengthOfMatrix > MAX_MATRIX_LENGTH)) then
  151.                begin
  152.                   WriteLn('Error! The length of the array does not satisfy the range of values from ', MIN_MATRIX_LENGTH, ' to ', MAX_MATRIX_LENGTH, '.');
  153.                   IsInvalidInput := False;
  154.                end;
  155.                if (IsInvalidInput) then
  156.                begin
  157.                   SetLength(MethodMatrix, LengthOfMatrix, LengthOfMatrix);
  158.                   FullLengthOfMatrix := LengthOfMatrix;
  159.                   LengthOfMatrix := LengthOfMatrix - 1;
  160.                   for i := 0 to LengthOfMatrix do
  161.                      if IsInvalidInput then
  162.                      begin
  163.                         j := 0;
  164.                         while (not EOln(UserFile)) and (j < FullLengthOfMatrix) and (IsInvalidInput) do
  165.                         begin
  166.                            try
  167.                               Read(UserFile, MethodMatrix[i, j]);
  168.                            except
  169.                               WriteLn('Error! Not all array elements are numerical or span ' + #13#10 + 'value from ', MIN_INT, ' to ', MaxInt, '.');
  170.                               IsInvalidInput := False;
  171.                            end;
  172.                            Inc(j);
  173.                         end;
  174.                         ReadLn(UserFile);
  175.                         if (j < LengthOfMatrix) and (IsInvalidInput) then
  176.                         begin
  177.                            WriteLn('Error! The length of the array does not match the number of array elements in the file.');
  178.                            IsInvalidInput := False;
  179.                         end;
  180.                      end;
  181.                end;
  182.             end;
  183.             CloseFile(UserFile);
  184.          end;
  185.       end
  186.       else
  187.       begin
  188.          WriteLn(MISTAKE_NOT_FOUND_MESSAGE);
  189.          IsInvalidInput := False;
  190.       end;
  191.    until IsInvalidInput;
  192. end;
  193.  
  194. function ChooseOutput(): TOutput;
  195.  
  196. var
  197.    Symbol: Char;
  198.    IsCorrect: Boolean;
  199.  
  200. begin
  201.    repeat
  202.       WriteLn('If you want to put the result in a file, please enter "Y", otherwise, ' + #10#13 + 'please enter "N":');
  203.       ReadLn(Symbol);
  204.       Symbol := UpCase(Symbol);
  205.       if (Symbol <> 'Y') and (Symbol <> 'N') then
  206.           WriteLn('Attention, an error occurred while input. Please try again.');
  207.    until Symbol in ['Y', 'N'];
  208.    case Symbol of
  209.       'Y':  ChooseOutput := Yes;
  210.       'N':  ChooseOutput := No;
  211.    end;
  212. end;
  213.  
  214. procedure WriteToFile(Matrix: ToWorkArr; const LengthOfMatrix: Integer);
  215.  
  216. const
  217.    OUTPUT_MESSAGE = 'Please enter the name of the file in which you want to write the result.' + #13#10 + 'For example, "C:\Users\Eugene\Desktop\Answer.txt".';
  218.  
  219. var
  220.    IsInvalidOutput: Boolean;
  221.    ResultFile: TextFile;
  222.    FileName: String;
  223.    i, j: Integer;
  224.  
  225. begin
  226.    repeat
  227.       IsInvalidOutput := True;
  228.       Writeln(OUTPUT_MESSAGE);
  229.       Readln(FileName);
  230.       if FileExists(FileName) then
  231.       begin
  232.          try
  233.             Assign(ResultFile, FileName);
  234.             Rewrite(ResultFile);
  235.          except
  236.             WriteLn(MISTAKE_NOT_OPEN_MESSAGE);
  237.             IsInvalidOutput := False;
  238.          end;
  239.          if IsInvalidOutput then
  240.          begin
  241.             Writeln(ResultFile, 'Result change lines matrix:');
  242.             for i := 0 to LengthOfMatrix do
  243.             begin
  244.                for j := 0 to LengthOfMatrix do
  245.                   Write(ResultFile, Matrix[i, j]:5, '  ');
  246.                Writeln(ResultFile);
  247.             end;
  248.             CloseFile(ResultFile);
  249.             WriteLn('Data recording completed successfully.');
  250.          end;
  251.       end
  252.       else
  253.       begin
  254.          WriteLn(MISTAKE_NOT_FOUND_MESSAGE);
  255.          IsInvalidOutput := False;
  256.       end;
  257.    until IsInvalidOutput;
  258. end;
  259.  
  260. procedure ChangeLinesMatrix(var MethodMatrix: ToWorkArr; const LengthOfMatrix: Integer);
  261.  
  262. var
  263.    i, j, Buffer, IncCounter, DivLengthOfMatrix, DecCounter: Integer;
  264.  
  265. begin
  266.    DivLengthOfMatrix := LengthOfMatrix div 2;
  267.    IncCounter := 0;
  268.    DecCounter := LengthOfMatrix;
  269.    for i := 1 to DivLengthOfMatrix do
  270.    begin
  271.       for j := 0 to LengthOfMatrix do
  272.       begin
  273.          Buffer := MethodMatrix[IncCounter, j];
  274.          MethodMatrix[IncCounter, j] := MethodMatrix[DecCounter, j];
  275.          MethodMatrix[DecCounter, j] := Buffer;
  276.       end;
  277.       Inc(IncCounter);
  278.       Dec(DecCounter);
  279.    end;
  280. end;
  281.  
  282. procedure Main();
  283.  
  284. const
  285.    THEME_MESSAGE = 'Topic: This program changes the rows of the matrix, the first with the last, '+ #10#13 + 'the second with the penultimate, etc.';
  286.    DOTTED_LINE = '--------------------------------------------------------------------------------------';
  287.  
  288. var
  289.    Matrix: ToWorkArr;
  290.    i, j, MatrixLength, TaskAnswer: Integer;
  291.    IsCorrect: Boolean;
  292.    Input: TInput;
  293.    Output: TOutput;
  294.  
  295. begin
  296.    WriteLn(THEME_MESSAGE);
  297.    WriteLn(DOTTED_LINE);
  298.    Input := ChooseInput();
  299.    case Input of
  300.       ChooseKeyboard: FillMatrixFromKeyboard(Matrix, MatrixLength);
  301.       ChooseFile: InputMatrixFromFile(Matrix, MatrixLength);
  302.    end;
  303.    Writeln('Entered matrix ', (MatrixLength + 1), ' x ', (MatrixLength + 1));
  304.    ConsoleOutput(Matrix, MatrixLength);
  305.    ChangeLinesMatrix(Matrix, MatrixLength);
  306.    Writeln('Result change lines matrix:');
  307.    ConsoleOutput(Matrix, MatrixLength);
  308.    Output := ChooseOutput();
  309.    if Output = Yes then
  310.       WriteToFile(Matrix, MatrixLength);
  311.    Writeln(DOTTED_LINE);
  312.    Writeln('Program completed.');
  313.    ReadLn;
  314. end;
  315.  
  316. begin
  317.    Main();
  318. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement