Respuesta :
#include <iostream>
#include <string>
#include <queue>
#include <cstdlib>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
struct SymbolFrequency
{
char symbol;
int frequency;
};
struct FibonacciCode
{
char symbol;
int frequency;
string code;
};
queue<SymbolFrequency> symbolQueue;
queue<FibonacciCode> codeQueue;
sem_t semaphore;
void* generateFibonacciCode(void* arg)
{
// Wait on semaphore
sem_wait(&semaphore);
// Get symbol and its frequency from the queue
SymbolFrequency sf = symbolQueue.front();
symbolQueue.pop();
// Calculate Fibonacci code
int a = 0;
int b = 1;
string code = "";
for (int i = 0; i < sf.frequency; i++)
{
int c = a + b;
a = b;
b = c;
code += '1';
}
for (int i = 0; i < sf.frequency - 1; i++)
{
code += '0';
}
// Create FibonacciCode struct
FibonacciCode fc;
fc.symbol = sf.symbol;
fc.frequency = sf.frequency;
fc.code = code;
// Push it to the queue
codeQueue.push(fc);
// Print symbol, frequency, and code
cout << "Symbol: " << fc.symbol << ", Frequency: " << fc.frequency << ", Code: " << fc.code << endl;
// Post semaphore
sem_post(&semaphore);
return nullptr;
}
int main()
{
// Read number of symbols
int n;
cin >> n;
// Read symbols and their frequencies
for (int i = 0; i < n; i++)
{
SymbolFrequency sf;
cin >> sf.symbol >> sf.frequency;
symbolQueue.push(sf);
}
// Initialize semaphore
sem_init(&semaphore, 0, 1);
// Create n threads
pthread_t threads[n];
for (int i = 0; i < n; i++)
{
pthread_create(&threads[i], nullptr, generateFibonacciCode, nullptr);
}
// Wait for threads to finish
for (int i = 0; i < n; i++)
{
pthread_join(threads[i], nullptr);
}
// Read and decompress file
string fileName;
cin >> fileName;
ifstream file(fileName);
string compressedString;
file >> compressedString;
file.close();
string decompressedString = "";
for (int i = 0; i < compressedString.length(); i++)
{
for (int j = 0; j < codeQueue.size(); j++)
{
if (codeQueue.front().code[i] == compressedString[i])
{
decompressedString += codeQueue.front().symbol;
break;
}
else
{
FibonacciCode fc = codeQueue.front();
codeQueue.pop();
codeQueue.push(fc);
}
}
}
cout << "Decompressed message = " << decompressedString << endl;
return 0;
}
What is symbol frequency?
Symbol frequency is a measure of the number of times a symbol appears in a given sample of data. It is used in coding theory, cryptography, telecommunications, and other areas of mathematics to analyze the structure and complexity of a message or data stream. Symbol frequency can be used to detect patterns and anomalies in data, or to encode and decode messages. In telecommunications, it is used to measure the quality of a signal or data transmission, or to determine the strength of a signal. Symbol frequency analysis is also used to study the language of a document, such as its syntax, grammar, and punctuation.
To learn more about symbol frequency
https://brainly.in/question/2750753
#SPJ4