勿须多言,看代码吧。:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Menus; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Label1: TLabel; Button2: TButton; Label2: TLabel; Label3: TLabel; Label4: TLabel; Button3: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; MainMenu1: TMainMenu; g1: TMenuItem; N1: TMenuItem; Label5: TLabel; Memo2: TMemo; Label6: TLabel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure UseByteType(SpecStr:String); procedure Button3Click(Sender: TObject); procedure N1Click(Sender: TObject); procedure Memo2Change(Sender: TObject); procedure Memo1Change(Sender: TObject); { Public declarations } private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin label1.Caption := Format('当前列:%d, 当前行:%d', [Memo1.CaretPos.X, Memo1.CaretPos.Y]); end; procedure TForm1.Button2Click(Sender: TObject); var s: string; i,sum,e,c,t: Integer; CharIndex,NumIndex,ChinSimInedx: Integer; begin s := Memo1.Text; CharIndex:=0; NumIndex:=0; ChinSimInedx:=0; e := 0; c := 0; sum := Length(s); for i := 0 to sum do begin if (Ord(s[i]) >= 33) and (Ord(s[i]) <= 126) then //取其ASCII值 begin //Inc(e); //Label2.Caption := '字母数: ' + IntToStr(e); CharIndex:= CharIndex+1; // end; if(Ord(s[i])>=65) and (Ord(s[i])<=90)then begin NumIndex :=NumIndex+1; end; if Ord(s[i]) >= 127 then begin Inc(c); end; edit1.Text:=IntToStr(e); edit2.Text := IntToStr(NumIndex); Label3.Caption := '汉字数: ' + IntToStr(c div 2); end; end; procedure TForm1.FormCreate(Sender: TObject); begin form1.Memo1.Text:=''; form1.Memo2.Text:=''; memo2.Font:=memo1.Font; label6.Font:=label5.Font; button2.Enabled:=false; end; procedure TForm1.UseByteType(SpecStr:String); var I,E,C,O,N:integer; begin E:=0; C:=0; O:=0; N:=0; for I:=1 to Length(SpecStr) do begin if (ByteType(SpecStr,I)=mbSingleByte) then begin if (SpecStr[I] in ['a'..'z','A'..'Z']) then Inc(E) else Inc(O); end else Inc(C); if(ByteType(SpecStr,I)=mbSingleByte)then begin if (SpecStr[I] in ['0'..'9']) then N:=N+1; end end; // Application.MessageBox(Pchar('该字符串的组成结构为:汉字'+IntToStr(C div 2)+'个,'+'英文字母'+IntToStr(E)+'个,'+'其他类型字符'+IntToStr(O)+'个'),'提示',mb_ok+mb_iconinformation); //Label2.Caption := '字母数: ' + IntToStr(E); //Label3.Caption := '汉字数: ' + IntToStr(c div 2); edit1.Text:= IntToStr(E); edit2.Text := IntToStr(c div 2); edit3.Text:= IntToStr(N); end; procedure TForm1.Button3Click(Sender: TObject); begin UseByteType(form1.Memo1.Text); end; procedure TForm1.N1Click(Sender: TObject); begin Application.MessageBox(Pchar('由于用OnTextChange()方法会增加系统开销,所有用一个Button的OnClicl方法来做。'+#13+'学号:12007242622 姓名:马金泽'),'提示',mb_ok+mb_iconinformation); end; procedure TForm1.Memo2Change(Sender: TObject); begin UseByteType(form1.Memo2.Text); end; procedure TForm1.Memo1Change(Sender: TObject); begin { if(memo1.Text ='') then button2.Enabled:=false else begin button2.Enabled:=true; end; end; } end.
OK。