Nén và giải nén bằng mã huffman viết bằng C

Mình đang làm 1 cái đồ án về bài này,sắp bảo vệ rồi,mà mình vẫn chưa làm xong.bạn nào giúp mình được không?

1 Like

mình trình độ thấp nếu được Chín cứ tạo topic tất cả veè đồ án từ cách giải đến code tớiđâu cho mọi người xem. có lẽ giúp được đó. chứ nói vầy hơi khó hỳ. mình cũng muốn học hỏi với

I moved 2 posts to an existing topic: Off-topics will be moved here

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define len(x) ((int)log10(x)+1)

struct node{
    int value;
    char letter;
    struct node *left,*right;
};

typedef struct node Node;


int englishLetterFrequencies [27] = {81, 15, 28, 43, 128, 23, 20, 61, 71, 2, 1, 40, 24, 69, 76, 20, 1, 61, 64, 91, 28, 10, 24, 1, 20, 1, 130};

int findSmaller (Node *array[], int differentFrom){
    int smaller;
    int i = 0;

    while (array[i]->value==-1)
        i++;
    smaller=i;
    if (i==differentFrom){
        i++;
        while (array[i]->value==-1)
            i++;
        smaller=i;
    }

    for (i=1;i<27;i++){
        if (array[i]->value==-1)
            continue;
        if (i==differentFrom)
            continue;
        if (array[i]->value<array[smaller]->value)
            smaller = i;
    }

    return smaller;
}


void buildHuffmanTree(Node **tree){
    Node *temp;
    Node *array[27];
    int i, subTrees = 27;
    int smallOne,smallTwo;

    for (i=0;i<27;i++){
        array[i] = malloc(sizeof (Node));
        array[i]->value = englishLetterFrequencies[i];
        array[i]->letter = i;
        array[i]->left = NULL;
        array[i]->right = NULL;
    }

    while (subTrees>1){
        smallOne=findSmaller(array,-1);
        smallTwo=findSmaller(array,smallOne);
        temp = array[smallOne];
        array[smallOne] = malloc(sizeof(Node));
        array[smallOne]->value=temp->value+array[smallTwo]->value;
        array[smallOne]->letter=127;
        array[smallOne]->left=array[smallTwo];
        array[smallOne]->right=temp;
        array[smallTwo]->value=-1;
        subTrees--;
    }

    *tree = array[smallOne];

return;
}

void fillTable(int codeTable[], Node *tree, int Code){
    if (tree->letter<27)
        codeTable[(int)tree->letter] = Code;
    else{
        fillTable(codeTable, tree->left, Code*10+1);
        fillTable(codeTable, tree->right, Code*10+2);
    }

    return;
}

void compressFile(FILE *input, FILE *output, int codeTable[]){
    char bit, c, x = 0;
    int n,length,bitsLeft = 8;
    int originalBits = 0, compressedBits = 0;

    while ((c=fgetc(input))!=10){
        originalBits++;
        if (c==32){
            length = len(codeTable[26]);
            n = codeTable[26];
        }
        else{
            length=len(codeTable[c-97]);
            n = codeTable[c-97];
        }

        while (length>0){
            compressedBits++;
            bit = n % 10 - 1;
            n /= 10;
            x = x | bit;
            bitsLeft--;
            length--;
            if (bitsLeft==0){
                fputc(x,output);
                x = 0;
                bitsLeft = 8;
            }
            x = x << 1;
        }
    }

    if (bitsLeft!=8){
        x = x << (bitsLeft-1);
        fputc(x,output);
    }


    fprintf(stderr,"Original bits = %d\n",originalBits*8);
    fprintf(stderr,"Compressed bits = %d\n",compressedBits);
    fprintf(stderr,"Saved %.2f%% of memory\n",((float)compressedBits/(originalBits*8))*100);

    return;
}


void decompressFile (FILE *input, FILE *output, Node *tree){
    Node *current = tree;
    char c,bit;
    char mask = 1 << 7;
    int i;

    while ((c=fgetc(input))!=EOF){

        for (i=0;i<8;i++){
            bit = c & mask;
            c = c << 1;
            if (bit==0){
                current = current->left;
                if (current->letter!=127){
                    if (current->letter==26)
                        fputc(32, output);
                    else
                        fputc(current->letter+97,output);
                    current = tree;
                }
            }

            else{
                current = current->right;
                if (current->letter!=127){
                    if (current->letter==26)
                        fputc(32, output);
                    else
                        fputc(current->letter+97,output);
                    current = tree;
                }
            }
        }
    }

    return;
}


void invertCodes(int codeTable[],int codeTable2[]){
    int i, n, copy;

    for (i=0;i<27;i++){
        n = codeTable[i];
        copy = 0;
        while (n>0){
            copy = copy * 10 + n %10;
            n /= 10;
        }
        codeTable2[i]=copy;
    }

return;
}

int main(){
    Node *tree;
    int codeTable[27], codeTable2[27];
    int compress;
    char filename[20];
    FILE *input, *output;

    buildHuffmanTree(&tree);

    fillTable(codeTable, tree, 0);

    invertCodes(codeTable,codeTable2);


    printf("Type the name of the file to process: ");
    scanf("%s",filename);
    printf("\n Type 1 to compress and 2 to decompress:");
    scanf("%d",&compress);

    input = fopen(filename, "r");
    output = fopen("output.txt","w");

    if (compress==1)
        compressFile(input,output,codeTable2);
    else
        decompressFile(input,output, tree);

    return 0;
}

đoạn code này mình chạy thấy lỗi,chạy nén mãi mà không xong

Khi nào bảo vệ @Htc? Bài này Đạt có thể giúp, nhưng vì học lâu quá rồi nên quên kiến thức. Cuối tuần này được không?

Giờ Đạt Share bài này lên FB và các nguồn khác nhờ giúp đỡ.

từ 15 đến 20 bạn ạ,giúp mình nhe

2 Likes

hôm trước deedline nhiều quá làm không nổi nên bỏ bài này

1 Like

Thế giờ có thời gian làm bài này không @Dung_Kon, đây là một bài cơ bản nên nắm đấy. Tiếc là học lâu anh cũng quên mất tiêu rồi. Giờ muốn làm phải coi lại :expressionless:

ôn thi anh đạt ơi, qua tết mới rãnh

1 Like

https://drive.google.com/file/d/0B_Z2kG2C9TtDakw5SzltSmtsYWM/view?usp=sharing tặng you

1 Like

code này chưa hoàn chỉnh phải không bạn,chạy vẫn không ra

mình chưa kiểm tra nữa, đây là tài liệu của trường tự nhiên đó, bạn cố gắng tìm hiểu đi, chứ copy nguyên code thì không tốt đâu

ngồi đọc đoạn code bạn đưa cho thì cũng hiểu 90% còn mấy đoạn thắc mắc biết làm sao giờ

mình cũng muốn ngồi làm lắm nhưng mà mình bận ôn thi mất rồi :sob: , cố gắng lên nhé bạn

đạt ơi…giúp mình với

khong chay duoc la phai roi ban, code do chi nen, giai nen cac ky tu tu a den z va khoang trang thoi (khong tinh ky tu hoa va cac ky tu khac).

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?