exlpain#include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h>…
- User
exlpain#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];
qend--; 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; }
s[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
This is a C program that implements Huffman coding. It starts by defining a node structure that is used to build a Huffman tree. The program reads in a file and builds a Huffman tree based on the frequency of characters in the file. It then assigns a unique binary code to each character based on its position in the Huffman tree. The program can encode and decode files using this Huffman code.
The program uses several global variables, including the number of nodes, a priority queue, and a string array to store the binary codes for each character. The program also uses several functions, including new_node() to create a new node, qinsert() and qremove() to manage the priority queue, build_code() to assign binary codes to each character in the Huffman tree, and import_file() to read in a file and build a Huffman tree based on its character frequency.
The encode() function encodes a file using the binary codes assigned to each character in the Huffman tree and saves the output to a new file. The decode() function decodes a file using the Huffman tree and saves the output to a new file. The print_code() function prints out a table of characters, their frequency, and their binary codes.
- User
the program lack main function. Implement the main function based on this instruction : 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 (encode) 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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.