Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program task3;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils, Math;
- type
- TInput = Class
- Str: string;
- Int: Integer;
- Double: Double;
- procedure SetStr;
- procedure SetInt;
- procedure SetDouble;
- constructor Create;
- end;
- constructor TInput.Create;
- begin
- Str := '';
- Int := 0;
- Double := 0;
- end;
- procedure TInput.SetStr;
- begin
- Readln(Str);
- end;
- procedure TInput.SetDouble;
- begin
- Readln(Double);
- end;
- procedure TInput.SetInt;
- begin
- Readln(Int);
- end;
- var
- InputObj: TInput;
- VectorType: String;
- VectorLength, MatrixCols, MatrixRows, i, j, k: Integer;
- Vector: Array of Double;
- Matrix, Product: Array of Array of Double;
- function Inp(ValType: String): TInput;
- var
- NotCorrect: Boolean;
- begin
- ValType := LowerCase(ValType);
- NotCorrect := true;
- while (NotCorrect) do
- begin
- try
- if (ValType = 'string') then
- InputObj.SetStr()
- else if (ValType = 'integer') then
- InputObj.SetInt()
- else if (ValType = 'double') then
- InputObj.SetDouble();
- NotCorrect := false;
- except
- Writeln('Введите значение типа ', ValType);
- end;
- end;
- Result := InputObj;
- end;
- begin
- Writeln('Данная программа находит произведение вектора на матрицу');
- Writeln;
- VectorType := '';
- InputObj := TInput.Create;
- while ((VectorType <> 'вектор-столбец') and
- (VectorType <> 'вектор-строка')) do
- begin
- Writeln('Введите тип вектора (вектор-столбец или вектор-строка)');
- VectorType := AnsiLowerCase(Inp('string').Str);
- end;
- Writeln('Введите количество элементов вектора');
- VectorLength := Inp('integer').Int;
- Writeln('Введите количество строк в матрице');
- MatrixRows := Inp('integer').Int;
- if (((VectorType = 'вектор-столбец') and (MatrixRows <> 1)) or
- ((VectorType = 'вектор-строка') and (MatrixRows <> VectorLength))) then
- begin
- Writeln('Число строк в матрице должно быть равно числу столбцов в векторе');
- end
- else
- begin
- Writeln('Введите количество столбцов в матрице');
- MatrixCols := Inp('integer').Int;
- SetLength(Vector, VectorLength);
- SetLength(Matrix, MatrixRows, MatrixCols);
- if (VectorType = 'вектор-строка') then
- VectorLength := 1;
- for i := 0 to High(Vector) do
- begin
- Writeln('Введите ', i + 1, '-й элемент вектора');
- Vector[i] := Inp('double').double;
- end;
- for i := 0 to High(Matrix) do
- begin
- Writeln('Заполните ', i + 1, ' строку матрицы');
- for j := 0 to High(Matrix[i]) do
- begin
- Writeln('Введите ', j + 1, '-й элемент строки');
- Matrix[i, j] := Inp('double').Double;
- end;
- end;
- SetLength(Product, VectorLength, MatrixCols);
- for i := 0 to High(Product) do
- for j := 0 to High(Matrix) do
- for k := 0 to High(Matrix[j]) do
- Product[i, k] := Product[i, k] +
- Vector[IfThen(VectorLength = 1, j, i)] * Matrix[j, k];
- Writeln('Результат :');
- for i := 0 to High(Product) do
- begin
- for j := 0 to High(Product[i]) do
- Write(Product[i, j]:5:2, ' ');
- Writeln('');
- end;
- end;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement