A Simple Problem About Truth Table
Posted Intro1997
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A Simple Problem About Truth Table相关的知识,希望对你有一定的参考价值。
源代码:
#include<iostream> #include<string> #include<cmath> using namespace std; // 一个全局变量 char num[10]; //函数声明 void p_sort(const string & binary, int n); void to_binary(string binary, int i, int n, string expression); void judgement(string expression); //函数定义 void to_binary(string binary, int i, int n, string expression) { int x = i, j = n, count = 0, note; while (x != 0) { binary[j - 1] = (x % 2) + 48; // 将余数赋值给 binary 数组 x /= 2; // 将除数赋值给 x j--; } p_sort(binary, n); for (i = 0; i < expression.length(); i++) //将字母与存储顺序一一对应 { if (\'a\' <= expression[i] && expression[i] <= \'z\') { for (j = 0; j < n; j++) if (expression[i] == num[j]) expression[i] = j + 48; } } for (i = 0; i < expression.length(); i++) //将所有的字母修改为 T or F。 { if (\'0\' <= expression[i] && expression[i] <= \'9\') { for (j = 0; j < n; j++) { if (expression[i] == j + 48) expression[i] = binary[j] == \'1\' ? \'T\' : \'F\'; } } } for (i = 0; i < expression.length(); i++)//将所有前面有 \'~\' 号的字母和第一个 \'~\' 修改为 T or F { if (expression[i] == \'~\') { count++; if (count == 1) note = i; } if (expression[i] == \'~\' && expression[i + 1] != \'~\') { if (count % 2 != 0) expression[note] = expression[i + 1] = expression[i + 1] == \'T\' ? \'F\' : \'T\'; else expression[note] = expression[i + 1]; count = 0; } } judgement(expression); } void p_sort(const string & binary, int n) { int i; for (i = 0; i < n; i++) { // 打印 3 ~ n 行真值表 if (binary[i] == \'1\') cout << "T "; else cout << "F "; } } void judgement(string expression) // 判断表达式的最终结果 { int i; char res = \'T\'; for (i = 0; i < expression.length(); i++) { switch (expression[i]) { case\'*\': //A AND B if (expression[i + 1] == \'T\' && expression[i - 1] == \'T\') res = expression[i + 1] = \'T\'; else res = expression[i + 1] = \'F\'; break; case\'+\': //A OR B if (expression[i + 1] != \'F\' || expression[i - 1] != \'F\') res = expression[i + 1] = \'T\'; else res = expression[i + 1] = \'F\'; break; case\'%\': //A XOR B if (expression[i + 1] == expression[i - 1]) res = expression[i + 1] = \'F\'; else res = expression[i + 1] = \'T\'; break; case\'>\': //A IMPLY B if (expression[i - 1] == \'T\' && expression[i + 1] == \'F\') res = expression[i + 1] = \'F\'; else res = expression[i + 1] = \'T\'; break; case\'^\': //A IF AND ONLY IF B if (expression[i - 1] == \'T\' && expression[i + 1] == \'F\') res = expression[i + 1] = \'F\'; else res = expression[i + 1] = \'T\'; break; default: break; } } cout << "| " << res << endl; } int main() { int n, i, count, tem_n; string binary = " "; string temp = " "; string expression; while (cin >> n) { for (i = 0; i < n; i++) // 输入数据 cin >> num[i]; for (i = 0; i < n; i++) // 初始化二进制转换 binary[i] = \'0\'; temp = binary; cin >> expression; // 输入表达式 for (i = 0; i < n; i++) // 打印第一行真值表 cout << num[i] << " "; cout << "| "; cout << expression; cout << "\\n"; for (i = 0; i < (n + 1) * 3 + expression.length(); i++) cout << "-"; cout << endl; tem_n = n; i = n = pow(2, n); // 计算排列组合的基数 例如 2 ^ 3 = 8; while (i--) { temp = binary; to_binary(temp, i, tem_n, expression); //十进制转二进制 } cout << endl; } return 0; }
以上是关于A Simple Problem About Truth Table的主要内容,如果未能解决你的问题,请参考以下文章
a new way of thinking about a problem
bzoj 3489: A simple rmq problem