#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <omp.h>
#include <ctime>

#define WORD_LENGTH 20

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

int sing ()
{
    FILE *fp;
    struct wordcount wordcount[100000];

    char essay[200000], ch, temp[20];//定义essay数组存储文章内容，ch为每次读取的字符，temp数组暂存单词
    int i, j, k, len, count, t;

    fp = fopen("word.txt", "r");

	float start_time = clock();//记录开始时间

    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;  //将所有单词出现次数初始化为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++;
            }
        } 
    }

	float end_time = clock(); //记录结束时间
    float time_taken = double(end_time - start_time) / CLOCKS_PER_SEC;
    printf("sing_time: %f\n",time_taken);
    fclose(fp);//关闭文件
    return time_taken;
}


int main()
{
    FILE *fp;
    struct wordcount wordcount[100000];
    int num_words = 0;

    // 打开文件
    fp = fopen("word.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file word.txt\n");
        return 1;
    }

    // 读取文件内容
    char essay[200000];
    int len, i;
    float start_time = omp_get_wtime();

    for (i = 0; !feof(fp); i++) {
        essay[i] = fgetc(fp);
    }
    len = i - 1;
    fclose(fp);

    // 将文件内容的非字母字符替换为空格
   // #pragma omp parallel for shared(essay, len)
    for (int i = 0; i < len; i++) {
        if (!isalpha(essay[i])) {
            essay[i] = ' ';
        }
    }

    // 统计单词数量
    #pragma omp parallel shared(wordcount, num_words) 
    {
        struct wordcount count[100000] = {{0}};
        int num_words_local = 0;

        // 统计子数组中的单词数量
        #pragma omp for nowait
        for (int i = 0; i < len; i++) {
            if (isspace(essay[i])) {
                continue;  // 跳过空格
            }

            int j;
            char word[WORD_LENGTH + 1] = {0};
            for (j = 0; !isspace(essay[i+j]) && j < WORD_LENGTH && i+j < len; j++) {
                word[j] = tolower(essay[i+j]);
            }

            int found = 0;
            for (int k = 0; k < num_words_local; k++) {
                if (strcmp(count[k].word, word) == 0) {
                    count[k].count++;
                    found = 1;
                    break;
                }
            }

            if (!found) {
                strcpy(count[num_words_local].word, word);
                count[num_words_local].count = 1;
                num_words_local++;
            }

            i += j - 1;  // 跳过本次处理的单词
        }

        // 合并子数组中的统计结果
        #pragma omp critical
        {
            for (int i = 0; i < num_words_local; i++) {
                int found = 0;
                for (int j = 0; j < num_words; j++) {
                    if (strcmp(wordcount[j].word, count[i].word) == 0) {
                        #pragma omp atomic update
                        wordcount[j].count += count[i].count;
                        found = 1;
                    }
                }
                if (!found) {
                    strcpy(wordcount[num_words].word, count[i].word);
                    wordcount[num_words].count = count[i].count;
                    #pragma omp atomic update
                    num_words++;
                }
            }
        }
    }

    // 输出结果
    for (i = 0; i < num_words; i++) {
        printf("%s %d\n", wordcount[i].word, wordcount[i].count);
    }
    //结束时间
float end_time = omp_get_wtime();
//计算程序运行时间
float time_taken = end_time - start_time;
printf("time %f\n" , time_taken);
sing();
    return 0;
}
改进这个代码的并行部分，它耗时太长了