module Ficha6 where import IO --------------------------------------------------- leInt :: IO Int leInt = do putStr "Escreva um número: " l <- getLine return ((read l)::Int) fact :: Int -> Int fact 0 = 1 fact n = n * fact (n-1) prog1 :: IO () prog1 = do x <- leInt putStrLn ("o factorial de "++(show x)++" é "++(show (fact x))) --------------------------------------------------- --------------------------------------------------- type Nome = String type Numero = Int type NT = Maybe Float type NP = Maybe Float data Regime = Ordinario | TrabEstud deriving (Show, Eq, Read) type Aluno = (Numero, Nome, Regime, NT, NP) data BTree a = Empty | Node a (BTree a) (BTree a) deriving (Show, Read) type Turma = BTree Aluno --------------------------------------------------- num :: Aluno -> Numero num (n,_,_,_,_) = n insere :: Aluno -> Turma -> Turma insere a Empty = (Node a Empty Empty) insere a (Node x e d) | num a == num x = Node a e d | num a < num x = Node x (insere a e) d | num a > num x = Node x e (insere a d) pesquisa :: Numero -> Turma -> Maybe Aluno pesquisa n Empty = Nothing pesquisa n (Node x e d) | n == num x = Just x | n < num x = pesquisa n e | n > num x = pesquisa n d --------------------------------------------------- main = do { hSetBuffering stdout NoBuffering ; ciclo Empty } ciclo :: Turma -> IO () ciclo t = do { op <- menu ; case op of '1':_ -> do { t' <- insereAluno t ; ciclo t' } '2':_ -> do { listaAlunos t ; ciclo t } '3':_ -> do { procuraAluno t ; ciclo t } '4':_ -> do { writeTurma t ; ciclo t } '5':_ -> do { t' <- readTurma ; ciclo t' } '0':_ -> return () otherwise -> do { putStrLn "Opcao invalida" ; ciclo t } } menu :: IO String menu = do { putStrLn menutxt ; putStr "Opcao: " ; c <- getLine ; return c } where menutxt = unlines ["", "Inserir Aluno ....... 1", "Listar Alunos ....... 2", "Procurar Aluno ...... 3", "", "Sair ................ 0"] insereAluno :: Turma -> IO Turma insereAluno t = do { putStr "\nNumero: "; nu <- getLine; putStr "Nome: "; no <- getLine; putStr "Regime: "; re <- getLine; putStr "Nota Pratica: " ; np <- getLine; putStr "Nota Teorica: "; nt <- getLine; let reg = if re=="TE" then TrabEstud else Ordinario pra = if np=="" then Nothing else Just ((read np)::Float) teo = if nt=="" then Nothing else Just ((read nt)::Float) in return (insere ((read nu),no,reg,pra,teo) t) } listaAlunos :: Turma -> IO () listaAlunos _ = return () --- para completar procuraAluno :: Turma -> IO () procuraAluno _ = return () --- para completar --------------------------------------------------- writeTurma :: Turma -> IO () writeTurma t = do putStr "Nome do ficheiro: " f <- getLine writeFile f (show t) readTurma :: IO Turma readTurma = do putStr "Nome do ficheiro: " f <- getLine s <- readFile f return (read s)