用c语言编程求主析取范式,求主析取范式.cpp · wangzhankun/C-Programming-Learn - Gitee.com...

/*

实现功能:输入命题公式的合式公式,求出公式的真值表,并输出该公式的主合取范式和主析取范式。

输入:命题公式的合式公式

输出:公式的主析取范式和主析取范式,输出形式为:“ mi ∨ mj ; Mi ∧ Mj” ,极小项和 ∨ 符号之间有一个空格,极大项和 ∧ 符号之间有一个空格;

主析取范式和主合取范式之间用“ ; ”隔开,“ ; ”前后各有一个空格。 永真式的主合取范式为 1 ,永假式的主析取范式为 0 。

输入公式的符号说明:

! 非,相当于书面符号中的 “ ¬ ”

& 与,相当于书面符号中的 “ ∧ ”

| 或,相当于书面符号中的 “ ∨ ”

- 蕴含联结词,相当于书面符号中的 “ → ”

+ 等价联结词,相当于书面符号中的 “ ↔ ”

( 前括号

) 后括号

输入:

a&b

输出:

m3 ; M0 ∧ M1 ∧ M2

//*/

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

class Calculate

{

map optPriority;

string expression;

string expression_tmp;

int num_of_variables;

vector variables;

map dict; //存储变量以及其所代表的值

int error; //error == 1, divided by 0; error == 2, expression is wrong

stack numStack;

stack optStack;

int sTOi(int firstindex, int lastindex); //transport expression to number

string iTOs(int num); //translate number to string

void changeMinus(); //find '-' and judge if it means minus, if so, change it into '#'

void pushINTOstack();

void pushOPT(int index); //push the operation at index into optStack and calculate value if necessary

void computeAB(char opt); //comput 'a opt b'

void Error(); //do when error occurs

int cal();

void countVariable();

void assigement(string input);

void dfs(int level);

void kernel(string inpug);

public:

vector output;

Calculate();

int start(string expression);

};

Calculate::Calculate()

{

//this->expression = expression;

//value = 0;

error = 0;

optPriority['('] = 0;

optPriority['&'] = 1;

optPriority['|'] = 2;

optPriority['-'] = 3;

optPriority['+'] = 4;

optPriority['!'] = 5;

}

int Calculate::sTOi(int firstindex, int lastindex)

{

string tmp = expression.substr(firstindex, lastindex - firstindex);

int num_tmp = 0;

for (int i = 0; i < (int)tmp.size(); i++)

{

num_tmp *= 10;

num_tmp += tmp[i] - '0';

}

return num_tmp;

}

string Calculate::iTOs(int num)

{

ostringstream s;

s << num;

string tmp = s.str();

return tmp;

}

void Calculate::changeMinus()

{

if (expression[0] == '-')

expression[0] = '#';

for (int i = 1; i < (int)expression.size(); ++i)

{

if (expression[i] == '-')

{

if (expression[i - 1] == ')')

continue;

if (expression[i - 1] >= '0' && expression[i - 1] <= '9')

continue;

expression[i] = '#';

}

}

return;

}

void Calculate::computeAB(char opt)

{

if (numStack.empty()) //not enough number

{

if (!error)

error = 2;

return;

}

if (opt == '!')

{

int b = numStack.top();

numStack.pop();

if (b == 0)

b = 1;

else

b = 0;

numStack.push(b);

return;

}

int b = numStack.top();

numStack.pop();

if (numStack.empty()) //not enough number

{

if (!error)

error = 2;

return;

}

int a = numStack.top();

numStack.pop();

int c = 0;

switch (opt)

{

case '&':

c = a * b;

break;

case '|':

c = a + b;

if (c > 1)

c = 1;

break;

case '-':

if (a == 1 && b == 0)

c = 0;

else

c = 1;

break;

case '+':

if (a == b)

c = 1;

else

{

c = 0;

}

break;

}

numStack.push(c);

return;

}

void Calculate::Error()

{

if (error == 1)

{

cout << "Divide 0.\n";

}

else

{

cout << "error.\n";

}

return;

}

void Calculate::pushOPT(int index)

{

char opt = expression[index];

if (opt == '(' || opt == '!')

{

optStack.push(opt);

return;

}

else

{

if (opt == ')')

{

int flag = 1; //mark expression is wrong for there is no '('

while (!optStack.empty())

{

char opt_tmp = optStack.top();

optStack.pop();

if (opt_tmp == '(')

{

flag = 0;

break;

}

computeAB(opt_tmp);

if (error)

return;

}

if (flag) //there is no '('

{

if (!error)

error = 2;

return;

}

}

else

{

if (index)

{

if (expression[index - 1] == '(')

{

error = 2;

return;

}

}

while (!optStack.empty())

{

char opt_tmp = optStack.top();

optStack.pop();

if (optPriority[opt] >= optPriority[opt_tmp])

{

if (opt == '^')

{

optStack.push(opt_tmp);

break;

}

else

{

if (optPriority[opt] > optPriority[opt_tmp])

{

optStack.push(opt_tmp);

break;

}

}

}

computeAB(opt_tmp);

if (error)

{

return;

}

}

optStack.push(opt);

}

}

return;

}

void Calculate::pushINTOstack()

{

for (int i = 0; i < (int)expression.size(); ++i)

{

if (expression[i] >= '0' && expression[i] <= '9')

{

int j = i;

for (; expression[j] >= '0' && expression[j] <= '9'; j++)

;

numStack.push(sTOi(i, j));

i = j - 1;

}

else

{

pushOPT(i);

}

if (error)

return;

}

return;

}

int Calculate::cal()

{

// changeMinus();

pushINTOstack();

while (!optStack.empty())

{

if (error)

break;

char opt_tmp = optStack.top();

optStack.pop();

if (opt_tmp == '(')

{

if (!error)

error = 2;

}

computeAB(opt_tmp);

}

if ((int)numStack.size() != 1)

{

if (!error)

error = 2;

}

if (error)

{

Error();

return 0;

}

int value = numStack.top();

while (!numStack.empty())

{

numStack.pop();

}

while (!optStack.empty())

{

optStack.pop();

}

return value;

}

void Calculate::assigement(string input)

{

int index = input.find('=');

string tmp = input.substr(0, index);

if (*(input.end() - 1) == '0')

dict[tmp] = 0;

else

dict[tmp] = 1;

}

void Calculate::dfs(int level)

{

if (level == this->num_of_variables)

{

//计算操作

kernel(this->expression_tmp);

return;

}

//赋值

assigement(variables[level] + "=0");

dfs(level + 1);

assigement(variables[level] + "=1");

dfs(level + 1);

return;

}

void Calculate::kernel(string s)

{

this->expression = "";

for (int i = 0; i < (int)s.size(); ++i)

{

int j = i;

while (s[j] >= 'a' && s[j] <= 'z')

{

j++;

}

string tmp = s.substr(i, j - i);

if (tmp[0] >= 'a' && tmp[0] <= 'z')

this->expression += iTOs(dict[tmp]);

else

this->expression += tmp;

if (j < (int)s.size())

this->expression += s[j];

i = j;

}

output.push_back(cal());

// cout << *output.end() << endl;

}

int Calculate::start(string s)

{

this->expression_tmp = s;

countVariable();

dfs(0);

return 0;

}

void Calculate::countVariable()

{

// cout << this->expression_tmp << endl;

set variables;

for (int i = 0, j = 0; i < (int)this->expression_tmp.size(); i = j)

{

while (i < this->expression_tmp.size())

{

if ((int)this->expression_tmp[i] >= 'a' && this->expression_tmp[i] <= 'z')

break;

i++;

}

j = i;

if (i >= this->expression_tmp.size())

break;

while (j < (int)this->expression_tmp.size() && (int)this->expression_tmp[j] >= 'a' && this->expression_tmp[j] <= 'z')

{

j++;

}

string tmp = this->expression_tmp.substr(i, j - i);

// cout << "i=" << i << ";j=" << j << endl;

// cout << tmp << endl;

variables.insert(tmp);

}

for (auto it = variables.begin(); it != variables.end(); it++)

{

this->variables.push_back(*it);

// cout << *it << endl;

}

this->num_of_variables = variables.size();

// cout << num_of_variables << endl;

}

int main()

{

Calculate calculator;

string input;

cin >> input;

if(input.find('1') != input.npos)

{

cout << " ; 1" << endl;

return 0;

}

if(input.find('0') != input.npos)

{

cout << "0 ; " << endl;

return 0;

}

calculator.start(input);

string xiqu, hequ;

for (int i = 0; i < (int)calculator.output.size(); i++)

{

// cout << calculator.output[i] << endl;

if (calculator.output[i] == 0)

{

hequ += "M" + to_string(i) + " ∧ ";

}

else

{

xiqu += "m" + to_string(i) + " ∨ ";

}

}

if (hequ.size() > 0)

for (int i = 0; i < (int)xiqu.size() - 5; i++)

{

cout << xiqu[i];

}

else

cout << " ; 1";

if ((int)hequ.size() > 0)

{

if (xiqu.size() > 0)

{

cout << " ; ";

for (int i = 0; i < (int)hequ.size() - 5; i++)

{

cout << hequ[i];

}

}

else

cout << "0 ; ";

}

cout << endl;

return 0;

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

主析取范式(minterm)是一个逻辑函数的一种表达形式,它是由若干个变量的逻辑值组成的一组项,每个项都是这些变量中的每一个都取值为1或者0,且每个变量只出现一次,称为主项。 二进制主析取范式的算法如下: 1. 根据逻辑函数的真值表,将函数的取值为1的变量组合成minterm。 2. 对于每个minterm,将其转换为二进制,并在每个二进制数的位置上加上对应的变量名。 3. 将所有的主项用OR(或)连接起来,即可得到主析取范式。 下面是用C语言实现二进制主析取范式的代码: ```c #include <stdio.h> #include <string.h> void binary(int num, char *str) // 将整数转换为二进制字符串 { int i = 0; while (num > 0) { str[i++] = num % 2 + '0'; num /= 2; } str[i] = '\0'; strrev(str); // 翻转字符串 } void minterm(int *table, int rows, int cols) // minterm { int i, j; char var[cols]; // 存放变量名 char str[cols + 1]; // 存放二进制数 printf("主析取范式: "); for (i = 0; i < rows; i++) { if (table[i][cols - 1] == 1) { // 找到取值为1的行 for (j = 0; j < cols - 1; j++) { if (table[i][j] == 1) { var[j] = 'A' + j; // 将变量名转换为字符 } else { var[j] = 'A' + j; var[j] += 32; // 将变量名转换为小写字符 } } var[cols - 1] = '\0'; binary(i, str); // 将行号转换为二进制字符串 printf("%s%s", str, var); // 输出minterm if (i != rows - 1) { printf(" OR "); } } } } int main() { int table[][4] = {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}; int rows = sizeof(table) / sizeof(table[0]); int cols = sizeof(table[0]) / sizeof(table[0][0]); minterm(table, rows, cols); // 主析取范式 return 0; } ``` 输出结果为:0A'B'C'D + 0A'B'CD + 0AB'C'D + 0AB'CD + A'B'C'D + A'B'CD + AB'C'D + AB'CD 其中,A、B、C、D为变量名,A'、B'、C'、D'为对应的取反变量名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值