词频统计(第二周)
新功能需求:
1.小文件输入. 在控制台下输入命令。
2.支持命令行输入英文作品的文件名。
部分核心代码:
结构体定义:
定义一个结构体来存放读取的单词与其统计的次数。
typedef struct addup
{
    char word[50];
    int count;
}R;
读取文本:
       char temp[50];        R fin[10000]={"\0",0};        char file[10];
       fflush(stdin);
       gets(file);
       fp=freopen(file,"r",stdin);
单词匹配:
针对读取的每个单词,和结构体中之前存取的单词进行对比,如果相同则累计数量,如果不同则更新数组。
while(!feof(fp))
       {
           fscanf(fp,"%s",temp);
           q=strlen(temp);
           n++;
    for(i=0;i<n;++i)
        if(strcmp(fin[i].word,temp)==0)
        {
             fin[i].count++;
              n--;
              break;
         }
         if(i>=n)
         {
             strcpy(fin[n-1].word,temp);
             fin[n-1].count++;
         }
       }
标点判定:
将读取的字符串以字符为单位判断标点并去除。
for(i=0;i<q;i++)
{
     if(temp[i]==','||temp[i]=='.'||temp[i]=='?'||temp[i]=='!'||temp[i]=='"')
     temp[i]='\0';
}
冒泡排序:
for (i=0;i<n;i++)  
        for (j=0;j<n-i;j++)  
          {  
            if (fin[j].count<fin[j+1].count)  
               {  
               ls[0]=fin[j+1];  
               fin[j+1]=fin[j];  
               fin[j]=ls[0];  
               }  
          }  
输出结果:
       if(m!=1)
        printf("total  %d words\n\n",m);
      else printf("total  1 word\n\n");
     for(i=0;i<n;i++)
       {
         printf("%s : ",fin[i].word);
         s=0;
            for(j=0;j<fin[i].count;++j)
             s++;
         printf("%d 次",s);
         printf("\n");
       }
运行结果:
功能1:
功能2:
PSP表格
| C | C | S | E | I | T | 
| 分析 | 需求,设计 | 15:20 | 15:30 | 0 | 10 | 
| 编码 | 实现 | 15:30 | 16:20 | 0 | 50 | 
| 文档 | 程序说明,随笔 | 23:40 | 0:05 | 0 | 25 | 
| 讨论 | 程序改进 | -- | -- | -- | -- | 
| psp2.1 | |
| Planning | 12% | 
| Development | 59% | 
| Reporting | 29% | 
 
                    
                     
                    
                 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号