/*

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

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

输出:公式的主析取范式和主析取范式,输出形式为:“ 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

原始数据

按行查看

历史

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

  1. c语言坐标海伦公式,C语言:用海伦公式求三角形面积 , C语言编程问题,利用海伦公式求三角形面积...

    导航:网站首页 > C语言:用海伦公式求三角形面积 , C语言编程问题,利用海伦公式求三角形面积 C语言:用海伦公式求三角形面积 , C语言编程问题,利用海伦公式求三角形面积 匿名网友: 程序已 ...

  2. c语言1到1000的3的倍数之和,C语言编程:用for语句求1~100中是3的倍数的所有整数之和...

    #includeint main(){int i,s=0;for(i=1;i;i+)s+i;printf("%d\\n",s);return 0;}输出5050www.mh456. ...

  3. c语言编程用进退法求搜索区间代码,用c对函数进行优化的问题

    //多维无约束优化软件设计#include #include #include double det=1e-5; //计算精度double det1=1e-3; //梯度判断精度double ak=3 ...

  4. C语言编程用递归法求

    7,用递归法求: (x2!)+(xxx3!)+(5个x相乘5!)+-+((2n-2)个x相乘(2n-2)!)当N为某值是上式为几?(到第n项,n和x的值有键盘输入.) #include<stdi ...

  5. c语言编程欧拉方法求近似值,欧拉法求解已知初值微分方程解

    1.原理 数值积分算法是求解知初值的微分方程的重要方法. 如已知微分方程 d(y)/d(t) = f(y, t) y(t0) = y0 方程两边对t积分就会有 此式表示原函数t1时刻的解y(t1)为原 ...

  6. 单片机c语言编程30倒计时,急求51单片机倒计时三十秒程序

    急求51单片机倒计时三十秒程序 关注:117  答案:2  手机版 解决时间 2021-01-31 06:56 提问者青春统帅 2021-01-30 16:36 第二位同志请补充一下注释 谢谢 最佳答 ...

  7. c语言编程雅可比解方程,求雅可比迭代法解方程组的C\C++程序,急需一个运用雅可比迭代法求线性方程组的C/C++程序!...

    问题标题 求雅可比迭代法解方程组的C\C++程序,急需一个运用雅可比迭代法求线性方程组的C/C++程序! 2019-7-8来自ip:14.137.150.56的网友咨询 浏览量:218 手机版 问题补 ...

  8. C语言编程用递归法求5!

    问题描述:请利用递归方法求5!. 问题分析:递归公式:km=km_1*4! 程序源码: #include<stdio.h> int main() { int i; int fact(); ...

  9. c语言编程使蜂鸣器音乐,求: 用51单片机c语言操作使蜂鸣器奏出“祝你生日快乐”音乐的全部程序!...

    满意答案 yxy777li 推荐于 2017.10.05 采纳率:59%    等级:12 已帮助:8867人 #include sbit speaker=P1^2; unsigned char ti ...

  10. c语言编程cosx近似值,编程利用公式求 cosx 的近似值(精度为10-6):

    输入精度e 和实数x,用下列公式求cos x 的近似值,精确到最后一项的绝对值小于e.c语言编程, #include#include//这个函数需要返回double类型,不然结果很快就溢出了doubl ...

最新文章

  1. MySQL如何查看连接数和状态
  2. 基于 HTML5 WebGL 的 3D 棉花加工监控系统
  3. 数据结构之迷宫问题求解(一)利用栈与递归求解出口
  4. 最清晰的讲解各种梯度下降法原理与Dropout
  5. PHP获取当前页面的网址
  6. 带你认识SAP反记账
  7. Python中的itertools.product
  8. 详解基于 Cortex-M3 的任务调度(上)
  9. 面试:一文搞懂重载和重写的区别
  10. Oracle BBED 工具介绍
  11. Delphi--过程和函数
  12. mysql数据库查询的传统句子
  13. Hibernate的查询 HQL查询 查询某几列
  14. Android Content Provider基础
  15. 访问ntfs文件系统获取目标文件簇流
  16. 命名空间:不只是代码封装
  17. axure后台示例_【Axure电商案例】如何设计和真的后台一样给客户看
  18. 【状压DP】状态压缩动态规划入门超详解
  19. gVim配色和字体选择
  20. 2021抖音上热门技巧有哪些?

热门文章

  1. python怎么画多重饼状图_Python通过matplotlib画双层饼图及环形图简单示例
  2. pandas DataFrame isin
  3. 89. Leetcode 96. 不同的二叉搜索树 (动态规划-基础题)
  4. 未排序数组中累加和为给定值的最长子数组系列问题
  5. Git 笔记:基本操作工作流程
  6. MATLAB应用实战系列(五十二)-Excel数据的读取
  7. 产品经理必备知识之网页设计系列(三)-移动端适配无障碍设计及测试
  8. 日志和告警数据挖掘经验谈
  9. 深入推荐引擎相关算法 - 聚类
  10. KMeans和KMedoid 的Matlab实现