提问人:Damian Roberts 提问时间:6/30/2023 更新时间:6/30/2023 访问量:21
在获取代码以根据用户输入绘制 kmap 时遇到困难,并根据 SOP 和 POS 对它们进行分组,然后以简化和原始的方式编写它们
Having trouble getting code to draw kmap based on user input, and grouping them based on SOP and POS then writing them in simplified and original
问:
你能帮我修复这段代码吗?
#include
#include
#include
#include
#include
使用命名空间 std;
打印卡诺地图的功能
void printKMap(const vector<vector>& kMap, const vector& variables) {
int numRows = kMap.size();
int numCols = kMap[0].size();
cout << "========K Map========" << endl;
cout << "\\";
for (int j = 0; j < numCols; j++) {
cout << variables[j] << " ";
}
cout << "|" << endl;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (j == 0) {
cout << i << " | ";
}
cout << kMap[i][j] << " ";
}
cout << endl;
}
}
简化组和生成简化表达式的功能
string simplifyGroups(const vector<vector>& groups, const vector& variables) {
stringstream ss;
for (int i = 0; i < groups.size(); i++) {
bool isFirstTerm = true;
for (int j = 0; j < variables.size(); j++) {
if (groups[i][j] != -1) {
if (!isFirstTerm) {
ss << " + ";
}
if (groups[i][j] == 0) {
ss << variables[j] << "'";
} else {
ss << variables[j];
}
isFirstTerm = false;
}
}
}
return ss.str();
}
生成原始表达式的函数
string getOriginalExpression(const vector<vector>& groups, const vector& variables) {
stringstream ss;
for (int i = 0; i < groups.size(); i++) {
bool isFirstTerm = true;
for (int j = 0; j < variables.size(); j++) {
if (groups[i][j] != -1) {
if (!isFirstTerm) {
ss << " + ";
}
if (groups[i][j] == 0) {
ss << variables[j] << "'";
} else {
ss << variables[j];
}
isFirstTerm = false;
}
}
}
return ss.str();
}
int main() {
char runAgain = 'Y';
while (runAgain == 'Y') {
int numVariables;
cout << "Enter the number of variables: ";
cin >> numVariables;
vector<string> variables(numVariables);
cout << "Enter the variable letters: ";
for (int i = 0; i < numVariables; i++) {
cin >> variables[i];
}
int numLocations;
cout << "Enter the number of locations of the 1 values: ";
cin >> numLocations;
int numRows = pow(2, numVariables);
int numCols = numVariables;
vector<vector<int>> kMap(numRows, vector<int>(numCols, 0));
cout << "Enter the locations of the 1 values: ";
for (int i = 0; i < numLocations; i++) {
int location;
cin >> location;
kMap[location >> numVariables][location & ((1 << numVariables) - 1)] = 1;
}
printKMap(kMap, variables);
vector<vector<int>> groups;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (kMap[i][j] == 1) {
vector<int> group(numCols, -1);
group[j] = 1;
for (int k = j + 1; k < numCols; k++) {
if (kMap[i][k] == 1) {
group[k] = 1;
}
}
groups.push_back(group);
}
}
}
cout << endl;
for (int i = 0; i < groups.size(); i++) {
cout << "Group " << i + 1 << ": ";
for (int j = 0; j < numCols; j++) {
if (groups[i][j] != -1) {
cout << pow(2, j) * groups[i][j] << " ";
}
}
cout << endl;
}
string simplifiedExpr = simplifyGroups(groups, variables);
cout << "Simplified Expression: F(";
for (int i = 0; i < numVariables; i++) {
cout << variables[i];
if (i < numVariables - 1) {
cout << ",";
}
}
cout << ") = " << simplifiedExpr << endl;
string originalExpr = getOriginalExpression(groups, variables);
cout << "Original Expression: F(";
for (int i = 0; i < numVariables; i++) {
cout << variables[i];
if (i < numVariables - 1) {
cout << ",";
}
}
cout << ") = " << originalExpr << endl;
cout << "Run again (Y/N): ";
cin >> runAgain;
runAgain = toupper(runAgain);
}
return 0;
}
这就是我的教授想要的输出的样子,但我就是做不到。
输入变量字母:WXYZ
输入 1 值的位置:0 2 4 5 6 7 8 12
========K 地图=========
\YZ|
WX \ |00 01 11 10
-----+----------------
00 |1 |0 |0 |1 |
-----+---+---+---+----
01 |1 |1 |1 |1 |
-----+---+---+---+----
11 |1 |0 |0 |0 |
-----+---+---+---+----
10 |1 |0 |0 |0 |
第1组: 0 4 8 12
第 1 组 -> Y'Z' 的简化
第 2 组:4 5 6 7
第 2 组 -> W'X 的简化
第3组:0 2 4 6
第 3 组 -> W'Z' 的简化
简化表达式:F(W,X,Y,Z) = Y'Z' + W'X + W'Z'
原始表达式: F(W,X,Y,Z) = W'X'Y'Z' + W'X'YZ' + W'XY'Z' + W'XY'Z + W'XYZ'
- W'XYZ + WX'Y'Z' + WXY'Z'
再次运行(是/否):N
我尝试获得教授想要的输出,但 kmap 没有正确绘制,如果分组导致我无法绘制 kmap,我无法测试我的分组或简化。
答: 暂无答案
评论