2014年7月1日 星期二

Delphi 7 Class II

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  a: array[1..100] of integer;
  k: integer=0;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 k := k+1;
 a[k] := SpinEdit1.Value;
Label1.Caption := '第' + IntTOStr(k) + ' 位學生' + IntTOStr (A[k]) + '分'
 end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
 Memo1.Clear;
 for i := 1 to k do Memo1.Lines.Add(IntTOStr(a[i]))
end;

end.



------------------------------------------------------


unit Unit2;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    SpinEdit2: TSpinEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  stu = record
     name: string[12];
     eng,mat: integer;
   end;
var
  Form1: TForm1;
  a: array[1..100] of stu;
  k: integer=0;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 k := k+1;
 a[k].name := '學生' + IntTOStr(k);
 a[k].eng := SpinEdit1.Value;
 a[k].mat := SpinEdit2.Value;
 Label1.Caption := '第' + IntTOStr(k) + ' 位學生' + IntTOStr (A[k].eng) + ',' + IntTOStr (A[k].mat) + '分'
 end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
 Memo1.Clear;
 for i := 1 to k do Memo1.Lines.Add(a[i].name + ':' + IntTOStr(a[i].eng) + ',' + IntTOStr(a[i].mat));
end;

end.


---------------------------------------


unit Uni3;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    SpinEdit2: TSpinEdit;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

//宣告形式
  stu = record
     name: string[12];
     eng,mat: integer;
   end;

var
  Form1: TForm1;

//宣告陣列
  a: array[1..100] of stu;
  k: integer=0;
//宣告檔案 
 f: file of stu;
implementation

{$R *.dfm}

//bottom 1 點擊輸入成績
procedure TForm1.Button1Click(Sender: TObject);
begin
 k := k+1;
 a[k].name := '學生' + IntTOStr(k);
 a[k].eng := SpinEdit1.Value;
 a[k].mat := SpinEdit2.Value;
 Label1.Caption := '第' + IntTOStr(k) + ' 位學生' + IntTOStr (A[k].eng) + ',' + IntTOStr (A[k].mat) + '分'
 end;

//bottom2 點擊產生memo列表
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
 Memo1.Clear;
 for i := 1 to k do Memo1.Lines.Add(a[i].name + ':' + IntTOStr(a[i].eng) + ',' + IntTOStr(a[i].mat));
end;

//bot
procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
begin
  AssignFile(F, 'stu.txt'); Rewrite(f);
  for i :=  1 to k do Write(f, a[i]);
  CloseFile(f);
end;

end.


--------------------------------------


unit Uni3;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    SpinEdit2: TSpinEdit;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  stu = record
     name: string[12];
     eng,mat: integer;
     end;
 Const FileName = 'stu.txt';

 var
  Form1: TForm1;
  a: array[1..100] of stu;
  k: integer=0;
  f: file of stu;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 k := k+1;
 a[k].name := '學生' + IntTOStr(k);
 a[k].eng := SpinEdit1.Value;
 a[k].mat := SpinEdit2.Value;
 Label1.Caption := '第' + IntTOStr(k) + ' 位學生' + IntTOStr (A[k].eng) + ',' + IntTOStr (A[k].mat) + '分'
 end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
 Memo1.Clear;
 for i := 1 to k do Memo1.Lines.Add(a[i].name + ':' + IntTOStr(a[i].eng) + ',' + IntTOStr(a[i].mat));
end;

procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
begin
  AssignFile(F, FileName);
  if FileExists(FileName) then begin Reset(f); seek(f,FileSize(f)); end else Rewrite(f);
  for i :=  1 to k do Write(f, a[i]);
  CloseFile(f);
end;

procedure TForm1.Button4Click(Sender: TObject);
var student: stu;
begin
 AssignFile(f, FileName);
  if FileExists(FileName) then Reset(f) else begin ShowMessage('File not found'); Exit; end;
  while not eof(f) do begin
  Read(f, student);
  Memo1.Lines.Add(student.name + ':' + IntTOStr(student.eng) + ',' + IntTOStr(student.mat));
  End;
  CloseFile(f);
end;

end.

0 意見: