学生成绩管理

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

type
  studentrecord = record                          //记录类型
    xh,xm: string[6];                             //学号和姓名
    yw,sx,wy: integer;                            //三门成绩
  end;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);   //下一条按钮
begin                                             //清空编辑框
  edit1.Text := '';
  edit2.Text := '';
  edit3.Text := '';
  edit4.Text := '';
  edit5.Text := '';
end;

procedure TForm1.Button2Click(Sender: TObject);   //添加按钮
var
  t: studentrecord;                               //定义记录类型
  f: file of studentrecord;                       //定义文件记录类型
  size: integer;
begin
  t.xh := edit1.Text;                             //添加学号
  t.xm := edit2.Text;                             //添加姓名
  t.yw := strtoint(edit3.Text);                   //添加语文成绩
  t.wy := strtoint(edit4.Text);                   //添加数学成绩
  t.sx := strtoint(edit5.Text);                   //添加外语成绩
  assignfile(f,'c:\文件.dat');                    //关联文件
  reset(f);                                       //只读打开, 指针移到文件头
  size := filesize(f);                            //返回文件大小
  seek(f,size);                                   //将文件指针移至文件位置
  write(f,t);                                     //写入数据
  listbox1.Items.Clear;                           //清空列表框
  seek(f,0);                                      //将文件指针移至文件头
  while not eof(f) do                             //遍历文件
    begin
      read(f,t);                                  //读取文件数据
      listbox1.Items.Add(format('%s %15s %15s %15s %15s', [t.xh, t.xm, inttostr(t.yw), inttostr(t.wy), inttostr(t.sx)]));
    end;
  closefile(f);                                   //关闭文件
end;

procedure TForm1.Button3Click(Sender: TObject);   //删除
var
  pos: integer;
  t: studentrecord;
  f: file of studentrecord;
begin
  pos := listbox1.ItemIndex;
  assignfile(f,'c:\文件.dat');
  reset(f);                                       //重新载入数据
  seek(f,pos+1);                                  //将文件指针移向要删除数据的下一条
  while not eof(f) do
    begin
      read(f,t);                                  //读取文件数据到t
      seek(f,pos);                                //当前要删除数据的位置
      pos := pos + 1;
      write(f,t);                                 //将下一条数据覆盖当前要删除的数据
      seek(f,pos+1);                              //指针位移
    end;
  seek(f,pos);                                    //当前有效数据位置
  truncate(f);                                    //截去当前位置后的所有数据
  seek(f,0);                                      //将指针移至文件头
  listbox1.Items.Clear;
  while not eof(f) do
    begin
      read(f,t);
      listbox1.Items.Add(format('%s %15s %15s %15s %15s', [t.xh, t.xm, inttostr(t.yw), inttostr(t.wy), inttostr(t.sx)]));
    end;
  closefile(f);
end;

procedure TForm1.FormCreate(Sender: TObject);     //程序初始化
var
  t: studentrecord;
  f: file of studentrecord;
begin
  assignfile(f,'c:\文件.dat');
  if fileexists('c:\文件.dat') then               //如果文件存在
    reset(f)                                      //读取文件
  else                                            //文件不存在
    rewrite(f);                                   //创建文件
  while not eof(f) do
    begin
      read(f,t);
      listbox1.Items.Add(format('%s %15s %15s %15s %15s', [t.xh, t.xm, inttostr(t.yw), inttostr(t.wy), inttostr(t.sx)]));
    end;
  closefile(f);
  button3.Enabled := false;
end;

procedure TForm1.ListBox1Click(Sender: TObject);  //点击列表框
begin
  if listbox1.ItemIndex > -1 then
    button3.Enabled := true
  else
    button3.Enabled := false;
end;

end.

 

posted @ 2011-12-09 16:44  endsnow  阅读(285)  评论(0)    收藏  举报