|
入力した語の出現頻度を求めるプログラムを作成してるのですが、
ソースの部分がうまくいっていないみたいなのです。
実行しても重複した語句が直り、違う種類の語句が
出てくるだけなのです。
よろしくお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct{
char *words;
int count;
}Word;
void error(char *s)
{
fprintf(stderr,"%s\n",s);
exit(1);
}
char *strsave(char *s)
{
char *p;
if((p=malloc(strlen(s)+1))==NULL)
error("can not get memory for string");
strcpy(p,s);
return(p);
}
int regist_string(char *s,int smax,char *pw[])
{
int i;
for(i=0;i<smax;++i)
if(!strcmp(s,pw[i]))
return smax;
pw[smax++]=strsave(s);
return(smax);
}
void sortlist(int cmax,char *pw[])
{
int gap,i,j,n;
char *p,*w;
for(i=0;i<n;++i){
for(j=i;j<n;++j){
if(strcmp(pw[j],pw[i])<0){
w=pw[j];
pw[j]=pw[i];
pw[i]=w;
}
}
}
}
int getword(int bmax,char *s)
{
int c,i=0;
for(i=0;i<bmax-1;++i){
if(('a'<=(c=getchar())&&c<='z')
|| ('A'<=c&&c<='Z'))
*s++=c;
else
break;
}
*s='\0';
if(c==EOF&&i<=0)i=-1;
return(i);
}
int main(int argc,char *argv[])
{
int i,smax,len;
char str[80];
char *words[2048];
smax=0;
while(len=getword(80,str)){
if(len==0)continue;
smax=regist_string(str,smax,words);
}
sortlist(smax,words);
for(i=0;i<smax;++i)
printf("%d:%s\n",,words[i]);
}
|