真值表生成C++

离散数学使用C++实现真值表的生成

说明: !(否定)、 *(合取)、+(析取)、-(条件)、=(双条件)
只识别英文字符

#include 
#include 
#include 

using namespace std;
//获取公式中的变量 共有几个,及都是什么 
int getAlpha(char sentence[], char a[])
{
	int num = strlen(sentence );
	int key = 0;
	for(int i = 0; i < num; i++)
	{
		if(sentence[i] >= 'a' && sentence[i] <= 'z' || sentence[i] >= 'A' && sentence[i] <= 'Z')
		{
			int l = 0;
			for( int k = 0; k < key; k++)
			{
				if(sentence[i] == a[k])
				l++;
			}
			if( l == 0)
			{
				a[key] = sentence[i];
				key++;
			}
		}
	}
	return key; //得到的个数是key个 
}

//将!化简 
void negarecal( char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if( (i+1 < strlen(a)) && (a[i] == '!') && (a[i+1] == '1'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ( (i+1 < strlen(a)) && (a[i] == '!') && (a[i+1] == '0'))
		{
			a[i] = '1'; temp = 1;
		}
		
		if( temp == 1)
		{
			for( j = i+1; j < strlen(a) - 1; j++)
			{
				a[j] = a[j+1];
			}
			a[j] = '\0';
		}
		else i++;
	}
}
//给公式中的变量按次序赋值,有2^key次方种


//去括号
void  removeP(char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if((i + 2< strlen(a)) && (a[i] == '(') && (a[i +1] == '1') && (a[i +2] == ')'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '(') && (a[i +1] == '0') && (a[i +2] == ')'))
		{
			a[i] = '0'; temp = 1;
		}
		
		if(temp == 1)
		{
			for( j = i+1; j < strlen(a) -1; j++)
			{
				a[j] = a[j+2];
			}
			a[j+1] = '\0';
		}
		else i++;
	}
	
}

//合取化简
void  conjunction( char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '*') && (a[i +2] == '0'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '*') && (a[i +2] == '1'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '*') && (a[i +2] == '0'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '*') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		
		if(temp == 1)
		{
			for( j = i+1; j < strlen(a)-1; j++)
			{
				a[j] = a[j+2];
			}
			a[j+1] = '\0';
		}
		else i++;
	}
}

//双条件化简
void  bicon(char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '=') && (a[i +2] == '0'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '=') && (a[i +2] == '1'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '=') && (a[i +2] == '0'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '=') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		
		if(temp == 1)
		{
			for( j = i+1; j < strlen(a)-1; j++)
			{
				a[j] = a[j+2];
			}
			a[j+1] = '\0';
		}
		else i++;
	}
}

//条件式化简
void  con(char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '-') && (a[i +2] == '0'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '-') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '-') && (a[i +2] == '0'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '-') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		
		if(temp == 1)
		{
			for( j = i+1; j < strlen(a)-1; j++)
			{
				a[j] = a[j+2];
			}
			a[j+1] = '\0';
		}
		else i++;
	}
}

//析取化简
void extraction(char a[])
{
	int i = 0, j = 0;
	int temp = 0;
	while( i < strlen(a))
	{
		temp = 0;
		if((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '+') && (a[i +2] == '0'))
		{
			a[i] = '0'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '0') && (a[i +1] == '+') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '+') && (a[i +2] == '0'))
		{
			a[i] = '1'; temp = 1;
		}
		else if ((i + 2< strlen(a)) && (a[i] == '1') && (a[i +1] == '+') && (a[i +2] == '1'))
		{
			a[i] = '1'; temp = 1;
		}
		
		if(temp == 1)
		{
			for( j = i+1; j < strlen(a)-1; j++)
			{
				a[j] = a[j+2];
			}
			a[j+1] = '\0';
		}
		else i++;
	}
} 

//
int main()
{
	char OriFormula[120], Formula[120], CharL[120];
	//输入原公式
	cout << "Please input a Formula:" << endl;
	cin >> OriFormula;
	//复制原公式为后用
	int num = strlen(OriFormula);
	for( int i = 0; i < num; i++)
	{
		Formula[i] = OriFormula[i];
	}
	Formula[num] = '\0';
//	cout << num <
	int variable = 0; //公式中变量的个数
	variable =  getAlpha(OriFormula, CharL);
//	cout << variable << endl << CharL; //输出变量个数和提取出的变量 
	int line = 2;
	for(int i = 1; i < variable; i++)
	{
		line = line * 2;
	}//需要构造多少行 2的variable次方
	
	int a[variable] = {0};
	a[variable - 1] = -1;
	
	//打印真值表抬头
	for (int i = 0; i < variable; i++)
	{
		cout << CharL[i] << " ";
	}
	cout << " " << OriFormula << "  真值" << endl;
	
	//做行的循环 
	for(int i = 0; i < line; i++)
	{
		int d = variable;
		//给每一个变量赋值
		for(int j = d - 1; j >= 0; j--)
		{
			a[j]++;
			if(a[j] <= 1)
			break;
			else
			{
				a[j] = 0;
				d--;
			}
			
		}
		
		//将每个变量的值放入公式中
		
		for(int j = 0; j < variable; j++)
		{
			if(a[j] == 1)
			{
				for(int k = 0; k < num; k++)
				{
					if(Formula[k] == CharL[j])
					Formula[k] = '1';
				}
			}
			else
			{
				for(int k = 0; k < num; k++)
				{
					if(Formula[k] == CharL[j])
					Formula[k] = '0';
				}
			}
		}
		
		//打印变量 
		for(int j = 0; j<variable; j++)
		{
			cout << a[j] << " ";
		}
		cout << " " << Formula << "   "; //打印赋值后的公式 
		
		//运用定律推理
		int cnew = num;
		while(cnew > 1)
		{
			negarecal(Formula); //!
//			cout << Formula <
			removeP(Formula); //括号
//			cout << Formula <
			conjunction(Formula); //合取
//			cout << Formula <
			bicon(Formula); //双条件
//			cout << Formula <
			con(Formula); //条件 
//			cout << Formula <
			extraction(Formula); //析取
//			cout << Formula <
			cnew = strlen(Formula); 
			
		}
		cout << Formula <<endl; //打印真值 
		//在赋值后再恢复公式
		for( int i = 0; i < num; i++)
	{
		Formula[i] = OriFormula[i];
	}
	Formula[num] = '\0'; 
	}
	
	 
}


你可能感兴趣的:(离散数学,c++,c#)