Advertisement
Alyks

Untitled

Oct 27th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.38 KB | None | 0 0
  1. program task3;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils, Math;
  8.  
  9. type
  10.     TInput = Class
  11.         Str: string;
  12.         Int: Integer;
  13.         Double: Double;
  14.  
  15.         procedure SetStr;
  16.         procedure SetInt;
  17.         procedure SetDouble;
  18.         constructor Create;
  19.     end;
  20.  
  21. constructor TInput.Create;
  22. begin
  23.     Str := '';
  24.     Int := 0;
  25.     Double := 0;
  26. end;
  27.  
  28. procedure TInput.SetStr;
  29. begin
  30.     Readln(Str);
  31. end;
  32.  
  33. procedure TInput.SetDouble;
  34. begin
  35.     Readln(Double);
  36. end;
  37.  
  38. procedure TInput.SetInt;
  39. begin
  40.     Readln(Int);
  41. end;
  42.  
  43. var
  44.     InputObj: TInput;
  45.     VectorType: String;
  46.     VectorLength, MatrixCols, MatrixRows, i, j, k: Integer;
  47.     Vector: Array of Double;
  48.     Matrix, Product: Array of Array of Double;
  49.  
  50. function Inp(ValType: String): TInput;
  51. var
  52.     NotCorrect: Boolean;
  53. begin
  54.     ValType := LowerCase(ValType);
  55.     NotCorrect := true;
  56.     while (NotCorrect) do
  57.     begin
  58.         try
  59.             if (ValType = 'string') then
  60.                 InputObj.SetStr()
  61.             else if (ValType = 'integer') then
  62.                 InputObj.SetInt()
  63.             else if (ValType = 'double') then
  64.                 InputObj.SetDouble();
  65.             NotCorrect := false;
  66.         except
  67.             Writeln('Введите значение типа ', ValType);
  68.         end;
  69.     end;
  70.     Result := InputObj;
  71. end;
  72.  
  73. begin
  74.     Writeln('Данная программа находит произведение вектора на матрицу');
  75.     Writeln;
  76.     VectorType := '';
  77.     InputObj := TInput.Create;
  78.  
  79.     while ((VectorType <> 'вектор-столбец') and
  80.         (VectorType <> 'вектор-строка')) do
  81.     begin
  82.         Writeln('Введите тип вектора (вектор-столбец или вектор-строка)');
  83.         VectorType := AnsiLowerCase(Inp('string').Str);
  84.     end;
  85.  
  86.     Writeln('Введите количество элементов вектора');
  87.     VectorLength := Inp('integer').Int;
  88.     Writeln('Введите количество строк в матрице');
  89.     MatrixRows := Inp('integer').Int;
  90.  
  91.     if (((VectorType = 'вектор-столбец') and (MatrixRows <> 1)) or
  92.         ((VectorType = 'вектор-строка') and (MatrixRows <> VectorLength))) then
  93.     begin
  94.         Writeln('Число строк в матрице должно быть равно числу столбцов в векторе');
  95.     end
  96.     else
  97.     begin
  98.         Writeln('Введите количество столбцов в матрице');
  99.         MatrixCols := Inp('integer').Int;
  100.         SetLength(Vector, VectorLength);
  101.         SetLength(Matrix, MatrixRows, MatrixCols);
  102.  
  103.         if (VectorType = 'вектор-строка') then
  104.             VectorLength := 1;
  105.  
  106.         for i := 0 to High(Vector) do
  107.         begin
  108.             Writeln('Введите ', i + 1, '-й элемент вектора');
  109.             Vector[i] := Inp('double').double;
  110.         end;
  111.  
  112.         for i := 0 to High(Matrix) do
  113.         begin
  114.             Writeln('Заполните ', i + 1, ' строку матрицы');
  115.             for j := 0 to High(Matrix[i]) do
  116.             begin
  117.                 Writeln('Введите ', j + 1, '-й элемент строки');
  118.                 Matrix[i, j] := Inp('double').Double;
  119.             end;
  120.         end;
  121.  
  122.         SetLength(Product, VectorLength, MatrixCols);
  123.  
  124.         for i := 0 to High(Product) do
  125.             for j := 0 to High(Matrix) do
  126.                 for k := 0 to High(Matrix[j]) do
  127.                     Product[i, k] := Product[i, k] +
  128.                         Vector[IfThen(VectorLength = 1, j, i)] * Matrix[j, k];
  129.  
  130.         Writeln('Результат :');
  131.         for i := 0 to High(Product) do
  132.         begin
  133.             for j := 0 to High(Product[i]) do
  134.                 Write(Product[i, j]:5:2, ' ');
  135.             Writeln('');
  136.         end;
  137.     end;
  138.     Readln;
  139. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement