92 lines
2.9 KiB
Plaintext
92 lines
2.9 KiB
Plaintext
print
|
|
print " TRUTH TABLES"
|
|
print
|
|
print " Input a valid Boolean expression for creating the truth table "
|
|
print " Use lowercase 'and', 'or', 'xor', '(', 'not(' and ')'."
|
|
print
|
|
print " Take special care to precede closing bracket with a space."
|
|
print
|
|
print " You can use any alphanumeric variable names, but no spaces."
|
|
print " You can refer again to a variable used already."
|
|
print " Program assumes <8 variables will be used.."
|
|
print
|
|
print " eg 'A xor B and not( C or A )'"
|
|
print " or 'Too_High xor not( Fuel_Out )'"
|
|
|
|
print
|
|
|
|
[start]
|
|
input " "; expression$
|
|
if expression$ ="" then [start]
|
|
|
|
print
|
|
|
|
'used$ =""
|
|
numVariables =0 ' count of detected variable names
|
|
variableNames$ ="" ' filled with detected variable names
|
|
i =1 ' index to space-delimited word in the expression$
|
|
|
|
[parse]
|
|
m$ =word$( expression$, i, " ")
|
|
if m$ ="" then [analyse]
|
|
' is it a reserved word, or a variable name already met?
|
|
if m$ <>"and" and m$ <>"or" and m$ <>"not(" and m$ <>")" and m$ <>"xor"_
|
|
and not( instr( variableNames$, m$)) then
|
|
variableNames$ =variableNames$ +m$ +" ": numVariables =numVariables +1
|
|
end if
|
|
|
|
i =i +1
|
|
goto [parse]
|
|
|
|
[analyse]
|
|
for i =1 to numVariables
|
|
ex$ =FindReplace$( expression$, word$( variableNames$, i, " "), chr$( 64 +i), 1)
|
|
expression$ =ex$
|
|
next i
|
|
|
|
'print " "; numVariables; " variables, simplifying to "; expression$
|
|
|
|
print ,;
|
|
for j =1 to numVariables
|
|
print word$( variableNames$, j, " "),
|
|
next j
|
|
print "Result"
|
|
print
|
|
|
|
for i =0 to ( 2^numVariables) -1
|
|
print ,;
|
|
A =i mod 2: print A,
|
|
if numVariables >1 then B =int( i /2) mod 2: print B,
|
|
if numVariables >2 then C =int( i /4) mod 2: print C,
|
|
if numVariables >3 then D =int( i /4) mod 2: print D,
|
|
if numVariables >4 then E =int( i /4) mod 2: print E,
|
|
if numVariables >5 then F =int( i /4) mod 2: print F,
|
|
if numVariables >6 then G =int( i /4) mod 2: print G,
|
|
' .......................... etc
|
|
|
|
'e =eval( expression$)
|
|
if eval( expression$) <>0 then e$ ="1" else e$ ="0"
|
|
print "==> "; e$
|
|
next i
|
|
|
|
print
|
|
|
|
goto [start]
|
|
|
|
end
|
|
|
|
function FindReplace$( FindReplace$, find$, replace$, replaceAll)
|
|
if ( ( FindReplace$ <>"") and ( find$ <>"")) then
|
|
fLen = len( find$)
|
|
rLen = len( replace$)
|
|
do
|
|
fPos = instr( FindReplace$, find$, fPos)
|
|
if not( fPos) then exit function
|
|
pre$ = left$( FindReplace$, fPos -1)
|
|
post$ = mid$( FindReplace$, fPos +fLen)
|
|
FindReplace$ = pre$ +replace$ +post$
|
|
fPos = fPos +(rLen -fLen) +1
|
|
loop while ( replaceAll)
|
|
end if
|
|
end function
|