Turning A premise into Code? [closed]

眉间皱痕 提交于 2019-12-27 06:15:51

问题


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)

回答1:


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;


来源:https://stackoverflow.com/questions/46720065/turning-a-premise-into-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!