📄 sum_str.~pas
字号:
unit sum_str;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Label3: TLabel;
OpenDialog1: TOpenDialog;
Button2: TButton;
memo1: TMemo;
Button1: TButton;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i,total:integer;
fname:string;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var
k,n1,n2:integer;
infile:TextFile;
instring,ch,ch2:string;
begin
i:=0;
ch:=edit1.Text;
n1:= length(edit1.Text);
if opendialog1.Execute then
begin
fname:=opendialog1.FileName;
AssignFile(infile,fname);
Reset(infile);
memo1.Lines.Add('-----------------------------------------------------------------------');
memo1.Lines.Add('待统计的字符串为'+edit1.Text+',长度为'+inttostr(n1));
while not eof(infile) do
begin
total:=total+1;
readln(infile,instring);
n2:=length(instring)+1;
//memo1.Lines.add(instring);
if n2>n1 then
begin
for k:=1 to n2-n1 do
begin
ch2:=copy(instring,k,n1);
if ch=ch2 then i:=i+1;
end;
end;
end ;
end;
if ch='' then showmessage(' 待统计的字符串为空格,请确认。要统计其他字符串请重新运行!');
memo1.Lines.Add('统计结果如下:');
memo1.Lines.Add('该文件包含字符串'+edit1.Text+' 的个数为:'+inttostr(i));
memo1.Lines.Add(' ');
label3.Caption:='该文件总行数为:'+inttostr(total)+'.';
label3.Caption:=label3.Caption+#13+'包含字符串'+edit1.Text+' 的个数为:'+inttostr(i)+'.';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
i:=0;
//edit1.Text:='aniu';
total:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -