读取文本文件并从中构建矩阵,Pascal

Reading a text file and from it constructing a matrix, Pascal

谁能帮我解决这个问题。所以,我需要构造一个矩阵,列数和行数也在矩阵的第一行,我会制作和举例,这样更清楚。

4 3  
1 2 3 
5 6 7 
9 10 8 
1 11 13

其中 m=4(行数)和 n=3(列数) 这是一个文本文件的示例。这样的事情甚至可能吗?

编辑:

 Program Feb;

 const
   max=100;

 type
   Matrix=array[1..max,1..max] of integer;

var datoteka:text;
  m,n:integer;
  counter:integer;

begin
  assign(datoteka,'datoteka.txt');
  reset(datoteka);
  while not eoln(datoteka) do
  begin
    read(datoteka, m);
    read(datoteka, n);
  end;
  repeat
    read eoln(n)
  until eof(datoteka) 
  write (m,n);
end.

我的代码帮不上什么大忙,因为我不知道怎么写。我了解所有基础知识,但只是不知道如何将它们组合在一起。

更新

既然你已经更新了你的 q,很明显你没有按照正确的方式来做这件事,imo。

看看我为完成任务编写的代码,然后再看看下面我的原始答案。

program Matrixtest;

uses
  sysutils;

var
  NoOfCols,
  NoOfRows : Integer;
  Source : TextFile;
  Matrix : array of array of integer;
  FileName : String;
  Row,
  Col   : Integer; // for-loop iterators to access a single cell of the matrix
  Value : Integer;
begin
  //  First, construct the name of the file defining the matrix
  //  This assumes that the file is in the same folder as this app
  FileName := ExtractFilePath(ParamStr(0)) + 'MatrixDef.Txt';
  writeln(FileName);  // echo it back to the screen so we can see it

  //  Next, open the file
  Assign(Source, FileName);
  Reset(Source);

  read(Source, NoOfRows, NoOfCols);
  writeln('Cols: ', NoOfCols, 'Rows: ', NoOfRows);
  SetLength(Matrix, NoOfCols, NoOfRows);

  readln(source);  // move to next line in file

  //  Next, read the array data
  for Row := 1 to NoOfRows do begin
    for Col := 1 to NoOfCols do begin
      read(Source, Value);
      Matrix[Col - 1, Row - 1] := Value;
    end;
  end;

  //  Display the array contents
  for Row := 1 to NoOfRows do begin
    for Col := 1 to NoOfCols do begin
      writeln('Row: ', Row, ' contents', Matrix[Col - 1, Row - 1]);
    end;
  end;

  Close(Source);  //  We're done with the file, so close it to release OS resources

  readln;  // this waits until you press a key, so you can read what's been displayed
end.

原回答

Is something like this even possible?

当然有可能,否则谁设计了这个任务 - 我认为这是课程作业 某种 - 不会打扰。

正如@Ken White 指出的那样,SO 不是代码编写服务,所以我不会向您展示执行您指定的操作的代码(我花了大约 10 分钟来编写和测试它) , 但下面应该告诉你自己写需要知道什么。

在你的程序中,你可以用一个二维的array来表示你的矩阵。一个快速 google 将确认 FPC 支持多维数组,如果您还不知道,请参阅 http://wiki.lazarus.freepascal.org/Multidimensional_arrays

即便如此,您所描述的任务确实非常困难"my first program",所以 我假设程序员对 FPC 有足够的了解,能够 执行更简单的任务,即读取大小为 在编译时已知 的数组 来自文本文件。

任务中的问题是你应该阅读维度(数量 运行-time 时矩阵的行和列)来自包含矩阵内容的文件。

当然,一种(极其浪费的)方法是声明 具有巨大尺寸的矩阵数组,比您在实践中期望的任何东西都大, 使用上面链接的文章中的数组声明类型。 我想这会得到零分作为任务的答案。

有效解决这个问题的关键是FPC支持dynamic arrays,其 您可以在 运行 时设置维度。所以,要使用它,你需要知道

  1. 如何在FPC中声明一个dynamic array

  2. 如何在运行时设置数组的维度,一旦你拿起它们 来自你的矩阵定义文件(提示:SetLength 是这样做的方法,但是 问题是,你如何将它用于多维数组?)

  3. FPC dynamic array 是从零开始的事实

1 和 2 的答案可以通过谷歌搜索找到,只有一个答案 google 应该找到显示如何同时执行这两项操作的内容。

最简单的应对方式 第 3 点是编写代码(根据 RowColumn 变量),就像 矩阵被声明为 array[1..NoOfRows, 1..NoOfColumns] 并减去 数组索引中的一个 只有 当您实际访问数组时,如

Row := 3;
Column := 4;
Value := Matrix[Row - 1, Column - 1];

所以,尝试自己编写代码,看看进展如何。如果您遇到困难,一定要 post 一个 new 问题,显示您目前编写的代码并准确解释它的问题所在。