试编写代码如下:
#include
#include
#include
struct word
{
char str[30]; //单词
int num; //单词出现的次数
}words[500];
int sum;
void chuli(char s[])
{
int i,j;
int flag=0;
for(i=0;i<=sum;i++)
{
if(strcmpi(words[i].str,s)==0)
{
words[i].num++;
flag=1;
}
}
if(flag==0)
{
strcpy(words[sum].str,s);
words[sum].num++;
sum++;
}
}
void paixu1()
{
int i,j;
struct word a;
for(i=0;i{
for(j=i+1;jif(strcmp(words[i].str,words[j].str) > 0)
{
a=words[j];
words[j]=words[i];
words[i]=a;
}
}
}
void paixu2()
{
int i,j;
struct word a;
for(i=0; i{
for(j=sum-1; j>i; --j)
{
if(words[j].num>words[j-1].num)
{
a=words[j];
words[j]=words[j-1];
words[j-1]=a;
}
}
}
}
int main()
{
char s[30];
int imax,istart,iend,i,j,flag=0;
char str[] = "The president told the audit authorities to keep their thoughts and actions in line with the CPC Central Committee, resolutely safeguard the centralized and unified leadership of the committee and implement the requirements of the committee.";
char delims[] = " ";
char *sword = NULL;
sum=0;
for(i=0;i<500;i++)
words[i].num=0;
sword = strtok(str, delims);
while( sword != NULL ) {
sscanf(sword,"%[a-zA-Z]",s);
strlwr(s);
chuli(s);
sword = strtok( NULL, delims );
}
printf("该文章共有:%d个单词\n",sum);
paixu1();
printf("\n按单词字典顺序升序排列:\n");
for(i=0;iprintf("%s, %d\n",words[i].str,words[i].num);
paixu2();
printf("\n按出现次数降序排列:\n");
for(i=0;iprintf("%s, %d\n",words[i].str,words[i].num);
return 0;
}
实际运行截图:
#include
#include
'把下面代码复制到窗体,添加1个list 和一个command按钮运行就可以了,和一个文本框,数据输入在文本框里
Private Type English
word As String
count As Long
End Type
'判断是不是英文
Private Function En(ByVal s As String) As Boolean
If s = "" Then En = False: Exit Function
s = LCase(s)
For i = 1 To Len(s)
a = Asc(Mid(s, i, 1))
If a < 97 Or a > 122 Then En = False: Exit Function
Next
En = True
End Function
'统计数组内各个英文出现次数
Private Function TongJi(ByVal T_txt As String) As English()
s = Split(T_txt, " ")
n = 0
Dim English() As English
ReDim English(0)
For i = 0 To UBound(s)
If s(i) <> "" And En(s(i)) = True Then
temp = False
For j = 0 To UBound(English)
If English(j).word = "" Then
English(j).word = s(i): English(j).count = 1: Exit For
ElseIf English(j).word = s(i) Then
temp = True: English(j).count = English(j).count + 1: Exit For
End If
Next
If temp = False Then
ReDim Preserve English(n): English(n).word = s(i): English(n).count = 1: n = n + 1
End If
End If
Next
TongJi = English
End Function
Private Sub Command1_Click()
Dim e() As English
e = TongJi(Text1)
Cls
For i = 0 To UBound(e)
If e(i).count > 0 Then List1.AddItem e(i).word & " 出现过: " & e(i).count & " 次"
Next
End Sub