日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄频在线看 | 99热香蕉| 国色天香网站 | 青青久在线 | 亚洲欧美综合网 | 日韩视频在线观看二区 | 丰满圆润老女人hd | 成人免费片 | 天天操狠狠操 | 欧美精品日韩精品 | 精品久久无码视频 | 亚洲精品久久久中文字幕 | 被灌满精子的波多野结衣 | 超碰在线亚洲 | 主播福利在线 | 一级免费在线观看 | 这里只有精品国产 | 日本一二三不卡 | 超碰在| 操丝袜少妇 | 成人国产精品免费 | 亚洲av不卡一区二区 | 欧美激情欧美激情在线五月 | 91福利区 | 精品人伦一区二区三区 | 夜夜爽天天爽 | 亚洲精品免费在线观看视频 | 强行糟蹋人妻hd中文字幕 | 亚洲图片自拍偷拍 | 色网网站 | 免费一区二区视频 | 久久久久久久国产视频 | 91桃色网站 | 久久久久一区 | 一区一区三区产品乱码 | 亚洲精品日日夜夜 | 美女又大又黄 | 兄弟兄弟全集免费观看 | 日本熟妇色xxxxx日本免费看 | 久久久久高清 | 青青操在线观看 | 小仙女av| 少妇被躁爽到高潮 | 国产一级淫片免费 | 99自拍网| 中文字幕第三页 | gai视频在线观看资源 | 国产 欧美 日韩 一区 | 色多多在线视频 | 日韩一级片中文字幕 | 人妻洗澡被强公日日澡电影 | 成人性生交大片免费卡看 | 熟妇大屁股一区二区三区视频 | 亚洲av无码潮喷在线观看 | 少妇av一区二区三区无码 | 激情国产视频 | 韩国三级在线播放 | 影音先锋中文字幕人妻 | 精品国产成人亚洲午夜福利 | 51成人| 色黄网站在线观看 | 男生操女生在线观看 | 国产精品一区av | 日韩视频免费看 | 国产男女猛烈无遮挡免费视频动漫 | 亚洲男人第一av | 麻豆影视av | 三级不卡| 欧美jizz18性欧美 | 久久东京| 国产又粗又长又黄视频 | 一级片免费播放 | 亚洲人吸女人奶水 | 天天干天天舔天天操 | 成人免费不卡视频 | 亚洲精品成人a | 啪啪福利社 | 日韩在线视频观看 | 激情啪啪网 | 大又大粗又爽又黄少妇毛片 | 欧美乱视频 | 又紧又大又爽精品一区二区 | 伊人日韩 | 玉足脚交榨精h文 | 午夜综合网 | 亚洲女同女同女同女同女同69 | 欧美黄色大片视频 | 午夜精品久久久久久久99热浪潮 | 一区视频在线播放 | 男人与雌性宠物交啪啪 | 视色视频在线观看 | 天天看夜夜操 | 亚洲精品在线视频免费观看 | 伊人伊人伊人伊人 | 久久99综合 | 美女的隐私免费看 | 深夜福利免费在线观看 | a√天堂网 | 偷拍亚洲另类 |