From 1a45ec233037096ae93d431395777ae6fde0a457 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Sun, 8 Dec 2024 19:45:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D1=81=D1=82=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coursework/part1/src/Lib.hs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/coursework/part1/src/Lib.hs b/coursework/part1/src/Lib.hs index 943d290..6b8389f 100644 --- a/coursework/part1/src/Lib.hs +++ b/coursework/part1/src/Lib.hs @@ -61,10 +61,10 @@ data Operation = Add | Sub | Mul | Div deriving Show operationToString :: Operation -> String operationToString op = case op of - Add -> " + " - Sub -> " - " - Mul -> " * " - Div -> " / " + Add -> "+" + Sub -> "-" + Mul -> "*" + Div -> "/" operationToOperator :: Operation -> (Int -> Int -> Int) operationToOperator op = case op of @@ -84,3 +84,19 @@ operation = skipSpaces *> ( expression :: Parser Char (Int, Operation, Int) expression = (,,) <$> number <*> operation <*> number <* skipSpaces + +calculateExpression :: (Int, Operation, Int) -> Int +calculateExpression (a, op, b) = (operationToOperator op) a b + +processExpression :: String -> String +processExpression s = case runParser expression s of + Nothing -> err + Just (cs, (a, op, b)) -> case cs of + [] -> + show a ++ " " ++ + operationToString op ++ " " ++ + show b ++ " = " ++ + show (calculateExpression (a, op, b)) + _ -> err + where + err = error $ "Unable to parse the following expression: \"" ++ s ++ "\"" \ No newline at end of file