Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program task3;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils, Math, Classes;
- type
- Arr = Array of Double;
- TwoDimArr = Array of Arr;
- var
- MatrixRows, MatrixCols, VectorLength: Integer;
- VectorType, FilePath, Res: String;
- Vector: Arr;
- Matrix, Product: TwoDimArr;
- InputFile, OutputFile: TextFile;
- NotCorrect: Boolean;
- procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);
- begin
- ListOfStrings.Clear();
- ListOfStrings.Delimiter := Delimiter;
- ListOfStrings.DelimitedText := Str;
- end;
- function ReadNextLine(const FileToRead: TextFile): String;
- var
- Line: String;
- begin
- Line := '';
- try
- Readln(FileToRead, Line);
- except
- Writeln('Возникла ошибка при чтении файла.');
- end;
- ReadNextLine := Line;
- end;
- function ShowMatrix(const Matrix: TwoDimArr): String;
- var
- i, j: Integer;
- El: Double;
- Res: String;
- begin
- for i := 0 to High(Matrix) do
- begin
- for j := 0 to High(Matrix[i]) do
- begin
- El := Matrix[i, j];
- Write(El:5:2, ' ');
- Res := Res + FloatToStr(El) + ' ';
- end;
- Writeln;
- end;
- ShowMatrix := Res;
- end;
- procedure ShowVector(const Vector: Arr);
- var
- i: Integer;
- begin
- for i := 0 to High(Vector) do
- Write(Vector[i]:5:2, ' ');
- Writeln;
- end;
- function TakeRow(Str: String; Size: Integer): Arr;
- var
- i, Len, El, errPos: Integer;
- Row: Arr;
- NumsStr: String;
- OutputList: TStringList;
- begin
- SetLength(Row, Size);
- OutputList := TStringList.Create();
- Split(' ', Str, OutputList);
- Len := Size - 1;
- if (Len = OutputList.Count - 1) then
- for i := 0 to Len do
- begin
- Val(OutputList[i], El, errPos);
- if (errPos = 0) then
- Row[i] := El;
- end
- else
- Writeln('Количество столбцов, введенных в файле, должно совпадать с количеством столбцов в матрице');
- TakeRow := Row;
- end;
- function TakeVector(const InputFile: TextFile; var VectorType: String): Arr;
- var
- Vector: Arr;
- VectorLength: Integer;
- CurrentString: String;
- NotCorrect, Stop: Boolean;
- begin
- NotCorrect := true;
- Stop := false;
- Reset(InputFile);
- while (NotCorrect and (Not Stop)) do
- begin
- if (EOF(InputFile)) then
- begin
- Writeln('Вы не указали тип вектора');
- Stop := true;
- end
- else
- begin
- VectorType := ReadNextLine(InputFile);
- if ((VectorType = 'вектор-столбец') or
- (VectorType = 'вектор-строка')) then
- NotCorrect := false;
- end;
- end;
- if (Not NotCorrect) then
- begin
- VectorLength := StrToInt(ReadNextLine(InputFile));
- SetLength(Vector, VectorLength);
- CurrentString := ReadNextLine(InputFile);
- Vector := TakeRow(CurrentString, VectorLength);
- end;
- TakeVector := Vector;
- end;
- function TakeMatrixFromFile(const InputFile: TextFile): TwoDimArr;
- var
- CurrentString: String;
- MatrixRows, MatrixCols, i: Integer;
- Matrix: TwoDimArr;
- MatrixRow: Arr;
- begin
- i := 0;
- Reset(InputFile);
- MatrixRows := StrToInt(ReadNextLine(InputFile));
- MatrixCols := StrToInt(ReadNextLine(InputFile));
- SetLength(Matrix, MatrixRows, MatrixCols);
- while (i < MatrixRows) do
- begin
- CurrentString := ReadNextLine(InputFile);
- MatrixRow := TakeRow(CurrentString, MatrixCols);
- Matrix[i] := MatrixRow;
- i := i + 1;
- end;
- CloseFile(InputFile);
- TakeMatrixFromFile := Matrix;
- end;
- function TakeProduct(const Matrix: TwoDimArr; const Vector: Arr;
- VectorLength: Integer): TwoDimArr;
- var
- i, j, k: Integer;
- Product: TwoDimArr;
- begin
- SetLength(Product, VectorLength, Length(Matrix[0]));
- 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];
- TakeProduct := Product;
- end;
- begin
- NotCorrect := true;
- Writeln('Данная программа находит произведение вектора на матрицу');
- Writeln;
- Writeln('Введите путь до файла');
- Readln(FilePath);
- if (FileExists(FilePath)) then
- begin
- AssignFile(InputFile, FilePath);
- Matrix := TakeMatrixFromFile(InputFile);
- Writeln('Матрица:');
- ShowMatrix(Matrix);
- Vector := TakeVector(InputFile, VectorType);
- Writeln('Тип вектора: ', VectorType);
- Writeln('Вектор:');
- ShowVector(Vector);
- MatrixRows := Length(Matrix);
- MatrixCols := Length(Matrix[0]);
- VectorLength := Length(Vector);
- NotCorrect := false;
- end
- else
- Writeln('Указанный вами файл не существует');
- if (Not NotCorrect) then
- begin
- if (((VectorType = 'вектор-столбец') and (MatrixRows <> 1)) or
- ((VectorType = 'вектор-строка') and (MatrixRows <> VectorLength)))
- then
- begin
- Writeln('Число строк в матрице должно быть равно числу столбцов в векторе');
- end
- else
- begin
- if (VectorType = 'вектор-строка') then
- VectorLength := 1;
- SetLength(Product, VectorLength, MatrixCols);
- Product := TakeProduct(Matrix, Vector, VectorLength);
- Writeln('Результат:');
- Res := ShowMatrix(Product);
- try
- AssignFile(OutputFile, 'output.txt');
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Результат: ', Res);
- CloseFile(OutputFile);
- Writeln('Результат сохранен в файл output.txt');
- except
- Writeln('Произошла ошибка при сохранении файла');
- end;
- end;
- end;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement