Created
March 3, 2010 09:53
-
-
Save savonarola/320486 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad | |
data ExprTree = Var Char | Op ExprTree Char ExprTree | |
readExprS :: ReadS ExprTree | |
readExprS ('(':s) = do | |
(l, op:s1 ) <- readExprS s | |
(r, ')':t ) <- readExprS s1 | |
return ( (Op l op r), t ) | |
readExprS (c:s) = [((Var c),s)] | |
instance Read ExprTree where | |
readsPrec _ s = readExprS s | |
showsRPN :: ExprTree -> ShowS | |
showsRPN (Var v) = (v:) | |
showsRPN (Op l op r) = showsRPN l . showsRPN r . (op:) | |
instance Show ExprTree where | |
showsPrec _ t = showsRPN t | |
runTests :: Int -> IO() | |
runTests n = do | |
expr <- getLine | |
let tree = (read expr)::ExprTree | |
in putStrLn $ show tree | |
if n == 1 | |
then return () | |
else do | |
putStrLn "" | |
runTests (n-1) | |
main :: IO() | |
main = do | |
num <- liftM read getLine | |
runTests num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment