pennineacute Posted November 11, 2015 Share Posted November 11, 2015 I am after some help for a project I am doing pls.I am writing a program to display the truth table of a boolean expression and so far all is good.I decided to convert the input into reverse polish notation, so I could get the expression into priority order. This works.The program displays the truth table inputs, with no problems, and I am now working on the Boolean part.If you entered for eg, a+b.c, I have stored the reverse polish notation in a array. In this would be stored a b c . + in elements 0 to 4As each row input is calculated ega b c output 0 0 0 I then want to display the output.My first idea was to read through the array until I hit a boolean operator and then perform this with the top two variables in my stack, in this case a.bbut when trying values, I can get a boolean output for and, or and xor, but not for NOT, as can be see from the attachmentMy program is live at http://andylittlewood.co.ukThe coding I was using to test the boolean output is echo "1 and 1 = "; echo 1 && 1; echo "<br>1 or 1 = "; echo 1 || 1; echo "<br> 1 xor 1 = "; echo true ^ true; echo "<br>Not 1 = ";echo !(true); I cannot put my full coding on here, as it is a project and do not want to plagiarise. Quote Link to comment https://forums.phpfreaks.com/topic/299444-boolean/ Share on other sites More sharing options...
Jacques1 Posted November 11, 2015 Share Posted November 11, 2015 We cannot help you without seeing the actual code. Don't worry: Nobody will plagiarize a 10-line RPN parser. It's not like this was a novel idea. Generally speaking: You need to know the arity of each operator, because some of them are binary, others are unary (like the negation). Quote Link to comment https://forums.phpfreaks.com/topic/299444-boolean/#findComment-1526226 Share on other sites More sharing options...
requinix Posted November 11, 2015 Share Posted November 11, 2015 echo true ^ true; echo !(true); You're suffering from PHP's loose typing. ^ is not a logical operator but a bitwise operator. It acts on numbers. true^true is interpreted as 1^1. The answer is, of course, 0.However ! is a logical operator. !true is false, but PHP decided that the string representation (ie, what you get when you try to echo it) is empty. Try echo "1 and 1 = "; echo (1 && 1 ? "1" : "0"); echo "<br>1 or 1 = "; echo (1 || 1 ? "1" : "0"); echo "<br> 1 xor 1 = "; echo (1 ^ 1 ? "1" : "0"); echo "<br>Not 1 = "; echo (!1 ? "1" : "0");or function showvalue($expr) { echo $expr ? "1" : "0"; } echo "1 and 1 = "; showvalue(1 && 1); echo "<br>1 or 1 = "; showvalue(1 || 1); echo "<br> 1 xor 1 = "; showvalue(1 ^ 1); echo "<br>Not 1 = "; showvalue(!1);You should also (re)acquaint yourself with PHP's various operators. I suggest you stick with the bitwise operators only, meaning use & and | instead of && and ||. Quote Link to comment https://forums.phpfreaks.com/topic/299444-boolean/#findComment-1526231 Share on other sites More sharing options...
pennineacute Posted November 11, 2015 Author Share Posted November 11, 2015 Thanks for that, very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/299444-boolean/#findComment-1526245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.