aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-10 14:57:47 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-10 14:57:47 +0100
commitdf5d3f05ead68a6cdecfeccecadce45fd4b574fd (patch)
treed9e6a9733bf7fd086044fbe29ff5fb660ade2311 /src
parente5ba91f99722d047f90f94a710d77344db36e110 (diff)
downloadcomputorv1-df5d3f05ead68a6cdecfeccecadce45fd4b574fd.tar.gz
computorv1-df5d3f05ead68a6cdecfeccecadce45fd4b574fd.tar.bz2
computorv1-df5d3f05ead68a6cdecfeccecadce45fd4b574fd.zip
Solution commentary, parsing free from entrie (i.e X^1, 2*X) bonus
Diffstat (limited to 'src')
-rw-r--r--src/main.hs17
-rw-r--r--src/parser.hs29
2 files changed, 32 insertions, 14 deletions
diff --git a/src/main.hs b/src/main.hs
index a11f7fc..f0a83e5 100644
--- a/src/main.hs
+++ b/src/main.hs
@@ -23,9 +23,7 @@ tryMain = do
l = filterNull $ left reduced
putStrLn $ "Reduced From: " ++ show reduced
putStrLn $ "Polynomial degree: " ++ (show $ degree l)
- case l of [] -> putStrLn "Infinite solutions"
- [_] -> putStrLn "No solution"
- _ -> putSolutions l
+ putSolutions l
checkArgs :: [String] -> IO ()
checkArgs args
@@ -40,6 +38,13 @@ checkParsing input = case parse Parser.equationP input
Just (_, s) -> fail "Couldnt parse equation yo"
putSolutions :: Polynomial -> IO ()
-putSolutions p
- | degree p > 2 = fail "The polynomial degree is strictly greater then 2, can't solve."
- | otherwise = putStr $ intercalate "\n" (map show (solve p))
+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
diff --git a/src/parser.hs b/src/parser.hs
index 09b0421..bfab053 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -72,21 +72,34 @@ intP = prefixedIntP <|> (read <$> digitsP)
naturalP :: Parser Int
naturalP = read <$> digitsP
+floatPositiveP :: Parser Float
+floatPositiveP = (f <$> digitsP <*> charP '.' <*> digitsP) <|> (read <$> digitsP)
+ where f pos dot dec = read $ pos ++ [dot] ++ dec
+
signP :: Parser Char
signP = charP '-' <|> charP '+'
+optionnal :: Parser a -> a -> Parser a
+optionnal p placeholder = p <|> pure placeholder
+
-- Equation parsers
unsignedTermP :: Parser Term
-unsignedTermP = notConstantP <|> constantP
- where constantP = (\c -> Term (fromIntegral c) 0) <$> naturalP
- notConstantP = (\coef exp -> Term (fromIntegral coef) exp)
- <$> naturalP <*> (between *> naturalP)
- where between = spacesP *> charP '*' *>
- spacesP *> charP 'X' *>
- spacesP *> charP '^' *>
- spacesP
+unsignedTermP = fullP <|> varExpP <|> varConstP <|> constP
+ where
+ -- 1 * X ^ 1
+ fullP = (\c e -> Term c e) <$> floatPositiveP <* mulP <* varP <* expP <*> naturalP
+ -- X ^ 1
+ varExpP = (\e -> Term 1 e) <$> (varP *> expP *> naturalP)
+ -- 1 * X
+ varConstP = (\c -> Term c 1) <$> floatPositiveP <* mulP <* varP
+ -- 1
+ constP = (\c -> Term c 0) <$> floatPositiveP
+
+ mulP = spacesP *> charP '*' *> spacesP
+ varP = spacesP *> charP 'X' *> spacesP
+ expP = spacesP *> charP '^' *> spacesP
signedTermP :: Parser Term
signedTermP = signF <$> signP <*> (spacesP *> unsignedTermP)