写出B,C题的C++代码
时间: 2024-12-20 07:29:17 浏览: 13
以下是针对B题和C题的C++代码实现:
### B题:求任意一个命题公式的真值表
```cpp
#include <iostream>
#include <string>
#include <map>
#include <cmath>
using namespace std;
// 计算单个命题变元的真值
bool evalProposition(char p, bool value) {
return (p == 'T' || p == 't') ? true : false;
}
// 计算逻辑表达式的真值
bool evaluateFormula(string formula, map<char, bool> values) {
for (auto& pair : values) {
char var = pair.first;
bool val = pair.second;
replace(formula.begin(), formula.end(), var, (val ? 'T' : 'F'));
}
// 假设公式中只有 AND, OR, NOT 运算符
while (formula.find("!") != string::npos) {
size_t pos = formula.find("!");
if (pos + 1 < formula.length()) {
bool nextVal = (formula[pos + 1] == 'T');
formula.replace(pos, 2, nextVal ? "F" : "T");
}
}
while (formula.find("&") != string::npos) {
size_t pos = formula.find("&");
bool leftVal = (formula[pos - 1] == 'T');
bool rightVal = (formula[pos + 1] == 'T');
formula.replace(pos - 1, 3, leftVal && rightVal ? "T" : "F");
}
while (formula.find("|") != string::npos) {
size_t pos = formula.find("|");
bool leftVal = (formula[pos - 1] == 'T');
bool rightVal = (formula[pos + 1] == 'T');
formula.replace(pos - 1, 3, leftVal || rightVal ? "T" : "F");
}
return (formula[0] == 'T');
}
void printTruthTable(string formula) {
int numVars = 0;
set<char> variables;
for (char c : formula) {
if (isalpha(c)) {
variables.insert(c);
}
}
numVars = variables.size();
// 打印表头
for (char var : variables) {
cout << var << "\t";
}
cout << formula << endl;
// 生成所有可能的真值组合
int totalCombinations = pow(2, numVars);
for (int i = 0; i < totalCombinations; ++i) {
map<char, bool> values;
int temp = i;
for (char var : variables) {
values[var] = (temp % 2 == 1);
temp /= 2;
}
// 打印当前组合的真值
for (char var : variables) {
cout << (values[var] ? "T" : "F") << "\t";
}
// 计算并打印公式的真值
bool result = evaluateFormula(formula, values);
cout << (result ? "T" : "F") << endl;
}
}
int main() {
string formula;
cout << "请输入命题公式(使用 & 表示 AND,| 表示 OR,! 表示 NOT): ";
cin >> formula;
printTruthTable(formula);
return 0;
}
```
### C题:在B题基础上进一步求出其主析取、主合取范式
```cpp
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;
bool evalProposition(char p, bool value) {
return (p == 'T' || p == 't') ? true : false;
}
bool evaluateFormula(string formula, map<char, bool> values) {
for (auto& pair : values) {
char var = pair.first;
bool val = pair.second;
replace(formula.begin(), formula.end(), var, (val ? 'T' : 'F'));
}
while (formula.find("!") != string::npos) {
size_t pos = formula.find("!");
if (pos + 1 < formula.length()) {
bool nextVal = (formula[pos + 1] == 'T');
formula.replace(pos, 2, nextVal ? "F" : "T");
}
}
while (formula.find("&") != string::npos) {
size_t pos = formula.find("&");
bool leftVal = (formula[pos - 1] == 'T');
bool rightVal = (formula[pos + 1] == 'T');
formula.replace(pos - 1, 3, leftVal && rightVal ? "T" : "F");
}
while (formula.find("|") != string::npos) {
size_t pos = formula.find("|");
bool leftVal = (formula[pos - 1] == 'T');
bool rightVal = (formula[pos + 1] == 'T');
formula.replace(pos - 1, 3, leftVal || rightVal ? "T" : "F");
}
return (formula[0] == 'T');
}
void printTruthTable(string formula, vector<string>& minterms, vector<string>& maxterms) {
int numVars = 0;
set<char> variables;
for (char c : formula) {
if (isalpha(c)) {
variables.insert(c);
}
}
numVars = variables.size();
// 打印表头
for (char var : variables) {
cout << var << "\t";
}
cout << formula << endl;
// 生成所有可能的真值组合
int totalCombinations = pow(2, numVars);
for (int i = 0; i < totalCombinations; ++i) {
map<char, bool> values;
int temp = i;
for (char var : variables) {
values[var] = (temp % 2 == 1);
temp /= 2;
}
// 打印当前组合的真值
for (char var : variables) {
cout << (values[var] ? "T" : "F") << "\t";
}
// 计算并打印公式的真值
bool result = evaluateFormula(formula, values);
cout << (result ? "T" : "F") << endl;
// 记录主析取范式和主合取范式
if (result) {
string minterm = "";
for (char var : variables) {
minterm += (values[var] ? var : tolower(var));
minterm += "&";
}
minterm.pop_back();
minterms.push_back(minterm);
} else {
string maxterm = "";
for (char var : variables) {
maxterm += (values[var] ? tolower(var) : var);
maxterm += "|";
}
maxterm.pop_back();
maxterms.push_back(maxterm);
}
}
}
int main() {
string formula;
cout << "请输入命题公式(使用 & 表示 AND,| 表示 OR,! 表示 NOT): ";
cin >> formula;
vector<string> minterms;
vector<string> maxterms;
printTruthTable(formula, minterms, maxterms);
// 输出主析取范式
cout << "主析取范式: ";
for (const auto& term : minterms) {
cout << "(" << term << ") | ";
}
if (!minterms.empty()) {
cout << "\b\b\b"; // 删除最后一个 |
}
cout << endl;
// 输出主合取范式
cout << "主合取范式: ";
for (const auto& term : maxterms) {
cout << "(" << term << ") & ";
}
if (!maxterms.empty()) {
cout << "\b\b\b"; // 删除最后一个 &
}
cout << endl;
return 0;
}
```
### 说明
1. **B题**:实现了输入命题公式并生成其真值表的功能。
2. **C题**:在B题的基础上,增加了计算并输出主析取范式和主合取范式的功能。
### 使用方法
1. 编译并运行上述C++代码。
2. 输入命题公式(如 `(p&q)|(!r)`),程序会输出真值表以及主析取范式和主合取范式。
阅读全文