| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 397 人关注过本帖
标题:请求高手解答这个程序错在哪里?
只看楼主 加入收藏
瓦尔德的田野
Rank: 2
等 级:论坛游民
帖 子:18
专家分:22
注 册:2012-9-29
结帖率:100%
收藏
已结贴  问题点数:15 回复次数:4 
请求高手解答这个程序错在哪里?
/*编写一个程序,从键盘上读取任意长度的一段文本,确定该文本中每个单词出现的频率 */
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define TEXTLEN 10000
#define BUFFERSIZE 100
#define MAXWORDS 500
#define WORDLEN 15
int main(void)
{
    char text[TEXTLEN+1];
    char buffer[BUFFERSIZE];
    char endstr[]="*\n";

    const char space=' ';
    const char quote='\'';

    char words[MAXWORDS][WORDLEN+1];
    int nword[MAXWORDS];
    char word[WORDLEN+1];
    int wordlen=0;
    int wordcount=0;

    printf("Enter txtx on an arbitrary number of lines.");
    printf("\nEnter a line containing just an asterisk to end input:\n\n");

    while(true)
    {
        if(!strcmp(fgets(buffer,BUFFERSIZE,stdin),endstr))
            break;
        if(strlen(text)+strlen(buffer)+1>TEXTLEN)
        {
            printf("最大文本容量超出.终止程序!");
        return 1;
        }
    strcat(text,buffer);
    }
    for(int i=0;i<strlen(text);i++)
    {
        if(text[i]==quote||isalnum(text[i]))
            continue;
        text[i]=space;
    }
    int index=0;
    while(true)
    {
        while(text[index]==space)
            ++index;
        if(text[index]=='\0')
            break;

        wordlen=0;
        while(text[index]==quote||isalpha(text[index]))
        {
            if(wordlen==WORDLEN)
            {
                printf("最大文本容量超出.终止程序!");
                return 1;
            }
            word[wordlen++]=tolower(text[index++]);
        }
        word[wordlen]='\0';

        bool isnew=true;
        for(int i=0;i<wordcount;i++)
            if(strcmp(word,words[i])==0)
            {
                ++nword[i];
                isnew=false;
                break;
            }
            if(isnew)
            {
                if(wordcount>=MAXWORDS)
                {
                    printf("\n最大文本容量超出.终止程序!");
                    return 1;
                }
                strcpy(words[wordcount],word);
                nword[wordcount++]=1;
            }
    }
    for(int i=0;i<wordcount;i++)
    {
        if(!(i%3))
            printf("\n");
        printf("%-15s%5d",words[i],nword[i]);
    }
    return 0;
}

--------------------------------------------------------------------------------------
按道理最后的输出应该是:
Today is sunday,Tomorrow is monday.

Today 1
is 2
sunday 1
Tomorrow 1
monday 1
这样才对 。。可是我运行之后不管怎么输入 都是显示printf("最大文本容量超出.终止程序!");这个错误信息提示.
请问有高手知道这个程序究竟哪里错了么!?万分感谢.
搜索更多相关主题的帖子: include 
2012-11-22 22:55
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
收藏
得分:8 
对数组没有初始化,最好buffer[0]='\0';text[0]='\0';一下吧,还有由于你函数一直在while(TRUE)你看看吧
2012-11-22 23:21
jk_love
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:1
帖 子:196
专家分:965
注 册:2012-10-22
收藏
得分:8 
其实真正的原因是 wordcount 这个变量一直是0哦,亲
2012-11-23 15:57
瓦尔德的田野
Rank: 2
等 级:论坛游民
帖 子:18
专家分:22
注 册:2012-9-29
收藏
得分:0 
回复 3楼 jk_love
谢谢了 我也发现了 前面的代码我一直都没有去递增wordcount的值 后面却一直在用它 导致从for(int i=0;i<wordcount;i++)if(strcmp(word,words[i])==0){ ++nword[i];isnew=false;break;这里开始就从来没有执行过 后面的也执行不起来 不过这估计是导致错误的原因之一 还有其他原因 我想我应该重写。

你不努力就永远不知道自己能做到多好!
2012-11-23 20:27
瓦尔德的田野
Rank: 2
等 级:论坛游民
帖 子:18
专家分:22
注 册:2012-9-29
收藏
得分:0 
回复 2楼 fu2751653
我也发现了 谢谢!

你不努力就永远不知道自己能做到多好!
2012-11-23 20:28
快速回复:请求高手解答这个程序错在哪里?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015962 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved