检查一下我程序中的问题,最后添加一个输入单词返回原句的功能

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include

#define SIZE 50 //设置单词最长长度

//储存单词,数量,以及构成链表的指针
typedef struct words
{
char str[SIZE]; //单词最长100个
int count;
struct words *next;
}Words;

//初始化链表
Words* InitWord()
{
Words headWord = (Words)malloc(sizeof(Words));
strcpy(headWord->str,"");
headWord->count = 0;
headWord->next = NULL;
return headWord;
}

//头插入的方式插入新数据
void AddWord(Words *headWord,const char *str)//头插入
{
Words *pos = headWord->next; //储存当前第一个结构体的指针
Words newWord = (Words)malloc(sizeof(Words));
strcpy(newWord->str,str);
headWord->next = newWord;
if(pos == NULL) newWord->next = NULL;//第一个单词,储存在链表尾部,next指向空
else newWord->next = pos;
newWord->count=1;
}

int CheckStr(Words *headWord,const char *str)//检查单词是否出现过,若出现单词数量加一,返回1,没出现返回0
{
Words *p = headWord->next;
if(headWord->next == NULL)
{
AddWord(headWord,str);
return 1;
}
while(strcmp(p->str,str) != 0)
{
p = p->next;
if(p == NULL) break;
}
if(p == NULL) return 0;//没有该单词,返回0标记
else
{
++(p->count); //已有单词,数量加一
return 1;
}
return -1; //异常出错
}

//读单词操作——过滤标点与空格
int ReadWord(FILE *fp,int *plen,char *str)
{
fseek(fp, *plen, SEEK_SET);
unsigned char ch;
int flg_eof = 0; //标记 判断是否文件结束,如果再没有读入的内容了,即文件结束
int i=0;
while(fread(&ch,sizeof(char),1,fp) != 0) //注:isalpha(unsigned(ch)),需要强制转换
{
if(!isalpha(unsigned(ch)) && i == 0) continue;//排除单词是个字母无法读取问题
if(isalpha(unsigned(ch)) || ch == ''')//类似I'm中的 ' 符号,有且只能有一个
{
flg_eof = 1;
str[i++] = ch;
}
else
{
break;
}
}
if(flg_eof == 0) return 0; //没有读文件,文件结尾了
str[i] = '\0';
*plen = ftell(fp);
return *plen;
}

//读文件操作
void ReadFile(Words *headWord,char *name)//文件操作,从文件中读取单词
{
FILE fp ;
int len = 0; //标记文件指针在文件中的位置
if((fp= fopen(name,"r")) == NULL)
{
fp = fopen(name, "w+");/
如果创建失败,新建一个文件 w+:可读写
文件存在则文件长度清为零,文件不存在时创建一个 */
}

char str[SIZE];
while(ReadWord(fp,&len,str) != 0)//fscanf(fp,"%s",str) != EOF
{
    if(CheckStr(headWord,str) == 0)    //没有该单词,新增一个
    {
        AddWord(headWord,str);
    }    

}

fclose(fp);

}

//交换数据——单词/出现次数
void SwapWord(Words *posWord,Words *posNext)
{
char tmpWord[SIZE]="";
strcpy(tmpWord ,posWord->str);
strcpy(posWord->str, posNext->str);
strcpy(posNext->str, tmpWord);

int tmpCount;
tmpCount       = posWord->count;
posWord->count = posNext->count;
posNext->count = tmpCount;

}

//单词排序
void WordsSort(Words *headWord)
{
if(headWord->next == NULL) return;
Words *pos = headWord->next;
Words *pos_next = pos->next;
int flag;
while(pos_next != NULL)
{
flag = 0;

    /*-- _pos 表示内循环的遍历 --*//* ps:本来想通过这种方式优化,然后写的有点复杂了,已优化,链接见文章首行*/
    Words *_pos = pos;           
    Words *_pos_next = pos_next;

    while(_pos_next != NULL)
    {
        Words *tmp = _pos_next->next;
        if(_pos->count < _pos_next->count)
        {
            SwapWord(_pos,_pos_next);
            flag = 1;
        }
        _pos = _pos->next;
        _pos_next = _pos_next->next;

    }
    if(flag == 0)    break;
}

}

void PrintWords(Words *headWord,const char *name)
{
Words *pMove = headWord->next;
if(headWord->next == NULL) return;
int count = 0;
printf("\t<<%s>>中出现的最高频率的单词:\n",name);
printf("\t单词:出现次数:\n");
while(pMove != NULL && count<200)
{
printf("\t%-50s%d\n",pMove->str,pMove->count);
pMove = pMove->next;
count++;
}
printf("\n");
}

//释放链表
void free_Word(Words* headNode)
{
Words *point = NULL;
while(headNode != NULL)
{
point = headNode;//指向所释放的空间
headNode = headNode->next;//指针后移
free(point);
}
}

int main()
{
Words *headWord = InitWord(); //初始化链表,创建表头
char bookName[] = "输入文件地址及文件名";
ReadFile(headWord,bookName); //文件操作
WordsSort(headWord);
PrintWords(headWord,bookName);
free_Word(headWord);
return 0;
}

欧对了,哪个代码中word大小写是个问题我没改



程序运行不了还是不满足需求。

上传的代码,可以统一插入到代码块吗?现在这样代码看上去很乱,也没明白题主是碰到什么问题。

代码里单词字母大小写的问题,既然是单词的统计,可能还是区分大小写更合理些,不需处理。
添加一个输入单词返回原句的功能,单词统计提取是按单个字符采集,按单词返回原句的功能还得好好想想怎么写。
在单词查找的函数里int CheckStr(Words* headWord, const char* str),如果链表为空,这里不需加入链表的操作AddWord(headWord, str); ,直接返回 0 即可。代码整理如下,供参考:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define SIZE 50 //设置单词最长长度

//储存单词,数量,以及构成链表的指针
typedef struct words
{
    char str[SIZE]; //单词最长100个?
    int count;
    struct words* next;
}Words;

//初始化链表
Words* InitWord()
{
    Words* headWord = (Words*)malloc(sizeof(Words));
    strcpy(headWord->str, " ");
    headWord->count = 0;
    headWord->next = NULL;
    return headWord;
}

//头插入的方式插入新数据
void AddWord(Words* headWord, const char* str)//头插入
{
    Words* pos = headWord->next; //储存当前第一个结构体的指针
    Words* newWord = (Words*)malloc(sizeof(Words));
    strcpy(newWord->str, str);
    headWord->next = newWord;
    if (pos == NULL) newWord->next = NULL;//第一个单词,储存在链表尾部,next指向空
    else newWord->next = pos;
    newWord->count = 1;
}

int CheckStr(Words* headWord, const char* str)//检查单词是否出现过,若出现单词数量加一,返回1,没出现返回0
{
    Words* p = headWord->next;
    if (headWord->next == NULL)
    {
        //AddWord(headWord, str);如果链表为空,这里不需加入链表的操作,返回 0 即可。
        return 0; //return 1;
    }
    while (strcmp(p->str, str) != 0)
    {
        p = p->next;
        if (p == NULL) break;
    }
    if (p == NULL) return 0;//没有该单词,返回0标记
    else
    {
        ++(p->count); //已有单词,数量加一
        return 1;
    }
    return -1; //异常出错
}

//读单词操作——过滤标点与空格
int ReadWord(FILE* fp, int* plen, char* str)
{
    fseek(fp, *plen, SEEK_SET);
    unsigned char ch;
    int flg_eof = 0; //标记 判断是否文件结束,如果再没有读入的内容了,即文件结束
    int i = 0;
    while (fread(&ch, sizeof(char), 1, fp) != 0) //注:isalpha(unsigned(ch)),需要强制转换
    {
        if (!isalpha(unsigned(ch)) && i == 0) continue;//排除单词是个字母无法读取问题
        if (isalpha(unsigned(ch)) || ch == '\'')//类似I'm中的 ' 符号,有且只能有一个
        {
            flg_eof = 1;
            str[i++] = ch;
        }
        else
        {
            break;
        }
    }
    if (flg_eof == 0) return 0; //没有读文件,文件结尾了
    str[i] = '\0';
    *plen = ftell(fp);
    return *plen;
}

//读文件操作
void ReadFile(Words* headWord, char* name)//文件操作,从文件中读取单词
{
    FILE* fp;
    int len = 0; //标记文件指针在文件中的位置
    if ((fp = fopen(name, "r")) == NULL)
    {
        fp = fopen(name, "w+"); // 如果创建失败,新建一个文件 w + :可读写
                                 //文件存在则文件长度清为零,文件不存在时创建一个
    }

    char str[SIZE];
    while (ReadWord(fp, &len, str) != 0)//fscanf(fp,"%s",str) != EOF
    {
        if (CheckStr(headWord, str) == 0)    //没有该单词,新增一个
        {
            AddWord(headWord, str);
        }

    }
    fclose(fp);
}

//交换数据——单词/出现次数
void SwapWord(Words* posWord, Words* posNext)
{
    char tmpWord[SIZE] = "";
    strcpy(tmpWord, posWord->str);
    strcpy(posWord->str, posNext->str);
    strcpy(posNext->str, tmpWord);

    int tmpCount;
    tmpCount = posWord->count;
    posWord->count = posNext->count;
    posNext->count = tmpCount;
}

//单词排序
void WordsSort(Words* headWord)
{
    if (headWord->next == NULL) return;
    Words* pos = headWord->next;
    Words* pos_next = pos->next;
    int flag;
    while (pos_next != NULL)
    {
        flag = 0;

        //-- _pos 表示内循环的遍历 -- / ps:本来想通过这种方式优化,然后写的有点复杂了,已优化,链接见文章首行
        Words* _pos = pos;
        Words* _pos_next = pos_next;

        while (_pos_next != NULL)
        {
            Words* tmp = _pos_next->next;
            if (_pos->count < _pos_next->count)
            {
                SwapWord(_pos, _pos_next);
                flag = 1;
            }
            _pos = _pos->next;
            _pos_next = _pos_next->next;
        }
        if (flag == 0)    break;
    }
}

void PrintWords(Words* headWord, const char* name)
{
    Words* pMove = headWord->next;
    if (headWord->next == NULL) return;
    int count = 0;
    printf("<<%s>>中出现的最高频率的单词:\n", name);
    printf("单词:出现次数:\n");
    while (pMove != NULL && count < 200)
    {
        printf("%-50s%d\n", pMove->str, pMove->count);
        pMove = pMove->next;
        count++;
    }
    printf("\n");
}

//释放链表
void free_Word(Words* headNode)
{
    Words* point = NULL;
    while (headNode != NULL)
    {
        point = headNode;//指向所释放的空间
        headNode = headNode->next;//指针后移
        free(point);
    }
}

int main()
{
    Words* headWord = InitWord(); //初始化链表,创建表头
    char bookName[] = "输入文件地址及文件名";
    ReadFile(headWord, bookName); //文件操作
    WordsSort(headWord);
    PrintWords(headWord, bookName);
    free_Word(headWord);
    return 0;
}

朋友你方便重新上传一下程序么?代码运行不了报错信息看不了,不好帮忙解决,个人建议,望理解


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
 
#define SIZE 50 //设置单词最长长度
 
//储存单词,数量,以及构成链表的指针
typedef struct words
{
    char str[SIZE]; //单词最长100个?
    int count;
    struct words* next;
}Words;
 
//初始化链表
Words* InitWord()
{
    Words* headWord = (Words*)malloc(sizeof(Words));
    strcpy(headWord->str, " ");
    headWord->count = 0;
    headWord->next = NULL;
    return headWord;
}
 
//头插入的方式插入新数据
void AddWord(Words* headWord, const char* str)//头插入
{
    Words* pos = headWord->next; //储存当前第一个结构体的指针
    Words* newWord = (Words*)malloc(sizeof(Words));
    strcpy(newWord->str, str);
    headWord->next = newWord;
    if (pos == NULL) newWord->next = NULL;//第一个单词,储存在链表尾部,next指向空
    else newWord->next = pos;
    newWord->count = 1;
}
 
int CheckStr(Words* headWord, const char* str)//检查单词是否出现过,若出现单词数量加一,返回1,没出现返回0
{
    Words* p = headWord->next;
    if (headWord->next == NULL)
    {
        //AddWord(headWord, str);如果链表为空,这里不需加入链表的操作,返回 0 即可。
        return 0; //return 1;
    }
    while (strcmp(p->str, str) != 0)
    {
        p = p->next;
        if (p == NULL) break;
    }
    if (p == NULL) return 0;//没有该单词,返回0标记
    else
    {
        ++(p->count); //已有单词,数量加一
        return 1;
    }
    return -1; //异常出错
}
 
//读单词操作——过滤标点与空格
int ReadWord(FILE* fp, int* plen, char* str)
{
    fseek(fp, *plen, SEEK_SET);
    unsigned char ch;
    int flg_eof = 0; //标记 判断是否文件结束,如果再没有读入的内容了,即文件结束
    int i = 0;
    while (fread(&ch, sizeof(char), 1, fp) != 0) //注:isalpha(unsigned(ch)),需要强制转换
    {
        if (!isalpha(unsigned(ch)) && i == 0) continue;//排除单词是个字母无法读取问题
        if (isalpha(unsigned(ch)) || ch == '\'')//类似I'm中的 ' 符号,有且只能有一个
        {
            flg_eof = 1;
            str[i++] = ch;
        }
        else
        {
            break;
        }
    }
    if (flg_eof == 0) return 0; //没有读文件,文件结尾了
    str[i] = '\0';
    *plen = ftell(fp);
    return *plen;
}
 
//读文件操作
void ReadFile(Words* headWord, char* name)//文件操作,从文件中读取单词
{
    FILE* fp;
    int len = 0; //标记文件指针在文件中的位置
    if ((fp = fopen(name, "r")) == NULL)
    {
        fp = fopen(name, "w+"); // 如果创建失败,新建一个文件 w + :可读写
                                 //文件存在则文件长度清为零,文件不存在时创建一个
    }
 
    char str[SIZE];
    while (ReadWord(fp, &len, str) != 0)//fscanf(fp,"%s",str) != EOF
    {
        if (CheckStr(headWord, str) == 0)    //没有该单词,新增一个
        {
            AddWord(headWord, str);
        }
 
    }
    fclose(fp);
}
 
//交换数据——单词/出现次数
void SwapWord(Words* posWord, Words* posNext)
{
    char tmpWord[SIZE] = "";
    strcpy(tmpWord, posWord->str);
    strcpy(posWord->str, posNext->str);
    strcpy(posNext->str, tmpWord);
 
    int tmpCount;
    tmpCount = posWord->count;
    posWord->count = posNext->count;
    posNext->count = tmpCount;
}
 
//单词排序
void WordsSort(Words* headWord)
{
    if (headWord->next == NULL) return;
    Words* pos = headWord->next;
    Words* pos_next = pos->next;
    int flag;
    while (pos_next != NULL)
    {
        flag = 0;
 
        //-- _pos 表示内循环的遍历 -- / ps:本来想通过这种方式优化,然后写的有点复杂了,已优化,链接见文章首行
        Words* _pos = pos;
        Words* _pos_next = pos_next;
 
        while (_pos_next != NULL)
        {
            Words* tmp = _pos_next->next;
            if (_pos->count < _pos_next->count)
            {
                SwapWord(_pos, _pos_next);
                flag = 1;
            }
            _pos = _pos->next;
            _pos_next = _pos_next->next;
        }
        if (flag == 0)    break;
    }
}
 
void PrintWords(Words* headWord, const char* name)
{
    Words* pMove = headWord->next;
    if (headWord->next == NULL) return;
    int count = 0;
    printf("<<%s>>中出现的最高频率的单词:\n", name);
    printf("单词:出现次数:\n");
    while (pMove != NULL && count < 200)
    {
        printf("%-50s%d\n", pMove->str, pMove->count);
        pMove = pMove->next;
        count++;
    }
    printf("\n");
}
 
//释放链表
void free_Word(Words* headNode)
{
    Words* point = NULL;
    while (headNode != NULL)
    {
        point = headNode;//指向所释放的空间
        headNode = headNode->next;//指针后移
        free(point);
    }
}
 
int main()
{
    Words* headWord = InitWord(); 
    char bookName[] = "输入文件地址及文件名";
    ReadFile(headWord, bookName);
    WordsSort(headWord);
    PrintWords(headWord, bookName);
    free_Word(headWord);
    return 0;
}