新功能需求:數組
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% |