Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program matrix;
- {$APPTYPE CONSOLE}
- uses
- SysUtils ;
- type
- dynamic_array = array of array of integer ;
- var
- matrix_first, matrix_second: dynamic_array;
- flag: string;
- correctreading : boolean ;
- procedure read_file(addres: String; var array_tmp : dynamic_array );
- var
- matrix_file : Text;
- i , j : integer;
- correctvalue : boolean ;
- begin
- assign(matrix_file, addres);
- Reset(matrix_file);
- i := -1;
- correctvalue := true ;
- correctreading := true ;
- {$I-}
- while ((not Eof(matrix_file)) and (correctvalue)) do
- begin
- Inc(i);
- SetLength(array_tmp, i + 1);
- j:= -1;
- while not Eoln(matrix_file) do
- begin
- Inc(j);
- SetLength(array_tmp[i], j + 1);
- Read(matrix_file,array_tmp[i, j]) ;
- if IOResult <> 0 then
- begin
- writeln('Check the data in the file.');
- correctvalue := false ;
- correctreading := false;
- end;
- end;
- Readln(matrix_file);
- end;
- CloseFile(matrix_file);
- end;
- procedure counting (var arr_a, arr_b : dynamic_array; flag : string );
- var
- index_i, index_j: integer;
- begin
- if flag = 'sum' then
- begin
- for index_i := 0 to length(arr_a) - 1 do
- begin
- for index_j :=0 to length(arr_a[0]) - 1 do
- arr_a[index_i][index_j] := arr_a[index_i][index_j] + arr_b[index_i][index_j];
- end;
- end
- else
- begin
- for index_i := 0 to length(arr_a) - 1 do
- begin
- for index_j :=0 to length(arr_a[0]) - 1 do
- arr_a[index_i][index_j] := arr_a[index_i][index_j] - arr_b[index_i][index_j];
- end;
- end;
- end;
- // procedure for writing into file and console
- procedure writing_result( array_tmp : dynamic_array );
- var
- i , j : integer;
- matrix_result : text ;
- begin
- writeln('Calculation has been made successfully');
- assign(matrix_result, 'matrix_result.txt ');
- Rewrite (matrix_result) ;
- for i := 0 to length(array_tmp) - 1 do
- begin
- for j := 0 to length(array_tmp[i]) - 1 do
- begin
- write(matrix_result, array_tmp[i][j] , ' ');
- write( array_tmp[i][j] , ' ');
- end;
- writeln('');
- writeln(matrix_result,'');
- end;
- Close (matrix_result);
- end;
- begin
- read_file('matrix_first.txt' , matrix_first);
- read_file('matrix_second.txt' , matrix_second);
- if correctreading then
- begin
- repeat
- write('Enter "sum" for the sum and "res" for residual: ');
- readln(flag);
- if ( (flag <> 'sum') and (flag <> 'res' ) ) then
- writeln('Invalid input. Try again.');
- until ((flag = 'sum') or (flag = 'res' ));
- counting(matrix_first , matrix_second , flag);
- writing_result( matrix_first );
- end;
- readln ;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement