-module(calc). -export([factor/1]). factor(Input) -> {Tail, Number2} = parseNumber(Input, []), {_, Result} = factorExtended(Tail, Number2), Result. factor(Input, Number1) when is_integer(Number1) or is_float(Number1) -> {Tail, Number2} = parseNumber(Input, []), Result = math:pow(Number1, Number2), factorExtended(Tail, Result); factor([], Number) -> {[], Number}. factorExtended([$ |Tail], Result) -> factorExtended(Tail, Result); factorExtended([$^|Tail], Result) -> factor(Tail, Result); factorExtended(Tail, Result) -> {Tail, Result}. parseNumber([$ |Input], []) -> %% skip spaces at start parseNumber(Input, []); parseNumber([$-|Input], []) -> parseNumber(Input, "-"); parseNumber([Current|Input], Number) when ($0 =< Current) and ($9 >= Current) -> parseNumber(Input, Number ++ [Current]); parseNumber(Input, Number) -> {Input, list_to_integer(Number)}. %% eval(Formular) -> %% term(Formular,[]). %% term([" "|Input], Parsed) -> %% term(Input, Parsed); %% term([Current|Formular], Parsed) -> %% {ParsedFirst, Number1} = parseNumber([Current|Formular], []). %% parseOperator([$ |Input]) -> %% parseOperator(Input); %% parseOperator([Operator|Input]) -> %% {ok, list_to_atom(Operator)}; %% parseOperator([]) -> %% {error, unexpected_end_of_input}. %% sum(Number1, Number2, '+') -> %% {ok, Number1 + Number2}; %% sum(Number1, Number2, '-') -> %% {ok, Number1 - Number2}; %% sum(_, _, _) -> %% {error, invalid_operator}.