HP Prime for All
English
Русский
Name | Boolean calculator |
Description | Program for building thruth tables for boolean expressions |
Author | Vlad A.B. (Vlad_AB) |
Source code formatted by website engine
ICON resource lines were stripped.
BEGIN
LGInStr := SSS;
LGVarLst := {};
LGInLst := {};
LGStkLst := {};
LGOutLst := {};
LGStack := {};
IF LGInit() == 0 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGVars();
IF LGVarNum == 0 OR LGVarNum > 10 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
WHILE LGGetTok DO
IF INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ", LGCurTok) THEN
LGIn2Out();
END;
IF "(" == LGCurTok THEN
LGIn2Stk();
END;
IF ")" == LGCurTok THEN
IF SIZE(LGStkLst) == 0 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGPopStk2Brkt();
END;
IF INSTRING("⌉∧∨→↔∣↓⊕", LGCurTok) THEN
LGOps2Stk();
END;
END;
LGAllStk2Out();
LGCrMat();
LGEval();
RETURN list2mat(CONCAT(CONCAT(LGVarLst, LGInStr), mat2list(LGMat)), LGVarNum+1);
END;
//**********************************************
//ПОДПРОГРАММЫ
LGInit()
BEGIN
LOCAL Tst;
0▶Tst;
FOR I FROM 1 TO DIM(LGInStr) DO
IF INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ⌉∧∨→↔∣↓⊕()", MID(LGInStr, I, 1)) == 0 THEN
1▶Tst;
END;
END;
FOR I FROM 1 TO DIM(LGInStr) - 1 DO
IF INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ", MID(LGInStr, I, 1)) ≠ 0 AND INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ", MID(LGI…
1▶Tst;
END;
IF INSTRING("⌉∧∨→↔∣↓⊕", MID(LGInStr, I, 1)) ≠ 0 AND INSTRING("∧∨→↔∣↓⊕", MID(LGInStr, I+1, 1)) ≠ 0 THEN
1▶Tst;
END;
IF INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ", MID(LGInStr, I, 1)) ≠ 0 AND MID(LGInStr, I+1, 1) == "(" THEN
1▶Tst;
END;
IF INSTRING("∧∨→↔∣↓⊕", MID(LGInStr, I, 1)) ≠ 0 AND MID(LGInStr, I+1, 1) == ")" THEN
1▶Tst;
END;
IF MID(LGInStr, I, 1) == "(" AND MID(LGInStr, I+1, 1) == ")" THEN
1▶Tst;
END;
IF MID(LGInStr, I, 1) == ")" AND MID(LGInStr, I+1, 1) == "(" THEN
1▶Tst;
END;
END;
IF Tst ≠ 0 THEN
RETURN 0;
ELSE
FOR I FROM 1 TO DIM(LGInStr) DO
LGInLst := CONCAT(LGInLst, MID(LGInStr, I, 1));
END;
RETURN 1;
END;
END;
LGVars()
BEGIN
FOR I FROM 1 TO 26 DO
IF INSTRING(LGInStr, MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ", I, 1)) ≠ 0 THEN
LGVarLst := CONCAT(LGVarLst, MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ", I, 1));
END;
LGVarNum := SIZE(LGVarLst);
END;
END;
LGGetTok()
BEGIN
IF SIZE(LGInLst) ≠ 0 THEN
LGCurTok := head(LGInLst);
LGInLst := tail(LGInLst);
RETURN 1;
ELSE
LGCurTok := "";
RETURN 0;
END;
END;
LGIn2Out()
BEGIN
LGOutLst := CONCAT(LGOutLst, LGCurTok);
END;
LGIn2Stk()
BEGIN
LGStkLst := CONCAT(LGCurTok, LGStkLst);
END;
LGStk2Out()
BEGIN
IF SIZE(LGStkLst) ≠ 0 THEN
LGOutLst := CONCAT(LGOutLst, head(LGStkLst));
LGStkLst := tail(LGStkLst);
END;
END;
LGPopStk2Brkt()
BEGIN
WHILE head(LGStkLst) ≠ "(" DO
IF SIZE(LGStkLst) == 0 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGStk2Out();
END;
LGStkLst := tail(LGStkLst);
END;
LGGetPr(TTT) //Приоритеты изменять в этой подпрограмме
BEGIN
CASE
IF TTT == "⌉" THEN RETURN 1; END;
IF TTT == "∧" THEN RETURN 2; END;
IF TTT == "∣" THEN RETURN 2; END;
IF TTT == "↓" THEN RETURN 2; END;
IF TTT == "∨" THEN RETURN 3; END;
IF TTT == "→" THEN RETURN 4; END;
IF TTT == "↔" THEN RETURN 5; END;
IF TTT == "⊕" THEN RETURN 5; END;
DEFAULT RETURN 0;
END;
END;
LGComPriTok()
BEGIN
IF SIZE(LGStkLst) ≠ 0 THEN
IF LGGetPr(LGCurTok) * LGGetPr(head(LGStkLst)) ≠ 0 THEN
IF LGGetPr(LGCurTok) ≥ LGGetPr(head(LGStkLst)) THEN
RETURN 1;
ELSE
RETURN 0;
END;
ELSE
RETURN 0;
END;
ELSE
RETURN 0;
END;
END;
LGOps2Stk()
BEGIN
WHILE LGComPriTok() DO
LGStk2Out();
END;
LGIn2Stk();
END;
LGAllStk2Out()
BEGIN
IF POS(LGStkLst, "(") ≠ 0 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGOutLst := CONCAT(LGOutLst, LGStkLst);
LGStkLst := {};
END;
LGCrMat()
BEGIN
LOCAL LGTmp = 0;
LGMat := MAKEMAT(0, 2^LGVarNum, LGVarNum+1);
FOR I FROM 1 TO 2^LGVarNum DO
LGTmp := I-1;
FOR J FROM LGVarNum DOWNTO 1 DO
LGMat(I, J) := irem(LGTmp, 2);
LGTmp := iquo(LGTmp, 2);
END;
END;
END;
LGPushStk(VVV)
BEGIN
LGStack := CONCAT(VVV, LGStack);
END;
LGPopStk()
BEGIN
LGStack := tail(LGStack);
END;
LGStoVal(SSS, VVV)
BEGIN
CASE
IF SSS == "A" THEN VVV▶A END;
IF SSS == "B" THEN VVV▶B END;
IF SSS == "C" THEN VVV▶C END;
IF SSS == "D" THEN VVV▶D END;
IF SSS == "E" THEN VVV▶E END;
IF SSS == "F" THEN VVV▶F END;
IF SSS == "G" THEN VVV▶G END;
IF SSS == "H" THEN VVV▶H END;
IF SSS == "I" THEN VVV▶I END;
IF SSS == "J" THEN VVV▶J END;
IF SSS == "K" THEN VVV▶K END;
IF SSS == "L" THEN VVV▶L END;
IF SSS == "M" THEN VVV▶M END;
IF SSS == "N" THEN VVV▶N END;
IF SSS == "O" THEN VVV▶O END;
IF SSS == "P" THEN VVV▶P END;
IF SSS == "Q" THEN VVV▶Q END;
IF SSS == "R" THEN VVV▶R END;
IF SSS == "S" THEN VVV▶S END;
IF SSS == "T" THEN VVV▶T END;
IF SSS == "U" THEN VVV▶U END;
IF SSS == "V" THEN VVV▶V END;
IF SSS == "W" THEN VVV▶W END;
IF SSS == "X" THEN VVV▶X END;
IF SSS == "Y" THEN VVV▶Y END;
IF SSS == "Z" THEN VVV▶Z END;
END;
END;
LGSubVals(III)
BEGIN
FOR K FROM 1 TO LGVarNum DO
LGStoVal(LGVarLst(K), LGMat(III, K));
END;
END;
LGNot()
BEGIN
IF SIZE(LGStack) < 1 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGStack(1) := NOT LGStack(1);
END;
LGAnd()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := MIN(LGStack(2), LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LGOr()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := MAX(LGStack(2), LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LGImp()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := MAX(NOT LGStack(2), LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LGEqu()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := (LGStack(2) == LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LGXor()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := NOT (LGStack(2) == LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LG∣Op()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := NOT MIN(LGStack(2), LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LG↓Op()
BEGIN
IF SIZE(LGStack) < 2 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LOCAL CCC;
CCC := NOT MAX(LGStack(2), LGStack(1));
LGPopStk();
LGPopStk();
LGPushStk(CCC);
END;
LGSelOp()
BEGIN
CASE
IF LGCurTok == "⌉" THEN LGNot() END;
IF LGCurTok == "∧" THEN LGAnd() END;
IF LGCurTok == "∨" THEN LGOr() END;
IF LGCurTok == "→" THEN LGImp() END;
IF LGCurTok == "↔" THEN LGEqu() END;
IF LGCurTok == "⊕" THEN LGXor() END;
IF LGCurTok == "∣" THEN LG∣Op() END;
IF LGCurTok == "↓" THEN LG↓Op() END;
END;
END;
LGStkEval()
BEGIN
LGStack := {};
LGOutLst2 := LGOutLst;
WHILE SIZE(LGOutLst2) ≠ 0 DO
LGCurTok := head(LGOutLst2);
LGOutLst2 := tail(LGOutLst2);
CASE
IF INSTRING("01ABCDEFGHIJKLMNOPQRSTUVWXYZ", LGCurTok) ≠ 0 THEN LGPushStk(EXPR(LGCurTok)) END;
IF INSTRING("⌉∧∨→↔∣↓⊕", LGCurTok) ≠ 0 THEN LGSelOp() END;
END;
END;
END;
LGEval()
BEGIN
FOR M FROM 1 TO 2^LGVarNum DO
LGSubVals(M);
LGStkEval();
IF SIZE(LGStack) ≠ 1 THEN
MSGBOX("ERROR:синтаксическая ошибка.");
KILL;
END;
LGMat(M, LGVarNum+1) := LGStack(1);
END;
END;