把前提变成代码? [英] Turning A premise into Code?

查看:76
本文介绍了把前提变成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这项作业,需要将前提条件语句转换为代码.我的想法是,我需要打印出真值表以作前提声明. 这是前提: ((((P v Q)^(Q-> R))XOR(P v R))<->(R ^ Q)

我可以创建一个手动的真相表上述前提的真相表

我只是不明白如何将其转换为代码?我将如何使用典型的基本库(例如iostream,字符串,数学等)来解决这个问题.我不能利用向量,集合等其他任何东西.

您不必编写文字代码,但是也许某些伪代码可能会有所帮助,甚至只是提示.

我知道"^"是&&,"v"是||,->"是"if-else",但是我不确定<->"和"XOR"或只是如何将其放入代码中.

谢谢.

更新: 在我的stackoverflow同辈的帮助下,我们设法将每个逻辑运算符语句的字面意思都转换为c ++方法.

(P v Q)   = P OR Q           = (P || Q)
(P ^ Q)   = P AND Q          = (P && Q)
(P XOR Q) = P ^ Q            = (P ^ Q)
(P -> Q)  = if P then Q      = (!P || Q)
(p <-> Q) = Only If P then Q = !(P ^ Q)

解决方案

您可以(并且应该)使用布尔变量:

// declare variables and initialize.
bool p = true;
bool q = true;
bool r = true;
// Input values (from user or file)
//...

// Output some columns:
cout << p << " | " << q << " | " << r << " | ";
cout << (p || q) << " | ";
//...
cout << endl;

I have this assignment that requires me to turn a premise statement into code. The idea is that i need to print out the truth table for a premise statement. This is the premise: (((P v Q) ^ (Q -> R)) XOR (P v R)) <-> (R ^ Q)

I can create a manual Truth table Truth Table for the above premise

I just dont understand how to transform that into code? How would i approach that using the typical basic libraries such as iostream, string, math, etc. I can't utilize anything else like vectors, sets, etc.

You don't have to write literal code but perhaps some pseudocode might help or even justs tips.

I understand that "^" is &&, "v" is ||, "->" is "if-else" but i'm not sure about "<->" and "XOR" or simply how to put that in code.

Thanks in advance.

UPDATE: As per the assistance of my fellow stackoverflow peers, we've managed to obtain the literal meaning of each logical operator statement into a c++ approach.

(P v Q)   = P OR Q           = (P || Q)
(P ^ Q)   = P AND Q          = (P && Q)
(P XOR Q) = P ^ Q            = (P ^ Q)
(P -> Q)  = if P then Q      = (!P || Q)
(p <-> Q) = Only If P then Q = !(P ^ Q)

解决方案

You can (and should) use Boolean variables:

// declare variables and initialize.
bool p = true;
bool q = true;
bool r = true;
// Input values (from user or file)
//...

// Output some columns:
cout << p << " | " << q << " | " << r << " | ";
cout << (p || q) << " | ";
//...
cout << endl;

这篇关于把前提变成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