blob: 5d809aabf672142aaeea2232570b1ea44ca66a2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import System.Environment
import System.IO
import System.IO.Error
import Control.Exception
import Data.List
import Parser
import Equation
main :: IO ()
main = catchIOError tryMain handler
where handler e
| isUserError e = putStrLn $ trimUserError (show e)
| otherwise = putStrLn "Error"
where trimUserError s = init $ tail $ dropWhile (/='(') s
tryMain :: IO ()
tryMain = do
args <- getArgs
checkArgs args
equ <- checkParsing (head args)
let reduced = reduce equ
l = filterNull $ left reduced
putStrLn $ "Reduced From: " ++ show reduced
putStrLn $ "Polynomial degree: " ++ (show $ degree l)
putSolutions l
checkArgs :: [String] -> IO ()
checkArgs args
| length args == 0 = fail "Usage ./computor equation"
| length args > 1 = fail "Too many arguments"
| otherwise = return ()
checkParsing :: String -> IO Equation
checkParsing input = case parse Parser.equationP input
of Nothing -> fail "Couldnt parse equation"
Just (equ, "") -> return equ
Just (_, s) -> fail "Couldnt parse equation yo"
putSolutions :: Polynomial -> IO ()
putSolutions [] = putStrLn "Infinite solutions" -- 0 = 0
putSolutions [_] = putStrLn "No solution" -- c = 0
putSolutions p = case degree p of
1 -> putStrLn $ "The solution is:\n" ++ show (head solutions)
2 -> do case length solutions of 0 -> putStrLn "Discriminant is strictly negative, there is no solution."
1 -> putStrLn "Discriminant is equal to 0, the solution is:"
2 -> putStrLn "Discriminant is strictly positive, the two solutions are:"
putStr $ intercalate "\n" $ map show solutions
_ -> fail "The polynomial degree is strictly greater then 2, can't solve."
where solutions = sort $ solve p
|