用Delphi轻松实现字数统计
点击次数:39 次 发布日期:2008-11-09 08:42:19 作者:源代码网
|
源代码网推荐 一、编程思路: 源代码网推荐 程序通过调用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. 源代码网推荐 以上程序在Delphi 6.0中编写,并在Windows 98/2000中通过。 源代码网推荐 源代码网推荐 源代码网供稿. |
