南邮离散数学实验1 (简单版) 根据真值求真值表和主范式

来源:互联网 发布:苹果美版网络锁查询 编辑:程序博客网 时间:2024/06/06 02:19
#include <iostream>#include <cmath>using namespace std;int const MAX = 1e6;short true_value[MAX]; //真值short true_table[MAX][10]; //真值表short pdnf[MAX];    //主析取范式short pcnf[MAX];    //主合取范式char varible[10] = {'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'}; //定义变元int cnt1 = 0, cnt2 = 0; //cnt1记录真值为1的数目,cnt2记录真值为0的数目void Output_pdnf()  //输出主析取范式{    cout << "主析取范式为 : " << endl;    if(cnt2 == 1)        cout << 'm' << pdnf[0] << endl;    else    {        for(int i = 0; i < cnt2 - 1; i++)            cout << 'm' << pdnf[i] << " V ";        cout << 'm' << pdnf[cnt2 - 1] << endl;    }}void Output_pcnf() //输出主合取范式{    cout << "主合取范式为 : " << endl;    if(cnt1 == 1)        cout << 'M' << pcnf[0] << endl;    else    {        for(int i = 0; i < cnt1 - 1; i++)            cout << 'M' << pcnf[i] << " ∧ ";        cout << 'M' << pcnf[cnt1 - 1] << endl;    }}int main(){    int n, i;    cout << "请输入命题变元的个数 :" << endl;    cin >> n;    cout << "请输入表达式的真值 :" << endl;    for(i = 0; i < (int) pow(2.0, n * 1.0); i++)    {        cin >> true_value[i]; //输入真值        if(true_value[i])            pcnf[cnt1++] = i;        else            pdnf[cnt2++] = i;    }    cout << "真值表为 :" << endl;    for(i = 0; i < n; i++)        cout << "\t" << varible[i];    cout << "\t" << 'A' << endl;    for(i = 0; i < (int) pow(2.0, n * 1.0); i++)     {        int tmp = (int) pow(2.0, n * 1.0) - 1 - i;        for(int j = n - 1; j >= 0; j--)        {            true_table[i][j] = tmp % 2;            tmp /= 2;        }    }    for(i = 0; i < (int) pow(2.0, n * 1.0); i++)        true_table[i][n] = true_value[i];    for(i = 0; i < (int) pow(2.0, n * 1.0); i++)     {        for(int j = 0; j < n + 1; j++)            cout << "\t" << true_table[i][j];        cout << endl;    }    Output_pdnf();    Output_pcnf();    return 0;}


修改版:

#include <iostream>#define MAX 100000using namespace std;short trueValue[MAX]; //真值short trueTable[MAX][10]; //真值表short pdnf[MAX];    //主析取范式short pcnf[MAX];    //主合取范式int cnt1 = 0, cnt2 = 0; //cnt1记录真值为1的数目,cnt2记录真值为0的数目int n, i;void Output_pdnf() {//输出主析取范式    cout << "主析取范式为 : " << endl;    string ans = "";    int tmpcnt = 0;    for (int i = 0; i < (1 << n); i ++) {          if (trueTable[i][n] == 1) {            tmpcnt ++;            ans += '(';            for (int j = 0; j < n; j ++) {                  if (trueTable[i][j] == 0) {                   ans += '!';                }                ans += (j + 'P');                if (j != n - 1) {                    ans += " ∧ ";                }            }            ans += ')';            if (tmpcnt < cnt1) {                ans += " V ";            }        }    }    cout << ans << endl;}void Output_pcnf() {//输出主合取范式    cout << "主合取范式为 : " << endl;    string ans = "";    int tmpcnt = 0;    for (int i = 0; i < (1 << n); i ++) {          if (trueTable[i][n] == 0) {            tmpcnt ++;            ans += '(';            for (int j = 0; j < n; j ++) {                  if (trueTable[i][j] == 1) {                    ans += '!';                }                ans += (j + 'P');                if (j != n - 1) {                    ans += " V ";                }            }            ans += ')';            if (tmpcnt < cnt2) {                ans += " ∧ ";            }        }    }    cout << ans << endl;}int main() {    cout << "请输入命题变元的个数 :" << endl;    cin >> n;    cout << "请输入表达式的真值 :" << endl;    for (i = 0; i < (1 << n); i ++) {        cin >> trueValue[i]; //输入真值        if (trueValue[i]) {            pcnf[cnt1 ++] = i;        }        else {            pdnf[cnt2 ++] = i;        }    }    for (i = 0; i < (1 << n); i ++) {        int tmp = i;        for (int j = n - 1; j >= 0; j--) {            trueTable[i][j] = tmp % 2;            tmp /= 2;        }    }    for (i = 0; i < (1 << n); i++) {        trueTable[i][n] = trueValue[i];    }    Output_pdnf();    Output_pcnf();    return 0;}


0 0
原创粉丝点击