Advertisement
vana_shimko

код Вадима

Oct 25th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.23 KB | None | 0 0
  1. program matrix;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.     SysUtils ;
  5. type
  6.     dynamic_array = array of array of integer ;
  7. var
  8.     matrix_first, matrix_second: dynamic_array;
  9.     flag: string;
  10.     correctreading : boolean ;
  11.  
  12.     procedure read_file(addres: String; var array_tmp : dynamic_array );
  13.     var
  14.         matrix_file : Text;
  15.         i , j : integer;
  16.         correctvalue : boolean ;
  17.     begin
  18.         assign(matrix_file, addres);
  19.         Reset(matrix_file);
  20.         i := -1;
  21.         correctvalue := true ;
  22.         correctreading := true ;
  23.         {$I-}
  24.         while ((not Eof(matrix_file)) and (correctvalue)) do
  25.         begin
  26.             Inc(i);
  27.             SetLength(array_tmp, i + 1);
  28.             j:= -1;
  29.             while not Eoln(matrix_file) do
  30.             begin
  31.                 Inc(j);
  32.                 SetLength(array_tmp[i], j + 1);
  33.                 Read(matrix_file,array_tmp[i, j]) ;
  34.                 if IOResult <> 0 then
  35.                 begin
  36.                     writeln('Check the data in the file.');
  37.                     correctvalue := false ;
  38.                     correctreading := false;
  39.                 end;
  40.             end;
  41.             Readln(matrix_file);
  42.         end;
  43.         CloseFile(matrix_file);
  44.     end;
  45.    
  46.     procedure counting (var arr_a, arr_b : dynamic_array; flag : string );
  47.     var
  48.         index_i, index_j: integer;
  49.     begin
  50.         if flag = 'sum'  then
  51.         begin
  52.             for index_i := 0 to length(arr_a) - 1  do
  53.             begin
  54.                 for index_j :=0 to length(arr_a[0]) - 1 do
  55.                     arr_a[index_i][index_j] := arr_a[index_i][index_j] + arr_b[index_i][index_j];
  56.             end;
  57.         end
  58.         else
  59.         begin
  60.             for index_i := 0 to length(arr_a) - 1  do
  61.             begin
  62.                 for index_j :=0 to length(arr_a[0]) - 1 do
  63.                     arr_a[index_i][index_j] := arr_a[index_i][index_j] - arr_b[index_i][index_j];
  64.             end;
  65.         end;
  66.     end;
  67.     // procedure for writing into file and console
  68.     procedure writing_result( array_tmp : dynamic_array );
  69.     var
  70.         i , j : integer;
  71.         matrix_result : text ;
  72.     begin
  73.         writeln('Calculation has been made successfully');
  74.         assign(matrix_result, 'matrix_result.txt ');
  75.         Rewrite (matrix_result) ;
  76.         for i := 0 to length(array_tmp) - 1  do
  77.         begin
  78.             for j := 0 to length(array_tmp[i]) - 1  do
  79.             begin
  80.                 write(matrix_result, array_tmp[i][j] , ' ');
  81.                 write( array_tmp[i][j] , ' ');
  82.             end;
  83.             writeln('');
  84.             writeln(matrix_result,'');
  85.         end;
  86.         Close (matrix_result);
  87.     end;
  88. begin
  89.     read_file('matrix_first.txt' , matrix_first);
  90.     read_file('matrix_second.txt' , matrix_second);
  91.     if correctreading then
  92.     begin
  93.         repeat
  94.             write('Enter "sum" for the sum and "res" for residual: ');
  95.             readln(flag);
  96.             if ( (flag <> 'sum') and (flag <> 'res' ) ) then
  97.                 writeln('Invalid input. Try again.');
  98.         until ((flag = 'sum') or (flag = 'res' ));
  99.  
  100.         counting(matrix_first , matrix_second , flag);
  101.         writing_result( matrix_first );
  102.     end;
  103.     readln ;
  104. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement