南邮|离散数学实验一(编程实现用真值表法求合式公式的主析取范式以及主合取范式)

目的:编程实现用真值表法求合式公式的主析取范式以及主合取范式。

要求:

        1.根据所给合式公式列出真值表

        2.求出主析取范式以及主合取范式

#include 
#include 
#include 
#include 
using namespace std;

// 宏定义变元的最大个数
#define N 10

//合取公式中的与、或、非、在计算机中定义为 &、|、!

string str; // 定义输入的合式公式
int variable_num; // 定义合式公式中的变元个数
char variable_arr[N]; // 定义合式公式的变元数组

typedef struct right_decomposition {
	string str;
	char ope;
	struct right_decomposition* next;
} right_decomposition;

typedef struct decomposition {
	string str;//字符串
	char ope;//运算符
	struct decomposition *next;
	struct decomposition *last;
	struct right_decomposition* r;
} decomposition;
decomposition *Decomposition = (decomposition *) malloc (sizeof(decomposition));



//  计算合式公式中的变元个数 及变元数组
void calculate_variable_num() {
	int num = str.length();
	for(int i = 0 ; i < num ; i++) {
		if(str[i] >= 'A' && str[i] <= 'Z') {
			int flag = 1;
			for(int j = 0; j < N; j++ ) {
				if(variable_arr[j] == str[i]) {
					flag=0;
					break;
				}
			}
			if(flag) {
				variable_arr[i] = str[i];
				variable_num++;
			}
		}
	}
}

int ci = 0;
void calculate_decomposition(decomposition *dec,string s) {
	ci++;
	int lindex  = str.find("(");
	int rindex = str.find(")");
	if(lindex+rindex>0) {
		int lcount = 0;
		int rcount = 0;
		for(int i = lindex+1; i< rindex ; i++ ) {
			if(s[i] == '(') {
				lcount++;
			}
		}
		for(int i = rindex; i< s.length(); i++) {
			if(s[i] == ')' ) {
				if(rcount == lcount) {
					rindex = i;
					break;
				} else {
					rcount++;
				}
			}
		}
		dec->str = s.substr(lindex+1,rindex-lindex-1);
		if(rindex + 1 < s.length()) {
			dec->ope = s[rindex+1];
			decomposition *next_decomposition = (decomposition *)malloc(sizeof(decomposition));
			next_decomposition->str = s.substr(rindex+2);
			dec->next = next_decomposition;
			calculate_decomposition(next_decomposition,next_decomposition->str);
		}  
	} else {
		dec->str = s;
	}
}

// 输出链表的内容
void cout_decomposition_list() {
	decomposition *head  = Decomposition;
	cout << (head == NULL) << endl;
	while(head->next != NULL ) {
		cout << head->str << "	" << head->ope << endl;
		head = head->next;
	}
}

int main() {
	cin >> str;
	calculate_variable_num();
	calculate_decomposition(Decomposition,str);
	cout << ci<

南邮|离散数学实验一(编程实现用真值表法求合式公式的主析取范式以及主合取范式)_第1张图片

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