formatters #14
@ -402,11 +402,8 @@ one_gaju() -> 1_000_000_000_000_000_000.
|
|||||||
|
|
||||||
-spec read(Format) -> Result
|
-spec read(Format) -> Result
|
||||||
when Format :: string(),
|
when Format :: string(),
|
||||||
Result :: {ok, Pucks} | {error, Reason},
|
Result :: {ok, Pucks} | error,
|
||||||
Pucks :: integer(),
|
Pucks :: integer().
|
||||||
Reason :: {badarg, Partial :: string(), Rest :: term()}
|
|
||||||
| {incomplete, Partial :: string(), Rest :: binary()}
|
|
||||||
| format.
|
|
||||||
%% @doc
|
%% @doc
|
||||||
%% Convery any valid string formatted representation and output a value in pucks.
|
%% Convery any valid string formatted representation and output a value in pucks.
|
||||||
%% This routine can fail in the special case of `ch' style formatting with a single
|
%% This routine can fail in the special case of `ch' style formatting with a single
|
||||||
@ -433,13 +430,12 @@ read([C | Rest])
|
|||||||
C =:= $\n ->
|
C =:= $\n ->
|
||||||
read(Rest);
|
read(Rest);
|
||||||
read([C | Rest]) when $0 =< C andalso C =< $9 ->
|
read([C | Rest]) when $0 =< C andalso C =< $9 ->
|
||||||
read(Rest, [C], []);
|
read(Rest, [C], {[], []});
|
||||||
read([C | Rest]) when $0 =< C andalso C =< $9 ->
|
read([C | Rest]) when $0 =< C andalso C =< $9 ->
|
||||||
NumC = C - $0 + $0,
|
NumC = C - $0 + $0,
|
||||||
read(Rest, [NumC], []);
|
read(Rest, [NumC], {[], []});
|
||||||
read(_) ->
|
read(_) ->
|
||||||
io:format("Barfing~n"),
|
error.
|
||||||
{error, format}.
|
|
||||||
|
|
||||||
read_w_gajus([C | Rest], A) when $0 =< C andalso C =< $9 ->
|
read_w_gajus([C | Rest], A) when $0 =< C andalso C =< $9 ->
|
||||||
read_w_gajus(Rest, [C | A]);
|
read_w_gajus(Rest, [C | A]);
|
||||||
@ -462,9 +458,9 @@ read_w_gajus([], A) ->
|
|||||||
G = list_to_integer(lists:reverse(A)) * one_gaju(),
|
G = list_to_integer(lists:reverse(A)) * one_gaju(),
|
||||||
{ok, G};
|
{ok, G};
|
||||||
read_w_gajus([C, 32 | Rest], A) ->
|
read_w_gajus([C, 32 | Rest], A) ->
|
||||||
read(Rest, [], [{C, A}]);
|
read(Rest, [], {[{C, A}], []});
|
||||||
read_w_gajus([32, $G | Rest], A) ->
|
read_w_gajus([32, $G | Rest], A) ->
|
||||||
case read(Rest, [], [{$G, A}]) of
|
case read(Rest, [], {[{$G, A}], []}) of
|
||||||
{ok, P} ->
|
{ok, P} ->
|
||||||
G = list_to_integer(lists:reverse(A)) * one_gaju(),
|
G = list_to_integer(lists:reverse(A)) * one_gaju(),
|
||||||
{ok, G + P};
|
{ok, G + P};
|
||||||
@ -472,8 +468,7 @@ read_w_gajus([32, $G | Rest], A) ->
|
|||||||
Error
|
Error
|
||||||
end;
|
end;
|
||||||
read_w_gajus(_, _) ->
|
read_w_gajus(_, _) ->
|
||||||
io:format("Derping~n"),
|
error.
|
||||||
{error, format}.
|
|
||||||
|
|
||||||
read_w_pucks([C | Rest], A) when $0 =< C andalso C =< $9 ->
|
read_w_pucks([C | Rest], A) when $0 =< C andalso C =< $9 ->
|
||||||
read_w_pucks(Rest, [C | A]);
|
read_w_pucks(Rest, [C | A]);
|
||||||
@ -494,27 +489,44 @@ read([C | Rest], A, S) when $0 =< C andalso C =< $9 ->
|
|||||||
read([C | Rest], A, S) when $0 =< C andalso C =< $9 ->
|
read([C | Rest], A, S) when $0 =< C andalso C =< $9 ->
|
||||||
NumC = C - $0 + $0,
|
NumC = C - $0 + $0,
|
||||||
read(Rest, [NumC | A], S);
|
read(Rest, [NumC | A], S);
|
||||||
read([$木], A, S) ->
|
read([$木], A, {G, P}) ->
|
||||||
calc([{$木, A} | S]);
|
calc({[{$木, A} | G], P});
|
||||||
read([$木, 32 | Rest], A, S) ->
|
read([$木, 32 | Rest], A, {G, P}) ->
|
||||||
read(Rest, [], [{$木, A} | S]);
|
read(Rest, [], {[{$木, A} | G], P});
|
||||||
read([$本], A, S) ->
|
read([$本], A, {G, []}) ->
|
||||||
calc([{$本, A} | S]);
|
calc({[], [{$本, A} | G]});
|
||||||
read([32, $G], A, S) ->
|
read([$本], A, {G, P}) ->
|
||||||
calc([{$G, A} | S]);
|
calc({G, [{$本, A} | P]});
|
||||||
read([32, $G, 32 | Rest], A, S) ->
|
read([32, $G], A, {G, P}) ->
|
||||||
read(Rest, [], [{$G, A} | S]);
|
calc({[{$G, A} | G], P});
|
||||||
read([32, $P], A, S) ->
|
read([32, $G, 32 | Rest], A, {G, P}) ->
|
||||||
calc([{$P, A} | S]);
|
read(Rest, [], {[{$G, A} | G], P});
|
||||||
read([C, 32 | Rest], A, S) ->
|
read([32, $P], A, {G, []}) ->
|
||||||
read(Rest, [], [{C, A} | S]);
|
calc({[], [{$P, A} | G]});
|
||||||
read([C | Rest], A, S) ->
|
read([32, $P], A, {G, P}) ->
|
||||||
read(Rest, [], [{C, A} | S]);
|
calc({G, [{$P, A} | P]});
|
||||||
|
read([C, 32 | Rest], A, {G, []}) ->
|
||||||
|
read(Rest, [], {[{C, A} | G], []});
|
||||||
|
read([C, 32 | Rest], A, {G, P}) ->
|
||||||
|
read(Rest, [], {G, [{C, A} | P]});
|
||||||
|
read([C | Rest], A, {G, []}) ->
|
||||||
|
read(Rest, [], {[{C, A} | G], []});
|
||||||
|
read([C | Rest], A, {G, P}) ->
|
||||||
|
read(Rest, [], {G, [{C, A} | P]});
|
||||||
read(_, _, _) ->
|
read(_, _, _) ->
|
||||||
{error, format}.
|
error.
|
||||||
|
|
||||||
calc(S) ->
|
calc({G, P}) ->
|
||||||
calc(S, 0).
|
io:format("G: ~tp, P: ~tp~n", [G, P]),
|
||||||
|
case calc(G, 0) of
|
||||||
|
{ok, Gajus} ->
|
||||||
|
case calc(P, 0) of
|
||||||
|
{ok, Pucks} -> Gajus + Pucks;
|
||||||
|
error -> error
|
||||||
|
end;
|
||||||
|
error ->
|
||||||
|
error
|
||||||
|
end.
|
||||||
|
|
||||||
calc([{_, []} | S], A) ->
|
calc([{_, []} | S], A) ->
|
||||||
calc(S, A);
|
calc(S, A);
|
||||||
@ -529,37 +541,23 @@ calc([{M, Cs} | S], A) ->
|
|||||||
calc([], A) ->
|
calc([], A) ->
|
||||||
{ok, A}.
|
{ok, A}.
|
||||||
|
|
||||||
%magnitude_j("万") -> 1_0000;
|
|
||||||
%magnitude_j("億") -> 1_0000_0000;
|
|
||||||
%magnitude_j("兆") -> 1_0000_0000_0000;
|
|
||||||
%magnitude_j("京") -> 1_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("垓") -> 1_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("秭") -> 1_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("穣") -> 1_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("溝") -> 1_0000_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("澗") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("正") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("載") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("極") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
|
|
||||||
%magnitude_j("木") -> one_gaju();
|
|
||||||
%magnitude_j("本") -> 1.
|
|
||||||
|
|
||||||
|
magnitude($木) ->
|
||||||
magnitude("木") ->
|
|
||||||
{ok, one_gaju()};
|
{ok, one_gaju()};
|
||||||
magnitude("本") ->
|
magnitude($本) ->
|
||||||
{ok, 1};
|
{ok, 1};
|
||||||
magnitude("G") ->
|
magnitude($G) ->
|
||||||
{ok, one_gaju()};
|
{ok, one_gaju()};
|
||||||
magnitude("P") ->
|
magnitude($P) ->
|
||||||
{ok, 1};
|
{ok, 1};
|
||||||
magnitude(Mark) ->
|
magnitude(Mark) ->
|
||||||
|
io:format("Mark: ~ts~n", [[Mark]]),
|
||||||
case rank(Mark, ranks(jp), 1_0000, 1) of
|
case rank(Mark, ranks(jp), 1_0000, 1) of
|
||||||
{ok, J} ->
|
{ok, J} ->
|
||||||
{ok, J};
|
{ok, J};
|
||||||
error ->
|
error ->
|
||||||
case rank(Mark, ranks(metric), 1_000, 1) of
|
case rank(Mark, ranks(metric), 1_000, 1) of
|
||||||
{ok, N} -> {ok, N};
|
{ok, J} -> {ok, J};
|
||||||
error -> rank(Mark, ranks(heresy), 1_000, 1)
|
error -> rank(Mark, ranks(heresy), 1_000, 1)
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
@ -570,6 +568,7 @@ rank(Mark, [Mark | _], Magnitude, Sum) ->
|
|||||||
rank(Mark, [_ | Rest], Magnitude, Sum) ->
|
rank(Mark, [_ | Rest], Magnitude, Sum) ->
|
||||||
rank(Mark, Rest, Magnitude, Sum * Magnitude);
|
rank(Mark, Rest, Magnitude, Sum * Magnitude);
|
||||||
rank(_, [], _, _) ->
|
rank(_, [], _, _) ->
|
||||||
|
io:format("failure to rank!~n"),
|
||||||
error.
|
error.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user