![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Huffman coding....
well yeah my project thtat i need to submit is to do compress(huffing) & decompressing(dehuffing) a file using huffman code ..... this is my first real project,,, i am still a novice ... this is what the program should be able to do
C:\Tc\Zip (%filename.extension)compreses thefile to azipfile(%filename.zip) C:\Tc\unzip (%filename) uncompress the file to a zip file .... ok i am start doing this until i finish this even if i have to sit for the next 19 hrs left for the class .... 1. my first problem is how to add the argument for which file to open. SHouldnt we keep the argument in main(argument) .... |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Check out the tutorials section, coddeath has written a nice tutorial about getting arguments in C/C++.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
ok ther argunment section is over thanx for the tutorial colddeath ... i am loving this forum ... anyways next problem i am facing is ...... 2. i need to know the weight(frequency) of each character ..... i dont want to use any array any suggestions... i am gonna a priority queue to make my huffman tree and then will compress my txt file
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Do you have a knowledge of the Huffman algorithm? If so, you should be able to find the file's byte frequency easily.
Why don't you want to use arrays?
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;} |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Ok Half Done ... Only Main Remainning.... Me And My Friend Did It ...
only main remaining and i have no time ....... i am going to university ..have to submit this .all this hardwork only for an incomplete code. ..pls scrutinise this
#include<stdio.h> ///HEADERS #include<stdlib.h> // dA cONSTANTS #define NUM_CHARS 256 #define TRUE 1 #define COMPOSITE_NODE -1 // DA STRUCTURES typedef struct huffman_node_t { int value; /* character(s) represented by this entry */ int count; /* number of occurrences of value (probability) */ char ignore; /* TRUE -> already handled or no need to handle */ int level; /* depth in tree (root is 0) */ struct huffman_node_t *left, *right, *parent; } huffman_node_t; typedef struct code_list_t { byte_t codeLen; /* number of bits used in code (1 - 255) */ code_t code[32]; /* code used for symbol (left justified) */ } code_list_t; /**************************************************************************** * Function : GenerateTreeFromFile * Description: This routine creates a huffman tree optimized for encoding * the file passed as a parameter. * Parameters : inFile - Name of file to create tree for * Effects : Huffman tree is built for file. * Returned : Pointer to resulting tree ****************************************************************************/ huffman_node_t *GenerateTreeFromFile(char *inFile) { huffman_node_t *huffmanTree; /* root of huffman tree */ int c; FILE *fp; /* open file as binary */ if ((fp = fopen(inFile, "rb")) == NULL) { printf("there is no such file.FILETA KHOLTESA KENO"); exit(1); } /* allocate array of leaves for all possible characters */ for (c = 0; c < NUM_CHARS; c++) { huffmanArray[c] = AllocHuffmanNode(c); } /* count occurrence of each character */ while ((c = fgetc(fp)) != EOF) { if (totalCount < 256) { totalCount++; /* increment count for character and include in tree */ huffmanArray[c]->count++; /* check for overflow */ huffmanArray[c]->ignore = FALSE; } else { fprintf(stderr, "HEY THE MOST WEIGHT OR FREQUENCY OF A CHARACTERTHAT I CAN HANDLE IS 256\N "); exit(1); } } fclose(fp); /* put array of leaves into a huffman tree */ huffmanTree = BuildHuffmanTree(huffmanArray, NUM_CHARS); return(huffmanTree); } /**************************************************************************** * Function : AllocHuffmanNode * Description: This routine allocates and initializes memory for a node * (tree entry for a single character) in a Huffman tree. * Parameters : value - character value represented by this node * Effects : Memory for a huffman_node_t is allocated from the heap * Returned : Pointer to allocated node. NULL on failure to allocate. ****************************************************************************/ huffman_node_t *AllocHuffmanNode(int value) { huffman_node_t *ht; ht = (huffman_node_t *)(malloc(sizeof(huffman_node_t))); if (ht != NULL) { ht->value = value; ht->ignore = TRUE; /* will be FALSE if one is found */ /* at this point, the node is not part of a tree */ ht->count = 0; ht->level = 0; ht->left = NULL; ht->right = NULL; ht->parent = NULL; } else { perror("Allocate Node"); exit(EXIT_FAILURE); } return ht; } huffman_node_t *BuildHuffmanTree(huffman_node_t **ht, int elements) { int min1, min2; /* two nodes with the lowest count */ /* keep looking until no more nodes can be found */ for (; ![]() { /* find node with lowest count */ min1 = FindMinimumCount(ht, elements); if (min1 == NONE) { /* no more nodes to combine */ break; } ht[min1]->ignore = TRUE; /* remove from consideration */ /* find node with second lowest count */ min2 = FindMinimumCount(ht, elements); if (min2 == NONE) { /* no more nodes to combine */ break; } ht[min2]->ignore = TRUE; /* remove from consideration */ /* combine nodes into a tree */ ht[min1] = AllocHuffmanCompositeNode(ht[min1], ht[min2]); ht[min2] = NULL; } return ht[min1]; } /**************************************************************************** * Function : AllocHuffmanCompositeNode * Description: This routine allocates and initializes memory for a * composite node (tree entry for multiple characters) in a * Huffman tree. The number of occurrences for a composite is * the sum of occurrences of its children. * Parameters : left - left child in tree * right - right child in tree * Effects : Memory for a huffman_node_t is allocated from the heap * Returned : Pointer to allocated node ****************************************************************************/ huffman_node_t *AllocHuffmanCompositeNode(huffman_node_t *left, huffman_node_t *right) { huffman_node_t *ht; ht = (huffman_node_t *)(malloc(sizeof(huffman_node_t))); if (ht != NULL) { ht->value = COMPOSITE_NODE; /* represents multiple chars */ ht->ignore = FALSE; ht->count = left->count + right->count; /* sum of children */ ht->level = max(left->level, right->level) + 1; /* attach children */ ht->left = left; ht->left->parent = ht; ht->right = right; ht->right->parent = ht; ht->parent = NULL; } else { perror("WHY DONT U Allocate Composite NODE"); exit(1); } return ht; } void WriteHeader(huffman_node_t *ht, FILE *fp) { count_byte_t byteUnion; int i; for(; ![]() { /* follow this branch all the way left */ while (ht->left != NULL) { ht = ht->left; } if (ht->value != COMPOSITE_NODE) { /* write symbol and count to header */ fputc(ht->value, fp); byteUnion.count = ht->count; for(i = 0; i < sizeof(count_t); i++) { fputc(byteUnion.byte[i], fp); } } while (ht->parent != NULL) { if (ht != ht->parent->right) { ht = ht->parent->right; break; } else { /* parent's right tried, go up one level yet */ ht = ht->parent; } } if (ht->parent == NULL) { /* we're at the top with nowhere to go */ break; } } /* now write end of table char 0 count 0 */ fputc(0, fp); for(i = 0; i < sizeof(count_t); i++) { fputc(0, fp); } } |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
use [C ODE][/CO DE] tags around your code please.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In addition to adding code tags, please read the forum's rules/FAQ, and a "How to Post..." thread.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|