為了能慢慢熟悉Delphi語言
之前寫過猜數字以後
這次寫一個身分證產生器當作另一個題目練習
身分證驗證規則如圖
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0O6tCHHg_0GIr5vZXHnH57bRYI2_SbOTJRVd2btogG-pwi5UC0Wbk3yifo37NQjVNdVvyvT5EAgHymBM0_KLhSnUZg-CJefkaaRsRWVG2_KAUqJmMSNKOAVSmsS0j_zmI6n0S1B1UvJc/s400/123.jpg)
表單格式如上圖
程式碼 :
unit bodycode;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
bodyCode: TEdit;
Button1: TButton;
ComboBox1: TComboBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
fir,sel,fin,sec : String;
i,las,pos,len,s1,s2,sum,s1a,s1b : Integer;
s : array[0..6] of Integer;
begin
pos := combobox1.ItemIndex; //user選擇的選項位置
sel := combobox1.Items.Strings[pos]; //把所選的字串抓出
len := Length(sel); //字串長度算出
fir := copy(sel,len,1); //抓出最後面的數字
s1 := StrToInt(copy(sel,1,2))+9; //將編號+9
if Radiobutton1.Checked = true then
sec := '1' //選男生就等於1
else if Radiobutton2.Checked = true then
sec := '2' //女生等於2
else
begin
showMessage('Please Choose gender!');
exit;
end;
sum := 0;
for i := 0 to 6 do
begin
Randomize;
s[i] := trunc(Random(10)); //隨機產生中間7碼
sum := sum + s[i]*(7-i); //產生後乘上數字累加上去
fin := fin + IntToStr(s[i]); //同時將中間字串產生
end;
s2 := StrToInt(sec)*8;
s1a := (s1 div 10)*1;
s1b := (s1 mod 10)*9;
sum := sum+s1a+s1b+s2; //產生驗證數字總合
if (sum mod 10) = 0 then
las := 0
else
las := 10-(sum mod 10); //產生最後一碼
fin := fir + sec + fin + IntToStr(las); //身分證字號產出
bodyCode.Text := fin;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.ItemIndex := 0; //強制設定一開始就選到第一個選項
end;
end.
沒有留言:
張貼留言