编程语言
首页 > 编程语言> > C++实现真值表

C++实现真值表

作者:互联网

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

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

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

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

  2.去非  !;

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

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

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

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 string s, tmp; //表达式
  6 int cnt; //变量个数
  7 char st[26];//统计字母,全部小写字母表示
  8 int f[26];//统计每个变量的赋值情况
  9 int f_t, f_f;//判断表达式的类别
 10 void input(); //界面提示
 11 void Vs(string s, char *c);//变量统计
 12 void output();//输出变量符合和表达式
 13 string vSubstitution();//变量替换
 14 string rBrackets(); //去括号 包括()/(p)
 15 string gToAfrica();//去非
 16 string tJoin();//去析取
 17 string tDisjunct();//去合取
 18 string tContain();//去蕴含
 19 string dEquivalent();//去等价
 20 int main() {
 21     input();
 22     cin >> s;
 23     puts("");
 24     puts("");
 25     cout << s << ' ';
 26     puts("的真值表如下:");
 27     puts("");
 28     tmp = s;
 29     Vs(s, st);
 30     output();
 31     int n;//不同赋值的方法数
 32     n = pow(2, cnt);
 33     cout << '|';
 34     for (int i = 0; i < cnt; i++) cout << "---";
 35     for (int i = 1; i <= tmp.length(); i ++) cout << '-';
 36     cout << '|';
 37     puts("");
 38     for (int i = 0; i < n; i ++) {
 39         cout << '|';
 40         for (int j = 0; j < cnt; j ++) { //赋值过程
 41             f[st[j] - 'a'] = (1 & (i >> (cnt - 1 - j)));
 42         }
 43         for (int j = 0; j < cnt; ++j) {
 44             cout << f[st[j] - 'a'] << " |";
 45         }
 46         for (int i = 0; i < tmp.length() / 2; i ++) cout << ' ';
 47         s = vSubstitution();
 48 
 49         while (s.length() > 1) { //循环判断
 50             s = rBrackets();
 51             s = gToAfrica();
 52             s = tJoin();
 53             s = tDisjunct();
 54             s = tContain();
 55             s = dEquivalent();
 56         }
 57         cout << s;
 58         if (!(tmp.length() % 2)) {
 59             for (int i = 1; i < tmp.length() / 2; i ++) cout << ' ';
 60             cout << '|';
 61         } else if (tmp.length() == 1) cout << "|";
 62         else {
 63             for (int i = 1; i < tmp.length() / 2; i ++) cout << ' ';
 64             cout << " |";
 65         }
 66         puts("");
 67         if (s == "0") f_f++;
 68         else f_t++;
 69         s = tmp;
 70     }
 71 
 72     cout << '|';
 73     for (int i = 0; i < cnt; i++) cout << "---";
 74     for (int i = 1; i <= tmp.length(); i ++) cout << '-';
 75     cout << '|';
 76     puts("");
 77     cout << tmp <<  " 的类型是:";
 78     if (f_f == n) puts("矛盾式");
 79     else if (f_t == n) puts("重言式");
 80     else puts("可满足式");
 81 
 82 
 83     return 0;
 84 }
 85 
 86 void input() {
 87     puts("表达式规则如下:");
 88     puts("否定连接词:!");
 89     puts("合取连接词:|");
 90     puts("析取连接词:&");
 91     puts("蕴含连接词:->");
 92     puts("等价连接词:<->");
 93     puts("请按规范输入表达式,字符间请不要有空格!否则将会程序无法正常运行。");
 94     puts("----------------------------------------------------------------------------------------------------------------------");
 95     puts("请输入你的表达式:") ;
 96 }
 97 
 98 void Vs(string s, char *c) {
 99     int ch[26] = {};
100     for (int i = 0; i < s.length(); i ++) {
101         if (isalpha(s[i])) ch[s[i] - 'a']++; //isalpha判断字符是否为小写字母
102     }
103     for (int i = 0; i < 26; i ++) {
104         if (ch[i]) st[cnt++] = i + 97;
105     }
106 }
107 
108 void output() {
109     cout << '|';
110     for (int i = 0; i < cnt; i++) cout << "---";
111     for (int i = 1; i <= tmp.length(); i ++) cout << '-';
112     cout << '|';
113     puts("");
114     cout << '|';
115     for (int i = 0; i < cnt; i ++) {
116         cout << st[i] << " |";
117     }
118     cout << s << '|' << endl;
119 }
120 
121 string vSubstitution() {
122     string ss = "";
123     for (int i = 0; i < s.length(); i ++) {
124         if (s[i] <= 'z' && s[i] >= 'a') {
125             ss += f[s[i] - 'a'] ? "1" : "0";
126         } else ss += s[i];
127     }
128     return ss;
129 }
130 
131 string  rBrackets() {
132     string ss = "";
133     for (int i = 0; i < s.length(); i ++) {
134         if (s[i] == '(' && (i + 2) < s.length() && s[i + 2] == ')') ss += s[i + 1], i += 2;
135         else ss += s[i];
136     }
137     return ss;
138 }
139 
140 string gToAfrica() {
141     string ss = "";
142     for (int i = 0; i < s.length(); i ++) {
143         if (s[i] == '!' && (i + 1) < s.length() && s[i + 1] == '0') ss += '1', i ++;
144         else if (s[i] == '!' && (i + 1) < s.length() && s[i + 1] == '1') ss += '0', i ++;
145         else ss += s[i];
146     }
147     return ss;
148 }
149 
150 string tJoin() {
151     string ss = "";
152     for (int i = 0; i < s.length(); i ++) {
153         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;
154         else if ((s[i] == '0' && (i + 2) < s.length() && s[i + 2] == '0' && s[i + 1] == '|')) ss += '0', i += 2;
155         else ss += s[i];
156     }
157     return ss;
158 }
159 
160 string tDisjunct() {
161     string ss = "";
162     for (int i = 0; i < s.length(); i ++) {
163         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;
164         else if ((s[i] == '1' && (i + 2) < s.length() && s[i + 2] == '1' && s[i + 1] == '&')) ss += '1', i += 2;
165         else ss += s[i];
166     }
167     return ss;
168 }
169 
170 string tContain() {
171     string ss = "";
172     for (int i = 0; i < s.length(); i ++) {
173         if (s[i] == '1' && (i + 3) < s.length() && s[i + 3] == '0' && s[i + 1] == '-' && s[i + 2] == '>') ss += '0', i += 3;
174         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;
175         else ss += s[i];
176     }
177     return ss;
178 }
179 
180 string dEquivalent() {
181     string ss = "";
182     for (int i = 0; i < s.length(); i ++) {
183         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;
184         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;
185         else ss += s[i];
186     }
187     return ss;
188 }

 

标签:int,ss,真值表,++,C++,实现,length,&&,string
来源: https://www.cnblogs.com/msluli/p/16670827.html