#include
#include
#include "string.h"
typedef struct{
char word[20];
int count;
}WORD;
int find(char* source, char target)
{
int i,j;
int s_len=strlen(source);
for(i=0;i {
if(source[i]==target)
{
return i;
}
}
return -1;
}
char* right(char* source,int n)
{
char* buf;
int len=strlen(source);
buf=(char*)malloc(sizeof(char)*(len-n)+1);
int i,j=0;
for(i=n;i {
buf[j++]=source[i];
}
buf[j]='\0';
return buf;
}
void del_double_space(char* s)
{
int i,j=0,len=strlen(s);
for(i=0;i {
s[j++]=s[i++];
if(s[i]==' ' && s[i-1]==' ')
{
i++;
}
}
s[j]='\0';
len=strlen(s);
for(i=0;i {
if(s[i]>='A' && s[i]<='Z')
{
s[i]=s[i]+32;
}
}
}
char* left(char* source,int n)
{
char* buf;
buf=(char*)malloc(sizeof(char)*n+1);
int len,i,j=0;
len=strlen(source);
for(i=0;i {
if(source[i]!='\0')
{
buf[j++]=source[i];
}
else
{
break;
}
}
buf[j]='\0';
return buf;
}
void sort(WORD* w)
{
WORD t;
int count=0;
int i,j;
for(i=0;i<100;i++)
{
if(w[i].count>0)
{
count++;
}
}
for(i=0;i {
for(j=0;j {
if(w[j].count {
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
else if(w[j].count==w[j+1].count)
{
if(strcmp(w[j].word,w[j+1].word)>0)
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
}
}
}
}
int main(int argc, char *argv[])
{
char all[1000]={'\0'};
while(1)
{
strcpy(all,"\0");
printf("\n\n请输入一段英文文字:");
fflush(stdin);
scanf("%[^\n]",all);
WORD word[100];
int i,duan=0,len,count=0;
len=strlen(all);
for(i=0;i<100;i++)
{
strcpy(word[i].word,"\0");
word[i].count=0;
}
for(i=0;i {
if(all[i]==',' || all[i]=='.' || all[i]=='!' || all[i]=='?' || all[i]==';')
{
duan++;
if(i==len-1)
{
all[i]='\0';
}
else
{
all[i]=' ';
}
}
else if(i==len-1)
{
duan++;
}
}
del_double_space(all);
while(1)
{
char *buf,*newbuf;
int n;
buf=(char*)malloc(sizeof(char)*20);
newbuf=(char*)malloc(sizeof(char)*1000);
n=find(all,' ');
if(n!=-1)
{
buf=left(all,n);
}
else
{
strcpy(buf,all);
}
// printf("\n%s",buf);
int isfind=0;
for(i=0;i {
if(strcmp(word[i].word,buf)==0)
{
word[i].count=word[i].count+1;
isfind=1;
break;
}
}
if(isfind==0)
{
strcpy(word[count].word,buf);
word[count].count=1;
count++;
}
if(find(all,' ')==-1)
{
free(buf);
free(newbuf);
break;
}
newbuf=right(all,n+1);
strcpy(all,newbuf);
free(buf);
free(newbuf);
// printf("\n%s",all);
// getch();
}
sort(word);
printf("\n共有%d句话",duan);
printf("\n共有%d个单词",count);
printf("\n单词 词频");
for(i=0;i {
printf("\n%-10s %d",word[i].word,word[i].count);
}
printf("\n\n是否继续?[y/n]");
char n;
fflush(stdin);
scanf("%c",&n);
if(n=='y' || n=='Y')
{
continue;
}
else if( n=='n' || n=='N')
{
break;
}
}
return 0;
}