C++实现真值表

  这一片文章主要是关于真值表,在完成之前我也遇到了许多问题。比如怎么去求解表达式的值,怎么去将每个变量进行赋值,也就是如何 将n个字符进行01全排列。

  01全排列真的神奇,01全排列其实就是2^n。他可以表示全部的01,真是神奇。

  怎么去判断表达式的值呢?我们可以分步骤:

  1.先去括号 即 ()/(a);

  2.去非  !;

  3.去&和|,经证明,他们两个的先后顺序不影响表达式的值;

  4.去蕴含  -> 根据其含义进行判断就行;

  5.最后就是去等价了 <-> ,同上。

#include<bits/stdc++.h>

using namespace std;

string s, tmp; //表达式
int cnt; //变量个数
char st[26];//统计字母,全部小写字母表示
int f[26];//统计每个变量的赋值情况
int f_t, f_f;//判断表达式的类别  
void input(); //界面提示
void Vs(string s, char *c);//变量统计
void output();//输出变量符合和表达式
string vSubstitution();//变量替换
string rBrackets(); //去括号 包括()/(p)
string gToAfrica();//去非
string tJoin();//去析取
string tDisjunct();//去合取
string tContain();//去蕴含
string dEquivalent();//去等价
int main() {
	input();
	cin >> s;
	puts("");
	puts("");
	cout << s << ' ';
	puts("的真值表如下:");
	puts("");
	tmp = s;
	Vs(s, st);
	output();
	int n;//不同赋值的方法数
	n = pow(2, cnt);
	cout << '|';
	for (int i = 0; i < cnt; i++) cout << "---";
	for (int i = 1; i <= tmp.length(); i ++) cout << '-';
	cout << '|';
	puts("");
	for (int i = 0; i < n; i ++) {
		cout << '|';
		for (int j = 0; j < cnt; j ++) { //赋值过程
			f[st[j] - 'a'] = (1 & (i >> (cnt - 1 - j)));
		}
		for (int j = 0; j < cnt; ++j) {
			cout << f[st[j] - 'a'] << " |";
		}
		for (int i = 0; i < tmp.length() / 2; i ++) cout << ' ';
		s = vSubstitution();

		while (s.length() > 1) { //循环判断
			s = rBrackets();
			s = gToAfrica();
			s = tJoin();
			s = tDisjunct();
			s = tContain();
			s = dEquivalent();
		}
		cout << s;
		if (!(tmp.length() % 2)) {
			for (int i = 1; i < tmp.length() / 2; i ++) cout << ' ';
			cout << '|';
		} else if (tmp.length() == 1) cout << "|";
		else {
			for (int i = 1; i < tmp.length() / 2; i ++) cout << ' ';
			cout << " |";
		}
		puts("");
		if(s=="0") f_f++;
		else f_t++;
		s = tmp;
	}

	cout << '|';
	for (int i = 0; i < cnt; i++) cout << "---";
	for (int i = 1; i <= tmp.length(); i ++) cout << '-';
	cout << '|';
	puts("");
	cout << tmp <<  " 的类型是:";
	if(f_f==n) puts("矛盾式"); 
	else if(f_t==n) puts("重言式");
	else puts("可满足式"); 
	

	return 0;
}

void input() {
	puts("表达式规则如下:");
	puts("否定连接词:!");
	puts("合取连接词:|");
	puts("析取连接词:&");
	puts("蕴含连接词:->");
	puts("等价连接词:<->");
	puts("请按规范输入表达式,字符间请不要有空格!否则将会程序无法正常运行。");
	puts("----------------------------------------------------------------------------------------------------------------------");
	puts("请输入你的表达式:") ;
}

void Vs(string s, char *c) {
	int ch[26] = {};
	for (int i = 0; i < s.length(); i ++) {
		if (isalpha(s[i])) ch[s[i] - 'a']++; //isalpha判断字符是否为小写字母
	}
	for (int i = 0; i < 26; i ++) {
		if (ch[i]) st[cnt++] = i + 97;
	}
}

