如何排除'-' '_' 的干扰~~·求解
在当前目录中存在文件名为"case1.in"(其中case后为数字1,不是字母l,写错提交后会判错)的文本文件,其内容为一篇英文文章(以EOF作为结束标志)。现要求读取该文本文件内容,统计文章中每个单词出现的次数,
并输出出现次数最多的前5个单词及其出现次数(按出现次数由多到少的顺序输出,次数相同时按字典顺序输出,
不足5个单词时,按序输出全部单词)。程序中注意如下细节:
(1) 空格、标点符号与回车符起到分隔单词的作用。
(2) 文章一行的末尾可能有连字符,出现连字符时,该行最末的字符串与下行最先出现的字符串构一个单词;
(3) 名词缩写算一个单词;
(4) 数字不算单词;
(5) 单词不区分大小写;
(6) 输出时单词全使用小写;
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"
_______________________
main()
{
_______________________
}
输入格式
文件case1.in中一篇英文文章,包含多段文字,单词数不超过10000,每个单词不超过20个字符
输出格式
按题意输出答案
输入样例
(如case1.in内容如下)
I am a student. My school is SCAU. It is a beau-
tiful university. I like it.
输出样例
a 2
i 2
is 2
it 2
am 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我提交的程序如下;
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000][20],ch[10000];
char real[1000][20],t[20];
char tem[20];
int i=0,j=0,k=0,sum=0,q=0,r=0;
int n ,m,p=0,count[1000]= {0};
int temp,len;
FILE *fp;
fp=fopen("case1.txt","r");
while((ch[k]=fgetc(fp))!=EOF)
{
if(ch[k]>='A'&&ch[k]<='Z')
ch[k]+=32;
if(ch[k]>='a'&&ch[k]<='z'||ch[k]=='-')
{s[i][j]=ch[k];j++;}
else
{s[i][j]='\0';i++;j=0;sum++;}
k++;
}
for(m=0; m<sum; m++)
{
len=strlen(s[m]);
for(n=0; n<len; n++)
if(s[m][n]=='_')
{s[m][n]='\0';strcat(s[m],s[m+1]);s[m+1][0]='\0';}
}
for(m=sum; m>0; m--)
{
for(n=0; n<m; n++)
if(strcmp(s[n],s[n+1])>0)
{strcpy(t,s[n]); strcpy(s[n],s[n+1]); strcpy(s[n+1],t);}
}
for (m=0; m<sum; m++)
{
for(n=m+1; n<sum; n++)
if(strcmp(s[m],s[n])==0) s[n][0]='\0';}
for(m=0; m<sum; m++)
{
if(s[m][0]!='\0')
{
for(n=m+1; n<sum; n++)
{
if(s[n][0]=='\0') count[r]++;
else if(s[n][0]!='\0') break;
}
r++;
}
if(s[m][0]!='\0')
{strcpy(real[p],s[m]);
p++;
q++;}
}
for(i=q-1; i>0; i--)
{
for(j=0; j<i; j++)
{
if(count[j]<count[j+1])
{
temp=count[j];count[j]=count[j+1];count[j+1]=temp;
strcpy(tem,real[j]);
strcpy(real[j],real[j+1]);
strcpy(real[j+1],tem);
}
}
}
if(q<5)
for(i=0; i<q; i++)
{
printf("%s",real[i]);
printf(" ");
printf("%d\n",count[i]+1);
}
else
for(i=0; i<5; i++)
{
printf("%s",real[i]);
printf(" ");
printf("%d\n", count[i]+1);
}
fclose(fp);
}
~~测试数据用例 No.1
标准输入数据:
I am a student. -1 = 1 - 2. q=a-m. z = i - w. My school is SCAU. It is a beau-
tiful university. t-t-t-t, 123 123. I like it.
标准输出答案:
1|t 4
2|a 3
3|i 3
4|is 2
5|it 2
你的错误输出结果:
1|- 3
2|i 3
3|a 2
4|is 2
5|it 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am a student. My school is SCAU. It is a beau-
tiful university. I like it.
I am a student. My school is SCAU. It is a i-
s university. I like it.
1 2 3 4 5 6 7 8 9
12 12 12 12
II I
标准输出答案:
1|i 5
2|is 5
3|a 4
4|it 4
5|am 2~~~~~~~~~~~~~~~~~~~~这一组测试数据可以正确输出~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~怎么才能把俩组数据都正确输出