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

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

#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

猜你喜欢

转载自blog.csdn.net/qq_41090676/article/details/83719103