LoginSignup
0
0

More than 1 year has passed since last update.

概要

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

練習問題

elixirで貸借対照表をtable表示せよ。

サンプルコード

defmodule Vs do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
			[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
		end, name: __MODULE__)
	end
	def get(p) do
		Agent.get(__MODULE__, fn v ->
			Enum.at(v, p)
		end)
	end
	def set(p, x) do
		Agent.update(__MODULE__, fn v ->
			List.update_at(v, p, fn _ -> 
	            x
            end)
		end)
	end
end
defmodule Main do
    def print(nil) do
        IO.puts "ok"
    end
    def print(l) do
		#IO.puts l
		l = String.trim l
		l = String.replace(l, "    ", "  ")
		s = String.split(l, "  ")
		#IO.puts Enum.at(s, 0)
        #IO.puts "#{Enum.at(s, 0)} !#{Enum.at(s, 1)} !#{Enum.at(s, 2)}<br>"
        cond do
		Enum.at(s, 1) == "パソコン" ->
			Vs.set(0, Enum.at(s, 0))
		Enum.at(s, 1) == "棚卸資産" ->
			Vs.set(1, Enum.at(s, 0))
		Enum.at(s, 1) == "現金" ->
			Vs.set(2, Enum.at(s, 0))
		Enum.at(s, 1) == "車" ->
			Vs.set(3, Enum.at(s, 0))
		Enum.at(s, 1) == "Liabilities:借入金" ->
			Vs.set(4, Enum.at(s, 0))
		Enum.at(s, 1) == "資本金" ->
			Vs.set(5, Enum.at(s, 0))
		Enum.at(s, 1) == "利益剰余金" ->
			Vs.set(6, Enum.at(s, 0))
		true ->
			Vs.set(9, Enum.at(s, 0))
		end
    end
    def main() do
        csv = """
               -1030  Assets
                -150    パソコン
                -150    棚卸資産
                -330    現金
                -400    車
                 300  Equity
                 100    利益剰余金
                 200    資本金
               -1270  Expenses
               -1000    売上原価
                 -20    支払利息
                -100    減価償却費
                -150    税金(費用)
                1600  Income:売上
                 400  Liabilities:借入金
--------------------
                   0
"""
        Enum.map(String.split(csv, "\n"), fn l ->
			print(l)
		end)

        IO.puts """
<!doctype html>
<html>
<head>
</head>
<body>
<table border="1">
<caption><h2>貸借対照表</h2></caption>
<tbody>
<tr>
    <th scope="col" colspan="2" ><strong>資産の部</strong></th>
    <th scope="col" colspan="2" ><strong>負債の部</strong></th>
</tr>
<tr>
    <th >パソコン</th>
    <td >#{Vs.get(0)}</td>
    <th >借入金</th>
    <td >#{Vs.get(4)}</td>
</tr>
<tr>
    <th >棚卸資産</th>
    <td >#{Vs.get(1)}</td>
    <th ></th>
    <td ></td>
</tr>
<tr>
    <th >現金</th>
    <td >#{Vs.get(2)}</td>
    <th ></th>
    <td ></td>
</tr>
<tr>
    <th >車</th>
    <td >#{Vs.get(3)}</td>
    <th ></th>
    <td ></td>
</tr>
<tr>
    <th ></th>
    <td ></td>
    <th scope="col" colspan="2"><strong>資本の部</strong></th>
</tr>
<tr>
    <th ></th>
    <td ></td>
    <th >資本金</th>
    <td >#{Vs.get(5)}</td>
</tr>
<tr>
    <th ></th>
    <td ></td>
    <th >利益剰余金</th>
    <td >#{Vs.get(6)}</td>
</tr>
</tbody>
</table>
</body>
</html>
"""
	end
end
Vs.start_link
Main.main




実行結果

image.png

成果物

以上。

0
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
0
0