我只是在捕获生成的真值表上的条件的循环上有一个小问题。所以你输入一个逻辑表达式然后它把它变成一个真值表,它还解释它是有效的、无效的还是不一致的。到目前为止,这是解释它的程序的一部分,但它只捕获无效或有效......你能指导我吗?谢谢
*edit// 所以程序是这样运行的:
*******欢迎!********
键入 (LogicStart) 开始或 (exit) 随时退出。
;; 加载文件 MyLogic.lisp
T [2]> (LogicStart) 输入逻辑表达式或公式:“(p^(~p))”
p (~p) (p^(~p))
T 无 无
无 无
公式无效
所以输入只是一个逻辑表达式,那么输出就是那个表达式的真值表......并且也可以解释它,但是我的代码只有两种解释:无效或有效(重言式),因为上面的例子应该是不一致/无法满足(因为对公式/表达式的所有解释都是错误的)
结束编辑
(defun interpret() ; interpret if valid or not or inconsistent
(setq lastcolumn (- (column) 1))
(setq lastcolumnROW 1)
(loop
(unless (aref (aref tbl lastcolumn) lastcolumnROW) (progn (princ "The formula is Invalid")(return)))
(setq lastcolumnROW (+ lastcolumnROW 1))
(when (= lastcolumnROW (+ 1 (row))) (progn (princ "The formula is a Tautology ") (return)))
)
)
编辑两个:///
这是 LogicStart 函数:
(defun LogicStart()
;Function to run program
(princ "Enter Logical Expression or Formula: " )
(setq input (read))
;Get input
(format t "-----------------------------------------------~C" #\linefeed)
;Create two dimension array(table)
(setq tbl (make-array (column)))
(setq index 0)
(loop
(setf (aref tbl index) (make-array (+ (row) 1)))
(setq index (+ 1 index))
(when (= index (column))(return))
)
(setAtoms)
(setFirstValue)
(tblReplaceValue)
(watchTable)
(format t "-----------------------------------------------~C" #\linefeed)
(interpret)
)
setAtoms 功能:
(defun setAtoms()
;Get ALL possible formula
(setq indexOFTBL (make-array (column)))
(setq openP (make-array (- (column) (length Latoms))))
; Get index of open Parenthesis
(setq cOpenP 0)
(setq closeP (make-array (- (column) (length Latoms))))
;Get index of close Parenthesis
(setq cCloseP 0)
(setq index 0)
(loop
(when (char-equal (char input index) #\()
(progn
(setf (aref openP cOpenP) index)
(setq cOpenP (+ 1 cOpenP))
)
)
(when (char-equal (char input index) #\))
(progn
(setf (aref closeP cCloseP) index)
(setq cCloseP (+ 1 cCloseP))
)
)
(setq index (+ 1 index))
(when (= index (length input)) (return))
)
;(print openP)
;(print closeP)
(setq index 0)
(loop
(if (< index (length Latoms))
(progn
(setf (aref (aref tbl index) 0) (char Latoms index))
(setf (aref indexOFTBL index) index)
)
(progn
(setq OpIndex cOpenP)
(loop
(setq OpIndex (- OpIndex 1))
(setq CpIndex 0)
(loop
(if (or (> (aref openP OpIndex) (aref closeP CpIndex)) (= -1 (aref closeP CpIndex)))
(progn
(setq CpIndex (+ CpIndex 1))
)
(progn
(setf (aref (aref tbl index) 0) (subseq input (aref openP OpIndex) (+ 1 (aref closeP CpIndex))))
(setf (aref closeP CpIndex) -1)
(return)
)
)
(when (= CpIndex (length closeP))(return))
)
(setq index (+ index 1))
(when (= OpIndex 0) (return))
)
(return)
)
)
(setq index (+ index 1))
(when (= index (column)) (return))
)
)
watchTable 和列函数
(defun watchTable()
; View table
(setq ro 0)
(loop
(setq co 0)
(loop
(princ(aref (aref tbl co) ro))(format t "~C" #\tab)
(setq co (+ 1 co))
(when (= co (column))(return))
)
(format t "~C" #\linefeed)
(setq ro (+ 1 ro))
(when (= ro (+ (row) 1))(return))
)
)
(defun column()
; Get the number of columns
(+ (atoms) (symbols))
)
//edit 3 所以对于 (OR A (NOT A)),@jkiiski 的代码中的表格缺少“not A”
A | NOT A | (OR A (NOT A))
----+----------+--------
NIL | T | T
T | NIL | T
This expression is a Tautology.
另一个参考示例:虽然 P 隐含 Q,但此代码接受的隐含为:>
; Logical Connectives:
; ~ negation
; - biconditional
; > conditional
; ^ and
; v or
; Example Input:
; "(~((a^b)>c))"
; "(p>q)"
p q p>q
T T T
T NIL NIL
NIL T T
NIL NIL T
Another example:
Enter an expression: "((p>q)^r)"
T <- True
NIL <- False
--------------------------------------------
p q r (p>q) ((p>q)^r)
T T T T T
T T NIL T NIL
T NIL T NIL NIL
T NIL NIL NIL NIL
NIL T T T T
NIL T NIL T NIL
NIL NIL T T T
NIL NIL NIL T NIL
--------------------------------------------
所以它在 (p>q)^r 中显示 p, q, r, (p>q) 最后在真值表上显示 (p>q)^r..
编辑四//
(defun generate-value-combinations (variables)
(let ((combinations (list)))
(labels ((generate (variables &optional (acc (list)))
(if (endp variables)
(push (reverse acc) combinations)
(loop for value in '(t nil)
for var-cell = (cons (car variables) value)
do (generate (cdr variables) (cons var-cell acc))))))
(generate variables)
combinations)))
to this one?
(defun generate-value-combinations (variables)
(let ((combinations (list)))
(labels ((generate (variables &optional (acc (list)))
(if (endp variables)
(push (reverse acc) combinations)
(loop for value in '(t nil)
for var-cell = (cons (car variables) value)
do (generate (cdr variables) (cons var-cell acc))))))
(generate variables) nreverse combinations)))