枚举类型应用

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

type
  week = (sun, mon, tue, wed, thu, fri, sat);           //枚举类型的定义

{$R *.dfm}

function mday(day: week): string;                       //自定义星期转换函数
begin
  case day of
    sun: mday := '星期天';
    mon: mday := '星期一';
    tue: mday := '星期二';
    wed: mday := '星期三';
    thu: mday := '星期四';
    fri: mday := '星期五';
    sat: mday := '星期六';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);         //按钮1事件
var
  today, yesterday, tomorrow: week;                     //声明枚举类型变量
  n: integer;
begin
  n := dayofweek(now);                                  //得到整数序号
  case n of
    1: today := sun;                                    //为枚举变量赋值
    2: today := mon;
    3: today := tue;
    4: today := wed;
    5: today := thu;
    6: today := fri;
    7: today := sat;
  end;
  if today = low(week) then                             //如果今天星期天
    yesterday := high(week)                             //昨天是星期六
  else
    yesterday := pred(today);
  if today = high(week) then                            //如果今天是星期六
    tomorrow := low(week)                               //明天是星期天
  else
    tomorrow := succ(today);
  button1.Caption := '昨天';
  button2.Caption := '今天';
  button3.Caption := '明天';
  case (sender as tbutton).Tag of                       //三个按钮共享一段代码
    0: button1.Caption := '昨天是' + mday(yesterday);
    1: button2.Caption := '今天是' + mday(today);
    2: button3.Caption := '明天是' + mday(tomorrow);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);           //窗体创建事件
var
  year, month, day: word;
begin
  decodedate(date, year, month, day);
  edit1.Text := format('%d年%d月%d日', [year, month, day]);
end;

end.

 

posted @ 2011-12-08 08:13  endsnow  阅读(138)  评论(0)    收藏  举报