Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

#include <stdio.h> #include <string.h> struct worldcount{ char word[20]; int count; };…

  1. User

    #include <stdio.h> #include <string.h>

    struct worldcount{ char word[20]; int count; }; //数组超限 int main() { FILE *fp; struct worldcount wordcount[100000000];

    char essay[200000], ch, temp[20];
    int i, j, k, len, count, t;
    
    fp = fopen("word.txt", "r");
    
    for(i=0; (ch = fgetc(fp)) != EOF; i++)
    	essay[i] = ch;
    len = i;//计算文本长度
    
    //全部字符写到essay数组中 
    for(i=0; i<len; i++)
    {
    	if(essay[i]=='-')
    	{
    		for(j=i+1; j<len; j++)
    			essay[j-1] = essay[j];
    		len--;
    	}	
    	else if(essay[i]>='A' && essay[i]<='Z')
    		essay[i] +=32;
    	else if(essay[i]>='a' && essay[i]<='z')
    		essay[i]= essay[i];
    	else essay[i] = ' ';
    }
    //这个函数将所有的字符都写到数组里面
    
    //删除多余空格 
    for(i=1; i<len; i++)
    {
    	if(essay[i-1]==' ' && essay[i]==' ')
    	{
    		for(j=i+1; j<len; j++)
    			essay[j-1] = essay[j];
    		len--;
    	}
    }//删除多的空格因为我们是靠空格去识别单词的
    
    //分词 
    k=0;
    for(i=0; i<10000, k<len; i++)
    {
    	for(j=0; j<20 && essay[k]!=' ' && k<len; j++, k++)
    		wordcount[i].word[j] = essay[k];
    	k++;
    }
    count = i;
    
    //赋值  
    for(i=0; i<count; i++)
    	wordcount[i].count = 1;
    
    //计算单词数 
    for(i=0; i<count; i++)
    {
    	for(j=i+1; j<count; j++)
    	{
    		if(strcmp(wordcount[i].word, wordcount[j].word)==0)
    		{
    			wordcount[i].count += wordcount[j].count;
    			for(k=j+1; k<count; k++)
    				strcpy(wordcount[k-1].word, wordcount[k].word);
    			count--;
    		}
    	}
    }
    
    //按单词排序
    for(i=0; i<count-1; i++)
    {
    	for(j=1; j<count-i; j++)
    	{
    		if(strcmp(wordcount[j-1].word, wordcount[j].word) > 0)
    		{
    			strcpy(temp, wordcount[j-1].word);
    			strcpy(wordcount[j-1].word, wordcount[j].word);
    			strcpy(wordcount[j].word, temp);
    			t = wordcount[j-1].count;
    			wordcount[j-1].count = wordcount[j].count;
    			wordcount[j].count = t;
    		}
    	}
    }
    
    //按数量排序 
    for(i=0; i<count-1; i++)
    {
    	for(j=1; j<count-i; j++)
    	{
    		if(wordcount[j-1].count < wordcount[j].count)
    		{
    			strcpy(temp, wordcount[j-1].word);
    			strcpy(wordcount[j-1].word, wordcount[j].word);
    			strcpy(wordcount[j].word, temp);
    			t = wordcount[j-1].count;
    			wordcount[j-1].count = wordcount[j].count;
    			wordcount[j].count = t;
    		}
    	}
    }
    
    if(count<5)
    	for(i=0; i<count; i++)
    		printf("%s %d\n", wordcount[i].word, wordcount[i].count);
    else
    	for(i=0; i<5; i++)
    		printf("%s %d\n", wordcount[i].word, wordcount[i].count);
    

    }改进一下这个代码,标点符号用英文,记得输出格式要有空格

  2. Assistant

    #include <stdio.h> #include <string.h> #include <ctype.h>

    struct wordcount{ char word[20]; int count; };

    int main() { FILE *fp; struct wordcount wordcount[100000]; //缩小数组长度,防止数组超限

    char essay[200000], ch, temp[20];
    int i, j, k, len, count, t;
    
    fp = fopen("word.txt", "r");
    
    for(i=0; (ch = fgetc(fp)) != EOF; i++)
        essay[i] = ch;
    len = i;
    
    for(i=0; i<len; i++)
    {
        if(essay[i]!='-' && isalpha(essay[i])) //加上判断是否为字母,将'-'后第一个字符加到essay数组中
            essay[i] = tolower(essay[i]);
        else essay[i] = ' ';
    }
    
    k = 0;
    for(i=0; i<100000 && k<len; i++)
    {
        for(j=0; j<20 && essay[k]!=' ' && k<len; j++, k++)
            wordcount[i].word[j] = essay[k];
        k++;
    }
    count = i;
    
    for(i=0; i<count; i++)
        wordcount[i].count = 1;
    
    for(i=0; i<count; i++)
    {
        for(j=i+1; j<count; j++)
        {
            if(strcmp(wordcount[i].word, wordcount[j].word)==0)
            {
                wordcount[i].count += wordcount[j].count;
                for(k=j+1; k<count; k++)
                    strcpy(wordcount[k-1].word, wordcount[k].word);
                count--;
            }
        }
    }
    
    for(i=0; i<count-1; i++)
    {
        for(j=1; j<count-i; j++)
        {
            if(strcmp(wordcount[j-1].word, wordcount[j].word) > 0)
            {
                strcpy(temp, wordcount[j-1].word);
                strcpy(wordcount[j-1].word, wordcount[j].word);
                strcpy(wordcount[j].word, temp);
                t = wordcount[j-1].count;
                wordcount[j-1].count = wordcount[j].count;
                wordcount[j].count = t;
            }
        }
    }
    
    for(i=0; i<count-1; i++)
    {
        for(j=1; j<count-i; j++)
        {
            if(wordcount[j-1].count < wordcount[j].count)
            {
                strcpy(temp, wordcount[j-1].word);
                strcpy(wordcount[j-1].word, wordcount[j].word);
                strcpy(wordcount[j].word, temp);
                t = wordcount[j-1].count;
                wordcount[j-1].count = wordcount[j].count;
                wordcount[j].count = t;
            }
        }
    }
    
    if(count < 5)
        for(i=0; i<count; i++)
            printf("%s %d\n", wordcount[i].word, wordcount[i].count);
    else
        for(i=0; i<5; i++)
            printf("%s %d\n", wordcount[i].word, wordcount[i].count);
    
    fclose(fp);
    return 0;
    

    }

  3. User

    count-能不能换个方式写,这样不对运行不了

  4. Assistant

    可以尝试用另外一种方式实现计算单词数,不过需要注意变量名和循环的顺序。

    下面是一个例子:

    //计算单词数
    int wordcount = 0;
    for(i=0; i<count; i++)
    {
        int flag = 1;
        for(j=0; j<i; j++) //遍历之前的单词
        {
            if(strcmp(wordcount[i].word, wordcount[j].word)==0) //如果和之前的单词一样,就不是新单词
            {
                flag = 0;
                break;
            }
        }
        if(flag) //如果是新单词,计数器加一
            wordcount++;
    }
    

    在这个代码中,我们使用一个wordcount变量来记录新单词的数量,然后在循环中,遍历之前的所有单词,如果发现有和当前单词一样的,说明当前单词不是新单词,标记一下。最后根据标记来决定是否给wordcount加一。

    记得在循环之前初始化wordcount为0。

  5. User

    用这种方法重新输出刚刚让你改进的代码

  6. Assistant

    好的,我这里用新的方式重新计算了单词数,代码如下:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct wordcount{
        char word[20];
        int count;
    };
    
    int main()
    {
        FILE *fp;
        struct wordcount wordcount[100000];
    
        char essay[200000], ch, temp[20];
        int i, j, k, len, count, t;
    
        fp = fopen("word.txt", "r");
    
        for(i=0; (ch = fgetc(fp)) != EOF; i++)
            essay[i] = ch;
        len = i;
    
        for(i=0; i<len; i++)
        {
            if(essay[i]!='-' && isalpha(essay[i]))
                essay[i] = tolower(essay[i]);
            else essay[i] = ' ';
        }
    
        k = 0;
        for(i=0; i<100000 && k<len; i++)
        {
            for(j=0; j<20 && essay[k]!=' ' && k<len; j++, k++)
                wordcount[i].word[j] = essay[k];
            k++;
        }
        count = i;
    
        for(i=0; i<count; i++)
            wordcount[i].count = 1;
    
        int newwordcount = 0; //新的单词数
        for(i=0; i<count; i++)
        {
            int flag = 1;
            for(j=0; j<i; j++)
            {
                if(strcmp(wordcount[i].word, wordcount[j].word)==0)
                {
                    flag = 0;
                    break;
                }
            }
            if(flag)
                newwordcount++;
        }
    
        for(i=0; i<count; i++)
        {
            for(j=i+1; j<count; j++)
            {
                if(strcmp(wordcount[i].word, wordcount[j].word)==0)
                {
                    wordcount[i].count += wordcount[j].count;
                    for(k=j+1; k<count; k++)
                        strcpy(wordcount[k-1].word, wordcount[k].word);
                    count--;
                }
            }
        }
    
        for(i=0; i<count-1; i++)
        {
            for(j=1; j<count-i; j++)
            {
                if(strcmp(wordcount[j-1].word, wordcount[j].word) > 0)
                {
                    strcpy(temp, wordcount[j-1].word);
                    strcpy(wordcount[j-1].word, wordcount[j].word);
                    strcpy(wordcount[j].word, temp);
                    t = wordcount[j-1].count;
                    wordcount[j-1].count = wordcount[j].count;
                    wordcount[j].count = t;
                }
            }
        }
    
        for(i=0; i<count-1; i++)
        {
            for(j=1; j<count-i; j++)
            {
                if(wordcount[j-1].count < wordcount[j].count)
                {
                    strcpy(temp, wordcount[j-1].word);
                    strcpy(wordcount[j-1].word, wordcount[j].word);
                    strcpy(wordcount[j].word, temp);
                    t = wordcount[j-1].count;
                    wordcount[j-1].count = wordcount[j].count;
                    wordcount[j].count = t;
                }
            }
        }
    
        if(newwordcount < 5)
            for(i=0; i<count; i++)
                printf("%s %d\n", wordcount[i].word, wordcount[i].count);
        else
            for(i=0; i<5; i++)
                printf("%s %d\n", wordcount[i].word, wordcount[i].count);
        
        fclose(fp);
        return 0;
    }
    

    注意到我们计算新单词数是在循环中完成的,因此输出词频前5的单词时要用新计算的单词数。

  7. User

    不用这个版本了,用上一个改进的版本,听着,我不想要输出标点的数量,我该怎么更改代码’

  8. Assistant

    好的,要忽略标点符号,可以将判断字符是否为字母的代码段中去掉else部分,将不是字母也不是短横线的字符都视为分隔符,具体代码如下:

    for(i=0; i<len; i++)
    {
        if(isalpha(essay[i]) || essay[i]=='-')
            essay[i] = tolower(essay[i]);
        else essay[i] = ' ';
    }
    

    然后在输出词频时,不输出数量为1的单词,以及单词中含有标点符号的单词,具体代码如下:

    if(newwordcount < 5)
    {
        for(i=0; i<count; i++)
        {
            if(wordcount[i].count>1 && strspn(wordcount[i].word, "abcdefghijklmnopqrstuvwxyz")==strlen(wordcount[i].word))
                printf("%s %d\n", wordcount[i].word, wordcount[i].count);
        }
    }
    else
    {
        int num = 0;
        for(i=0; i<count && num<5; i++)
        {
            if(wordcount[i].count>1 && strspn(wordcount[i].word, "abcdefghijklmnopqrstuvwxyz")==strlen(wordcount[i].word))
            {
                printf("%s %d\n", wordcount[i].word, wordcount[i].count);
                num++;
            }
        }
    }
    

    这里用strspn函数判断单词是否只包含字母,如果是,则输出单词和数量。同时,修改了输出的格式,每行单词和数量之间只留一个空格。

  9. User

    我不要输出前5个最多的了,我要统计并输出每一个单词的数目

  10. Assistant

    好的,如果要统计每一个单词的数量,我们需要以单词为单位,遍历数组,并统计每一个单词的出现次数。

    下面是一个实现这个功能的代码:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct wordcount{
        char word[20];
        int count;
    };
    
    int main()
    {
        FILE *fp;
        struct wordcount wordcount[100000];
    
        char essay[200000], ch, temp[20];
        int i, j, k, len, count, t;
    
        fp = fopen("word.txt", "r");
    
        for(i=0; (ch = fgetc(fp)) != EOF; i++)
            essay[i] = ch;
        len = i;
    
        for(i=0; i<len; i++)
        {
            if(isalpha(essay[i]) || essay[i]=='-')
                essay[i] = tolower(essay[i]);
            else essay[i] = ' ';
        }
    
        k = 0;
        for(i=0; i<100000 && k<len; i++)
        {
            for(j=0; j<20 && essay[k]!=' ' && k<len; j++, k++)
                wordcount[i].word[j] = essay[k];
            k++;
        }
        count = i;
    
        for(i=0; i<count; i++)
            wordcount[i].count = 0;
    
        for(i=0; i<count; i++)
        {
            int found = 0;
            for(j=0; j<i; j++)
            {
                if(strcmp(wordcount[i].word, wordcount[j].word)==0)
                {
                    found = 1;
                    break;
                }
            }
            if(!found)
            {
                for(j=0; j<count; j++)
                {
                    if(strcmp(wordcount[i].word, wordcount[j].word)==0)
                        wordcount[i].count++;
                }
            }
        }
    
        for(i=0; i<count; i++)
        {
            printf("%s %d\n", wordcount[i].word, wordcount[i].count+1); //加1是因为之前初始化的时候给count赋值为0
        }
    
        fclose(fp);
        return 0;
    }
    

    在这个代码中,我们先将所有的单词都初始化为0,然后遍历每一个单词,判断是否是新单词,如果是,就遍历整个数组,统计该单词出现的次数。最后输出每一个单词和它的数量。

    注意到,因为有一部分代码逻辑重复,因此可以对其进行简化。具体地,我们可以先将skip用一个数组来表示,然后用循环来统计单词出现的次数。这样可以避免重复的代码,而且更加简单易懂。下面是修改后的代码:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct wordcount{
        char word[20];
        int count;
    };
    
    int main()
    {
        FILE *fp;
        struct wordcount wordcount[100000];
    
        char essay[200000], ch, temp[20];
        int i, j, k, len, count, t;
    
        fp = fopen("word.txt", "r");
    
        for(i=0; (ch = fgetc(fp)) !=