\startMPdefinitions
numeric u,gap;
numeric uk, wd, ht;
% numeric tVal[][],a,b,c;
string val[],codeOne[],codeTwo[];
uk := LineHeight;
wd := 1.5uk;
ht := uk+5pt;
def KmapFrameNew(expr first,second) =
codeOne[0] := "0";
codeOne[1] := "1";
codeTwo[0] := "00";
codeTwo[1] := "01";
codeTwo[2] := "11";
codeTwo[3] := "10";
% label rows
label.top(textext("$" & first & "$"), (-.7wd,0));
label.top(textext("$" & second & "$"), (-.3wd,.5ht));
% drawing lines
draw (-1.2wd,0) -- origin -- ((2*(length second)+1)*wd,0);
draw (0, 1.1ht) -- origin -- (0,-(2*(length first)+.2)*ht);
draw (-wd,ht) --origin;
% label the rows
if (length first) = 1:
for i = 0,1:
label.top(textext(codeOne[i]), ((-.7wd, -(i+1)*ht)));
endfor;
else:
for i = 0 upto 3:
label.top(textext(codeTwo[i]), ((-.7wd, -(i+1)*ht)));
endfor;
fi;
%label the columns
if (length second) = 1:
for i = 0,1:
label.top(textext(codeOne[i]), ((i+1)*wd,.3ht));
endfor;
else:
for i = 0 upto 3:
label.top(textext(codeTwo[i]), ((i+1)*wd,.3ht));
endfor;
fi;
setbounds currentpicture to boundingbox currentpicture enlarged 2mm;
draw boundingbox currentpicture;
enddef;
\stopMPdefinitions
\startluacode
function document.MakeHead(p,a)
if not a then
local t = p
p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|"
a = t
end
context.starttabulate { p }
context.FL()
for i=1,#a do
context.NC() context(a[i])
end
context.NC() context.NR()
context.LL()
end
function document.MakeFooter()
context.HL()
context.stoptabulate()
end
local tf = {false, true }
local tfList = {}
local val = 0
function document.truthTable(a,func)
document.MakeHead(a)
for i,s in ipairs(tf) do
for j,t in ipairs(tf) do
for k,u in ipairs(tf) do
context.NC()
context(s and "1" or "0")
context.NC()
context(t and "1" or "0")
context.NC()
if #a == 3 then
break
end
context(u and "1" or "0")
context.NC()
val = func(s,t,u) and "1" or "0"
context(val)
context.NC()
context.AR()
table.insert(tfList, val)
-- context(#tfList)
end
if #a == 3 then
val = func(s,t) and "1" or "0"
context(val)
context.NC()
context.AR()
table.insert(tfList, val)
end
end
end
document.MakeFooter()
table.save("tempList.lua", tfList)
end
\stopluacode
\startbuffer[tbl:p2q]
\startluacode
function document.logicF(p,q)
return ((not p) or q)
end
document.truthTable({ "x", "y", "x \\to y" },document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_p2qR]
numeric n,u,k ;
lua("MP = { } MP.data = table.load('tempList.lua')") ;
lua("mp.print('n := ',\#MP.data)") ;
KmapFrameNew("p","q")
for i = 0,1:
for j = 1,2:
label.top(textext(lua("MP.data[" & decimal (2*i+j) & "]")),(j*wd,-(i+1)*ht));
endfor;
endfor;
draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ;
draw unitsquare xyscaled (1.8wd,ht) shifted (.6wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\startbuffer[tbl:ABC]
\startluacode
function document.logicF(p,q,r)
return (not p) and (not(p and r) or q)
end
document.truthTable({ "A", "B", "C","(\\overbar{AC}+B)\\overbar{A}"},document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_ABCR]
lua("MP = { } MP.data = table.load('tempList.lua')") ;
numeric n,k ;
%lua("mp.print('n := ',\#MP.data)") ;
KmapFrameNew("A","BC")
for i = 0,1:
k := 1;
for j = 1,2,4,3:
label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht));
k := k + 1;
endfor;
endfor;
draw unitsquare xyscaled (3.6wd,ht) shifted (.7wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\startbuffer[tbl:xyz]
\startluacode
function document.logicF(p,q,r)
return (not q) and (p or r)
end
document.truthTable({ "x", "y", "z","f(x,y,x)"},document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_xyzR]
lua("MP = { } MP.data = table.load('tempList.lua')") ;
numeric n,k ;
KmapFrameNew("x","yz")
for i = 0,1:
k := 1;
for j = 1,2,4,3:
label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht));
k := k + 1;
endfor;
endfor;
draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ;
draw unitsquare xyscaled (1.8wd,1ht) shifted (.6wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\starttext
\startplacetable[]
{\startcombination[distance=1.5cm,2*1]
{\getbuffer[tbl:p2q]}{진리표}
{\processMPbuffer[Kmap_p2qR]}{K-map}
\stopcombination}
\stopplacetable
\startplacetable[]
{\startcombination[distance=1.5cm,2*1]
{\getbuffer[tbl:ABC]}{진리표}
{\processMPbuffer[Kmap_ABCR]}{K-map}
\stopcombination}
\stopplacetable
\startplacetable[location=here,reference=tbl:Fxyz]
{\startcombination[distance=1.5cm,1*2]
{\getbuffer[tbl:xyz]}{}
{\processMPbuffer[Kmap_xyzR]}{}
\stopcombination}
\stopplacetable
\stoptext