From ef9f0448e4bf5b068c272376a87416d9e23b4fa6 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Sun, 17 Nov 2024 19:21:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A8=D0=B8=D1=84=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=20=D1=86=D0=B5=D0=B7=D0=B0?= =?UTF-8?q?=D1=80=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 7 ++++++- lab3/src/Lib.hs | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 87061f2..056ced4 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -2,8 +2,13 @@ module Main (main) where import Lib +caesarShift :: Int +caesarShift = 66 + main :: IO () main = do inputText <- readFile "resources/biography.txt" putStrLn $ take 30 inputText - putStrLn $ createAlphabetFromText inputText \ No newline at end of file + let alphabet = createAlphabetFromText inputText + putStrLn $ show (length alphabet) + putStrLn $ take 30 (encryptCaesar alphabet caesarShift inputText) \ No newline at end of file diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index 47ce077..117a89d 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -1,6 +1,7 @@ module Lib ( - createAlphabetFromText + createAlphabetFromText, + encryptCaesar ) where createAlphabetFromText :: String -> [Char] @@ -9,4 +10,15 @@ createAlphabetFromText (x:xs) | x `elem` alphabet = alphabet | otherwise = x : alphabet where - alphabet = createAlphabetFromText xs \ No newline at end of file + alphabet = createAlphabetFromText xs + +indexOf :: (Eq t) => [t] -> t -> Int +indexOf [] _ = -1 +indexOf (x : xs) target + | x == target = 0 + | otherwise = 1 + indexOf xs target + +encryptCaesar :: [Char] -> Int -> String -> String +encryptCaesar alphabet shift text = map caesarChar text + where + caesarChar c = alphabet !! ((indexOf alphabet c + shift) `mod` length alphabet) \ No newline at end of file