• 命题逻辑--主析取主合取范式


    主要是对位运算的操作,,每次加一。

    #include <iostream>
    #include <math.h>
    using namespace std;
    int main()
    {
    	int n, num, a[1024], b[1024][10], cnt, cmp,cnt1 = 0, cnt2 = 0, k = 0;
    	char name[10]={'P','Q','R','S','T','U','V','W','X','Y',};
    	cout<<"Please enter the number of propositions:"<<endl;
    	cin>>n;
    	num=pow(2,n);
    	cout<<"Please enter the "<<num<<" num.The 1 represent the true,the 0 is flase."<<endl;
    	for (int i=0;i<num;i++) {
    		cin>>a[i];
    	}
    	cout<<"Truth table:"<<endl;
    	for (int i=0;i<n;i++) {
    		cout<<name[i]<<" ";
    	} 
    	cout<<"value"<<endl;
    	cnt=num-1;//初始化所有位为一
    	cmp=pow(2,n-1);//初始化最高位为一
    	for (int i=0;i<num;i++) {
    		for (int j=0;j<n;j++) {
    			if (cnt&cmp) {
    				cout<<1<<" ";
    				b[i][j]=1;
    			}
    			else {
    				cout << 0 << " ";
    				b[i][j] = 0;
    			}
    			cmp>>=1;
    		}
    		cnt--;
    		cmp=pow(2,n-1);
    		cout<<a[i]<<endl; 
    	}//位运算判断并打印一和零
    	for (int i = 1; i < num;i++) {
    		if (a[i])
    			cnt1++;
    		else
    			cnt2++;
    	}
    	cnt1 -= 1;//避免出现多余的行尾符号
    	cnt2 -= 1;
    	cout << "Main disjunction paradigm:" << endl;
    	for (int i=0;i<num;i++) {
    		if (a[i]) {
    			cout<<"("; 
    			for (int j=0;j<n;j++) {
    				if (b[i][j]) {
    					cout<<" "<<name[j]<<" ";
    					if (j!=n-1)
    				    	cout<<"^";
    				}
    				else {
                        cout << " !" << name[j]<<" ";
                        if (j!=n-1)
                            cout << "^";
                    }
    			}
    			cout<<")";
    			if (k<cnt1) {
    				k++;
    				cout << "V" ;
    			}
    		}
    	}//按格式输出
    	k = 0;
    	cout << endl;
    	cout << "Master conjunction paradigm:" << endl;
        for (int i=0;i<num;i++) {
    		if (!a[i]) {
    			cout<<"("; 
    			for (int j=0;j<n;j++) {
    				if (!b[i][j]) {
    					cout<<" "<<name[j]<<" ";
    					if (j!=n-1)
    				    	cout<<"V";
    				}
    				else {
                        cout << " !" << name[j]<<" ";
                        if (j!=n-1)
                            cout << "V";
                    }
    			}
    			cout<<")";
    			if (k<cnt2) {
    				cout << "^";
    				k++;
    			}
    				
    		}
    	}
        getchar();
        getchar();
        return 0;
     } 
    //3
    //1 0 0 1 0 0 0 1
  • 相关阅读:
    设置密码等级判断
    密码验证包含数字字母字符的两个或两个以上的组合
    解决ps不能直接把文件拖进去的问题
    图片上传js
    关于手机ios和安卓和pc的点击事件的兼容
    css设置两行多余文字用..显示
    对于奇数和偶数的轮播
    手机端开发的问题(摘要)
    懒加载
    Django admin 后台 数据展示
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/10211376.html
Copyright © 2020-2023  润新知