void output() {
	cout << '|';
	for (int i = 0; i < cnt; i++) cout << "---";
	for (int i = 1; i <= tmp.length(); i ++) cout << '-';
	cout << '|';
	puts("");
	cout << '|';
	for (int i = 0; i < cnt; i ++) {
		cout << st[i] << " |";
	}
	cout << s << '|' << endl;
}

string vSubstitution() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if (s[i] <= 'z' && s[i] >= 'a') {
			ss += f[s[i] - 'a'] ? "1" : "0";
		} else ss += s[i];
	}
	return ss;
}

string  rBrackets() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if (s[i] == '(' && (i + 2) < s.length() && s[i + 2] == ')') ss += s[i + 1], i += 2;
		else ss += s[i];
	}
	return ss;
}

string gToAfrica() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if (s[i] == '!' && (i + 1) < s.length() && s[i + 1] == '0') ss += '1', i ++;
		else if (s[i] == '!' && (i + 1) < s.length() && s[i + 1] == '1') ss += '0', i ++;
		else ss += s[i];
	}
	return ss;
}

string tJoin() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if ((s[i] == '0' && (i + 2) < s.length() && s[i + 2] == '1' && s[i + 1] == '|') || (s[i] == '1' && (i + 2) < s.length() && s[i + 2] == '0' && s[i + 1] == '|') || (s[i] == '1' && (i + 2) < s.length() && s[i + 2] == '1' && s[i + 1] == '|')) ss += '1', i += 2;
		else if ((s[i] == '0' && (i + 2) < s.length() && s[i + 2] == '0' && s[i + 1] == '|')) ss += '0', i += 2;
		else ss += s[i];
	}
	return ss;
}

string tDisjunct() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if ((s[i] == '0' && (i + 2) < s.length() && s[i + 2] == '1' && s[i + 1] == '&') || (s[i] == '0' && (i + 2) < s.length() && s[i + 2] == '0' && s[i + 1] == '&') || (s[i] == '1' && (i + 2) < s.length() && s[i + 2] == '0' && s[i + 1] == '&')) ss += '0', i += 2;
		else if ((s[i] == '1' && (i + 2) < s.length() && s[i + 2] == '1' && s[i + 1] == '&')) ss += '1', i += 2;
		else ss += s[i];
	}
	return ss;
}

string tContain() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if (s[i] == '1' && (i + 3) < s.length() && s[i + 3] == '0' && s[i + 1] == '-' && s[i + 2] == '>') ss += '0', i += 3;
		else if ((s[i] == '1' && (i + 3) < s.length() && s[i + 3] == '1' && s[i + 1] == '-' && s[i + 2] == '>') || (s[i] == '0') && (i + 3) < s.length() && s[i + 1] == '-' && s[i + 2] == '>' && (s[i + 3] == '0' || s[i + 3] == '1') ) ss += '1', i += 3;
		else ss += s[i];
	}
	return ss;
}

string dEquivalent() {
	string ss = "";
	for (int i = 0; i < s.length(); i ++) {
		if ((s[i] == '0' && (i + 4) < s.length() && s[i + 4] == '0' && s[i + 1] == '<' && s[i + 2] == '-' && s[i + 3] == '>') || (s[i] == '1' && (i + 4) < s.length() && s[i + 4] == '1' && s[i + 1] == '<' && s[i + 2] == '-' && s[i + 3] == '>') ) ss += '1', i += 4;
		else if ((s[i] == '1' && (i + 4) < s.length() && s[i + 4] == '0' && s[i + 1] == '<' && s[i + 2] == '-' && s[i + 3] == '>') || (s[i] == '0' && (i + 4) < s.length() && s[i + 4] == '1' && s[i + 1] == '<' && s[i + 2] == '-' && s[i + 3] == '>') ) ss += '0', i += 4;
		else ss += s[i];
	}
	return ss;
}

 

posted @ 2022-09-08 20:45  Luli&  阅读(356)  评论(0编辑  收藏  举报