1. 程式人生 > >A Simple Problem About Truth Table

A Simple Problem About Truth Table

函數定義 進制轉換 blog binary expr cas 初始化 表達 n)

技術分享

源代碼:

#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