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 }
C++实现真值表的更多相关文章
- php版的求表达式的真值表-TrueValueTable
贴上代码: <?php error_reporting(E_ALL & ~E_NOTICE); $expression=$_GET['TrueTable']; //读取输入框数据 if( ...
- 离散数学——python实现真值表和打印主范式
最近用python实现了真值表,经过有点儿曲折,刚开始没考虑优先级,直到前天才发现这个问题(离散数学没学好啊),用栈改了一下.话说python就是强,把列表类型当栈用,直接调用列表的pop()和app ...
- 关于74HC4051的逻辑真值表及延时的重要性/在AD测量中的校准
一 关于74HC4051: 在/E=0使能输出的条件下,S2S1S0的三个值,能选通Y0~Y7其中的一个通道从Z输出. 二:问题提出:在按照IC给出的真值表进行芯片操作时,输出逻辑完全对不上 三:分析 ...
- 构造命题公式的真值表--biaobiao88
对给出的任意一个命题公式(不超过四个命题变元),使学生会用C语言的程序编程表示出来,并且能够计算它在各组真值指派下所应有的真值,画出其真值表. #include<iostream> usi ...
- C++实现求离散数学命题公式的真值表
一.实验内容 (1)求任意一个命题公式的真值表. (2)利用真值表求任意一个命题公式的主范式. (3)利用真值表进行逻辑推理. 注:(2)和(3)可在(1)的基础上完成. 二.实验目的 真值表是命题逻 ...
- C#生成真值表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- python真值表
author:headsen chen date :2018-06-01 10:53:39 notice:not allowed to copy or you will count law que ...
- empty(), is_null(), isset()真值表(区别)
- CRC、反码求和校验 原理分析
3月份开始从客户端转后台,算是幸运的进入全栈工程师的修炼阶段.这段时间一边是老项目的客户端加服务器两边的维护和交接,一边是新项目加加加班赶工,期间最长经历了连续工作三天只睡了四五个小时的煎熬,人生也算 ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
随机推荐
- Yarn上运行spark-shell和spark-sql命令行
spark-shell On Yarn spark-shell \ --master yarn-client \ --executor-memory 1G \ --num-executors 10 s ...
- MariaDB数据库 主-从 部署
〇.前言 好久没碰数据库了 准备环境: centos7自带的MariaDB,没有的话下面是安装命令 yum install -y mariadb mariadb-server systemctl re ...
- ElasticSearch介绍和基本用法(一)
ElasticSearch 引言 1.在海量数据中执行搜索功能时,如果使用MySQL, 效率太低. 2.如果关键字输入的不准确,一样可以搜索到想要的数据. 3.将搜索关键字,以红色的字体展示. 介绍: ...
- vCenter 升级错误 VCSServiceManager 1603
近日,看到了VMware发布的vCenter 6.7 Update 1b的更新消息.其中有一条比较震撼.有误删所有VM的概率,这种BUG谁也承受不起. Removing a virtual machi ...
- Nginx+lua+openresty精简系列
1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...
- 第一个Django应用 - 第六部分:静态文件
前面我们编写了一个经过测试的投票应用,现在让我们给它添加一张样式表和一张背景图片. 除了由服务器生成的HTML文件外,WEB应用一般需要提供一些其它的必要文件,比如图片文件.JavaScript脚本和 ...
- Elasticsearch:跨集群搜索 Cross-cluster search(CCS)及安全
文章转载自:https://elasticstack.blog.csdn.net/article/details/116569527
- Logstash:使用 Logstash 导入 CSV 文件示例
转载自:https://elasticstack.blog.csdn.net/article/details/114374804 在今天的文章中,我将展示如何使用 file input 结合 mult ...
- 4.Gitlab CI 与 Kubernetes 的结合
参考网址:https://www.qikqiak.com/post/gitlab-ci-k8s-cluster-feature/
- JavaScript根据参数获取url中参数名的值
//假设ulr如下var localhost="http://127.0.0.1?name=tom&sex=男&id=1";//正则方法封装function Get ...