美文网首页
C-单词统计-初版本

C-单词统计-初版本

作者: DKider | 来源:发表于2019-04-23 22:54 被阅读0次

https://www.jianshu.com/p/5ebae8a62c81

最新的版本已发

就这么个题目,我们的C语言附加题作业,可真是让我发愁啊!!

本身对C语言不太熟悉,而且习惯了python的语法,在来写C代码,非常难受,丢三落四,而且对于编程工具的使用并不是很熟悉。做起来非常吃力,不过有了编程的基础,除了在链表排序的地方,花了不少时间,其他的还好。现在我先写出了最初的版本,所有的操作都在一个main()函数中完成,这显然不合理,所以明天会抽空将整个程序分开,作为多个函数来共同完成结果。

题目:

单词处理

~~~~~~~~~~~

对读入的某个文本文件input.txt中,拆出英文单词,输出一个按字典顺序排列的单词表,结果输出在文本文件output.txt中,每个单词一行,并在单词后输出该单词出现的个数,两个字段之间用逗号分隔。约定单词仅由英文字母组成,单词间由非英文字母分隔,相同单词只输出一个,大小写不区分。

例如文本文件input.txt为:

Hello world.

Hello every one.

Let us go.

则输出文本文件output.txt为:

every,1

go,1

hello,2

let,1

one,1

us,1

world,1

试编一个完整的程序完成该功能。(要求:必须用链表实现,结构体如下)

struct myword

{

 int num;

 char *word;

 struct myword  *next; 

};

最初的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct myword)

struct myword
{
    int     num;
    char    *word;
    struct  myword  *next;    /*  指向下一个节点 */
};


void  main()
{
    struct myword *head;
    head = NULL;
    struct myword *tmp, *cur,*nxt;
    cur=(struct myword *) malloc(LEN);
    head = cur;
    int size=0; 
    char  infile_name[]={"word.txt"}, outfile_name[]={"output.txt"};
    char ch;
    char word[20];
    char *t_word;
    int i=0,j;
    FILE * input, *output;
    input = fopen(infile_name, "r");
    if (input == NULL)
    {
        printf("打开文件%s失败\n",infile_name);
        exit(0);
    }
    while (! feof(input))
    {
        
        ch = fgetc(input);
        if ((ch>=65 && ch <=90) || (ch>=97 && ch <=122))
        {
            word[i]=ch;
            i++;
        }
        else
        {
            word[i]='\0';
            if (strlen(word) !=0)
            {
                tmp=(struct myword *) malloc(LEN);
                t_word = (char *) malloc(strlen(word));
                strcpy(t_word,strlwr(word));
                tmp->word = t_word;
                tmp->num = 1;
                tmp->next=NULL;
                printf(tmp->word);
                printf("\n");
                if (size==0)
                {
                    cur->next = tmp;
                    cur = tmp;
                    size++; 
                }
                cur = head->next;
                while (cur!=NULL)
                {
                    
                    if (strcmp(tmp->word,cur->word)<0)
                    {
                        //小于第一个值 ,因为链表有序,如果比第一个小,那就是最小的 
                        tmp->next = cur;
                        head->next = tmp;
                        break;
                    }
                    if (strcmp(tmp->word,cur->word)==0)
                    {
                        //等于当前值 
                            cur->num++;
                            break;
                    }
                    else if (strcmp(tmp->word,cur->word)>0)
                    {   
                        //大于当前值 
                        if (cur->next!=NULL)// && (tmp->word,cur->next->word)<0
                        {
                            //后面不为空 
                            if (strcmp(tmp->word,cur->next->word)<0)
                            {
                                //小于下一个值 ,插入当前节点之后 
                                tmp->next = cur->next;
                                cur->next = tmp;
                                break; 
                            }
                            //大于等于下一个值 
                            cur = cur->next;
                        }
                    else
                    {
                        //后面为空 
                        cur->next= tmp; 
                        break; 
                    }
                } 
            }
            }
            i=0;
        }
    }
    //cur->next=NULL;
    fclose(input);

    output = fopen(outfile_name, "w");
    if (input == NULL)
    {
        printf("打开文件%s失败\n",outfile_name);
        exit(0);
    }
    struct myword *p;
    p = head->next;
    printf("NOW,print LINK......\n");
//    if (head->next != NULL)
//    {
        do
        {
            fprintf(output,"%s,%d\n",p->word,p->num);
            printf("%s,%d\n",p->word,p->num);
            p = p->next;
        }while(p!=NULL);
//    }
    fclose(output);
    printf("Done!");
}

今天写这个忘了时间。哈哈哈哈。

最后的输出:

输出

相关文章

  • C-单词统计-初版本

    https://www.jianshu.com/p/5ebae8a62c81 最新的版本已发 就这么个题目,我们的...

  • C-单词统计 2.2

    emmmmm....... 详解: 问题: 对读入的某个文本文件input.txt中,拆出英文单词,输出一个按字典...

  • 单词统计

    有一个文本文件,被分成了4份,分别放到了4台服务器中存储 Text1:the weather is goodTex...

  • MapReduce案例

    一、单词统计 需求分析统计每个单词出现的次数 输入样例 输出样例 示例代码 二、温度统计 需求分析统计每一年的每个...

  • Hadoop单词统计

    1. 本地创建文本文件 2. 将文件上传到hadoop 3. 启用hadoop自带单词统计进行处理 查看结果

  • 单词数统计

    splitlines函数可以对文本中的换行符进行去除操作,然后利用sub函数替换文本中的符号,对末尾为-的单词进行...

  • 单词统计实现

  • 哈利波特与密室 单词统计 第一章

    哈利波特与密室 单词统计 CHAPTER 1 本篇单词统计:所有词汇标注共216个,其中GRE词汇共56个,托福词...

  • 哈利波特与密室 单词统计 第二章

    哈利波特与密室 单词统计 CHAPTER 2 本篇单词统计:所有词汇标注共233个,其中GRE词汇共72个,托福词...

  • 2020-03-17 刷题1(字符串)

    1160 拼写单词 题解的做法是用哈希表统计每个字母表中每个字母出现的次数,然后遍历每个单词,统计单词内每个字母出...

网友评论

      本文标题:C-单词统计-初版本

      本文链接:https://www.haomeiwen.com/subject/eadigqtx.html