代码为#include<bitsdc++.h> using namespace std; // 定义逻辑运算函数 int AND(int a, int b) { return a && b; } int OR(int a, int b) { return a || b; } int NOT(int a) { return !a; } int IMPLIES(int a, int b) { return !a || b; } int IFF(int a, int b) { return a == b; } int shuai_ge(int a,int b){ return a^b; } // 获取表达式中的逻辑变量 vector<char> getVariables(string expr) { vector<char> vars; for (char c : expr) { if (islower(c) && find(vars.begin(), vars.end(), c) == vars.end()) { vars.push_back(c); } } sort(vars.begin(), vars.end()); return vars; } // 输出逻辑变量的所有组合情况 void printVariableCombinations(vector<char> vars) { int n = vars.size(); for (int i = 0; i < pow(2, n); i++) { for (int j = n - 1; j >= 0; j--) { cout << ((i >> j) & 1) << " "; } cout << "\n"; } } int main() { string expr; while (getline(cin, expr)) { vector<string> tokens; vector<char> vars; string token; // 分割表达式为单词 for (char c : expr) { if (isalpha(c)) { token += c; } else if (token != "") { tokens.push_back(token); token = ""; } if (c == '(' || c == ')' || c == '!' || c == '&' || c == '|' || c == '-' || c == '<' || c == '>'||c == '^') { tokens.push_back(string(1, c)); } } if (token != "") { tokens.push_back(token); } // 获取逻辑变量 vars = getVariables(expr); // 输出单词和逻辑变量 for (string t : tokens) { cout << t << " "; } cout << "\n"; for (char v : vars) { cout << v << " "; } cout << "\n"; // 输出逻辑变量的所有组合情况 printVariableCombinations(vars); } return 0; }
时间: 2024-02-14 08:13:32 浏览: 99
将代码的输出顺序反过来的话,你需要先输出逻辑变量的所有组合情况,再输出逻辑变量和单词,最后再输出表达式中的逻辑变量。修改后的代码如下:
```
#include<bitsdc++.h>
using namespace std;
// 定义逻辑运算函数
int AND(int a, int b) {
return a && b;
}
int OR(int a, int b) {
return a || b;
}
int NOT(int a) {
return !a;
}
int IMPLIES(int a, int b) {
return !a || b;
}
int IFF(int a, int b) {
return a == b;
}
int shuai_ge(int a,int b){
return a^b;
}
// 获取表达式中的逻辑变量
vector<char> getVariables(string expr) {
vector<char> vars;
for (char c : expr) {
if (islower(c) && find(vars.begin(), vars.end(), c) == vars.end()) {
vars.push_back(c);
}
}
sort(vars.begin(), vars.end());
return vars;
}
// 输出逻辑变量的所有组合情况
void printVariableCombinations(vector<char> vars) {
int n = vars.size();
for (int i = pow(2, n) - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
cout << ((i >> j) & 1) << " ";
}
cout << "\n";
}
}
int main() {
string expr;
while (getline(cin, expr)) {
vector<string> tokens;
vector<char> vars;
string token;
// 分割表达式为单词
for (char c : expr) {
if (isalpha(c)) {
token += c;
} else if (token != "") {
tokens.push_back(token);
token = "";
}
if (c == '(' || c == ')' || c == '!' || c == '&' || c == '|' || c == '-' || c == '<' || c == '>'||c == '^') {
tokens.push_back(string(1, c));
}
}
if (token != "") {
tokens.push_back(token);
}
// 获取逻辑变量
vars = getVariables(expr);
// 输出逻辑变量的所有组合情况
printVariableCombinations(vars);
// 输出单词和逻辑变量
for (string t : tokens) {
cout << t << " ";
}
cout << "\n";
for (char v : vars) {
cout << v << " ";
}
cout << "\n";
}
return 0;
}
```
阅读全文