Đếm và phân loại số lượng kí tự

Yêu cầu:

Giả sử cho 1 file text (không dùng unicode) với nội dung như sau:
Hello world!!!
Chương trình sẽ đọc file và đếm số lượng kí tự trong file này và thực hiện và hiển thị ra màn hình thông tin về việc phân loại kí tự:

H              [1]
e              [1]
l              [3]
o              [2]
w              [1]
r              [1]
d              [1]
space          [1]
!              [3]

Giải thuật: dùng danh sách liên kết lưu kí tự và số lượng kí tự đó vào 1 node. Đọc từng kí tự từ file. Sau đó kiểm tra kí tự đó xem đã tồn tại trong danh sách liên kết chưa?, nếu tồn tại thì tăng biến đếm kí tự này lên 1 và đồng thời ko lưu kí tự này vào danh sách liên kết, nếu chưa tồn tại thì tạo 1 node mới và lưu kí tự này vào và đồng thời gán biến đếm kí tự này bằng 1.

/**************************CountCharacters***********************************
Count characters from reading file
Author : VNCODING Group
Date : 11/24/2013
*****************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

// Path of filename
#define FILE_DIR "C:\\test.txt"

typedef struct 
{
    char c; // character
    int count; // the number of character
}CHAR_INFO;

typedef struct NODE
{
    CHAR_INFO char_info;
    NODE *pNext; 
}NODE;


/****************************************************************************
Function : creatNode()
Description : Insert character into linked list
Parameter : character
Return : NODE pointer
*****************************************************************************/
NODE* creatNode(char c)
{
    NODE *pNode;
    pNode = (NODE*)malloc(1*sizeof(NODE));
    if (!pNode)
    {
        printf("\nError in allocate memory");
        return NULL;
    }
    pNode->char_info.c = c;
    pNode->char_info.count = 1;
    pNode->pNext = NULL;

    return pNode;
}

/****************************************************************************
Function : freeList()
Description : free linked list
Parameter : pointer points to linked list
Return : void
*****************************************************************************/
void freeList(NODE* pHead)
{
    NODE* p = NULL;
    NODE* pNode = NULL;
    pNode = p = pHead;
    while (p)
    {
        pNode = pNode->pNext;
        free(p);
        p = pNode;
    }
}

/****************************************************************************
Function : OutputScreen()
Description : Output result
Parameter : pointer points to linked list
Return : void
*****************************************************************************/
void OutputScreen(NODE* pHead)
{
    NODE *p;
    p = pHead;
    printf("\n========================COUNT CHARS=====================\n"); 
    printf("\nCharacters\tNumber");
    printf("\n=====\t\t========");
    while(p != NULL)
    {
        if (isprint(p->char_info.c))
        {
            printf("\n%c\t\t[%d]", p->char_info.c, p->char_info.count);
        }
        p = p->pNext;
    }
}

void main()
{
    NODE* pHead = NULL;
    NODE* p = NULL;
    FILE *fp = NULL;
    char c;

    fp = fopen(FILE_DIR, "rt");
    if(!fp)
    {
        printf("\nError in opening file");
    }
    else
    {
        do
        {
            if((c = fgetc(fp)) == EOF) // End of file
            {
                break;
            } 
            if(pHead == NULL)
            {
                pHead = creatNode(c);
            }
            else
            {
                p = pHead;
                while(p != NULL )
                {
                    // character exists
                    if(p->char_info.c == c)
                    {
                        (p->char_info.count) = p->char_info.count + 1;
                        break;
                    }
                    // new character
                    if( p->pNext == NULL) 
                    {
                        p->pNext = creatNode(c);
                        break;
                    }
                    p = p->pNext;
                }
            }
        }
        while(c != EOF);
        fclose(fp);
    }
    OutputScreen(pHead);
    freeList(pHead);
    pHead = NULL;
    getch();
}

Kết quả:

File test
File test
Đếm số lượng kí tự
Đếm số lượng kí tự

Be the first to comment

Leave a Reply