let root
= document
.getElementById("root");
let table
= document
.createElement('table');
let caption
= table
.createCaption();
caption
.innerHTML
="真值表";
let p
= [true,true,true,true,false,false,false,false];
let q
= [true,true,false,false,true,true,false,false];
let r
= [true,false,true,false,true,false,true,false];
let n_p
= p
.map((item
, index
) => !p
[index
]);
let p_q
= p
.map((item
, index
) => item
&& q
[index
]);
let n_p_r
= n_p
.map((item
, index
) => item
&& r
[index
]);
let result
= p_q
.map((item
, index
) => item
|| n_p_r
[index
]);
let lisan_arr
= [];
let or_stand
= '';
let all_stand
= '';
let arr
=[
["P","Q","R","P且Q","非P且R","ReSult"]
];
for(let i
= 0; i
< 2 ** 3; i
++){
lisan_arr
[i
] = [];
lisan_arr
[i
].push(p
[i
], q
[i
], r
[i
], p_q
[i
], n_p_r
[i
], result
[i
]);
}
all_stand
= (function getAll(){
result
.forEach(function(item
, index
, arr
){
if(! result
[index
]){
all_stand
+= '('
all_stand
+= lisan_arr
[index
][0] === true ? '非p' : 'p';
all_stand
+= 'v'
all_stand
+= lisan_arr
[index
][1] === true ? '非q' : 'q';
all_stand
+= 'v'
all_stand
+= lisan_arr
[index
][2] === true ? '非r' : 'r';
all_stand
+= ')'
all_stand
+= "^";
}
})
return all_stand
;
})()
or_stand
= (function getOr(){
result
.forEach(function(item
, index
, arr
){
if(result
[index
]){
or_stand
+= '('
or_stand
+= lisan_arr
[index
][0] === true ? 'p' : '非p';
or_stand
+= '^'
or_stand
+= lisan_arr
[index
][1] === true ? 'q' : '非q';
or_stand
+= '^'
or_stand
+= lisan_arr
[index
][2] === true ? 'r' : '非r';
or_stand
+= ')'
or_stand
+= "v";
}
})
return or_stand
;
})()
function getLastCharOut(str
){
let str_arr
= [...str
];
str_arr
.splice(str_arr
.length
-1, 1);
return str_arr
.reduce((pre
,cur
) => pre
+ cur
);
}
or_stand
= getLastCharOut(or_stand
);
all_stand
= getLastCharOut(all_stand
);
document
.getElementById('result2').innerHTML
= or_stand
;
document
.getElementById('result1').innerHTML
= all_stand
;
arr
= arr
.concat(lisan_arr
);
addcells(table
);
function addcells(base
){
for(let i
= 0 ,len
= arr
.length
; i
< len
; i
++){
let row
=base
.insertRow();
let jarr
= arr
[i
],jlen
= jarr
.length
,j
= 0;
for(;j
< jlen
; j
++){
let cell
= row
.insertCell();
cell
.innerHTML
= (typeof jarr
[j
] === 'boolean') ? jarr
[j
] === true ? 'T' : 'F' : jarr
[j
] ;
}
}
}
root
.appendChild(table
);
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#root{
margin-left: calc(50% - 200px);
width: 400px;
}
</style>
</head>
<body>
<div id = "root">
<p>式子:
</p>
<div style = "color:red">((P^Q) V (非P^R))
</div>
<p>主合取范式:
</p>
<div id = "result1" style="color:red">
</div>
<p>主析取范式:
</p>
<div id = 'result2' style ="color:red;margin-bottom: 20px">
</div>
</div>
<script type="text/javascript" src ="lisan.js">
</script>
</body>
</html>
JS写法,喜欢自取。。。