📄 parserfm.pas
字号:
unit ParserFM;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,StrFunc;
type
TForm1 = class(TForm)
EdtInput: TEdit;
BtnAdd: TButton;
LBWords: TListBox;
BtnAddWord: TButton;
BtnDelWord: TButton;
EdtInputWord: TEdit;
BtnFindWord: TButton;
BtnSort: TButton;
BtnClearAll: TButton;
Label1: TLabel;
Label2: TLabel;
RGSort: TRadioGroup;
procedure BtnAddClick(Sender: TObject);
procedure BtnFindWordClick(Sender: TObject);
procedure BtnAddWordClick(Sender: TObject);
procedure BtnDelWordClick(Sender: TObject);
procedure BtnSortClick(Sender: TObject);
procedure BtnClearAllClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BtnAddClick(Sender: TObject);
var
S,T:string;
InWord: boolean;
Index,FirstChar:Integer;
begin
LBWords.Items.Clear; //先清空列表框里的所有单词
S:=EdtInput.Text +' '; //字符串S代表EdtInput里的文本,后面跟一个空格
InWord:=False; //是在单词中还是在单词外
Index:=1; //Index代表当前读到字符串S的第几个字符了
FirstChar:=1; //当前单词的一个字母在S中的索引值
While Index <= Length(S) do //只要字符串S没结束就继续
begin
T:=Copy(S,Index,1); //读出S的当前字符
if ( (T>='a') and (T<='z')) or
( (T>='A') and (T<='Z')) then //如果是英文字母
begin
if not InWord then FirstChar:=Index; //如果是一个单词的开始,就让FirstChar:=Index
InWord:=True; //现在在单词中
end
else if T=' ' then //如果是空格,表明不在单词中或者单词结束
begin
if InWord then //单词结束
begin
T:=Copy(S,FirstChar,Index-FirstChar); //把单词拷贝出来
LBWords.Items.Add(T); //添加到列表框里
InWord:=False; //不在单词中
end;
end
else //输入了其它字符,报错,推出循环
begin
ShowMessage('你的输入有误');
Break;
end;
Inc(Index); //增加索引值。不可少!否则死循环
end; //循环结束
end;
procedure TForm1.BtnFindWordClick(Sender: TObject);
var
S:string;
Index:integer;
begin
S:=Trim(EdtInputWord.Text);
Index:=LBWords.Items.IndexOf(S); //在LBWords中寻找一下S,返回索引值
if Index=-1 then //未找到
ShowMessage('未找到 '+S)
else //找到
LBWords.Selected[Index]:=True; //选择它
end;
procedure TForm1.BtnAddWordClick(Sender: TObject);
var
S:string;
begin
S:=Trim(EdtInputWord.Text);
if IsAWord(S) then
LBWords.Items.Add(S)
else
ShowMessage('输入的单词有误');
end;
procedure TForm1.BtnDelWordClick(Sender: TObject);
begin
LBWords.Items.Delete(LBWords.ItemIndex);
end;
procedure TForm1.BtnSortClick(Sender: TObject);
begin
if RGSort.ItemIndex=0 then //排序
LBWords.Sorted:=True
else
LBWords.Sorted:=False;
end;
procedure TForm1.BtnClearAllClick(Sender: TObject);
begin
LBWords.Clear;
end;
//one two three four five six seven eight nine ten
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -