日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用c语言编程求主析取范式,求主析取范式.cpp · wangzhankun/C-Programming-Learn - Gitee.com... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

/*

實(shí)現(xiàn)功能:輸入命題公式的合式公式,求出公式的真值表,并輸出該公式的主合取范式和主析取范式。

輸入:命題公式的合式公式

輸出:公式的主析取范式和主析取范式,輸出形式為:“ mi ∨ mj ; Mi ∧ Mj” ,極小項(xiàng)和 ∨ 符號(hào)之間有一個(gè)空格,極大項(xiàng)和 ∧ 符號(hào)之間有一個(gè)空格;

主析取范式和主合取范式之間用“ ; ”隔開,“ ; ”前后各有一個(gè)空格。 永真式的主合取范式為 1 ,永假式的主析取范式為 0 。

輸入公式的符號(hào)說明:

! 非,相當(dāng)于書面符號(hào)中的 “ ? ”

& 與,相當(dāng)于書面符號(hào)中的 “ ∧ ”

| 或,相當(dāng)于書面符號(hào)中的 “ ∨ ”

- 蘊(yùn)含聯(lián)結(jié)詞,相當(dāng)于書面符號(hào)中的 “ → ”

+ 等價(jià)聯(lián)結(jié)詞,相當(dāng)于書面符號(hào)中的 “ ? ”

( 前括號(hào)

) 后括號(hào)

輸入:

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; //存儲(chǔ)變量以及其所代表的值

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)

{

//計(jì)算操作

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;

}

一鍵復(fù)制

編輯

Web IDE

原始數(shù)據(jù)

按行查看

歷史

總結(jié)

以上是生活随笔為你收集整理的用c语言编程求主析取范式,求主析取范式.cpp · wangzhankun/C-Programming-Learn - Gitee.com...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。