LoginSignup
1
0

wslでelixir その117

Last updated at Posted at 2023-12-12

概要

wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。

練習問題

Livebookで論理回路の真理値表を作成せよ。

方針

  • 組み合わせ回路の真理値表
  • 論理式を使わない。
  • インタープリター方式
  • xorの場合
    lut 3
    lin 2
    lout 1
    code 0 0
    code 1 1
    code 2 1
    code 3 0
    出力
    2 1 1
    0 0 0
    0 1 1
    1 0 1
    1 1 0
  • kino.js使う。

写真

image.png

サンプルコード

defmodule KinoOhi.Lut do
  use Kino.JS

  def new(html) do
    Kino.JS.new(__MODULE__, html)
  end

  asset "main.js" do
    """
    var c0;

    class LUT {
    constructor(n) {
    this.h = "";
    this.n = n;
    this.s = [];
    this.a = 0;
    this.b = 0;
    this.bin = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
    return this;
    }
    in(a) {
    this.a = a;
    return this;
    }
    out(a) {
    this.b = a;
    return this;
    }
    code(a, b) {
    var t = new Array(this.n);
    var i,
      j,
      k;
    for (i = 0; i < this.a; i++)
    {
      j = this.a - i - 1;
      k = a >> j;
      k = k & 1;
      if (k == 1) t[i] = 1;
      else t[i] = 0;
    }
    for (i = 0; i < this.b; i++)
    {
      j = this.b - i - 1;
      k = b >> j;
      k = k & 1;
      if (k == 1) t[this.a + i] = 1;
      else t[this.a + i] = 0;
    }
    this.s.push(t);
    return this;
    }
    table() {
    this.h += '<table>';
    this.h += '<tr>';
    for (var a = 0; a < this.a; a++)
    {
      this.h += '<th>' + this.bin[this.a - a - 1] + '</th>';
    }
    for (var b = 0; b < this.b; b++)
    {
      this.h += '<th>' + this.bin[this.b - b - 1] + '</th>';
    }
    this.h += '</tr>';
    for (var row = 0; row < this.s.length; row++)
    {
      this.h += '<tr>';
      for (var col = 0; col < this.n; col++)
      {
        var v = this.s[row][col];
        this.h += '<td>' + v + '</td>';
      }
      this.h += '</tr>';
    }
    this.h += '</table>';
    var out = document.getElementById("out");
    out.innerHTML = this.h;
    }
    }

    function run(str) {
    var codes = str.split("\\n");
    var len = codes.length;
    var i;
    for (i = 0; i < len; i++)
    {
    var code = codes[i].split(" ");
    switch (code[0])
    {
    case "":
    break;
    case "lut":
      var a = parseInt(code[1]);
      c0 = new LUT(a);
    break;
    case "lin":
      var a = parseInt(code[1]);
      c0.in(a);
    break;
    case "lout":
      var a = parseInt(code[1]);
      c0.out(a);
    break;
    case "code":
      var a = parseInt("0x" + code[1]);
      var b = parseInt("0x" + code[2]);
      c0.code(a, b);
    break;
    default:
      alert(code[0]);
    break;
    }
    }
    c0.table();
    }

    export function init(ctx, html) {
      ctx.importCSS("main.css");
      const out0 = document.createElement("div");
    out0.id = "out";
    out0.style.width = "100%";
    out0.style.height = "300px";
    ctx.root.appendChild(out0);
      run(html);
    }
    """
  end

  asset "main.css" do
    """
    table {
      border-collapse: collapse;
    }
    td,th {
      width: 30px;
      border: 1px solid #000;
    }
    td {
      text-align: center;
    }
    th {
      background-color: #ccc;
    }
    """
  end
end

KinoOhi.Lut.new("""
lut 4
lin 3
lout 1
code 0 0
code 1 0
code 2 0
code 3 1
code 4 0
code 5 0
code 6 1
code 7 0
""")

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0