]]> ]]>

Квадратное уравнение в Erlang

Пример для версий erl 5.7.3
-module(prog).
 
-export([main/0]).
 
solve(A, B, C) -> 
    D = B*B - 4*A*C,
    if (D == 0) -> io:format("x = ~f~n", [-B*0.5/A]);
    true -> 
        if (D > 0) -> 
                SQ = math:sqrt(D),
                io:format("x1 = ~f~nx2 = ~f", [(-B+SQ)/2/A, (-B-SQ)/2/A]);
        true -> SQ = math:sqrt(-D),
                io:format("x1 = (~f,~f)~nx2 = (~f,~f)", [-0.5*B/A, 0.5*SQ/A, -0.5*B/A, -0.5*SQ/A])
        end
    end
.
    
main() -> 
    case io:fread("A = ", "~d") of
    eof -> true;
    {ok, X} ->
        [A] = X,
        if (A == 0) -> io:format("Not a quadratic equation.");
        true -> 
        case io: fread("B = ", "~d") of
            eof -> true;
            {ok, Y} ->
                [B] = Y,
                case io: fread("C = ", "~d") of
                eof -> true;
                {ok, Z} -> 
                     [C] = Z,
                     solve(A, B, C)
                end
            end
        end
    end.

Комментарии

]]>

blog comments powered by Disqus

]]>

Работа программистам