Crazy chemical equations You should get string of chemical equation (no spaces, only letters (upper-case and lower-case), numbers, brackets and math signs) from user and print the answer if equation is balanced or not (any pair of positive/negative answers: Yes/No, true/false, 1/0). To make code shorter you can assume that input strings can contain only these elements: Al, Ar, B, Be, C, Cl, Cr, Cu, Fe, H, He, K, N, O, S. And one more thing: there could be - signs. It's all about math: + means addition, - means subtraction.

Examples:

Input:

C6H5COOH-O2=7CO2+3H2O


Output:

No




Input:

2Fe(CN)6+2SO2+202=Fe2(SO4)2+6C2N2


Output:

Yes




Input:

2SO2=2SO4-2O2


Output:

Yes


The shortest code wins.
Haskell, 400 351 308 characters

import Data.List
import Text.Parsec
(&amp;)=fmap
r=return
s=string
l f b=(&gt;&gt;=(&amp;b).f)
x=(=&lt;&lt;).replicate
m=sort&amp;chainl1(l x(concat&amp;many1(l(flip x)n i))n)((s"+"&gt;&gt;r(++))&lt;|&gt;(s"-"&gt;&gt;r(\\)))
i=between(s"(")(s")")m&lt;|&gt;(:[])&amp;(l(:)(many lower)upper)
n=option 1$read&amp;many1 digit
main=getContents&gt;&gt;=parseTest(l(==)(s"="&gt;&gt;m)m)


This just might have all the golf squeezed out of it. I don't know if there is another 100 51 8 characters to be saved!

&amp; echo 'C6H5COOH-O2=7CO2+3H2O' | runhaskell Chemical.hs
False

&amp; echo '2Fe(CN)6+2SO2+2O2=Fe2(SO4)2+6C2N2' | runhaskell Chemical.hs
True

&amp; echo '2SO2=2SO4-2O2' | runhaskell Chemical.hs
True


Here's the ungolf'd version, in case anyone want to follow along. It is a simple Parsec based parser:

import Control.Applicative ((&lt;$&gt;), (&lt;*&gt;))
import Data.List
import Text.Parsec
import Text.Parsec.String (Parser)

type Atom = String

{- golf'd as x -}
multiple :: Int -&gt; [Atom] -&gt; [Atom]
multiple n = concat . replicate n

{- golf'd as m -}
chemicals :: Parser [Atom]
chemicals = sort &lt;$&gt; chainl1 molecules op
  where
    op :: Eq a =&gt; Parser ([a] -&gt; [a] -&gt; [a])
    op = (char '+' &gt;&gt; return (++))
     &lt;|&gt; (char '-' &gt;&gt; return (\\))

    molecules :: Parser [Atom]
    molecules = multiple &lt;$&gt; number &lt;*&gt; terms

    terms :: Parser [Atom]
    terms = concat &lt;$&gt; many1 term

    term :: Parser [Atom]
    term = flip multiple &lt;$&gt; item &lt;*&gt; number

{- gofl'd as i -}
item :: Parser [Atom]
item = between (char '(') (char ')') chemicals
   &lt;|&gt; (:[]) &lt;$&gt; atom
  where
    atom :: Parser Atom
    atom = (:) &lt;$&gt; upper &lt;*&gt; many lower

{- gofl'd as n -}
number :: Parser Int
number = option 1 $ read &lt;$&gt; many1 digit

{- gofl'd as main -}
main :: IO ()
main = getContents &gt;&gt;= parseTest chemEquality
  where
    chemEquality :: Parser Bool
    chemEquality = (==) &lt;$&gt; chemicals &lt;*&gt; (char '=' &gt;&gt; chemicals)