diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.hs | 17 | ||||
| -rw-r--r-- | src/parser.hs | 29 |
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) |
