-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImplicant.cpp
122 lines (90 loc) · 2.4 KB
/
Implicant.cpp
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
#include "Implicant.h"
Implicant::Implicant() {}
Implicant::Implicant(int variables, int n){
this->noVariables = variables;
this->binary = dectoBin(n);
pad(variables); // padding the binary with zeroes
generateName();
coveredTerms.insert(n);
}
Implicant::Implicant(string x, set<int> coveredTerms) {
this->binary = x;
this->noVariables = x.size();
this->coveredTerms = coveredTerms;
generateName();
}
Implicant::Implicant(const Implicant& imp){
this->binary = imp.binary;
this->name = imp.name;
this->coveredTerms = imp.coveredTerms;
this->noVariables = imp.noVariables;
generateName();
}
void Implicant::generateName() {
name = "";
for(char c = 'A'; c < char('A'+ noVariables); c++){ // write the expression in Implicants of letters
int idx = c - 'A';
if (binary[idx] != '-') {
name += c;
if(binary[idx] == '0'){
name += '\'';
}
}
}
}
string Implicant::dectoBin(int n){
if(n == 0)
return "0";
if(n % 2 == 0)
return dectoBin(n/2)+"0";
return dectoBin(n/2)+"1";
}
void Implicant::pad(int variables){
if (binary.length() > variables) {
binary = binary.substr(1, binary.size() - 1);
return;
}
int actualLengthBeforePadding = binary.length();
int zeroesPadded= variables-actualLengthBeforePadding;
string b = "";
for(int i = 0;i < zeroesPadded; i++){
b += "0";
}
b = b + this->binary;
this->binary=b; // zeroes added to the beginning of the binary
}
string Implicant::getBin(){
return binary;
}
string Implicant::getName(){
return name;
}
string Implicant::replace_complements(int idx){
string temp = binary;
temp[idx] = '-';
return temp;
}
bool Implicant:: operator ==(const Implicant& Implicant1){
return this->binary == Implicant1.binary;
}
char& Implicant:: operator[](int i){
return this->binary[i];
}
bool Implicant:: operator <(const Implicant& Implicant1){
return this->binary<Implicant1.binary;
}
void Implicant::addTerm(int num){
coveredTerms.insert(num);
}
int Implicant::getNoTerms(){
return this->coveredTerms.size();
}
set<int> Implicant::getCoveredTerms() const {
return this->coveredTerms;
}
void Implicant::changeBit(int i){
if(this->binary[i] == '0')
this->binary[i] = '1';
else
this->binary[i] = '0';
}