新功能需求:数组
1.小文件输入. 在控制台下输入命令。编码
2.支持命令行输入英文做品的文件名。spa
部分核心代码:命令行
结构体定义:设计
定义一个结构体来存放读取的单词与其统计的次数。code
typedef struct addup { char word[50]; int count; }R;
读取文本:blog
char temp[50]; R fin[10000]={"\0",0}; char file[10]; fflush(stdin); gets(file); fp=freopen(file,"r",stdin);
单词匹配:排序
针对读取的每一个单词,和结构体中以前存取的单词进行对比,若是相同则累计数量,若是不一样则更新数组。ci
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% |