主题:各位好心人,能帮帮我尽快解决这道C语言编程问题吗?
keoias5463
[专家分:0] 发布于 2010-12-06 21:01:00
谢谢啦,小弟急着要答案,不过以自己的水平真的无从入手。。。。。
在当前目录中存在文件名为"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()
{
_______________________
}
——————————————————————————————————-----
Input
文件case1.in中一篇英文文章,包含多段文字,单词数不超过10000,每个单词不超过20个字符
Output
按题意输出答案
Sample Input
(如case1.in内容如下)
I am a student. My school is SCAU. It is a beau-
tiful university. I like it.
Sample Output
a 2
i 2
is 2
it 2
am 1
回复列表 (共4个回复)
沙发
liudan319 [专家分:3780] 发布于 2010-12-07 14:23:00
how much
板凳
liudan319 [专家分:3780] 发布于 2010-12-07 14:24:00
网上很多代码,你可以搜下
3 楼
vrgdfdsg [专家分:0] 发布于 2010-12-10 11:10:00
A couple [url=http://www.mmopowerlevel.net]wow power leveling[/url] of hours work at toon level 6-8 will give [url=http://www.mogxe.com/PowerLevel.php?
gid=1]wow power leveling[/url] you 3-4 stacks of light leather, 2-3 stacks of herbs, 1-2 stacks or metals bars (yep, smelt them for mining experience before
selling them ... if [url=http://www.mmopowerlevel.net/powerlist.php?fid=688]wow power leveling[/url] you are "grey" on smelting copper (ie no mining XP form
it), sell [url=http://www.mmopowerlevel.net/gamelist.php?fid=7656]aion kina[/url] the stacks of raw ore) and if you are working in an area of humanoids 1-2
stacks of [url=http://www.mogxe.com]buy wow gold[/url] linen cloth. Dont waste your linen on bandages (FirstAid) .. at least not yet.
Dont waste bag storage space [url=http://www.mmopowerlevel.net/buy.php]gold in wow[/url] on grey usable items, ruined pelts, broken teeth etc. unless you are
filling up an inventory for [url=http://www.mmopowerlevel.net/buy.php]gold in wow[/url] the run home. Keep green items for Auction House sale if your
[url=http://www.mmopowerlevel.net/powerlist.php?fid=7422]cheap aion power leveling[/url] toon (or an alt) doesnt need them.Once every two hours, a group of
Sha'tari Skyguard will launch an attack against Bash'ir Landing to the north east. They [url=http://www.mmopowerlevel.net/gamelist.php?fid=4044]EVE ISK[/url]
fly out from the Skyguard Base.
4 楼
牧野风间00 [专家分:50] 发布于 2010-12-13 15:22:00
[code=c]
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
typedef struct
{
char p[20];//存放单词
int count_p;//统计出现次数
int flag;//单词是否已出现
}Type;
void count(Type a[],int k)
{
int i,j;
for(i = 0; i < k; i++)
for(j = i+1; j < k; j++)
if(a[i].flag == 0)
if(strcmp(a[i].p,a[j].p) == 0)
{
a[j].flag = 1;
a[i].count_p++;
}
}
int main()
{
FILE *fp;
Type a[100];
int i=0,j,k=0;
char c;
if((fp=fopen("av.txt","r")) == NULL)//打开当前目录下的文本文件
{
printf("cannot open this file\n");
exit(0);
}
c = fgetc(fp);
while(c!=EOF)
{
j=0;
while(c == ' ' || c == '.') c = fgetc(fp);//其他标点一样处理,建议在while条件判断处写个函数
do
{
if(c == '-')
{
c = fgetc(fp);
while(c == ' ' || c == '.') c = fgetc(fp);
if(c == '\n') c = fgetc(fp);
}
a[k].p[j++] = c;//复制单词
c = fgetc(fp);
}while(c != ' ' && c != '.');
a[k].p[j] = '\0';
k++;
c = fgetc(fp);
}
fclose(fp);
for(i = 0; i < k; i++)
{
a[i].flag = 0;
a[i].count_p = 1;
}
count(a,k);
printf("\n");
for(j = 0; j < k; j++)
if(a[j].flag == 0)
printf("%s %d\n",a[j].p,a[j].count_p);
getch();
return 0;
}
/* do
{
j = 0;
//找到单词的开始位置,在while条件中,还有其他标点,换行其他条件
{
if(s[i] == '-')
i++;
else
a[k].p[j++]=s[i++];
}
}while((c = s[i]) != '\0');
printf("\t%d\n",num);
for(i = 0; i < k; i++)
{
a[i].flag = 0;
a[i].count_p = 1;
}
count(a,k);
for(i = 0; i < strlen(s); i++)
printf("%c",s[i]);
printf("\n");
for(j = 0; j < k; j++)
if(a[j].flag == 0)
printf("%s %d\n",a[j].p,a[j].count_p);
getch();
return 0;
}*/[/code]
主要思想是分割文本文件,找到每一个单词,复制到p[20]中,在统计出现次数。此时,在对每个单词按字典顺序排序,在用一个稳定的排序算法,则可以得出出现最多的前5个单词。程序,只写到,统计出现的次数,对于排序,还要你自己去完成。多尝试,别怕自己写不来。
我来回复