1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include <iostream> #include <vector> #include <queue> #include <algorithm> class Multiplexer { private: std::vector<std::queue<int>> inputQueues; // Input queues for multiplexing std::queue<int> outputQueue; // Output queue for multiplexed data public: // Constructor Multiplexer(int numInputs) { inputQueues.resize(numInputs); } // Function to add data to an input queue void addData(int inputIndex, int data) { if (inputIndex < inputQueues.size()) { inputQueues[inputIndex].push(data); } else { std::cerr << "Invalid input index.\n"; } } // Function to multiplex data from all input queues into the output queue void multiplex() { outputQueue = {}; // Clear the output queue for (auto& queue : inputQueues) { while (!queue.empty()) { outputQueue.push(queue.front()); queue.pop(); } } } // Function to get data from the output queue int getData() { if (outputQueue.empty()) { throw std::runtime_error("Output queue is empty."); } int data = outputQueue.front(); outputQueue.pop(); return data; } // Function to check if the output queue is empty bool isOutputEmpty() const { return outputQueue.empty(); } }; class Demultiplexer { private: std::vector<std::queue<int>> outputQueues; // Output queues for demultiplexing public: // Constructor Demultiplexer(int numOutputs) { outputQueues.resize(numOutputs); } // Function to demultiplex data into the specified output queue void demultiplex(int outputIndex, int data) { if (outputIndex < outputQueues.size()) { outputQueues[outputIndex].push(data); } else { std::cerr << "Invalid output index.\n"; } } // Function to get data from a specific output queue int getData(int outputIndex) { if (outputIndex >= outputQueues.size()) { throw std::runtime_error("Invalid output index."); } if (outputQueues[outputIndex].empty()) { throw std::runtime_error("Output queue is empty."); } int data = outputQueues[outputIndex].front(); outputQueues[outputIndex].pop(); return data; } // Function to check if a specific output queue is empty bool isOutputEmpty(int outputIndex) const { if (outputIndex >= outputQueues.size()) { throw std::runtime_error("Invalid output index."); } return outputQueues[outputIndex].empty(); } }; int main() { int numInputs = 3; int numOutputs = 2; Multiplexer mux(numInputs); Demultiplexer demux(numOutputs); // Add data to input queues mux.addData(0, 10); mux.addData(1, 20); mux.addData(2, 30); mux.addData(0, 40); mux.addData(1, 50); // Perform multiplexing mux.multiplex(); // Perform demultiplexing while (!mux.isOutputEmpty()) { int data = mux.getData(); // Simulate demultiplexing based on some criteria int outputIndex = (data % numOutputs); // Example criterion for output index demux.demultiplex(outputIndex, data); } // Display data from output queues for (int i = 0; i < numOutputs; ++i) { std::cout << "Output Queue " << i << ": "; while (!demux.isOutputEmpty(i)) { std::cout << demux.getData(i) << " "; } std::cout << "\n"; } return 0; } |
Explanation
- Class Definitions:
Multiplexer
: Simulates the multiplexing of data from multiple input queues into a single output queue.- Private Members:
inputQueues
: A vector of queues where data is added for multiplexing.outputQueue
: A single queue where data from all input queues is combined.
- Public Methods:
addData(int inputIndex, int data)
: Adds data to a specific input queue.multiplex()
: Combines data from all input queues into the output queue.getData()
: Retrieves and removes data from the output queue.isOutputEmpty()
: Checks if the output queue is empty.
- Private Members:
Demultiplexer
: Simulates the demultiplexing of data from a single input queue into multiple output queues.- Private Members:
outputQueues
: A vector of queues where data is distributed.
- Public Methods:
demultiplex(int outputIndex, int data)
: Adds data to a specific output queue.getData(int outputIndex)
: Retrieves and removes data from a specific output queue.isOutputEmpty(int outputIndex)
: Checks if a specific output queue is empty.
- Private Members:
main
Function:- Creates instances of
Multiplexer
andDemultiplexer
. - Adds data to the multiplexer’s input queues.
- Performs multiplexing to combine data into the output queue.
- Demultiplexes the combined data into specific output queues based on a criterion.
- Displays the data from each output queue.
- Creates instances of