概要
paiza.ioでelixirやってみた。
練習問題やってみた。
練習問題
xorの真理値表を描け。
サンプルコード
IO.puts """
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
}
td,th {
width: 30px;
border: 1px solid #000;
}
td {
text-align: center;
}
th {
background-color: #ccc;
}
</style>
</head>
<body>
<script>
class C {
constructor(n) {
this.n = n;
this.s = [];
var t = new Array(n);
for (var i = 0; i < n; i++)
{
t[i] = 0;
}
this.s.push(t);
return this;
}
init(a, b) {
b = b + 1;
for (var i = a; i < b; i++)
{
var t = [];
for (var j = 0; j < this.s.length; j++)
{
var u = this.s[j];
u[i] = 0;
t.push(u.slice());
u[i] = 1;
t.push(u);
}
this.s = t;
}
return this;
}
xor(a, b, c) {
for (var j = 0; j < this.s.length; j++)
{
this.s[j][c] = this.s[j][a] ^ this.s[j][b];
}
return this;
}
and(a, b, c) {
for (var j = 0; j < this.s.length; j++)
{
this.s[j][c] = this.s[j][a] & this.s[j][b];
}
return this;
}
or(a, b, c) {
for (var j = 0; j < this.s.length; j++)
{
this.s[j][c] = this.s[j][a] | this.s[j][b];
}
return this;
}
get() {
return this.s;
}
table() {
document.write('<table>');
for (var row = 0; row < this.s.length + 1; row++)
{
document.write('<tr>');
for (var col = 0; col < this.n + 1; col++)
{
if (col === 0 && row === 0)
{
document.write('<th> <\/th>');
}
else if (col === 0 && row !== 0)
{
document.write('<th>' + row + '<\/th>');
}
else if (row === 0)
{
document.write('<th>' + (col - 1) + '<\/th>');
}
else
{
var v = this.s[row - 1][col- 1];
document.write('<td>' + v + '<\/td>');
}
}
document.write('<\/tr>');
}
document.write('<\/table>');
}
}
"""
IO.puts """
var c0 = new C(3);
c0.init(0, 1);
c0.xor(0, 1, 2);
c0.table();
"""
IO.puts """
</script>
</body>
</html>
"""
成果物
以上。