离散数学,命题公式(真值表+范式(析&&主))

//此代码只支持三个变相的命题公式
#include
#include
#include
#include
#include
using namespace std;
int Com(char a)
{
	switch (a)
	{
	case '(':return 5; break;
	case '!':return 4; break;
	case '&':return 3; break;
	case '|':return 2; break;
	case '-':return 1; break;
	case '+':return 0; break;

	case ')':return -1; break;
	}
}
int main()
{
	string s;//用来读入数据的
	stack Stack;//字符型栈
	char save[10001];//用来存储后缀表达式的
	int follow = 0;//后缀的下标
	cout << "请输入命题公式F:\n";
	cin >> s;
	for (int i = 0; i < s.length(); i++)//中---》后
	{
		if (isalpha(s[i]))
		{//如果是字母就保存在save数组中
			save[follow++] = s[i];
		}
		if (s[i] == '!' || s[i] == '&' || s[i] == '|' || s[i] == '-' || s[i] == '+' || s[i] == '(' || s[i] == ')')
		{//如果为运算符
			if (Stack.empty())//判断栈是否 为空
			{//如果栈是空的那么说明里面没有放运算符,那么就压入
				Stack.push(s[i]);
			}
			else {//否则   ,比较栈顶运算符和s[i]的优先级,如果s[i] num;
				for (int e = 0; e < follow; e++)
				{
					if (isalpha(save[e]))
					{
						switch (save[e])
						{
						case 'p':num.push(p); break;
						case 'q':num.push(q); break;
						case 'r':num.push(r); break;
						}
					}
					else if (save[e] == '!')
					{
						int v = !num.top(); num.pop();
						num.push(v);
					}
					else if (save[e] == '&')
					{
						int k1 = num.top(); num.pop();
						int k2 = num.top(); num.pop();
						int k3 = k1 && k2;
						num.push(k3);
					}
					else if (save[e] == '|')
					{
						int k1 = num.top(); num.pop();
						int k2
							= num.top(); num.pop();
						int k3 = k1 || k2;
						num.push(k3);
					}
					else if (save[e] == '-')
					{
						int k1 = num.top(); num.pop();
						int k2 = num.top(); num.pop();
						int k3;
						if (k2 == 1 && k1 == 0)
						{
							k3 = 0;
						}
						else
						{
							k3 = 1;
						}
						num.push(k3);
					}
					else if (save[e] == '+') {
						int k1 = num.top(); num.pop();
						int k2 = num.top(); num.pop();
						int k3;
						if (k1 == k2) {
							k3 = 1;
						}
						else {
							k3 = 0;
						}
						num.push(k3);
					}
				}
				cout << num.top() << '\n';
				if(num.top()==1){
					Array[grounp][0] = p;
				Array[grounp][1] = q;
				Array[grounp++][2] = r;
				}
				else{
					Array2[grounp2][0] = p;
				Array2[grounp2][1] = q;
				Array2[grounp2++][2] = r;
				}
				num.pop();
			
			}
		}
	}
	//输出主析取式
	cout << "主析取式为:\n";
	for (int y = 0; y < grounp; y++)
	{
		cout << '(';
		if (Array[y][0] == 0) {
			cout << "(!p)";
		}
		else {
			cout << 'p';
		}
		if (Array[y][1] == 0) {
			cout << "&(!q)";
		}
		else {
			cout << "&q";
		}
		if (Array[y][2] == 0)
		{
			cout << "&(!r)";
		}
		else {
			cout << "&r";
		}
		if (y == grounp - 1) {
			cout << ")";
		}
		else {
			cout << ")|";
		}
	}
	cout<<"\n\n求主合取范式:\n";
	for (int k = 0; k < grounp2; k++)
	{
		cout << '(';
		if (Array2[k][0] == 0) {
			cout << "p";
		}
		else {
			cout << "(!p)";
		}
		if (Array2[k][1] == 0) {
			cout << "|q";
		}
		else {
			cout << "|(!q)";
		}
		if (Array2[k][2] == 0)
		{
			cout << "|r";
		}
		else {
			cout << "|(!r)";
		}
		if (k == grounp2 - 1) {
			cout << ")";
		}
		else {
			cout << ")&";
		}
	}
	cout<<'\n';
	return 0;
}

你可能感兴趣的:(c++)