-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
116 lines (101 loc) · 2.93 KB
/
main.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
/*********************************************************************************************************************************************
* *
* *
* MEMBROS DO GRUPO: *
* Adriel Martins de Souza RA: 1140482422019 *
* João Vitor Cavalcante Barros RA: 1140482422035 *
* Vinícius Gabriel Dias Batista RA: 1140482422009 *
* *
* *
* *
********************************************************************************************************************************************/
#include <cmath>
#include <cstdio>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
using namespace std;
vector<int> get_bits(int x, int nvar) {
vector<int> resultado;
for (int i = nvar - 1; i >= 0; i--) {
resultado.push_back((x & (1 << i)) != 0);
}
return resultado;
}
void array_expose(string name, const vector<string> &array) {
cout << name << endl;
for (const auto &item : array) {
cout << item << endl;
}
}
int formula(unsigned int target, vector<string> *array, vector<int> y,
vector<string> var_names) {
if (target != 0 && target != 1) {
cerr << "target precisa ser binário: " << target << endl;
return 1;
}
int nvar = var_names.size();
for (int i = 0; i < pow(2, nvar); i++) {
if (y[i] == target) {
vector<int> vars = get_bits(i, nvar);
string buf;
for (int j = 0; j < vars.size(); j++) {
if (vars[j] == 1) {
buf += var_names[j];
} else {
buf += "~" + var_names[j];
}
if (j < vars.size() - 1)
buf += (target == 1) ? " * " : " + ";
}
array->push_back(buf);
}
}
return 0;
}
int main() {
vector<int> y;
vector<string> mintermo;
vector<string> maxtermo;
vector<string> var_names;
int nvar;
cout << "Número de variáveis: ";
cin >> nvar;
cout << "Nomes das variáveis: ";
for (int i = 0; i < nvar; i++) {
string temp;
cin >> temp;
var_names.push_back(temp);
}
// Receber os valores de y
cout << "# |";
for (int i = 0; i < nvar; i++) {
cout << var_names[i] << " |";
}
cout << " Y\n";
for (int i = 0; i < pow(2, nvar); i++) {
while (true) {
cout << i << " |";
for (int j = 0; j < nvar; j++) {
cout << get_bits(i, nvar)[j] << " |";
}
cout << " ";
int buf;
cin >> buf;
if (buf == 1 || buf == 0) {
y.push_back(buf);
break;
} else {
cerr << "Insira um número válido (0 ou 1)" << endl;
}
}
}
// Gerar mintermos e maxtermos
formula(1, &mintermo, y, var_names); // Mintermos (produto de somas)
formula(0, &maxtermo, y, var_names); // Maxtermos (soma de produtos)
// Expor os resultados
array_expose("Mintermo:", mintermo);
array_expose("Maxtermo:", maxtermo);
return 0;
}