Nanjing Normal University re-examination Zhenti --C language training program, count the number of each word (character) appears, and each word (character) and the frequency of its output to the screen that appears and files

119. The existing text file test.txt, in which the content is hello, how are you.Welcome you to China! Write a program that reads test.txt, count the number of times each word appears, and each word and its emerging the number of output to the screen and file.

Only a single sentence, two lines need to be slightly modified.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Word      //定义单词链表
{
    char w[20];  //单词
    int count;   //该单词出现的次数
    struct Word *next;
};

void getData(char str[])    //从文件中逐个读入字符,保存到str字符数组中
{
    FILE *fp;
    char ch;
    int i;

    if((fp=fopen("test.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    i=0;
    while((ch=fgetc(fp))!=EOF)
        str[i++]=ch;
    str[i]='\0';
    fclose(fp);
}

struct Word *Caculate(char str[])   //统计单词及出现的频数
{
    int i=0,j=0;
    char word1[20];     //找字符串中的每一个单词
    struct Word *head=(struct Word *)malloc(sizeof(struct Word));   //建立单词空链表
    head->next=NULL;
    for(; str[i]!='\0'; i++)
    {
        if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))  //找到了一个单词使j=0
        {
            word1[j]='\0';    //此时word1中已存放一个单词

            /*构造一个单词结构体*/
            struct Word *q;
            q=(struct Word *)malloc(sizeof(struct Word));
            strcpy(q->w,word1);
            q->next=NULL;

            /*每找到一个单词从表头开始查找,若有此单词,则个数增1;否则将此单词添加进链表*/
            struct Word *p=head;

            while(p->next!=NULL)   //查找
            {
                if(strcmp(p->next->w,word1)==0)
                {
                    p->next->count++;
                    break;
                }
                p=p->next;
            }
            if(p->next==NULL) //链表中无此单词,则插入链表尾
            {
                p->next=q;
                q->count=1;
            }

            j=0;   //为找下一个单词做好准备
        }
        else
            word1[j++]=str[i];
    }
    return head;
}

void print(struct Word *head)
{
    struct Word *p=head;
    while(p->next!=NULL)
    {
        printf("%s:%d\n",p->next->w,p->next->count);
        p=p->next;
    }
}
int main()
{
    char str[1000];
    getData(str);
    printf("test.txt数据:%s\n",str);
    struct Word *head;
    head=Caculate(str);
    printf("统计单词及出现的个数:\n");
    print(head);

    return 0;
}


operation result:

120. The existing text file test.txt, in which the content is Hello, welcome you to Nanjing Normal University! Luck to you! Write a program that reads test.txt, count the number of times each character appears, and each character and their the number of occurrences of the output to the screen and file.

Ibid similar questions

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Ch      //定义字符链表
{
    char ch;  //字符
    int count;   //该字符出现的次数
    struct Ch *next;
};

void getData(char str[])    //从文件中逐个读入字符,保存到str字符数组中
{
    FILE *fp;
    char ch;
    int i;

    if((fp=fopen("test.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    i=0;
    while((ch=fgetc(fp))!=EOF)
        str[i++]=ch;
    str[i]='\0';
    fclose(fp);
}

struct Ch *Caculate(char str[])   //统计字符及出现的频数
{
    int i;
    struct Ch *head=(struct Ch *)malloc(sizeof(struct Ch));   //建立字符空链表
    head->next=NULL;
    for(i=0; str[i]!='\0'; i++)
    {
        if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))  //找到了一个字符
        {
            /*构造一个字符结构体*/
            struct Ch *q;
            q=(struct Ch *)malloc(sizeof(struct Ch));
            q->ch=str[i];
            q->next=NULL;

            /*每找到一个字符从表头开始查找,若有此字符,则个数增1;否则将此字符添加进链表*/
            struct Ch *p=head;

            while(p->next!=NULL)   //查找
            {
                if(p->next->ch==str[i])
                {
                    p->next->count++;
                    break;
                }
                p=p->next;
            }
            if(p->next==NULL) //链表中无此字符,则插入链表尾
            {
                p->next=q;
                q->count=1;
            }
        }
    }
    return head;
}

void print(struct Ch *head)
{
    struct Ch *p=head;
    while(p->next!=NULL)
    {
        printf("%c:%d\n",p->next->ch,p->next->count);
        p=p->next;
    }
}
int main()
{
    char str[1000];
    getData(str);
    printf("test.txt数据:%s\n",str);
    struct Ch *head;
    head=Caculate(str);
    printf("统计字符及出现的个数:\n");
    print(head);

    return 0;
}

operation result:

 

Published 462 original articles · won praise 55 · views 320 000 +

Guess you like

Origin blog.csdn.net/LY_624/article/details/105158931