文字统计

类别:Delphi 点击:0 评论:0 推荐:

       现在有不少文字处理软件能实现文字统计功能,如果我们自己也能动手编制一个小程序来实现这一功能不就更有意义了吗?
    一、编程思路:
程序通过调用ord函数将Memo控件中所有字符转换为对应的数值,再通过Length获得Memo中字符的字节数,然后通过for I:=1 To Length(s)do来判断各个字节所对应的字符数值是否在33~126之间以确定是否为英文字符(中文字符个数即为它们所占字节数除以2)。
    二、编程步骤:
首先新建一个工程,保存好以后,form1的Caption属性设置为“字数统计”,从Standard页上添加3个Label,其Caption属性分别为:“请输入文字”,“字母数”和“汉字数”,同时设置Color属性为“clblack”。
添加MeMo控件,设置Color属性为“clmoneygreen”,name属性为“Memo1”,Scrollbars属性为“ssVertial”,添加Button控件4个,设置他们的CAPTION分别为:“粘贴文字”,“开始统计”和“退出清空”。最后再添加Edit控件2个,设置Color属性为“clmoneygreen”。
图一 运行界面
    三、完整程序代码
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button3: TButton;
Button4: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var
i,e,c:integer;
s:string;
begin
s:=memo1.Text ;
e:=0;c:=0;
for i:=1 to length(s) do
begin
if(ord(s[i])>=33)and(ord(s[i])<=126) then
begin
inc(e);
edit1.Text:=inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
edit2.Text:=inttostr(c div 2);
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.PasteFromClipboard ;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
memo1.clear;
edit1.clear;
edit2.clear;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;
end.

本文地址:http://com.8s8s.com/it/it5318.htm