the following code should print essential prime implicant but i doesnt help and I also want to
print Also, print the minterms that are not covered by the essential PIs.this is c++
#include
#include
using namespace std;
// Define a structure to store a prime implicant
struct PrimeImplicant {
vector value; // The bit array representing the value of the term
vector dontcare; // The bit array representing the don't care bits (X) of the term
int size; // The size of the term (number of variables)
// Constructor to initialize a prime implicant with a given value and size
PrimeImplicant(vector v, int s) {
value = v;
dontcare = vector(s, false); // Initially no don't care bits
size = s;
}
// A function to print a prime implicant as a Boolean expression
void print() {
char var = 'A'; // The first variable name
bool first = true; // A flag to indicate if this is the first literal in the term
for (int i = 0; i < size; i++) {
if (!dontcare[i]) { // If this bit is not a don't care bit
if (!first) cout << ""; // Print a multiplication sign if not the first literal
else first = false;
if (!value[i]) {
cout << var; // Print the variable name
cout << "'"; // Print a negation sign if this bit is zero
}
else cout << var; // Print the variable name
}
var++; // Increment the variable name
}
cout << endl;
}
};
// A function to compare two prime implicants for equality
bool equal(PrimeImplicant p1, PrimeImplicant p2) {
for (int i = 0; i < p1.size; i++) {
if (p1.value[i] != p2.value[i] || p1.dontcare[i] != p2.dontcare[i]) return false;
}
return true;
}
// A function to check if one prime implicant is contained by another
bool contained(PrimeImplicant p1, PrimeImplicant p2) {
for (int i = 0; i < p1.size; i++) {
if ((p1.value[i] != p2.value[i] && !p2.dontcare[i]) || (p1.dontcare[i] && !p2.dontcare[i]))
return false;
}
return true;
}
// A function to merge two adjacent prime implicants into one larger one
PrimeImplicant merge(PrimeImplicant p1, PrimeImplicant p2) {
int diff = -1; // The index where they differ by one bit
for (int i = 0; i < p1.size; i++) {
if (p1.value[i] != p2.value[i]) { // If they differ by one bit at this index
if (diff == -1) diff = i; // Record the index if this is the first difference found
else return p1; // Otherwise return either one of them as they are not adjacent
}
else if (p1.dontcare[i] != p2.dontcare[i]) return p1; // If they differ by don't care bits at this
index, return either one of them as they are not adjacent
}
PrimeImplicant result(p1.value, p1.size);
result.dontcare[diff] = true;// Make a new prime implicant with same value and size as either
one of them and make its don't care bit true at the index where they differ
return result;// Return the new merged prime implicant
}
// Define an array to store all the given primeimplicants as structures
//vector given;
// Define an array to store all he essential prime implicantsas structures
vector essential;
// A function to find and print all the essential prime implicants using the g.
the following code should print essential prime implicant but i does.pdf
1. the following code should print essential prime implicant but i doesnt help and I also want to
print Also, print the minterms that are not covered by the essential PIs.this is c++
#include
#include
using namespace std;
// Define a structure to store a prime implicant
struct PrimeImplicant {
vector value; // The bit array representing the value of the term
vector dontcare; // The bit array representing the don't care bits (X) of the term
int size; // The size of the term (number of variables)
// Constructor to initialize a prime implicant with a given value and size
PrimeImplicant(vector v, int s) {
value = v;
dontcare = vector(s, false); // Initially no don't care bits
size = s;
}
// A function to print a prime implicant as a Boolean expression
void print() {
char var = 'A'; // The first variable name
bool first = true; // A flag to indicate if this is the first literal in the term
for (int i = 0; i < size; i++) {
if (!dontcare[i]) { // If this bit is not a don't care bit
if (!first) cout << ""; // Print a multiplication sign if not the first literal
else first = false;
if (!value[i]) {
cout << var; // Print the variable name
cout << "'"; // Print a negation sign if this bit is zero
}
else cout << var; // Print the variable name
}
var++; // Increment the variable name
}
cout << endl;
}
2. };
// A function to compare two prime implicants for equality
bool equal(PrimeImplicant p1, PrimeImplicant p2) {
for (int i = 0; i < p1.size; i++) {
if (p1.value[i] != p2.value[i] || p1.dontcare[i] != p2.dontcare[i]) return false;
}
return true;
}
// A function to check if one prime implicant is contained by another
bool contained(PrimeImplicant p1, PrimeImplicant p2) {
for (int i = 0; i < p1.size; i++) {
if ((p1.value[i] != p2.value[i] && !p2.dontcare[i]) || (p1.dontcare[i] && !p2.dontcare[i]))
return false;
}
return true;
}
// A function to merge two adjacent prime implicants into one larger one
PrimeImplicant merge(PrimeImplicant p1, PrimeImplicant p2) {
int diff = -1; // The index where they differ by one bit
for (int i = 0; i < p1.size; i++) {
if (p1.value[i] != p2.value[i]) { // If they differ by one bit at this index
if (diff == -1) diff = i; // Record the index if this is the first difference found
else return p1; // Otherwise return either one of them as they are not adjacent
}
else if (p1.dontcare[i] != p2.dontcare[i]) return p1; // If they differ by don't care bits at this
index, return either one of them as they are not adjacent
}
PrimeImplicant result(p1.value, p1.size);
result.dontcare[diff] = true;// Make a new prime implicant with same value and size as either
one of them and make its don't care bit true at the index where they differ
return result;// Return the new merged prime implicant
}
// Define an array to store all the given primeimplicants as structures
//vector given;
// Define an array to store all he essential prime implicantsas structures
vector essential;
3. // A function to find and print all the essential prime implicants using the given prime implicants
void findEssential(vector &given) {
vector temp;// Define a temporary array to store intermediate results
vector used(given.size(), false);// Define an array of flags to mark which given prime
implicants are used or not
bool done;
// A flag to indicate if the simplification process is done or not
done = false;
// Copy all the given prime implicants to the temporary array
for (int i = 0; i < given.size(); i++) {
temp.push_back(given[i]);
}
// Repeat until all essential prime implicants are found
while (!done) {
done = true;
// Check each prime implicant in the temporary array against all the others
for (int i = 0; i < temp.size(); i++) {
bool essentialFlag = true; // A flag to indicate if this prime implicant is essential
for (int j = 0; j < temp.size(); j++) {
if (i != j && contained(temp[i], temp[j])) {
essentialFlag = false; // If this prime implicant is contained in another, it is not
essential
break;
}
}
if (essentialFlag) { // If this prime implicant is essential, add it to the array of essential
prime implicants
essential.push_back(temp[i]);
for (int j = 0; j < given.size(); j++) {
if (equal(temp[i], given[j])) used[j] = true; // Mark the corresponding given prime
implicant as used
}
temp.erase(temp.begin() + i); // Remove this prime implicant from the temporary
array
done = false; // Set the flag to indicate that the simplification process is not done yet
break; // Start over from the beginning of the temporary array
4. }
}
}
// Print the essential prime implicants
cout << "Essential Prime Implicants:" << endl;
for (int i = 0; i < essential.size(); i++) {
essential[i].print();
}
}
int main() {
// Define the given prime implicants
vector given = {
PrimeImplicant({0,0,0}, 3),
PrimeImplicant({0,0,1}, 3),
PrimeImplicant({0,1,0}, 3),
PrimeImplicant({1,1,0}, 3),
PrimeImplicant({1,1,1}, 3)
};
// Find and print the essential prime implicants
findEssential(given);
return 0;
}