C++实现离散数学之真值表(试着自写头文件)
生活随笔
收集整理的這篇文章主要介紹了
C++实现离散数学之真值表(试着自写头文件)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路參考:https://blog.csdn.net/wjh2622075127/article/details/79843339?感謝作者。
試著寫了一下頭文件,并修改優化了。
頭文件:
#ifndef TRUTH_TABLE_H #define TRUTH_TABLE_H #include<iostream> #include<string> #include<iomanip> #include<cmath> #include<algorithm> using namespace std; class TruthTable {friend void read(istream& is, TruthTable& formula); //讀取輸入friend void print(TruthTable& formula); //計算并輸出 public:void GetVariable(string str, string& var, int& count); //獲取命題變元void fei(string& str); //非void hequ(string& str); //合取void xiqu(string& str); //析取void tiaojian(string& str); //條件void shuangtiaojian(string& str); //雙條件void ToValue(string& str, int val[]); //賦真值給 變元void DelKuohao(string& str); //去除無用的括號(括號內只有一個變元的情況) private:string str, tmp, var;int count = 0;int val[30]{}; }; void TruthTable::GetVariable(string str, string& var, int& count) {int ch[30]{};for (auto i = 0; i != str.size(); ++i)if (isalpha(str[i]))++ch[str[i] - 'A'];for (auto i = 0; i < 26; ++i)if (ch[i]){var.push_back(i + 65);++count;} } void TruthTable::fei(string& str) {for (auto i = 0;i!=str.size();++i){if (i + 1 < str.size() && str[i] == '!' && str[i + 1] == '0')str.replace(i, 2 , "1");else if ( i + 1 < str.size() && str[i] == '!' && str[i + 1] == '1')str.replace(i, 2, "0");} } void TruthTable::hequ(string& str) {for (auto i = 0;i!=str.size();++i){if (str[i] == '1' && i + 2 < str.size() && str[i + 2] == '1' && str[i + 1] == '&')str.replace(i, 3 , "1");else if (i + 2 < str.size() && str[i + 1] == '&' && ((str[i] == '1' && str[i + 2] == '0')|| (str[i] == '0' && str[i + 2] == '1') || (str[i] == '0' && str[i + 2] == '0')))str.replace(i, 3, "0");} } void TruthTable::xiqu(string& str) {for (auto i = 0; i != str.size(); ++i){if (str[i] == '0' && i + 2 < str.size() && str[i + 2] == '0' && str[i + 1] == '|')str.replace(i, 3, "0");else if (i + 2 < str.size() && str[i + 1] == '|' && ((str[i] == '1' && str[i + 2] == '0')|| (str[i] == '0' && str[i + 2] == '1') || (str[i] == '1' && str[i + 2] == '1')))str.replace(i, 3, "1");} } void TruthTable::tiaojian(string& str) {for (auto i = 0; i != str.size(); ++i){if (str[i + 1] == '>' && str[i] == '1' && str[i + 2] == '0')str.replace(i, 3, "0");else if (str[i + 1] == '>' && ((str[i] == '1' && str[i + 2] == '1')|| str[i] == '0' && (str[i + 2] == '1' || str[i + 2] == '0')))str.replace(i, 3, "1");} } void TruthTable::shuangtiaojian(string& str) {for (auto i = 0; i != str.size(); ++i){if (str[i + 1] == '-' && ((str[i] == '1' && str[i + 2] == '1') || (str[i] == '0' && str[i + 2] == '0')))str.replace(i, 3, "1");else if (str[i + 1] == '-' && ((str[i] == '1' && str[i + 2] == '0') || (str[i] == '0' && str[i + 2] == '1')))str.replace(i, 3, "0");} } void TruthTable::ToValue(string& str, int val[]) {for (auto i = 0; i != str.size(); ++i){if (isalpha(str[i]))str.replace(i, 1, val[str[i] - 'A'] ? "1" : "0");} } void TruthTable::DelKuohao(string& str) {for (auto i = 0; i != str.size(); ++i){if (str[i] == '(' && i + 2 < str.size() && str[i + 2] == ')'){string s;s += str[i + 1];str.replace(i, 3, s);}} } void read(istream& is, TruthTable& formula) {is >> formula.str;transform(formula.str.begin(), formula.str.end(), formula.str.begin(), ::toupper);formula.tmp = formula.str;formula.GetVariable(formula.str, formula.var, formula.count); }void print(TruthTable& formula) {for (auto i : formula.var) //輸出真值表格式第一行cout << i << " ";cout << formula.str << endl;for (int i = 0; i != pow(2, formula.count); ++i) //二進制枚舉法{for (int j = 0; j != formula.count; ++j)formula.val[formula.var[j] - 'A'] = (1 & (i >> (formula.count - 1 - j))); //位運算賦值for (int j = 0; j != formula.count; ++j)cout << formula.val[formula.var[j] - 'A'] << " ";formula.ToValue(formula.str, formula.val);while (formula.str.size() != 1){formula.DelKuohao(formula.str);formula.fei(formula.str);formula.hequ(formula.str);formula.xiqu(formula.str);formula.tiaojian(formula.str);formula.shuangtiaojian(formula.str);}cout << setw(formula.tmp.size() / 2 + 1) << formula.str << endl;formula.str = formula.tmp;} } #endif // !TRUTH_TABLEC++文件:
//定義: !為非 &為合取 |為析取 >為條件 -為雙條件 #include"TrueTable.h" int main() {TruthTable formula;cout << "請輸入命題公式(命題變元是大寫字母)" << endl;cout << "注:定義:!為非 &為合取 |為析取 >為條件 -為雙條件" << endl;read(cin, formula);print(formula);system("pause"); }?
總結
以上是生活随笔為你收集整理的C++实现离散数学之真值表(试着自写头文件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日英语:Lighting: Twigs
- 下一篇: 在Visual Studio中利用NTV