Created
June 23, 2015 18:39
-
-
Save ryo0223/3ad2e7d677d25998cf21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @author ryo-py | |
%%% @copyright (C) 2015, <COMPANY> | |
%%% @doc | |
%%% | |
%%% @end | |
%%% Created : 20. 6 2015 12:35 | |
%%%------------------------------------------------------------------- | |
-module(calc). | |
-author("ryo-py"). | |
%% API | |
-export([rpn/1]). | |
rpn(L) when is_list(L) -> | |
[Res] = lists:foldl(fun rpn/2, [] , string:tokens(L," ")), | |
Res. | |
read(N) -> | |
case string:to_float(N) of | |
{error,no_float} -> list_to_integer(N); | |
{F,_} -> F | |
end. | |
rpn("+", [N1,N2|S]) -> [N2+N1|S]; | |
rpn("-", [N1,N2|S]) -> [N2-N1|S]; | |
rpn("=", [N1,N2|S]) -> [N2*N1|S]; | |
rpn("/", [N1,N2|S]) -> [N2/N1|S]; | |
rpn("*", [N1,N2|S]) -> [N2*N1|S]; | |
rpn("ln", [N|S]) -> [math:log(N)|S]; | |
rpn("log10", [N|S]) -> [math:log10(N)|S]; | |
rpn("^", [N1,N2|S]) -> [math:pow(N2,N1)|S]; | |
rpn("sum", [N1,N2|[]]) -> | |
[N1+N2]; | |
rpn("sum", [N1,N2|S]) -> | |
rpn("sum",[N1+N2|S]); | |
rpn("prog", [N1,N2|[]]) -> | |
[N1*N2]; | |
rpn("prog", [N1,N2|S]) -> | |
rpn("prog",[N1*N2|S]); | |
rpn(X,Stack) -> [read(X)|Stack]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment