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

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

要求:

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

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

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
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<<endl; 
	cout_decomposition_list();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任性不认命~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值