📄 main.pas
字号:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxControls, cxContainer, cxEdit, cxTextEdit, cxMemo, cxRichEdit,
Menus, cxLookAndFeelPainters, cxCheckBox, StdCtrls, cxButtons, iNearOne,
iControls, cxLookAndFeels, Buttons;
type
TForm4 = class(TDialogForm)
edtISBN1: TcxRichEdit;
edtISBN2: TcxRichEdit;
cxButton1: TcxButton;
cxButton2: TcxButton;
chkSeparate: TcxCheckBox;
Label1: TLabel;
Label2: TLabel;
cxLookAndFeelController1: TcxLookAndFeelController;
Label3: TLabel;
Label4: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure cxButton2Click(Sender: TObject);
procedure cxButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
{ ISBN 校验}
function isbn_Examine(var isbn: String): Boolean;
{ ISBN 拆分 }
function isbn_Separate(isbn: String): String;
implementation
uses iStdComm;
{$R *.dfm}
{ isbn_Examine }
function isbn_Examine(var isbn: String): Boolean;
var
i, a, b, c: Integer;
long: Boolean;
begin
Result := False;
while Pos('-',isbn)<>0 do
Delete(isbn, Pos('-',isbn), 1);
if Length(isbn)<9 then Exit;
for I := 1 to 9 do
if not (isbn[I] in ['0'..'9','X','x']) then Exit;
long := Pos('978', isbn)=1;
a := 0;
if long then
begin
isbn := Copy(isbn, 1, 12);
a := 0; b := 0;
for I := 1 to Length(isbn) do
begin
if I mod 2=0 then
a := a + VarAsInteger(isbn[I])
else
b := b + VarAsInteger(isbn[I]);
end;
a := a*3;
c := a + b;
c := c mod 10;
if c<>0 then c := 10 - c;
isbn := isbn + VarToStr(c);
end else
begin
isbn := Copy(isbn, 1, 9);
for I := 1 to Length(isbn) do
a := a + VarAsInteger(isbn[I]) *(11-i);
a := (11 - (a mod 11));
if a=11 then
isbn := isbn + '0'
else if a=10 then
isbn := isbn + 'X'
else
isbn := isbn + VarToStr(a);
end;
Result := True;
end;
{ isbn_Separate }
function isbn_Separate(isbn: String): String;
var
S: array[0..3] of String;
c: Char;
i, len: Integer;
long: Boolean;
begin
Result := isbn;
long := Pos('978', isbn)=1;
if not isbn_Examine(isbn) then Exit;
if long then Delete(isbn, 1, 3);
s[0] := isbn[1];
if s[0]='7' then //中国标准书号
begin
C := isbn[2];
case C of
'0': Len := 2;
'1'..'4': Len := 3;
'5'..'7': Len := 4;
'8': Len := 5;
'9': Len := 6;
else Len := 2;
end;
s[1] := Copy(isbn, 2, Len);
Inc(Len);
s[2] := Copy(isbn, Len+1, 9-Len);
s[3] := Copy(isbn, 10, 1);
end else //国际标准书号
begin
if Copy(isbn,1, 1)<'8' then
Len := 1
else if Copy(isbn,1, 2)<'95' then
Len := 2
else if Copy(isbn,1, 3)<'996' then
Len := 3
else if Copy(isbn,1, 3)<'9990' then
Len := 4
else
Len := 5;
s[0] := Copy(isbn, 1, Len);
I := Len;
Inc(Len);
if Copy(isbn,Len, 2)<'20' then
Len := 2
else if Copy(isbn,Len, 3)<'700' then
Len := 3
else if Copy(isbn,Len, 4)<'8500' then
Len := 4
else if Copy(isbn,Len, 5)<'90000' then
Len := 5
else if Copy(isbn,Len, 5)<'950000' then
Len := 6
else
Len := 7;
s[1] := Copy(isbn, I+1, Len);
s[2] := Copy(isbn, I+Len+1, 9-Len-I);
s[3] := Copy(isbn, 10, 1);
end;
Result := s[0] + '-' + s[1] + '-' + s[2] + '-' + s[3];
if long then Result := '978-' + Result;
end;
procedure TForm4.cxButton1Click(Sender: TObject);
var
I: Integer;
S: String;
begin
edtISBN2.Lines.Text := '';
with edtISBN1.Lines do
for I := 0 to Count - 1 do
if Trim(Strings[I])<>'' then
begin
S := Strings[I];
if Pos('978', S)=1 then System.Delete(S, 1, 3);
S := '978' + S;
isbn_Examine(S);
if chkSeparate.Checked then
S := isbn_Separate(S);
edtISBN2.Lines.Add(S);
end else
edtISBN2.Lines.Add('');
end;
procedure TForm4.cxButton2Click(Sender: TObject);
var
I: Integer;
S: String;
begin
edtISBN1.Lines.Text := '';
with edtISBN2.Lines do
for I := 0 to Count - 1 do
if Trim(Strings[I])<>'' then
begin
S := Strings[I];
if Pos('978', S)=1 then System.Delete(S, 1, 3);
isbn_Examine(S);
if chkSeparate.Checked then
S := isbn_Separate(S);
edtISBN1.Lines.Add(S);
end else
edtISBN1.Lines.Add('');
end;
procedure TForm4.SpeedButton1Click(Sender: TObject);
begin
with TOpenDialog.Create(nil) do
begin
Filter := '文本文件 (*.txt)|*.TXT|所有文件(*.*)|*.*';
DefaultExt := '.txt';
if Execute then
case TControl(Sender).Tag of
0: edtISBN1.Lines.LoadFromFile(FileName);
1: edtISBN2.Lines.LoadFromFile(FileName);
end;
Free;
end;
end;
procedure TForm4.SpeedButton2Click(Sender: TObject);
var
FList: TStringList;
begin
with TSaveDialog.Create(nil) do
begin
Filter := '文本文件 (*.txt)|*.TXT|所有文件(*.*)|*.*';
DefaultExt := '.txt';
if Execute then
begin
FList := TStringList.Create;
case TControl(Sender).Tag of
0: FList.Assign(edtISBN1.Lines);
1: FList.Assign(edtISBN2.Lines);
end;
ShowMessage(FList.Text);
FList.SaveToFile(FileName);
FList.Free;
end;
Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -