Chemistry is all about reactions—you throw a bunch of stuff into a test-tube, heat it up, hoping that it will neither explode nor poison you, then cool it down and try to work out whether what you have is what you expected. That's the easy (and fun) bit—much harder is recording it all. As is usual with skills of this type, chemistry instructors the world over rely on drill—a seemingly endless set of reactions that the students have to complete. The trick is that everything that appears on the left side (the reagents, or 'input') must appear on the right side (the products, or 'output'). This ought to be simple, but generations of chemistry students have demonstrated otherwise.
Professor Plumbius is getting tired of writing the same comments on his students' worksheets over and over, and he wants to automate the process. He wants to enter the equations as written by the students and have the computer produce the comments automatically, giving him more time to dream up new equations for his students to practise on. This is where you come in.
Write a program that reads a chemistry equation and determines whether it is balanced. If it isn't, your program must report which elements are out of balance and by how much.
Normally a chemistry equation is written like this:
2H2O + SO2 ←→ H2 + H2SO4
but because of the limitations of computer input we present it like this:
2H2O + SO2 = H2 + H2SO4
This example shows the essentials of an equation: each side consists of one or more molecules, separated by '+' signs (the spaces are optional). Each molecule may have a multiplier before it that specifies how many instances of that molecule take part in the reaction. A molecule consists of one or more elements. Each element has a symbol, which is either an uppercase letter, e.g. 'H', or an uppercase letter followed by a lowercase letter, e.g. 'Br'. A symbol may be followed by a multiplier specifying how many atoms of that element are present in that part of the molecule. Thus the first term says that there are two instances of a molecule consisting of two atoms of H and one atom of O. (This happens to be water, but you do not need to know that.)
Because these are exercises handed out to students, the left-hand sides are, by definition, correct. Your job is therefore to determine whether all the atoms that appear on the left also appear on the right. If they do, the equation is balanced. If not, you must report which elements have been created or lost and by how much.
Input consists of a number of equations, each on a line by itself. Each line contains no more than 250 characters. Each equation represents a set of reagents and a set of products, separated by an '=' sign. Each set consists of one or more molecules, possibly with multipliers, separated by '+' signs. There may be zero or more spaces on either side of the '+' and '=' signs.
The last line of input is a '#' on a line by itself. This line should not be processed.
There is at least one line of output for each equation in the input. If the equation is balanced, this line reads 'Equation n is balanced.', where n is the equation number (starting from 1). If the equation does not balance, the output line reads 'Equation n is unbalanced.' and is followed by a series of lines of the form
You have <created or destroyed> m <atom or atoms> of element.
where element is the symbol of the element concerned, m is the number of extra or missing atoms of that element, and 'atom(s)' is singular or plural as appropriate. For each unbalanced equation, these lines are ordered alphabetically by element symbol and terminated by a blank line. (Note that this means the final line of your output may be a blank line.)