南京邮电大学离散数学求主析取范式和主合取范式实验。

南京邮电大学离散数学求主析取范式和主合取范式实验代码。
因为CSDN上没有好的代码,于是博主自己写了一篇。
```cpp
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <map>
const int LEN = 140 + 10; 
int beg = 80;             
using namespace std;
int main()
{
        int num = 0;
        map<char, int> alpha;
        map<char, bool> map;
       
    //得到参数个数
        cout << "欢迎来到求主析取范式和主合取范式系统"<<endl;
        string Biao;
        cout << "请按照ASCll码P,Q,R,S,T,U,V,W,X,Y,Z(大写字母)顺序输入命题" << "(PS:!表示否, v(小写字母V)表示析取, ^ 表示合取,->表示单条件,<->表示双条件)" << endl;
        cout << "输入命题表达式(类似!Pv(Q^R)->S):";
        cin >> Biao;
        for (auto x : Biao)
        {
            
            if (isalpha(x) == 1)
            {
                if (alpha[x] == 0)
                {
                    num++;
                    alpha[x]++;
                }
            }
        }
       
        int sum = pow(2, double(num));
        vector<bool> b(sum);
        //得到命题表达式!PvR
        

       //得到对应!PvR的真假值P!Rv
        stack<char> Fuhao;
        vector<char> a;
        for (auto x : Biao)
        {
            if (isalpha(x) == 1)
            {
                a.push_back(x);
                if (!Fuhao.empty() && Fuhao.top() == '!')
                {
                    a.push_back(Fuhao.top());
                    Fuhao.pop();
                }
            }
            if (x == '!')
            {
                Fuhao.push(x);
            }
            if (x == '^')
            {
                if (!Fuhao.empty() && Fuhao.top() == '^')
                {
                    a.push_back('^');
                }
                else
                {
                    Fuhao.push(x);
                }
            }
            if (x == 'v')
            {
                while (!Fuhao.empty() && (Fuhao.top() == '^' || Fuhao.top() == 'v'))
                {
                    a.push_back(Fuhao.top());
                    Fuhao.pop();
                }
                Fuhao.push('v');
            }
            if (x == '>')
            {
                if (!Fuhao.empty()&&Fuhao.top() != '<')
                {
                    while (!Fuhao.empty() && (Fuhao.top() == '^' || Fuhao.top() == 'v' || Fuhao.top() == '>'))
                    {
                        a.push_back(Fuhao.top());
                        Fuhao.pop();
                    }
                    Fuhao.push('>');
                }
                if (Fuhao.empty())
                {
                    Fuhao.push('>');
                }
            }
            if (x == '<')
            {
                while (!Fuhao.empty() && (Fuhao.top() == '^' || Fuhao.top() == 'v' || Fuhao.top() == '>'|| Fuhao.top() == '<'))
                {
                    a.push_back(Fuhao.top());
                    Fuhao.pop();
                }
                Fuhao.push('<');
            }
            if (x == '(')
            {
                Fuhao.push(x);
            }
            if (x == ')')
            {
                while (!Fuhao.empty() && Fuhao.top() != '(')
                {
                    a.push_back(Fuhao.top());
                    Fuhao.pop();
                }
                Fuhao.pop();
            }
        }
        while (!Fuhao.empty())
        {
            a.push_back(Fuhao.top());
            Fuhao.pop();
        }

        //转成后缀表达式
        int brr[LEN][4 + 10];
        int cnt1 = 0, cnt2 = 0;
        for (int i = sum - 1; i >=0 ; i--)
        {
            cnt1 = 0;
            int val = i;
            while (cnt1 < num)
            {
                brr[cnt2][cnt1] = val % 2;
                cnt1++;
                val = val / 2;
            }
            cnt2++;
        }
        cout << "输出公式对应的真值表 : " << endl;
        for (int i = 1; i <= num; i++)
        {
                cout << char(beg++) << "      ";
        }
        cout << 'A';
        cout << endl;
        for(int i =0;i<num*4;i++)
        {
            cout << "—";
        }
        cout << endl;
        for (int i = 0; i < sum; i++)
        {
            beg = 80;
            for (int j = 0; j < num; j++)
            {
                if (brr[i][j] == 1)
                {
                    cout << 'T' << "      ";
                    map[char(beg++)] = true;
                }
                else
                {
                    cout << 'F' << "      ";
                    map[char(beg++)] = false;
                }
            } 
            stack<bool> Consequence;
            bool result= true;
            bool lhs;
            bool rhs;
            int n = 0;
            for (auto x : a)
            {
                if (isalpha(x) == 1)
                {
                    Consequence.push(map[x]);
                }
                if (x == '!')
                {
                    if (!Consequence.empty())
                    {
                        result = Consequence.top();
                        Consequence.pop();
                        if(result)
                            Consequence.push(false);
                        else
                            Consequence.push(true);

                    }
                }                
                if (x == 'v')
                {
                    rhs = Consequence.top();
                    Consequence.pop();
                    lhs = Consequence.top();
                    Consequence.pop();
                    Consequence.push(rhs || lhs);
                }
                if (x == '^')
                {
                    rhs = Consequence.top();
                    Consequence.pop();
                    lhs = Consequence.top();
                    Consequence.pop();
                    Consequence.push(rhs && lhs);
                }
                if (x == '>')
                {
                    rhs = Consequence.top();
                    Consequence.pop();
                    lhs = Consequence.top();
                    Consequence.pop();
                    Consequence.push(!lhs || rhs);
                }
                if (x == '<')
                {
                    rhs = Consequence.top();
                    Consequence.pop();
                    lhs = Consequence.top();
                    Consequence.pop();
                    Consequence.push(rhs == lhs);
                }
            }
            result = Consequence.top();
            if (result)
                cout << "T";
            else
                cout << "F";
            b[i] = result;
            cout << endl;
        }

        int togetherT = 0;
        int togetherF = 0;
        for (int i = 0; i < sum; i++)
        {
            if (b[i])
                togetherT++;
            else
                togetherF++;
        }
    
    //主析取取真
        beg = 80;
        cout << "输出主析取范式:" << endl;
        for (int i = 0; i < sum; i++)
        {
            if (b[i])
            {
                togetherT--;
                cout << '(';
                for (int j = 0; j <num; j++)
                {
                    if (brr[i][j] == 1)
                    {
                        cout << (char)(beg++);
                    }
                    else
                    {
                        cout << "┓" << (char)(beg++);
                    }
                    if (j < num - 1)
                        cout << "∧";
                }
                cout << ')';
                if(togetherT>0)
                cout << "∨";
            }
            beg = 80;
        }
        cout << endl << endl;
    //主合取取假
        beg = 80;
        cout << "输出主合取范式:" << endl;
        for (int i = 0; i < sum; i++)
        {
            if (!b[i])
            {
                togetherF--;
                cout << '(';
                for (int j = 0; j < num; j++)
                {
                    if (brr[i][j] == 1)
                    {
                        cout << (char)(beg++);
                    }
                    else
                    {
                        cout << "┓" << (char)(beg++);
                    }
                    if (j <num-1)
                        cout << "∨";
                }
                cout << ')';
                if (togetherF>0)
                    cout << "∧";
            }
            beg = 80;
        }
        cout << endl << endl;
    return 0;
}

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值