Pascal之(二维数组)

二维数组
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBCtrls;

type
  queue = array[1..4, 1..20] of real;
  TForm1 = class(TForm)
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    lbl5: TLabel;
    btn1: TButton;
    edt1: TEdit;
    edt2: TEdit;
    edt3: TEdit;
    edt4: TEdit;
    dblst1: TDBListBox;
    lbl6: TLabel;
    btn2: TButton;
    btn3: TButton;
    labresult: TLabel;
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    function AVG(queue1: queue; m, n: integer): real;
    { Public declarations }
  end;

var
  Form1: TForm1;
  i: integer;
  score: queue;

implementation

{$R *.dfm}

function TForm1.AVG(queue1: queue; m, n: integer): real;
var
  k: integer;
  sum: real;
begin
  sum := 0;
  for k := 1 to n do
    sum := sum + queue1[m][k];
  Result := sum / n;
end;

procedure TForm1.btn2Click(Sender: TObject);
    var
    engave,mathave,vfpave,dataave:real;     // 分别存放四门功课的平均成绩
begin
     engave:=AVG(score,1,i);  // 调用函数aver,求i个学生英语成绩的平均值
    mathave:=AVG(score,2,i); // 调用函数aver,求i个学生数学成绩的平均值
    vfpave:=AVG(score,3,i);  // 调用函数aver,求i个学生vfp成绩的平均值
    dataave:=AVG(score,4,i); // 调用函数aver,求i个学生数据结构的平均值
    // 计算结果显示在labresult上
    labresult.caption:='各科的平均成绩是:'+chr(13)+chr(13);
    labresult.caption:=labresult.caption+'   英语:'+floattostr(engave);
    labresult.caption:=labresult.caption+' ; '+'   数学:'+floattostr(mathave);
    labresult.caption:=labresult.caption+' ; '+'   VFP:'+floattostr(vfpave);
    labresult.caption:=labresult.caption+';'+'   数据结构:'+floattostr(dataave);
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  str1: string;
begin
      i:=i+1;                             // 每输入一学生信息i加1
    score[1][i]:=strtofloat(edt1.Text);    //  score数组的第1行存放英语成绩
    score[2][i]:=strtofloat(edt2.Text);    //  score数组的第2行存放数学成绩
    score[3][i]:=strtofloat(edt3.Text);      //  score数组的第3行存放vfp成绩
    score[4][i]:=strtofloat(edt4.Text);    //  score数组的第4行存放数据结构
    str1:=edt1.Text+' '+edt2.Text +'  '+edt3.Text+'  ' +edt4.Text;
    dblst1.Items.add(str1) ; // 将第i个学生的各门成绩添加到scorelist列表中
    edt1.Text:='';    //清空用于输入成绩的各文本框
    edt2.Text:='';
    edt3.Text:='';
    edt4.Text:='';
    edt1.SetFocus;    // 光标定位在engedit文本框上,等待下次输入
end;

end.
原文地址:https://www.cnblogs.com/anbylau2130/p/2806953.html