//#include<power.h>
#include<iostream>
#include<cmath>
#include<map>
#include<string>
#include<initializer_list>
using namespace std;
class MakeTable {
private:
int char_num;
map<char, char>charAndValue;
string str;
string tableStr;
public:
friend class Main_Disjunctive_Normal_Form;//方便操作
friend class Main_And_Normal_Form;
MakeTable()noexcept {
str = "";
tableStr = "";
char_num = 0;
}
//获取字符,初始化map
void getAlphaAndDisplayTable() {
const int str_long = str.length();
for (int i = 0; i < str_long; ++i) {
if (isalpha(str[i])) {//保证不重复无序map
charAndValue.emplace(str[i], 0);
}
}
for (auto it = charAndValue.begin(); it != charAndValue.end(); it++) {
cout << it->first << " ";
}
cout << str << "\n";
char_num = charAndValue.size();
}
//设置字符串
void setString(string str) {
this->str = str;
}
//字符数目
int charNum()noexcept {
return char_num;
}
//判断是不是数字
bool isNum(char num)noexcept {
return num > '9' || num < '0' ? false : true;
}
bool isNum(char num1, char num2)noexcept {
return (num1 > '9' || num1 < '0' || num2>'9' || num2 < '0') ? false : true;
}
//得到字符与真值指配
map<char, char>& get_charAndValue()noexcept {
return charAndValue;
}
//得到str
string get_str() {
return str;
}
//获得含有真值的表达式
void charToNum() {
for (int i = 0; i < str.size(); ++i) {
if (isalpha(str[i])) {
str[i] = charAndValue.find(str[i])->second;
}
}
}
//去掉无用括号
void removeBrackets() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '(' && str[i + 2] == ')') {
s += str[i + 1];
i += 2;
}
else {
s += str[i];
}
}
this->str = s;
}
//非运算
void noCla() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '!' && str[i + 1] == '1') {
s += '0';
++i;
}
else if (str[i] == '!' && str[i + 1] == '0') {
s += '1';
++i;
}
else {
s += str[i];
}
}
this->str = s;
}
//与运算&代替
void andCla() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '&' && isNum(str[i - 1], str[i + 1]) && (str[i - 1] == '0' || str[i + 1] == '0')) {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '0');
s += '0';
--i;
}
else if (str[i] == '&' && isNum(str[i - 1], str[i + 1]) && str[i - 1] == '1' && str[i + 1] == '1') {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '1');
s += '1';
--i;
}
else {
s += str[i];
}
}
this->str = s;
}
//或运算|代替
void orCla() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '|' && isNum(str[i - 1], str[i + 1]) && (str[i - 1] == '1' || str[i + 1] == '1')) {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '1');
s += '1';
--i;
}
else if (str[i] == '|' && isNum(str[i - 1], str[i + 1]) && str[i - 1] == '0' && str[i + 1] == '0') {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '0');
s += '0';
--i;
}
else {
s += str[i];
}
}
this->str = s;
}
//条件运算>代替
void if_soCla() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '>' && ((isNum(str[i + 1]) && str[i - 1] == '0') || (str[i - 1] == '1' && str[i + 1] == '1'))) {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '1');
s += '1';
--i;
}
else if (str[i] == '>' && str[i - 1] == '1' && str[i + 1] == '0') {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '0');
s += '0';
--i;
}
else {
s += str[i];
}
}
this->str = s;
}
//双条件运算=代替
void two_if_soCal() {
string s = "";
for (int i = 0; i < str.length(); ++i) {
if (str[i] == '=' && isNum(str[i - 1], str[i + 1]) && str[i - 1] == str[i + 1]) {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '1');
s += '1';
--i;
}
else if (str[i] == '=' && isNum(str[i - 1], str[i + 1]) && str[i - 1] != str[i + 1]) {
s = s.substr(0, s.length() - 1);
str.replace(i - 1, 3, 1, '0');
s += '0';
--i;
}
else {
s += str[i];
}
}
this->str = s;
}
//更新真值表
void updateTableStr(string s) {
if (s != "")this->tableStr += s;
}
};
//制造主析取范式类
class Main_Disjunctive_Normal_Form {
private:
string min_items_num;//小项的二进制编码
string main_normal_form;//主析取范式
public:
//十进制编码变二进制//数学运算可能会溢出
string decToBin(int dec) {
int result = 0, temp = dec, j = 1;
while (temp) {
result = result + j * (temp % 2);
temp = temp / 2;
j = j * 10;
}
return to_string(result);
}
//真值表里1的个数
int true_num(string s) {
int result = 0;
const int s_long = s.size();
for (int i = 0; i < s_long; ++i) {
if (s.at(i)=='1') { ++result; }
}
return result;
}
//输出主析取范式
Main_Disjunctive_Normal_Form& operator<<(MakeTable& table) {
string bin = ""; int times = true_num(table.tableStr);
for (int i = 0; i < table.tableStr.size(); ++i) {//i为小项的十进制编码
if (table.tableStr.at(i) == '1') {//当真值为1时操作
main_normal_form += "(";
bin = decToBin(i);
min_items_num = string(table.char_num - bin.size(), '0') + bin;//小项二进制编码
auto it = table.charAndValue.begin();
for (int j = 0; j < min_items_num.size(); ++j) {
if (min_items_num.at(j) == '0') {
main_normal_form += "┐ ";
main_normal_form.push_back(it->first);
}
else {
main_normal_form.push_back(it->first);
}
if (j < min_items_num.size() - 1) { main_normal_form+="∧"; }
++it;
}
main_normal_form.push_back(')');
}
if (main_normal_form.size()>0&& main_normal_form.at(main_normal_form.size()-1)==')'&× > 1) { main_normal_form+="∨"; --times; }
}
cout << this->main_normal_form<<"\n";
return *this;
}
};
//制造主和取范式类
class Main_And_Normal_Form {
private:
string max_items_num;//小项的二进制编码
string main_normal_form;//主析取范式
public:
//十进制编码变二进制//数学运算可能会溢出
string decToBin(int dec) {
int result = 0, temp = dec, j = 1;
while (temp) {
result = result + j * (temp % 2);
temp = temp / 2;
j = j * 10;
}
return to_string(result);
}
//真值表里1的个数
int false_num(string s) {
int result = 0;
const int s_long = s.size();
for (int i = 0; i < s_long; ++i) {
if (s.at(i) == '0') { ++result; }
}
return result;
}
//输出主析取范式
Main_And_Normal_Form& operator<<(MakeTable& table) {
string bin = ""; int times = false_num(table.tableStr);
for (int i = 0; i < table.tableStr.size(); ++i) {//i为小项的十进制编码
if (table.tableStr.at(i) == '0') {//当真值为1时操作
main_normal_form += "(";
bin = decToBin(i);
max_items_num = string(table.char_num - bin.size(), '0') + bin;//小项二进制编码
auto it = table.charAndValue.begin();
for (int j = 0; j < max_items_num.size(); ++j) {
if (max_items_num.at(j) == '1') {
main_normal_form += "┐ ";
main_normal_form.push_back(it->first);
}
else {
main_normal_form.push_back(it->first);
}
if (j < max_items_num.size() - 1) { main_normal_form += "∨"; }
++it;
}
main_normal_form.push_back(')');
}
if (main_normal_form.size() > 0 && main_normal_form.at(main_normal_form.size() - 1) == ')' && times > 1) { main_normal_form += "∧"; --times; }
}
cout << this->main_normal_form << "\n";
return *this;
}
};
//判断任意表达式真值
int main() {
MakeTable mt;
cout << "Please enter the expression" << "\n";
cout << "&为合取,|为析取,!为否定,>为条件,=为双条件" << "\n";
string ss;
cin >> ss;
mt.setString(ss);
mt.getAlphaAndDisplayTable();
for (int i = 0; i < pow(2, mt.charNum()); ++i) {//指配层数
auto it = mt.get_charAndValue().begin();
for (int j = 0; j < mt.charNum(); ++j) {
it->second = 1 & (i >> (mt.charNum() - j - 1)) ? '1' : '0';//指配
cout << it->second << " ";
it++;
}
mt.charToNum();
//计算
while (mt.get_str().size() > 1) {
mt.removeBrackets();
mt.noCla();
mt.andCla();
mt.orCla();
mt.if_soCla();
mt.two_if_soCal();
}
cout << string(ss.length() / 2, ' ');
cout << mt.get_str() << "\n";
mt.updateTableStr(mt.get_str());
mt.setString(ss);
}
/*cout<<"you want to det"*/
Main_Disjunctive_Normal_Form cout_Disjunctive_Form;
Main_And_Normal_Form cout_And_Form;
cout << "主析取范式为:"<<"\n";
cout_Disjunctive_Form << mt;
cout << "主和取范式" << "\n";
cout_And_Form << mt;
system("pause");
}
字符串操作