#include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include…
- User
#include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <stdlib.h>
//structure used to define a node typedef struct node_t { struct node_t *left, *right; int freq; char c; } *node;
//global variables int n_nodes = 0, qend = 1; //global variables for keep track of no.of nodes and end of the que struct node_t pool[256] = {{0}}; //pool of nodes node qqq[255], *q = qqq - 1; //the priority que char *code[128] = {0}, buf[1024]; //a string array of the codes for each letter int input_data = 0, output_data = 0, i;
//function used to create a new node node new_node(int freq, char c, node a, node b) { node n = pool + n_nodes++; if (n == NULL) { printf("Error: memory allocation failed\n"); return NULL; } if (freq != 0) { n->c = c; //assign the character 'c' to the character of the node (eventually a leaf) n->freq = freq; //assign frequency } else { n->left = a, n->right = b; //if there is no frequency provided with the invoking n->freq = a->freq + b->freq; //the removed nodes at the end of the que will be added to left and right } return n; }
//function used to insert a node into the priority que void qinsert(node n) { if (n == NULL) { printf("Error: null pointer\n"); return; } int j, i = qend++; while ((j = i / 2)) { if (q[j]->freq <= n->freq) break; q[i] = q[j], i = j; } q[i] = n; }
node qremove() { if (qend < 2) { printf("Error: queue is empty\n"); return NULL; } int i, l; node n = q[i = 1];
codeqend--; while ((l = i * 2) < qend) { if (l + 1 < qend && q[l + 1]->freq < q[l]->freq) l++; q[i] = q[l], i = l; } q[i] = q[qend]; return n; //return the node}
//go along the builded huffman tree and assign the code for each character void build_code(node n, char *s, int len) { if (n == NULL || s == NULL) { printf("Error: null pointer\n"); return; } static char *out = buf; if (n->c) { s[len] = 0; //if the provided node is a leaf (end node) strcpy(out, s); //it contains a character code[(int)n->c] = out; //therefore the code is copied in to the relevant character. out += len + 1; //out pointer is incremented return; }
codes[len] = '0'; build_code(n->left, s, len + 1); //recurring is used to write out the code s[len] = '1'; build_code(n->right, s, len + 1); //if right add a 1 and if right add a 0}
void import_file(FILE *fp_in, unsigned int *freq, int *fd) { if (fp_in == NULL || freq == NULL || fd == NULL) { printf("Error: null pointer\n"); return; } char c, s[16] = {0}; //temporary variables int i = 0; printf("File Read:\n"); while ((c = fgetc(fp_in)) != EOF) { freq[(int)c]++; //read the file character by character and increment the particular frequency putchar(c); } for (i = 0; i < 128; i++) if (freq[i]) qinsert(new_node(freq[i], i, 0, 0)); //insert new nodes into the que if there is a frequency while (qend > 2) qinsert(new_node(0, 0, qremove(), qremove())); //build the tree build_code(q[1], s, 0); //build the code for the characters
- Assistant
int main(int argc, char **argv) { if (argc < 3) { printf("Usage: %s [-e/-d] [input_file] [output_file]\n", argv[0]); return 0; } unsigned int freq[128] = {0}; char flag = argv[1][1]; //take the input flag as the second character of the second argument FILE *fp_in = fopen(argv[2], "rb"); //open file in binary mode if (fp_in == NULL) { printf("Error: can't open input file %s\n", argv[2]); return 1; } FILE *fp_out = fopen(argv[3], "wb"); //open file in binary mode if (fp_out == NULL) { printf("Error: can't open output file %s\n", argv[3]); return 1; } //Create pipe for frequency data int fd[2], err; err = pipe(fd); if (err == -1) { printf("Error: failed to create pipe\n"); return 1; } pid_t pid; //initialize child process pid = fork();
- User
explain what the code do
- Assistant
The code is an implementation of the Huffman encoding and decoding algorithm. It takes the input flag as the first argument (-e/-d) which determines whether to encode or decode the input file.
If the input flag is -e, the input file is read character by character, and the frequency of each character is counted and stored in an array. The array is then used to build a Huffman tree, and the codes for each character are generated by traversing the tree. The input file is then read again, and each character is replaced by its corresponding code. The codes are concatenated into a string, and the string is then split into 8-bit chunks, which are written to the output file.
If the input flag is -d, the first line of the input file contains the length of the encoded string in bits. The program reads the length, skips the newline character, and starts reading the encoded string character by character. Each character is treated as a binary string, and the Huffman tree is traversed starting from the root. Going left corresponds to a 0 bit, and going right corresponds to a 1 bit. When a leaf node is reached, the corresponding character is printed to the console, and the character is written to the output file.
- User
edit berdasarkan instruksi berikut : menerapkan konsep pipes dan fork. Gunakan 2 pipes. Pada parent process, baca file yang akan dikompresi dan hitung frekuensi kemunculan huruf pada file tersebut. Kirim hasil perhitungan frekuensi tiap huruf ke child process. Pada child process, lakukan kompresi file dengan menggunakan algoritma Huffman berdasarkan jumlah frekuensi setiap huruf yang telah diterima. Kemudian (pada child process), simpan Huffman tree pada file terkompresi. Untuk setiap huruf pada file, ubah karakter tersebut menjadi kode Huffman dan kirimkan kode tersebut ke program dekompresi menggunakan pipe. Kirim hasil kompresi ke parent process. Lalu, di parent process baca Huffman tree dari file terkompresi. Baca kode Huffman dan lakukan dekompresi. Di parent process, hitung jumlah bit setelah dilakukan kompresi menggunakan algoritma Huffman dan tampilkan pada layar perbandingan jumlah bit antara setelah dan sebelum dikompres dengan algoritma Huffman.
- Assistant