用VB设计程序,统计某个文本文件中字母个数,并将结果写入另个文本文件。
事先准备in.txt文件,存放于d:\:
var n,i,j:longint; a:array[0..4]of integer;
begin
readln(n);
i:=-1;
while (n>0)and(i<4) do
begin
inc(i);
if i<4 then begin a[i]:=n mod 7; n:=n div 7 end
else a[i]:=n
end;
n:=0;
for j:=0 to 3 do
begin
if (a[j]>4)or(a[j]>3)and(a[j+1]>3) then
begin dec(a[j],7); inc(a[j+1]) end;
inc(n,abs(a[j]));
end;
inc(n,a[4]);
writeln(n);
end.
VB程序如下:
Private Sub Command1_Click()
Dim s As String, a(65 To 122) As Integer
Open "d:\in.txt" For Input As #1
Open "d:\out.txt" For Output As #2
While Not EOF(1)
Input #1, s
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c >= "A" And c <= "Z" Or c >= "a" And c <= "z" Then
x = Asc(c)
a(x) = a(x) + 1
End If
Next i
Wend
For i = 65 To 122
If a(i) > 0 Then
Print #2, Chr(i); ":"; a(i)
n = n + a(i)
End If
Next i
Print #2, "总共有 "; n; " 个字母"
Close #1
Close #2
MsgBox "内容已存放到d:\out.txt文件,请查看!"
End Sub